blob: e71fb5f31e84db54672dede30c058eb8fc87129c [file] [log] [blame]
Andi Kleenf00898f2015-05-27 10:51:51 -07001#include "perf.h"
2#include "util/util.h"
3#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06004#include <subcmd/parse-options.h>
Andi Kleenf00898f2015-05-27 10:51:51 -07005#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
12struct branch_mode {
13 const char *name;
14 int mode;
15};
16
17static 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),
Stephane Eranian5b681642015-05-14 23:10:00 +020029 BRANCH_OPT("ind_jmp", PERF_SAMPLE_BRANCH_IND_JUMP),
Stephane Eranian43e41ad2015-10-13 09:09:11 +020030 BRANCH_OPT("call", PERF_SAMPLE_BRANCH_CALL),
Jin Yao60f83fa2017-07-18 20:13:11 +080031 BRANCH_OPT("save_type", PERF_SAMPLE_BRANCH_TYPE_SAVE),
Andi Kleenf00898f2015-05-27 10:51:51 -070032 BRANCH_END
33};
34
Andi Kleenac12f672016-10-12 14:02:06 -070035int parse_branch_str(const char *str, __u64 *mode)
Andi Kleenf00898f2015-05-27 10:51:51 -070036{
37#define ONLY_PLM \
38 (PERF_SAMPLE_BRANCH_USER |\
39 PERF_SAMPLE_BRANCH_KERNEL |\
40 PERF_SAMPLE_BRANCH_HV)
41
Andi Kleenac12f672016-10-12 14:02:06 -070042 int ret = 0;
43 char *p, *s;
44 char *os = NULL;
Andi Kleenf00898f2015-05-27 10:51:51 -070045 const struct branch_mode *br;
Andi Kleenac12f672016-10-12 14:02:06 -070046
47 if (str == NULL) {
48 *mode = PERF_SAMPLE_BRANCH_ANY;
49 return 0;
50 }
51
52 /* because str is read-only */
53 s = os = strdup(str);
54 if (!s)
55 return -1;
56
57 for (;;) {
58 p = strchr(s, ',');
59 if (p)
60 *p = '\0';
61
62 for (br = branch_modes; br->name; br++) {
63 if (!strcasecmp(s, br->name))
64 break;
65 }
66 if (!br->name) {
67 ret = -1;
Arnaldo Carvalho de Melo46cb25b2016-10-26 19:02:35 -020068 pr_warning("unknown branch filter %s,"
Andi Kleenac12f672016-10-12 14:02:06 -070069 " check man page\n", s);
70 goto error;
71 }
72
73 *mode |= br->mode;
74
75 if (!p)
76 break;
77
78 s = p + 1;
79 }
80
81 /* default to any branch */
82 if ((*mode & ~ONLY_PLM) == 0) {
83 *mode = PERF_SAMPLE_BRANCH_ANY;
84 }
85error:
86 free(os);
87 return ret;
88}
89
90int
91parse_branch_stack(const struct option *opt, const char *str, int unset)
92{
93 __u64 *mode = (__u64 *)opt->value;
Andi Kleenf00898f2015-05-27 10:51:51 -070094
95 if (unset)
96 return 0;
97
98 /*
99 * cannot set it twice, -b + --branch-filter for instance
100 */
101 if (*mode)
102 return -1;
103
Andi Kleenac12f672016-10-12 14:02:06 -0700104 return parse_branch_str(str, mode);
Andi Kleenf00898f2015-05-27 10:51:51 -0700105}