Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include "perf.h"
3#include "util/util.h"
4#include "util/debug.h"
5#include <subcmd/parse-options.h>
6#include "util/parse-branch-options.h"
7
8#define BRANCH_OPT(n, m) \
9 { .name = n, .mode = (m) }
10
11#define BRANCH_END { .name = NULL }
12
13struct branch_mode {
14 const char *name;
15 int mode;
16};
17
18static const struct branch_mode branch_modes[] = {
19 BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
20 BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
21 BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
22 BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
23 BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
24 BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
25 BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
26 BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX),
27 BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX),
28 BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX),
29 BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND),
30 BRANCH_OPT("ind_jmp", PERF_SAMPLE_BRANCH_IND_JUMP),
31 BRANCH_OPT("call", PERF_SAMPLE_BRANCH_CALL),
32 BRANCH_OPT("save_type", PERF_SAMPLE_BRANCH_TYPE_SAVE),
33 BRANCH_END
34};
35
36int parse_branch_str(const char *str, __u64 *mode)
37{
38#define ONLY_PLM \
39 (PERF_SAMPLE_BRANCH_USER |\
40 PERF_SAMPLE_BRANCH_KERNEL |\
41 PERF_SAMPLE_BRANCH_HV)
42
43 int ret = 0;
44 char *p, *s;
45 char *os = NULL;
46 const struct branch_mode *br;
47
48 if (str == NULL) {
49 *mode = PERF_SAMPLE_BRANCH_ANY;
50 return 0;
51 }
52
53 /* because str is read-only */
54 s = os = strdup(str);
55 if (!s)
56 return -1;
57
58 for (;;) {
59 p = strchr(s, ',');
60 if (p)
61 *p = '\0';
62
63 for (br = branch_modes; br->name; br++) {
64 if (!strcasecmp(s, br->name))
65 break;
66 }
67 if (!br->name) {
68 ret = -1;
69 pr_warning("unknown branch filter %s,"
70 " check man page\n", s);
71 goto error;
72 }
73
74 *mode |= br->mode;
75
76 if (!p)
77 break;
78
79 s = p + 1;
80 }
81
82 /* default to any branch */
83 if ((*mode & ~ONLY_PLM) == 0) {
84 *mode = PERF_SAMPLE_BRANCH_ANY;
85 }
86error:
87 free(os);
88 return ret;
89}
90
91int
92parse_branch_stack(const struct option *opt, const char *str, int unset)
93{
94 __u64 *mode = (__u64 *)opt->value;
95
96 if (unset)
97 return 0;
98
99 /*
100 * cannot set it twice, -b + --branch-filter for instance
101 */
102 if (*mode)
103 return -1;
104
105 return parse_branch_str(str, mode);
106}
1// SPDX-License-Identifier: GPL-2.0
2#include "util/debug.h"
3#include "util/event.h"
4#include <subcmd/parse-options.h>
5#include "util/parse-branch-options.h"
6#include <stdlib.h>
7#include <string.h>
8
9#define BRANCH_OPT(n, m) \
10 { .name = n, .mode = (m) }
11
12#define BRANCH_END { .name = NULL }
13
14struct branch_mode {
15 const char *name;
16 int mode;
17};
18
19static const struct branch_mode branch_modes[] = {
20 BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
21 BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
22 BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
23 BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
24 BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
25 BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
26 BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
27 BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX),
28 BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX),
29 BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX),
30 BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND),
31 BRANCH_OPT("ind_jmp", PERF_SAMPLE_BRANCH_IND_JUMP),
32 BRANCH_OPT("call", PERF_SAMPLE_BRANCH_CALL),
33 BRANCH_OPT("no_flags", PERF_SAMPLE_BRANCH_NO_FLAGS),
34 BRANCH_OPT("no_cycles", PERF_SAMPLE_BRANCH_NO_CYCLES),
35 BRANCH_OPT("save_type", PERF_SAMPLE_BRANCH_TYPE_SAVE),
36 BRANCH_OPT("stack", PERF_SAMPLE_BRANCH_CALL_STACK),
37 BRANCH_OPT("hw_index", PERF_SAMPLE_BRANCH_HW_INDEX),
38 BRANCH_OPT("priv", PERF_SAMPLE_BRANCH_PRIV_SAVE),
39 BRANCH_END
40};
41
42int parse_branch_str(const char *str, __u64 *mode)
43{
44#define ONLY_PLM \
45 (PERF_SAMPLE_BRANCH_USER |\
46 PERF_SAMPLE_BRANCH_KERNEL |\
47 PERF_SAMPLE_BRANCH_HV)
48
49 int ret = 0;
50 char *p, *s;
51 char *os = NULL;
52 const struct branch_mode *br;
53
54 if (str == NULL) {
55 *mode = PERF_SAMPLE_BRANCH_ANY;
56 return 0;
57 }
58
59 /* because str is read-only */
60 s = os = strdup(str);
61 if (!s)
62 return -1;
63
64 for (;;) {
65 p = strchr(s, ',');
66 if (p)
67 *p = '\0';
68
69 for (br = branch_modes; br->name; br++) {
70 if (!strcasecmp(s, br->name))
71 break;
72 }
73 if (!br->name) {
74 ret = -1;
75 pr_warning("unknown branch filter %s,"
76 " check man page\n", s);
77 goto error;
78 }
79
80 *mode |= br->mode;
81
82 if (!p)
83 break;
84
85 s = p + 1;
86 }
87
88 /* default to any branch */
89 if ((*mode & ~ONLY_PLM) == 0) {
90 *mode = PERF_SAMPLE_BRANCH_ANY;
91 }
92error:
93 free(os);
94 return ret;
95}
96
97int
98parse_branch_stack(const struct option *opt, const char *str, int unset)
99{
100 __u64 *mode = (__u64 *)opt->value;
101
102 if (unset)
103 return 0;
104
105 /*
106 * cannot set it twice, -b + --branch-filter for instance
107 */
108 if (*mode) {
109 pr_err("Error: Can't use --branch-any (-b) with --branch-filter (-j).\n");
110 return -1;
111 }
112
113 return parse_branch_str(str, mode);
114}