xref: /linux/tools/perf/util/parse-branch-options.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
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_OPT("ind_jmp", PERF_SAMPLE_BRANCH_IND_JUMP),
30 	BRANCH_END
31 };
32 
33 int
34 parse_branch_stack(const struct option *opt, const char *str, int unset)
35 {
36 #define ONLY_PLM \
37 	(PERF_SAMPLE_BRANCH_USER	|\
38 	 PERF_SAMPLE_BRANCH_KERNEL	|\
39 	 PERF_SAMPLE_BRANCH_HV)
40 
41 	uint64_t *mode = (uint64_t *)opt->value;
42 	const struct branch_mode *br;
43 	char *s, *os = NULL, *p;
44 	int ret = -1;
45 
46 	if (unset)
47 		return 0;
48 
49 	/*
50 	 * cannot set it twice, -b + --branch-filter for instance
51 	 */
52 	if (*mode)
53 		return -1;
54 
55 	/* str may be NULL in case no arg is passed to -b */
56 	if (str) {
57 		/* because str is read-only */
58 		s = os = strdup(str);
59 		if (!s)
60 			return -1;
61 
62 		for (;;) {
63 			p = strchr(s, ',');
64 			if (p)
65 				*p = '\0';
66 
67 			for (br = branch_modes; br->name; br++) {
68 				if (!strcasecmp(s, br->name))
69 					break;
70 			}
71 			if (!br->name) {
72 				ui__warning("unknown branch filter %s,"
73 					    " check man page\n", s);
74 				goto error;
75 			}
76 
77 			*mode |= br->mode;
78 
79 			if (!p)
80 				break;
81 
82 			s = p + 1;
83 		}
84 	}
85 	ret = 0;
86 
87 	/* default to any branch */
88 	if ((*mode & ~ONLY_PLM) == 0) {
89 		*mode = PERF_SAMPLE_BRANCH_ANY;
90 	}
91 error:
92 	free(os);
93 	return ret;
94 }
95