1 #include "perf.h"
2 #include "util/util.h"
3 #include "util/debug.h"
4 #include "util/parse-options.h"
5 #include "util/parse-branch-options.h"
6 
7 #define BRANCH_OPT(n, m) \
8 	{ .name = n, .mode = (m) }
9 
10 #define BRANCH_END { .name = NULL }
11 
12 struct branch_mode {
13 	const char *name;
14 	int mode;
15 };
16 
17 static const struct branch_mode branch_modes[] = {
18 	BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
19 	BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
20 	BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
21 	BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
22 	BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
23 	BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
24 	BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
25 	BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX),
26 	BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX),
27 	BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX),
28 	BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND),
29 	BRANCH_END
30 };
31 
32 int
33 parse_branch_stack(const struct option *opt, const char *str, int unset)
34 {
35 #define ONLY_PLM \
36 	(PERF_SAMPLE_BRANCH_USER	|\
37 	 PERF_SAMPLE_BRANCH_KERNEL	|\
38 	 PERF_SAMPLE_BRANCH_HV)
39 
40 	uint64_t *mode = (uint64_t *)opt->value;
41 	const struct branch_mode *br;
42 	char *s, *os = NULL, *p;
43 	int ret = -1;
44 
45 	if (unset)
46 		return 0;
47 
48 	/*
49 	 * cannot set it twice, -b + --branch-filter for instance
50 	 */
51 	if (*mode)
52 		return -1;
53 
54 	/* str may be NULL in case no arg is passed to -b */
55 	if (str) {
56 		/* because str is read-only */
57 		s = os = strdup(str);
58 		if (!s)
59 			return -1;
60 
61 		for (;;) {
62 			p = strchr(s, ',');
63 			if (p)
64 				*p = '\0';
65 
66 			for (br = branch_modes; br->name; br++) {
67 				if (!strcasecmp(s, br->name))
68 					break;
69 			}
70 			if (!br->name) {
71 				ui__warning("unknown branch filter %s,"
72 					    " check man page\n", s);
73 				goto error;
74 			}
75 
76 			*mode |= br->mode;
77 
78 			if (!p)
79 				break;
80 
81 			s = p + 1;
82 		}
83 	}
84 	ret = 0;
85 
86 	/* default to any branch */
87 	if ((*mode & ~ONLY_PLM) == 0) {
88 		*mode = PERF_SAMPLE_BRANCH_ANY;
89 	}
90 error:
91 	free(os);
92 	return ret;
93 }
94