1 // SPDX-License-Identifier: GPL-2.0 2 #include "perf.h" 3 #include "util/debug.h" 4 #include <subcmd/parse-options.h> 5 #include "util/parse-branch-options.h" 6 #include <stdlib.h> 7 8 #define BRANCH_OPT(n, m) \ 9 { .name = n, .mode = (m) } 10 11 #define BRANCH_END { .name = NULL } 12 13 struct branch_mode { 14 const char *name; 15 int mode; 16 }; 17 18 static 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_OPT("stack", PERF_SAMPLE_BRANCH_CALL_STACK), 34 BRANCH_END 35 }; 36 37 int parse_branch_str(const char *str, __u64 *mode) 38 { 39 #define ONLY_PLM \ 40 (PERF_SAMPLE_BRANCH_USER |\ 41 PERF_SAMPLE_BRANCH_KERNEL |\ 42 PERF_SAMPLE_BRANCH_HV) 43 44 int ret = 0; 45 char *p, *s; 46 char *os = NULL; 47 const struct branch_mode *br; 48 49 if (str == NULL) { 50 *mode = PERF_SAMPLE_BRANCH_ANY; 51 return 0; 52 } 53 54 /* because str is read-only */ 55 s = os = strdup(str); 56 if (!s) 57 return -1; 58 59 for (;;) { 60 p = strchr(s, ','); 61 if (p) 62 *p = '\0'; 63 64 for (br = branch_modes; br->name; br++) { 65 if (!strcasecmp(s, br->name)) 66 break; 67 } 68 if (!br->name) { 69 ret = -1; 70 pr_warning("unknown branch filter %s," 71 " check man page\n", s); 72 goto error; 73 } 74 75 *mode |= br->mode; 76 77 if (!p) 78 break; 79 80 s = p + 1; 81 } 82 83 /* default to any branch */ 84 if ((*mode & ~ONLY_PLM) == 0) { 85 *mode = PERF_SAMPLE_BRANCH_ANY; 86 } 87 error: 88 free(os); 89 return ret; 90 } 91 92 int 93 parse_branch_stack(const struct option *opt, const char *str, int unset) 94 { 95 __u64 *mode = (__u64 *)opt->value; 96 97 if (unset) 98 return 0; 99 100 /* 101 * cannot set it twice, -b + --branch-filter for instance 102 */ 103 if (*mode) 104 return -1; 105 106 return parse_branch_str(str, mode); 107 } 108