1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 38dd2a131SJiri Olsa #include <linux/err.h> 4347c2f0aSIan Rogers #include <linux/list_sort.h> 57f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 676b31a29SArnaldo Carvalho de Melo #include <dirent.h> 7a43783aeSArnaldo Carvalho de Melo #include <errno.h> 886a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 9391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 10b0742e90SArnaldo Carvalho de Melo #include "term.h" 11361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 1269aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 134b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1486470930SIngo Molnar #include "parse-events.h" 15a067558eSArnaldo Carvalho de Melo #include "string2.h" 168ec20b17SArnaldo Carvalho de Melo #include "strlist.h" 176e81c74cSMasami Hiramatsu #include "debug.h" 18592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 199c3516d1SJiri Olsa #include <perf/cpumap.h> 20c7e97f21SNamhyung Kim #include <util/parse-events-bison.h> 21c7e97f21SNamhyung Kim #include <util/parse-events-flex.h> 225f537a26SJiri Olsa #include "pmu.h" 231eaf496eSIan Rogers #include "pmus.h" 24b39b8393SJiri Olsa #include "asm/bug.h" 25ac12f676SAndi Kleen #include "util/parse-branch-options.h" 2695be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h" 27252a2fdcSArnaldo Carvalho de Melo #include "util/event.h" 28d180aa56SNamhyung Kim #include "util/bpf-filter.h" 29ea0c5239SIan Rogers #include "util/util.h" 309b7c7728SIan Rogers #include "tracepoint.h" 3189812fc8SJiri Olsa 3289812fc8SJiri Olsa #define MAX_NAME_LEN 100 3386470930SIngo Molnar 3482ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 3582ba1f2fSJiri Olsa extern int parse_events_debug; 3682ba1f2fSJiri Olsa #endif 37e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 38e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 39bcd3279fSFrederic Weisbecker 40705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 411dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 421dc12760SJiri Olsa .symbol = "cpu-cycles", 431dc12760SJiri Olsa .alias = "cycles", 441dc12760SJiri Olsa }, 451dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 461dc12760SJiri Olsa .symbol = "instructions", 471dc12760SJiri Olsa .alias = "", 481dc12760SJiri Olsa }, 491dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 501dc12760SJiri Olsa .symbol = "cache-references", 511dc12760SJiri Olsa .alias = "", 521dc12760SJiri Olsa }, 531dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 541dc12760SJiri Olsa .symbol = "cache-misses", 551dc12760SJiri Olsa .alias = "", 561dc12760SJiri Olsa }, 571dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 581dc12760SJiri Olsa .symbol = "branch-instructions", 591dc12760SJiri Olsa .alias = "branches", 601dc12760SJiri Olsa }, 611dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 621dc12760SJiri Olsa .symbol = "branch-misses", 631dc12760SJiri Olsa .alias = "", 641dc12760SJiri Olsa }, 651dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 661dc12760SJiri Olsa .symbol = "bus-cycles", 671dc12760SJiri Olsa .alias = "", 681dc12760SJiri Olsa }, 691dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 701dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 711dc12760SJiri Olsa .alias = "idle-cycles-frontend", 721dc12760SJiri Olsa }, 731dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 741dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 751dc12760SJiri Olsa .alias = "idle-cycles-backend", 761dc12760SJiri Olsa }, 771dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 781dc12760SJiri Olsa .symbol = "ref-cycles", 791dc12760SJiri Olsa .alias = "", 801dc12760SJiri Olsa }, 811dc12760SJiri Olsa }; 8286470930SIngo Molnar 83705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 841dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 851dc12760SJiri Olsa .symbol = "cpu-clock", 861dc12760SJiri Olsa .alias = "", 871dc12760SJiri Olsa }, 881dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 891dc12760SJiri Olsa .symbol = "task-clock", 901dc12760SJiri Olsa .alias = "", 911dc12760SJiri Olsa }, 921dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 931dc12760SJiri Olsa .symbol = "page-faults", 941dc12760SJiri Olsa .alias = "faults", 951dc12760SJiri Olsa }, 961dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 971dc12760SJiri Olsa .symbol = "context-switches", 981dc12760SJiri Olsa .alias = "cs", 991dc12760SJiri Olsa }, 1001dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1011dc12760SJiri Olsa .symbol = "cpu-migrations", 1021dc12760SJiri Olsa .alias = "migrations", 1031dc12760SJiri Olsa }, 1041dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1051dc12760SJiri Olsa .symbol = "minor-faults", 1061dc12760SJiri Olsa .alias = "", 1071dc12760SJiri Olsa }, 1081dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1091dc12760SJiri Olsa .symbol = "major-faults", 1101dc12760SJiri Olsa .alias = "", 1111dc12760SJiri Olsa }, 1121dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1131dc12760SJiri Olsa .symbol = "alignment-faults", 1141dc12760SJiri Olsa .alias = "", 1151dc12760SJiri Olsa }, 1161dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1171dc12760SJiri Olsa .symbol = "emulation-faults", 1181dc12760SJiri Olsa .alias = "", 1191dc12760SJiri Olsa }, 120d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 121d22d1a2aSAdrian Hunter .symbol = "dummy", 122d22d1a2aSAdrian Hunter .alias = "", 123d22d1a2aSAdrian Hunter }, 124bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 125bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 126bae9cc41SArnaldo Carvalho de Melo .alias = "", 127bae9cc41SArnaldo Carvalho de Melo }, 128fb6c79d7SNamhyung Kim [PERF_COUNT_SW_CGROUP_SWITCHES] = { 129fb6c79d7SNamhyung Kim .symbol = "cgroup-switches", 130fb6c79d7SNamhyung Kim .alias = "", 131fb6c79d7SNamhyung Kim }, 13286470930SIngo Molnar }; 13386470930SIngo Molnar 1341424dc96SDavid Ahern const char *event_type(int type) 1351424dc96SDavid Ahern { 1361424dc96SDavid Ahern switch (type) { 1371424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 1381424dc96SDavid Ahern return "hardware"; 1391424dc96SDavid Ahern 1401424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 1411424dc96SDavid Ahern return "software"; 1421424dc96SDavid Ahern 1431424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 1441424dc96SDavid Ahern return "tracepoint"; 1451424dc96SDavid Ahern 1461424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 1471424dc96SDavid Ahern return "hardware-cache"; 1481424dc96SDavid Ahern 1491424dc96SDavid Ahern default: 1501424dc96SDavid Ahern break; 1511424dc96SDavid Ahern } 1521424dc96SDavid Ahern 1531424dc96SDavid Ahern return "unknown"; 1541424dc96SDavid Ahern } 1551424dc96SDavid Ahern 1562b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term) 157e814fdddSWang Nan { 158e814fdddSWang Nan struct parse_events_term *term; 159e814fdddSWang Nan 160e814fdddSWang Nan if (!head_terms) 161e814fdddSWang Nan return NULL; 162e814fdddSWang Nan 163e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 1642b62b3a6SIan Rogers if (term->type_term == type_term) 165e814fdddSWang Nan return term->val.str; 166e814fdddSWang Nan 167e814fdddSWang Nan return NULL; 168e814fdddSWang Nan } 1697ae92e74SYan, Zheng 1702b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms) 1712b62b3a6SIan Rogers { 1722b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID); 1732b62b3a6SIan Rogers } 1742b62b3a6SIan Rogers 1752b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms) 1762b62b3a6SIan Rogers { 1772b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); 1782b62b3a6SIan Rogers } 1792b62b3a6SIan Rogers 18070c90e4aSIan Rogers /** 18170c90e4aSIan Rogers * fix_raw - For each raw term see if there is an event (aka alias) in pmu that 18270c90e4aSIan Rogers * matches the raw's string value. If the string value matches an 18370c90e4aSIan Rogers * event then change the term to be an event, if not then change it to 18470c90e4aSIan Rogers * be a config term. For example, "read" may be an event of the PMU or 18570c90e4aSIan Rogers * a raw hex encoding of 0xead. The fix-up is done late so the PMU of 18670c90e4aSIan Rogers * the event can be determined and we don't need to scan all PMUs 18770c90e4aSIan Rogers * ahead-of-time. 18870c90e4aSIan Rogers * @config_terms: the list of terms that may contain a raw term. 18970c90e4aSIan Rogers * @pmu: the PMU to scan for events from. 19070c90e4aSIan Rogers */ 19170c90e4aSIan Rogers static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu) 19270c90e4aSIan Rogers { 19370c90e4aSIan Rogers struct parse_events_term *term; 19470c90e4aSIan Rogers 19570c90e4aSIan Rogers list_for_each_entry(term, config_terms, list) { 196c3245d20SIan Rogers u64 num; 19770c90e4aSIan Rogers 19870c90e4aSIan Rogers if (term->type_term != PARSE_EVENTS__TERM_TYPE_RAW) 19970c90e4aSIan Rogers continue; 20070c90e4aSIan Rogers 201c3245d20SIan Rogers if (perf_pmu__have_event(pmu, term->val.str)) { 20270c90e4aSIan Rogers free(term->config); 20370c90e4aSIan Rogers term->config = term->val.str; 20470c90e4aSIan Rogers term->type_val = PARSE_EVENTS__TERM_TYPE_NUM; 20570c90e4aSIan Rogers term->type_term = PARSE_EVENTS__TERM_TYPE_USER; 20670c90e4aSIan Rogers term->val.num = 1; 20770c90e4aSIan Rogers term->no_value = true; 208c3245d20SIan Rogers continue; 20970c90e4aSIan Rogers } 21070c90e4aSIan Rogers 21170c90e4aSIan Rogers free(term->config); 21270c90e4aSIan Rogers term->config = strdup("config"); 21370c90e4aSIan Rogers errno = 0; 21470c90e4aSIan Rogers num = strtoull(term->val.str + 1, NULL, 16); 21570c90e4aSIan Rogers assert(errno == 0); 21670c90e4aSIan Rogers free(term->val.str); 21770c90e4aSIan Rogers term->type_val = PARSE_EVENTS__TERM_TYPE_NUM; 21870c90e4aSIan Rogers term->type_term = PARSE_EVENTS__TERM_TYPE_CONFIG; 21970c90e4aSIan Rogers term->val.num = num; 22070c90e4aSIan Rogers term->no_value = false; 22170c90e4aSIan Rogers } 22270c90e4aSIan Rogers } 22370c90e4aSIan Rogers 22432dcd021SJiri Olsa static struct evsel * 225410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 2267ae92e74SYan, Zheng struct perf_event_attr *attr, 22770943490SStephane Eranian bool init_attr, 2282b62b3a6SIan Rogers const char *name, const char *metric_id, struct perf_pmu *pmu, 229f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 230f0fbb114SAndi Kleen const char *cpu_list) 23189812fc8SJiri Olsa { 23232dcd021SJiri Olsa struct evsel *evsel; 2335d680be3SNamhyung Kim struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 2349c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 23589812fc8SJiri Olsa 236e552b7beSRob Herring if (pmu) 237e552b7beSRob Herring perf_pmu__warn_invalid_formats(pmu); 238e552b7beSRob Herring 239b9f01032SIan Rogers if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) { 240b9f01032SIan Rogers perf_pmu__warn_invalid_config(pmu, attr->config, name, 241b9f01032SIan Rogers PERF_PMU_FORMAT_VALUE_CONFIG, "config"); 242b9f01032SIan Rogers perf_pmu__warn_invalid_config(pmu, attr->config1, name, 243b9f01032SIan Rogers PERF_PMU_FORMAT_VALUE_CONFIG1, "config1"); 244b9f01032SIan Rogers perf_pmu__warn_invalid_config(pmu, attr->config2, name, 245b9f01032SIan Rogers PERF_PMU_FORMAT_VALUE_CONFIG2, "config2"); 246b9f01032SIan Rogers perf_pmu__warn_invalid_config(pmu, attr->config3, name, 247b9f01032SIan Rogers PERF_PMU_FORMAT_VALUE_CONFIG3, "config3"); 248b9f01032SIan Rogers } 24970943490SStephane Eranian if (init_attr) 25089812fc8SJiri Olsa event_attr_init(attr); 25189812fc8SJiri Olsa 2528f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attr, *idx); 2530f1b550eSNamhyung Kim if (!evsel) { 2540f1b550eSNamhyung Kim perf_cpu_map__put(cpus); 255410136f5SStephane Eranian return NULL; 2560f1b550eSNamhyung Kim } 25789812fc8SJiri Olsa 25875fc5ae5STaeung Song (*idx)++; 2595d680be3SNamhyung Kim evsel->core.cpus = cpus; 260fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 261d3345fecSAdrian Hunter evsel->core.requires_cpu = pmu ? pmu->is_uncore : false; 2621578e63dSIan Rogers evsel->core.is_pmu_core = pmu ? pmu->is_core : false; 26363ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 264f7400262SNamhyung Kim evsel->pmu = pmu; 265cae256aeSIan Rogers evsel->pmu_name = pmu && pmu->name ? strdup(pmu->name) : NULL; 266f30a79b0SJiri Olsa 2679db1763cSArnaldo Carvalho de Melo if (name) 26889812fc8SJiri Olsa evsel->name = strdup(name); 269930a2e29SJiri Olsa 2702b62b3a6SIan Rogers if (metric_id) 2712b62b3a6SIan Rogers evsel->metric_id = strdup(metric_id); 2722b62b3a6SIan Rogers 273930a2e29SJiri Olsa if (config_terms) 27499fc5941SAdrian Hunter list_splice_init(config_terms, &evsel->config_terms); 275930a2e29SJiri Olsa 27670943490SStephane Eranian if (list) 277b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 27870943490SStephane Eranian 279410136f5SStephane Eranian return evsel; 28089812fc8SJiri Olsa } 28189812fc8SJiri Olsa 28270943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 2832b62b3a6SIan Rogers const char *name, const char *metric_id, 2842b62b3a6SIan Rogers struct perf_pmu *pmu) 28570943490SStephane Eranian { 2862b62b3a6SIan Rogers return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name, 2872b62b3a6SIan Rogers metric_id, pmu, /*config_terms=*/NULL, 2882b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL); 28970943490SStephane Eranian } 29070943490SStephane Eranian 291c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 2928e8bbfb3SIan Rogers struct perf_event_attr *attr, const char *name, 2932b62b3a6SIan Rogers const char *metric_id, struct list_head *config_terms) 2947ae92e74SYan, Zheng { 2952b62b3a6SIan Rogers return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, 2962b62b3a6SIan Rogers /*pmu=*/NULL, config_terms, 2972b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM; 298f0fbb114SAndi Kleen } 299f0fbb114SAndi Kleen 300f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 301f0fbb114SAndi Kleen enum perf_tool_event tool_event) 302f0fbb114SAndi Kleen { 30332dcd021SJiri Olsa struct evsel *evsel; 304f0fbb114SAndi Kleen struct perf_event_attr attr = { 305f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 306f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 307f0fbb114SAndi Kleen }; 308f0fbb114SAndi Kleen 3092b62b3a6SIan Rogers evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL, 3102b62b3a6SIan Rogers /*metric_id=*/NULL, /*pmu=*/NULL, 3112b62b3a6SIan Rogers /*config_terms=*/NULL, /*auto_merge_stats=*/false, 3122b62b3a6SIan Rogers /*cpu_list=*/"0"); 313f0fbb114SAndi Kleen if (!evsel) 314f0fbb114SAndi Kleen return -ENOMEM; 315f0fbb114SAndi Kleen evsel->tool_event = tool_event; 316b03b89b3SFlorian Fischer if (tool_event == PERF_TOOL_DURATION_TIME 317b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_USER_TIME 318b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_SYSTEM_TIME) { 319b194c9cdSIan Rogers free((char *)evsel->unit); 320b194c9cdSIan Rogers evsel->unit = strdup("ns"); 321b194c9cdSIan Rogers } 322f0fbb114SAndi Kleen return 0; 3237ae92e74SYan, Zheng } 3247ae92e74SYan, Zheng 32570c90e4aSIan Rogers /** 32670c90e4aSIan Rogers * parse_aliases - search names for entries beginning or equalling str ignoring 32770c90e4aSIan Rogers * case. If mutliple entries in names match str then the longest 32870c90e4aSIan Rogers * is chosen. 32970c90e4aSIan Rogers * @str: The needle to look for. 33070c90e4aSIan Rogers * @names: The haystack to search. 33170c90e4aSIan Rogers * @size: The size of the haystack. 33270c90e4aSIan Rogers * @longest: Out argument giving the length of the matching entry. 33370c90e4aSIan Rogers */ 33470c90e4aSIan Rogers static int parse_aliases(const char *str, const char *const names[][EVSEL__MAX_ALIASES], int size, 33570c90e4aSIan Rogers int *longest) 33686470930SIngo Molnar { 33770c90e4aSIan Rogers *longest = -1; 33870c90e4aSIan Rogers for (int i = 0; i < size; i++) { 33970c90e4aSIan Rogers for (int j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 34070c90e4aSIan Rogers int n = strlen(names[i][j]); 34186470930SIngo Molnar 34270c90e4aSIan Rogers if (n > *longest && !strncasecmp(str, names[i][j], n)) 34370c90e4aSIan Rogers *longest = n; 34461c45981SPaul Mackerras } 34570c90e4aSIan Rogers if (*longest > 0) 34686470930SIngo Molnar return i; 34786470930SIngo Molnar } 34886470930SIngo Molnar 3498953645fSIngo Molnar return -1; 35086470930SIngo Molnar } 35186470930SIngo Molnar 35243d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 35343d0b978SWang Nan struct parse_events_term *term, 35443d0b978SWang Nan struct parse_events_error *err); 35543d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 35643d0b978SWang Nan struct parse_events_term *term, 35743d0b978SWang Nan struct parse_events_error *err); 35843d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 35943d0b978SWang Nan struct list_head *head, 36043d0b978SWang Nan struct parse_events_error *err, 36143d0b978SWang Nan config_term_func_t config_term); 36243d0b978SWang Nan 3636fd1e519SIan Rogers /** 3646fd1e519SIan Rogers * parse_events__decode_legacy_cache - Search name for the legacy cache event 3656fd1e519SIan Rogers * name composed of 1, 2 or 3 hyphen 3666fd1e519SIan Rogers * separated sections. The first section is 3676fd1e519SIan Rogers * the cache type while the others are the 3686fd1e519SIan Rogers * optional op and optional result. To make 3696fd1e519SIan Rogers * life hard the names in the table also 3706fd1e519SIan Rogers * contain hyphens and the longest name 37170c90e4aSIan Rogers * should always be selected. 37286470930SIngo Molnar */ 373251aa040SIan Rogers int parse_events__decode_legacy_cache(const char *name, int extended_pmu_type, __u64 *config) 3746fd1e519SIan Rogers { 3756fd1e519SIan Rogers int len, cache_type = -1, cache_op = -1, cache_result = -1; 3766fd1e519SIan Rogers const char *name_end = &name[strlen(name) + 1]; 3776fd1e519SIan Rogers const char *str = name; 3786fd1e519SIan Rogers 37970c90e4aSIan Rogers cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len); 38086470930SIngo Molnar if (cache_type == -1) 38189812fc8SJiri Olsa return -EINVAL; 38270c90e4aSIan Rogers str += len + 1; 38386470930SIngo Molnar 38470c90e4aSIan Rogers if (str < name_end) { 385c64e85e1SArnaldo Carvalho de Melo cache_op = parse_aliases(str, evsel__hw_cache_op, 38670c90e4aSIan Rogers PERF_COUNT_HW_CACHE_OP_MAX, &len); 38761c45981SPaul Mackerras if (cache_op >= 0) { 388c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(cache_type, cache_op)) 38989812fc8SJiri Olsa return -EINVAL; 39070c90e4aSIan Rogers str += len + 1; 39170c90e4aSIan Rogers } else { 392c64e85e1SArnaldo Carvalho de Melo cache_result = parse_aliases(str, evsel__hw_cache_result, 39370c90e4aSIan Rogers PERF_COUNT_HW_CACHE_RESULT_MAX, &len); 39461c45981SPaul Mackerras if (cache_result >= 0) 39570c90e4aSIan Rogers str += len + 1; 39670c90e4aSIan Rogers } 39770c90e4aSIan Rogers } 39870c90e4aSIan Rogers if (str < name_end) { 39970c90e4aSIan Rogers if (cache_op < 0) { 40070c90e4aSIan Rogers cache_op = parse_aliases(str, evsel__hw_cache_op, 40170c90e4aSIan Rogers PERF_COUNT_HW_CACHE_OP_MAX, &len); 40270c90e4aSIan Rogers if (cache_op >= 0) { 40370c90e4aSIan Rogers if (!evsel__is_cache_op_valid(cache_type, cache_op)) 40470c90e4aSIan Rogers return -EINVAL; 40570c90e4aSIan Rogers } 40670c90e4aSIan Rogers } else if (cache_result < 0) { 40770c90e4aSIan Rogers cache_result = parse_aliases(str, evsel__hw_cache_result, 40870c90e4aSIan Rogers PERF_COUNT_HW_CACHE_RESULT_MAX, &len); 40961c45981SPaul Mackerras } 41061c45981SPaul Mackerras } 41161c45981SPaul Mackerras 41286470930SIngo Molnar /* 41386470930SIngo Molnar * Fall back to reads: 41486470930SIngo Molnar */ 4158953645fSIngo Molnar if (cache_op == -1) 4168953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 41786470930SIngo Molnar 41886470930SIngo Molnar /* 41986470930SIngo Molnar * Fall back to accesses: 42086470930SIngo Molnar */ 42186470930SIngo Molnar if (cache_result == -1) 42286470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 42386470930SIngo Molnar 424251aa040SIan Rogers *config = cache_type | (cache_op << 8) | (cache_result << 16); 425251aa040SIan Rogers if (perf_pmus__supports_extended_type()) 426251aa040SIan Rogers *config |= (__u64)extended_pmu_type << PERF_PMU_TYPE_SHIFT; 4276fd1e519SIan Rogers return 0; 4286fd1e519SIan Rogers } 4296fd1e519SIan Rogers 430411ad22eSIan Rogers /** 431411ad22eSIan Rogers * parse_events__filter_pmu - returns false if a wildcard PMU should be 432411ad22eSIan Rogers * considered, true if it should be filtered. 433411ad22eSIan Rogers */ 434411ad22eSIan Rogers bool parse_events__filter_pmu(const struct parse_events_state *parse_state, 435411ad22eSIan Rogers const struct perf_pmu *pmu) 436411ad22eSIan Rogers { 437411ad22eSIan Rogers if (parse_state->pmu_filter == NULL) 438411ad22eSIan Rogers return false; 439411ad22eSIan Rogers 440411ad22eSIan Rogers if (pmu->name == NULL) 441411ad22eSIan Rogers return true; 442411ad22eSIan Rogers 443411ad22eSIan Rogers return strcmp(parse_state->pmu_filter, pmu->name) != 0; 444411ad22eSIan Rogers } 445411ad22eSIan Rogers 4466fd1e519SIan Rogers int parse_events_add_cache(struct list_head *list, int *idx, const char *name, 447411ad22eSIan Rogers struct parse_events_state *parse_state, 4482bdf4d7eSIan Rogers struct list_head *head_config) 4496fd1e519SIan Rogers { 4502bdf4d7eSIan Rogers struct perf_pmu *pmu = NULL; 4512bdf4d7eSIan Rogers bool found_supported = false; 4522bdf4d7eSIan Rogers const char *config_name = get_config_name(head_config); 4532bdf4d7eSIan Rogers const char *metric_id = get_config_metric_id(head_config); 4546fd1e519SIan Rogers 4559d6a1df9SIan Rogers /* Legacy cache events are only supported by core PMUs. */ 4569d6a1df9SIan Rogers while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 4572bdf4d7eSIan Rogers LIST_HEAD(config_terms); 4582bdf4d7eSIan Rogers struct perf_event_attr attr; 4592bdf4d7eSIan Rogers int ret; 4602bdf4d7eSIan Rogers 461411ad22eSIan Rogers if (parse_events__filter_pmu(parse_state, pmu)) 462411ad22eSIan Rogers continue; 463411ad22eSIan Rogers 46489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 46589812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 4662bdf4d7eSIan Rogers 4672bdf4d7eSIan Rogers ret = parse_events__decode_legacy_cache(name, pmu->type, &attr.config); 4686fd1e519SIan Rogers if (ret) 4696fd1e519SIan Rogers return ret; 47043d0b978SWang Nan 4712bdf4d7eSIan Rogers found_supported = true; 4722bdf4d7eSIan Rogers 47343d0b978SWang Nan if (head_config) { 474411ad22eSIan Rogers if (config_attr(&attr, head_config, parse_state->error, config_term_common)) 47543d0b978SWang Nan return -EINVAL; 47643d0b978SWang Nan 47743d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 47843d0b978SWang Nan return -ENOMEM; 47943d0b978SWang Nan } 48030def61fSJin Yao 4812bdf4d7eSIan Rogers if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name, 4822bdf4d7eSIan Rogers metric_id, pmu, &config_terms, /*auto_merge_stats=*/false, 4832bdf4d7eSIan Rogers /*cpu_list=*/NULL) == NULL) 4842bdf4d7eSIan Rogers return -ENOMEM; 48530def61fSJin Yao 48699fc5941SAdrian Hunter free_config_terms(&config_terms); 4872bdf4d7eSIan Rogers } 4882bdf4d7eSIan Rogers return found_supported ? 0 : -EINVAL; 48986470930SIngo Molnar } 49086470930SIngo Molnar 491378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 492272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 493d81fa63bSIan Rogers const char *sys, const char *name, int column) 49419658171SJiri Olsa { 4954584f084SIan Rogers const char *str; 49619658171SJiri Olsa char help[BUFSIZ]; 49719658171SJiri Olsa 498ec183d22SAdrian Hunter if (!e) 499ec183d22SAdrian Hunter return; 500ec183d22SAdrian Hunter 50119658171SJiri Olsa /* 50219658171SJiri Olsa * We get error directly from syscall errno ( > 0), 50319658171SJiri Olsa * or from encoded pointer's error ( < 0). 50419658171SJiri Olsa */ 50519658171SJiri Olsa err = abs(err); 50619658171SJiri Olsa 50719658171SJiri Olsa switch (err) { 50819658171SJiri Olsa case EACCES: 5094584f084SIan Rogers str = "can't access trace events"; 51019658171SJiri Olsa break; 51119658171SJiri Olsa case ENOENT: 5124584f084SIan Rogers str = "unknown tracepoint"; 51319658171SJiri Olsa break; 51419658171SJiri Olsa default: 5154584f084SIan Rogers str = "failed to add tracepoint"; 51619658171SJiri Olsa break; 51719658171SJiri Olsa } 51819658171SJiri Olsa 51919658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 520d81fa63bSIan Rogers parse_events_error__handle(e, column, strdup(str), strdup(help)); 52119658171SJiri Olsa } 52219658171SJiri Olsa 523c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 5248c619d6aSWang Nan const char *sys_name, const char *evt_name, 525272ed29aSJiri Olsa struct parse_events_error *err, 526d81fa63bSIan Rogers struct list_head *head_config, void *loc_) 527bcd3279fSFrederic Weisbecker { 528d81fa63bSIan Rogers YYLTYPE *loc = loc_; 5298f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 530bcd3279fSFrederic Weisbecker 53119658171SJiri Olsa if (IS_ERR(evsel)) { 532d81fa63bSIan Rogers tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name, loc->first_column); 5338dd2a131SJiri Olsa return PTR_ERR(evsel); 53419658171SJiri Olsa } 535bcd3279fSFrederic Weisbecker 536e637d177SHe Kuang if (head_config) { 537e637d177SHe Kuang LIST_HEAD(config_terms); 538e637d177SHe Kuang 539e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 540e637d177SHe Kuang return -ENOMEM; 541e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 542e637d177SHe Kuang } 543e637d177SHe Kuang 544b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 54582fe1c29SArnaldo Carvalho de Melo return 0; 546bcd3279fSFrederic Weisbecker } 547bcd3279fSFrederic Weisbecker 548c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5498c619d6aSWang Nan const char *sys_name, const char *evt_name, 550272ed29aSJiri Olsa struct parse_events_error *err, 551d81fa63bSIan Rogers struct list_head *head_config, YYLTYPE *loc) 552bcd3279fSFrederic Weisbecker { 55325a7d914SArnaldo Carvalho de Melo char *evt_path; 554bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 555bcd3279fSFrederic Weisbecker DIR *evt_dir; 55627bf90bfSJiri Olsa int ret = 0, found = 0; 557bcd3279fSFrederic Weisbecker 55825a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 55925a7d914SArnaldo Carvalho de Melo if (!evt_path) { 560d81fa63bSIan Rogers tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 56125a7d914SArnaldo Carvalho de Melo return -1; 56225a7d914SArnaldo Carvalho de Melo } 563bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 564bcd3279fSFrederic Weisbecker if (!evt_dir) { 56525a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 566d81fa63bSIan Rogers tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 56789812fc8SJiri Olsa return -1; 568bcd3279fSFrederic Weisbecker } 569bcd3279fSFrederic Weisbecker 57089812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 571bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 572bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 573bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 574bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 575bcd3279fSFrederic Weisbecker continue; 576bcd3279fSFrederic Weisbecker 57789812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 578fb1d2edfSMasami Hiramatsu continue; 579fb1d2edfSMasami Hiramatsu 58027bf90bfSJiri Olsa found++; 58127bf90bfSJiri Olsa 582e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 583d81fa63bSIan Rogers err, head_config, loc); 584bcd3279fSFrederic Weisbecker } 585bcd3279fSFrederic Weisbecker 58627bf90bfSJiri Olsa if (!found) { 587d81fa63bSIan Rogers tracepoint_error(err, ENOENT, sys_name, evt_name, loc->first_column); 58827bf90bfSJiri Olsa ret = -1; 58927bf90bfSJiri Olsa } 59027bf90bfSJiri Olsa 59125a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 5920bd3f084SJiri Olsa closedir(evt_dir); 59389812fc8SJiri Olsa return ret; 594bcd3279fSFrederic Weisbecker } 595bcd3279fSFrederic Weisbecker 596c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5978c619d6aSWang Nan const char *sys_name, const char *evt_name, 598272ed29aSJiri Olsa struct parse_events_error *err, 599d81fa63bSIan Rogers struct list_head *head_config, YYLTYPE *loc) 600f35488f9SJiri Olsa { 601f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 602e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 603d81fa63bSIan Rogers err, head_config, loc) : 604e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 605d81fa63bSIan Rogers err, head_config, loc); 606f35488f9SJiri Olsa } 607f35488f9SJiri Olsa 608c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 6098c619d6aSWang Nan const char *sys_name, const char *evt_name, 610272ed29aSJiri Olsa struct parse_events_error *err, 611d81fa63bSIan Rogers struct list_head *head_config, YYLTYPE *loc) 612f35488f9SJiri Olsa { 613f35488f9SJiri Olsa struct dirent *events_ent; 614f35488f9SJiri Olsa DIR *events_dir; 615f35488f9SJiri Olsa int ret = 0; 616f35488f9SJiri Olsa 6177014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 618f35488f9SJiri Olsa if (!events_dir) { 619d81fa63bSIan Rogers tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 620f35488f9SJiri Olsa return -1; 621f35488f9SJiri Olsa } 622f35488f9SJiri Olsa 623f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 624f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 625f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 626f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 627f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 628f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 629f35488f9SJiri Olsa continue; 630f35488f9SJiri Olsa 631f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 632f35488f9SJiri Olsa continue; 633f35488f9SJiri Olsa 634f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 635d81fa63bSIan Rogers evt_name, err, head_config, loc); 636f35488f9SJiri Olsa } 637f35488f9SJiri Olsa 638f35488f9SJiri Olsa closedir(events_dir); 639f35488f9SJiri Olsa return ret; 640f35488f9SJiri Olsa } 641378ef0f5SIan Rogers #endif /* HAVE_LIBTRACEEVENT */ 642f35488f9SJiri Olsa 64389812fc8SJiri Olsa static int 64489812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 6451b290d67SFrederic Weisbecker { 6461b290d67SFrederic Weisbecker int i; 6471b290d67SFrederic Weisbecker 6481b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 64989812fc8SJiri Olsa if (!type || !type[i]) 6501b290d67SFrederic Weisbecker break; 6511b290d67SFrederic Weisbecker 6527582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 6537582732fSJiri Olsa do { \ 6547582732fSJiri Olsa if (attr->bp_type & bit) \ 6557582732fSJiri Olsa return -EINVAL; \ 6567582732fSJiri Olsa else \ 6577582732fSJiri Olsa attr->bp_type |= bit; \ 6587582732fSJiri Olsa } while (0) 6597582732fSJiri Olsa 6601b290d67SFrederic Weisbecker switch (type[i]) { 6611b290d67SFrederic Weisbecker case 'r': 6627582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 6631b290d67SFrederic Weisbecker break; 6641b290d67SFrederic Weisbecker case 'w': 6657582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 6661b290d67SFrederic Weisbecker break; 6671b290d67SFrederic Weisbecker case 'x': 6687582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 6691b290d67SFrederic Weisbecker break; 6701b290d67SFrederic Weisbecker default: 67189812fc8SJiri Olsa return -EINVAL; 6721b290d67SFrederic Weisbecker } 6731b290d67SFrederic Weisbecker } 67489812fc8SJiri Olsa 6757582732fSJiri Olsa #undef CHECK_SET_TYPE 6767582732fSJiri Olsa 6771b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 6781b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 6791b290d67SFrederic Weisbecker 68089812fc8SJiri Olsa return 0; 6811b290d67SFrederic Weisbecker } 6821b290d67SFrederic Weisbecker 683f0617f52SAdrian Hunter int parse_events_add_breakpoint(struct parse_events_state *parse_state, 684f0617f52SAdrian Hunter struct list_head *list, 685f0617f52SAdrian Hunter u64 addr, char *type, u64 len, 686f0617f52SAdrian Hunter struct list_head *head_config __maybe_unused) 6871b290d67SFrederic Weisbecker { 68889812fc8SJiri Olsa struct perf_event_attr attr; 689f0617f52SAdrian Hunter LIST_HEAD(config_terms); 690f0617f52SAdrian Hunter const char *name; 6911b290d67SFrederic Weisbecker 69289812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 693aa98d848SIan Rogers attr.bp_addr = addr; 6941b290d67SFrederic Weisbecker 69589812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 69689812fc8SJiri Olsa return -EINVAL; 6971b290d67SFrederic Weisbecker 6983741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 6993741eb9fSJacob Shin if (!len) { 70089812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 7013741eb9fSJacob Shin len = sizeof(long); 702aa59a485SFrederic Weisbecker else 7033741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 7043741eb9fSJacob Shin } 7053741eb9fSJacob Shin 7063741eb9fSJacob Shin attr.bp_len = len; 707aa59a485SFrederic Weisbecker 70889812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 7094a841d65SJovi Zhang attr.sample_period = 1; 7101b290d67SFrederic Weisbecker 711f0617f52SAdrian Hunter if (head_config) { 712f0617f52SAdrian Hunter if (config_attr(&attr, head_config, parse_state->error, 713f0617f52SAdrian Hunter config_term_common)) 714f0617f52SAdrian Hunter return -EINVAL; 715f0617f52SAdrian Hunter 716f0617f52SAdrian Hunter if (get_config_terms(head_config, &config_terms)) 717f0617f52SAdrian Hunter return -ENOMEM; 718f0617f52SAdrian Hunter } 719f0617f52SAdrian Hunter 720f0617f52SAdrian Hunter name = get_config_name(head_config); 721f0617f52SAdrian Hunter 722f0617f52SAdrian Hunter return add_event(list, &parse_state->idx, &attr, name, /*mertic_id=*/NULL, 723f0617f52SAdrian Hunter &config_terms); 7241b290d67SFrederic Weisbecker } 7251b290d67SFrederic Weisbecker 7263b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 7273b0e371cSJiri Olsa struct parse_events_error *err, 7283b0e371cSJiri Olsa int type) 7293b0e371cSJiri Olsa { 7303b0e371cSJiri Olsa if (type == term->type_val) 7313b0e371cSJiri Olsa return 0; 7323b0e371cSJiri Olsa 7333b0e371cSJiri Olsa if (err) { 7346c191289SIan Rogers parse_events_error__handle(err, term->err_val, 735448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 736448d732cSIan Rogers ? strdup("expected numeric value") 737448d732cSIan Rogers : strdup("expected string value"), 738448d732cSIan Rogers NULL); 7393b0e371cSJiri Olsa } 7403b0e371cSJiri Olsa return -EINVAL; 7413b0e371cSJiri Olsa } 7423b0e371cSJiri Olsa 74317cb5f84SWang Nan /* 74417cb5f84SWang Nan * Update according to parse-events.l 74517cb5f84SWang Nan */ 74617cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 74717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 74817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 74917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 75017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 751204e7c49SRob Herring [PARSE_EVENTS__TERM_TYPE_CONFIG3] = "config3", 75217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 75317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 75417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 75517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 75617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 75717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 75817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 75917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 76017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 761792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 7622fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 763626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 764626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 765dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 766064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 7671b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 768eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 7692b62b3a6SIan Rogers [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id", 77070c90e4aSIan Rogers [PARSE_EVENTS__TERM_TYPE_RAW] = "raw", 7716fd1e519SIan Rogers [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] = "legacy-cache", 7725ea8f2ccSIan Rogers [PARSE_EVENTS__TERM_TYPE_HARDWARE] = "hardware", 77317cb5f84SWang Nan }; 77417cb5f84SWang Nan 7751669e509SWang Nan static bool config_term_shrinked; 7761669e509SWang Nan 7771669e509SWang Nan static bool 7781669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 7791669e509SWang Nan { 780448d732cSIan Rogers char *err_str; 781448d732cSIan Rogers 7821669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 7836c191289SIan Rogers parse_events_error__handle(err, -1, 784448d732cSIan Rogers strdup("Invalid term_type"), NULL); 7851669e509SWang Nan return false; 7861669e509SWang Nan } 7871669e509SWang Nan if (!config_term_shrinked) 7881669e509SWang Nan return true; 7891669e509SWang Nan 7901669e509SWang Nan switch (term_type) { 7911669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 7921669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 7931669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 794204e7c49SRob Herring case PARSE_EVENTS__TERM_TYPE_CONFIG3: 7951669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 7962b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 79706835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 798064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 7991669e509SWang Nan return true; 8001669e509SWang Nan default: 8011669e509SWang Nan if (!err) 8021669e509SWang Nan return false; 8031669e509SWang Nan 8041669e509SWang Nan /* term_type is validated so indexing is safe */ 805448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 806448d732cSIan Rogers config_term_names[term_type]) >= 0) 8076c191289SIan Rogers parse_events_error__handle(err, -1, err_str, NULL); 8081669e509SWang Nan return false; 8091669e509SWang Nan } 8101669e509SWang Nan } 8111669e509SWang Nan 8121669e509SWang Nan void parse_events__shrink_config_terms(void) 8131669e509SWang Nan { 8141669e509SWang Nan config_term_shrinked = true; 8151669e509SWang Nan } 8161669e509SWang Nan 8170b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 8183b0e371cSJiri Olsa struct parse_events_term *term, 8193b0e371cSJiri Olsa struct parse_events_error *err) 8208f707d84SJiri Olsa { 82116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 82216fa7e82SJiri Olsa do { \ 8233b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 82416fa7e82SJiri Olsa return -EINVAL; \ 82516fa7e82SJiri Olsa } while (0) 82616fa7e82SJiri Olsa 82716fa7e82SJiri Olsa switch (term->type_term) { 8288f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 82916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 8308f707d84SJiri Olsa attr->config = term->val.num; 8318f707d84SJiri Olsa break; 8328f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 83316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 8348f707d84SJiri Olsa attr->config1 = term->val.num; 8358f707d84SJiri Olsa break; 8368f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 83716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 8388f707d84SJiri Olsa attr->config2 = term->val.num; 8398f707d84SJiri Olsa break; 840204e7c49SRob Herring case PARSE_EVENTS__TERM_TYPE_CONFIG3: 841204e7c49SRob Herring CHECK_TYPE_VAL(NUM); 842204e7c49SRob Herring attr->config3 = term->val.num; 843204e7c49SRob Herring break; 8448f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 84516fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 8468f707d84SJiri Olsa break; 84709af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 84809af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 84909af2a55SNamhyung Kim break; 8508f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 851ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 852ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 853448d732cSIan Rogers parse_branch_str(term->val.str, 854448d732cSIan Rogers &attr->branch_sample_type)) { 8556c191289SIan Rogers parse_events_error__handle(err, term->err_val, 856448d732cSIan Rogers strdup("invalid branch sample type"), 857448d732cSIan Rogers NULL); 858ac12f676SAndi Kleen return -EINVAL; 859ac12f676SAndi Kleen } 8608f707d84SJiri Olsa break; 86132067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 86232067712SKan Liang CHECK_TYPE_VAL(NUM); 86332067712SKan Liang if (term->val.num > 1) { 8646c191289SIan Rogers parse_events_error__handle(err, term->err_val, 865448d732cSIan Rogers strdup("expected 0 or 1"), 866448d732cSIan Rogers NULL); 86732067712SKan Liang return -EINVAL; 86832067712SKan Liang } 86932067712SKan Liang break; 870d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 871d457c963SKan Liang CHECK_TYPE_VAL(STR); 872d457c963SKan Liang break; 873d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 874d457c963SKan Liang CHECK_TYPE_VAL(NUM); 875d457c963SKan Liang break; 876374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 877374ce938SWang Nan CHECK_TYPE_VAL(NUM); 878374ce938SWang Nan break; 879374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 880374ce938SWang Nan CHECK_TYPE_VAL(NUM); 881374ce938SWang Nan break; 882626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 883626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 884626a6b78SWang Nan break; 885626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 886626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 887626a6b78SWang Nan break; 8886b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 8896b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 8906b5fc39bSJiri Olsa break; 8912b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 8922b62b3a6SIan Rogers CHECK_TYPE_VAL(STR); 8932b62b3a6SIan Rogers break; 89470c90e4aSIan Rogers case PARSE_EVENTS__TERM_TYPE_RAW: 89570c90e4aSIan Rogers CHECK_TYPE_VAL(STR); 89670c90e4aSIan Rogers break; 897792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 898792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 899792d48b4SArnaldo Carvalho de Melo break; 9002fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 9012fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 9022fda5adaSArnaldo Carvalho de Melo break; 903064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 904064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 905064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 9066c191289SIan Rogers parse_events_error__handle(err, term->err_val, 907448d732cSIan Rogers strdup("expected 0 or 1"), 908448d732cSIan Rogers NULL); 909064b4e82SJin Yao return -EINVAL; 910064b4e82SJin Yao } 911064b4e82SJin Yao break; 9121b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 9131b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 9141b992154SAdrian Hunter break; 915eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 916eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 917eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 9186c191289SIan Rogers parse_events_error__handle(err, term->err_val, 919eb7a52d4SAdrian Hunter strdup("too big"), 920eb7a52d4SAdrian Hunter NULL); 921eb7a52d4SAdrian Hunter return -EINVAL; 922eb7a52d4SAdrian Hunter } 923eb7a52d4SAdrian Hunter break; 9248f707d84SJiri Olsa default: 9256c191289SIan Rogers parse_events_error__handle(err, term->err_term, 926448d732cSIan Rogers strdup("unknown term"), 927448d732cSIan Rogers parse_events_formats_error_string(NULL)); 9288f707d84SJiri Olsa return -EINVAL; 9298f707d84SJiri Olsa } 93016fa7e82SJiri Olsa 9311669e509SWang Nan /* 9324d39c89fSIngo Molnar * Check term availability after basic checking so 9331669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 9341669e509SWang Nan * 9354d39c89fSIngo Molnar * If check availability at the entry of this function, 9361669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 9371669e509SWang Nan * if an invalid config term is provided for legacy events 9381669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 9391669e509SWang Nan */ 9401669e509SWang Nan if (!config_term_avail(term->type_term, err)) 9411669e509SWang Nan return -EINVAL; 9428f707d84SJiri Olsa return 0; 94316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 9448f707d84SJiri Olsa } 9458f707d84SJiri Olsa 9460b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 9470b8891a8SHe Kuang struct parse_events_term *term, 9480b8891a8SHe Kuang struct parse_events_error *err) 9490b8891a8SHe Kuang { 9506fd1e519SIan Rogers if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) { 9511eaf496eSIan Rogers const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type); 9526fd1e519SIan Rogers 9535b10c18dSIan Rogers if (!pmu) { 9545b10c18dSIan Rogers char *err_str; 9555b10c18dSIan Rogers 9565b10c18dSIan Rogers if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0) 9575b10c18dSIan Rogers parse_events_error__handle(err, term->err_term, 9585b10c18dSIan Rogers err_str, /*help=*/NULL); 9595b10c18dSIan Rogers return -EINVAL; 9605b10c18dSIan Rogers } 9616fd1e519SIan Rogers if (perf_pmu__supports_legacy_cache(pmu)) { 9626fd1e519SIan Rogers attr->type = PERF_TYPE_HW_CACHE; 9636fd1e519SIan Rogers return parse_events__decode_legacy_cache(term->config, pmu->type, 9646fd1e519SIan Rogers &attr->config); 9656fd1e519SIan Rogers } else 9666fd1e519SIan Rogers term->type_term = PARSE_EVENTS__TERM_TYPE_USER; 9676fd1e519SIan Rogers } 9685ea8f2ccSIan Rogers if (term->type_term == PARSE_EVENTS__TERM_TYPE_HARDWARE) { 9691eaf496eSIan Rogers const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type); 9705ea8f2ccSIan Rogers 9715ea8f2ccSIan Rogers if (!pmu) { 972251aa040SIan Rogers char *err_str; 973251aa040SIan Rogers 974251aa040SIan Rogers if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0) 975251aa040SIan Rogers parse_events_error__handle(err, term->err_term, 976251aa040SIan Rogers err_str, /*help=*/NULL); 9775ea8f2ccSIan Rogers return -EINVAL; 9785ea8f2ccSIan Rogers } 9795ea8f2ccSIan Rogers attr->type = PERF_TYPE_HARDWARE; 980251aa040SIan Rogers attr->config = term->val.num; 981251aa040SIan Rogers if (perf_pmus__supports_extended_type()) 982251aa040SIan Rogers attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; 9835ea8f2ccSIan Rogers return 0; 9845ea8f2ccSIan Rogers } 985dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 9866fd1e519SIan Rogers term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) { 9870b8891a8SHe Kuang /* 9880b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 9890b8891a8SHe Kuang * at this point what type they need to have. 9900b8891a8SHe Kuang */ 9910b8891a8SHe Kuang return 0; 9926fd1e519SIan Rogers } 9930b8891a8SHe Kuang return config_term_common(attr, term, err); 9940b8891a8SHe Kuang } 9950b8891a8SHe Kuang 996378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 997e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 998e637d177SHe Kuang struct parse_events_term *term, 999e637d177SHe Kuang struct parse_events_error *err) 1000e637d177SHe Kuang { 1001e637d177SHe Kuang switch (term->type_term) { 1002e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1003e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1004374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1005374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1006792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 10072fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1008626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1009626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 10101b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1011eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1012e637d177SHe Kuang return config_term_common(attr, term, err); 1013e637d177SHe Kuang default: 1014e637d177SHe Kuang if (err) { 10156c191289SIan Rogers parse_events_error__handle(err, term->err_term, 1016448d732cSIan Rogers strdup("unknown term"), 1017448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1018e637d177SHe Kuang } 1019e637d177SHe Kuang return -EINVAL; 1020e637d177SHe Kuang } 1021e637d177SHe Kuang 1022e637d177SHe Kuang return 0; 1023e637d177SHe Kuang } 1024378ef0f5SIan Rogers #endif 1025e637d177SHe Kuang 10268f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 10273b0e371cSJiri Olsa struct list_head *head, 10280b8891a8SHe Kuang struct parse_events_error *err, 10290b8891a8SHe Kuang config_term_func_t config_term) 10308f707d84SJiri Olsa { 10316cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 10328f707d84SJiri Olsa 10338f707d84SJiri Olsa list_for_each_entry(term, head, list) 10343b0e371cSJiri Olsa if (config_term(attr, term, err)) 10358f707d84SJiri Olsa return -EINVAL; 10368f707d84SJiri Olsa 10378f707d84SJiri Olsa return 0; 10388f707d84SJiri Olsa } 10398f707d84SJiri Olsa 1040930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1041930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1042930a2e29SJiri Olsa { 104305e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 104435ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *__t; \ 1045930a2e29SJiri Olsa \ 1046930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1047930a2e29SJiri Olsa if (!__t) \ 1048930a2e29SJiri Olsa return -ENOMEM; \ 1049930a2e29SJiri Olsa \ 1050930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 105135ac0cadSArnaldo Carvalho de Melo __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 105205e54e23SIan Rogers __t->weak = __weak; \ 1053e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1054e884602bSLeo Yan 105505e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1056e884602bSLeo Yan do { \ 105705e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1058e884602bSLeo Yan __t->val.__name = __val; \ 1059e884602bSLeo Yan } while (0) 1060e884602bSLeo Yan 106105e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1062e884602bSLeo Yan do { \ 106305e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 10643220fb8dSLeo Yan __t->val.str = strdup(__val); \ 10653220fb8dSLeo Yan if (!__t->val.str) { \ 10663220fb8dSLeo Yan zfree(&__t); \ 10673220fb8dSLeo Yan return -ENOMEM; \ 10683220fb8dSLeo Yan } \ 10693220fb8dSLeo Yan __t->free_str = true; \ 1070930a2e29SJiri Olsa } while (0) 1071930a2e29SJiri Olsa 1072930a2e29SJiri Olsa struct parse_events_term *term; 1073930a2e29SJiri Olsa 1074930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1075930a2e29SJiri Olsa switch (term->type_term) { 1076ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 107705e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 107832067712SKan Liang break; 107909af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 108005e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 108109af2a55SNamhyung Kim break; 108232067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 108305e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 108432067712SKan Liang break; 1085d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 108605e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1087d457c963SKan Liang break; 1088ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 108905e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1090ac12f676SAndi Kleen break; 1091d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1092e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 109305e54e23SIan Rogers term->val.num, term->weak); 1094d457c963SKan Liang break; 1095374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1096e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 109705e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1098374ce938SWang Nan break; 1099374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1100e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 110105e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1102374ce938SWang Nan break; 1103792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1104e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 110505e54e23SIan Rogers term->val.num, term->weak); 1106792d48b4SArnaldo Carvalho de Melo break; 11072fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1108e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 110905e54e23SIan Rogers term->val.num, term->weak); 11102fda5adaSArnaldo Carvalho de Melo break; 1111626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1112e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 111305e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1114626a6b78SWang Nan break; 1115626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1116e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 111705e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1118626a6b78SWang Nan break; 1119dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 112005e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1121dd60fba7SMathieu Poirier break; 1122064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1123e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 112405e54e23SIan Rogers term->val.num ? true : false, term->weak); 1125064b4e82SJin Yao break; 11261b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1127e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 112805e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 11291b992154SAdrian Hunter break; 1130eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1131e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 113205e54e23SIan Rogers term->val.num, term->weak); 1133eb7a52d4SAdrian Hunter break; 1134930a2e29SJiri Olsa default: 1135930a2e29SJiri Olsa break; 1136930a2e29SJiri Olsa } 1137930a2e29SJiri Olsa } 1138a1ac7de6SAdrian Hunter return 0; 1139a1ac7de6SAdrian Hunter } 1140a1ac7de6SAdrian Hunter 1141a1ac7de6SAdrian Hunter /* 114235ac0cadSArnaldo Carvalho de Melo * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1143a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1144a1ac7de6SAdrian Hunter */ 1145a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1146a1ac7de6SAdrian Hunter struct list_head *head_terms) 1147a1ac7de6SAdrian Hunter { 1148a1ac7de6SAdrian Hunter struct parse_events_term *term; 1149a1ac7de6SAdrian Hunter u64 bits = 0; 1150a1ac7de6SAdrian Hunter int type; 1151a1ac7de6SAdrian Hunter 1152a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1153a1ac7de6SAdrian Hunter switch (term->type_term) { 1154a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 11557eb54733SIan Rogers type = perf_pmu__format_type(pmu, term->config); 1156a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1157a1ac7de6SAdrian Hunter continue; 1158da6a5afdSIan Rogers bits |= perf_pmu__format_bits(pmu, term->config); 1159a1ac7de6SAdrian Hunter break; 1160a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1161a1ac7de6SAdrian Hunter bits = ~(u64)0; 1162a1ac7de6SAdrian Hunter break; 1163a1ac7de6SAdrian Hunter default: 1164a1ac7de6SAdrian Hunter break; 1165a1ac7de6SAdrian Hunter } 1166a1ac7de6SAdrian Hunter } 1167a1ac7de6SAdrian Hunter 1168a1ac7de6SAdrian Hunter if (bits) 116905e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1170a1ac7de6SAdrian Hunter 1171a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1172930a2e29SJiri Olsa return 0; 1173930a2e29SJiri Olsa } 1174930a2e29SJiri Olsa 1175e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 11768c619d6aSWang Nan const char *sys, const char *event, 1177272ed29aSJiri Olsa struct parse_events_error *err, 1178d81fa63bSIan Rogers struct list_head *head_config, void *loc_) 1179e637d177SHe Kuang { 1180d81fa63bSIan Rogers YYLTYPE *loc = loc_; 1181378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1182e637d177SHe Kuang if (head_config) { 1183e637d177SHe Kuang struct perf_event_attr attr; 1184e637d177SHe Kuang 1185272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1186e637d177SHe Kuang config_term_tracepoint)) 1187e637d177SHe Kuang return -EINVAL; 1188e637d177SHe Kuang } 1189e637d177SHe Kuang 1190e637d177SHe Kuang if (strpbrk(sys, "*?")) 1191e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1192d81fa63bSIan Rogers err, head_config, loc); 1193e637d177SHe Kuang else 1194e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1195d81fa63bSIan Rogers err, head_config, loc); 1196378ef0f5SIan Rogers #else 1197378ef0f5SIan Rogers (void)list; 1198378ef0f5SIan Rogers (void)idx; 1199378ef0f5SIan Rogers (void)sys; 1200378ef0f5SIan Rogers (void)event; 1201378ef0f5SIan Rogers (void)head_config; 1202d81fa63bSIan Rogers parse_events_error__handle(err, loc->first_column, strdup("unsupported tracepoint"), 1203378ef0f5SIan Rogers strdup("libtraceevent is necessary for tracepoint support")); 1204378ef0f5SIan Rogers return -1; 1205378ef0f5SIan Rogers #endif 1206e637d177SHe Kuang } 1207e637d177SHe Kuang 12088bc75f69SIan Rogers static int __parse_events_add_numeric(struct parse_events_state *parse_state, 120987d650beSJiri Olsa struct list_head *list, 1210251aa040SIan Rogers struct perf_pmu *pmu, u32 type, u32 extended_type, 1211251aa040SIan Rogers u64 config, struct list_head *head_config) 121274d5b588SJaswinder Singh Rajput { 121389812fc8SJiri Olsa struct perf_event_attr attr; 1214930a2e29SJiri Olsa LIST_HEAD(config_terms); 12152b62b3a6SIan Rogers const char *name, *metric_id; 12169cbfa2f6SJin Yao int ret; 121774d5b588SJaswinder Singh Rajput 121889812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 121989812fc8SJiri Olsa attr.type = type; 122089812fc8SJiri Olsa attr.config = config; 1221251aa040SIan Rogers if (extended_type && (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)) { 1222251aa040SIan Rogers assert(perf_pmus__supports_extended_type()); 1223251aa040SIan Rogers attr.config |= (u64)extended_type << PERF_PMU_TYPE_SHIFT; 1224240de691Sbaomingtong001@208suo.com } 12258f707d84SJiri Olsa 1226930a2e29SJiri Olsa if (head_config) { 12275d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 12280b8891a8SHe Kuang config_term_common)) 12298f707d84SJiri Olsa return -EINVAL; 12308f707d84SJiri Olsa 1231930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1232930a2e29SJiri Olsa return -ENOMEM; 1233930a2e29SJiri Olsa } 1234930a2e29SJiri Olsa 12352b62b3a6SIan Rogers name = get_config_name(head_config); 12362b62b3a6SIan Rogers metric_id = get_config_metric_id(head_config); 12378bc75f69SIan Rogers ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name, 12388bc75f69SIan Rogers metric_id, pmu, &config_terms, /*auto_merge_stats=*/false, 12398bc75f69SIan Rogers /*cpu_list=*/NULL) ? 0 : -ENOMEM; 124099fc5941SAdrian Hunter free_config_terms(&config_terms); 124199fc5941SAdrian Hunter return ret; 1242b908debdSIngo Molnar } 1243b908debdSIngo Molnar 12448bc75f69SIan Rogers int parse_events_add_numeric(struct parse_events_state *parse_state, 12458bc75f69SIan Rogers struct list_head *list, 12468bc75f69SIan Rogers u32 type, u64 config, 12478bc75f69SIan Rogers struct list_head *head_config, 12488bc75f69SIan Rogers bool wildcard) 12498bc75f69SIan Rogers { 12508bc75f69SIan Rogers struct perf_pmu *pmu = NULL; 12518bc75f69SIan Rogers bool found_supported = false; 12528bc75f69SIan Rogers 12539d6a1df9SIan Rogers /* Wildcards on numeric values are only supported by core PMUs. */ 1254251aa040SIan Rogers if (wildcard && perf_pmus__supports_extended_type()) { 12559d6a1df9SIan Rogers while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 12568bc75f69SIan Rogers int ret; 12578bc75f69SIan Rogers 1258251aa040SIan Rogers found_supported = true; 1259411ad22eSIan Rogers if (parse_events__filter_pmu(parse_state, pmu)) 1260411ad22eSIan Rogers continue; 1261411ad22eSIan Rogers 1262251aa040SIan Rogers ret = __parse_events_add_numeric(parse_state, list, pmu, 1263251aa040SIan Rogers type, pmu->type, 12648bc75f69SIan Rogers config, head_config); 12658bc75f69SIan Rogers if (ret) 12668bc75f69SIan Rogers return ret; 12678bc75f69SIan Rogers } 1268251aa040SIan Rogers if (found_supported) 1269251aa040SIan Rogers return 0; 1270251aa040SIan Rogers } 1271251aa040SIan Rogers return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_type(type), 1272251aa040SIan Rogers type, /*extended_type=*/0, config, head_config); 12738bc75f69SIan Rogers } 12748bc75f69SIan Rogers 1275f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1276f0fbb114SAndi Kleen struct list_head *list, 12778228e936SIan Rogers int tool_event) 1278f0fbb114SAndi Kleen { 1279f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1280f0fbb114SAndi Kleen } 1281f0fbb114SAndi Kleen 1282064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1283064b4e82SJin Yao { 128435ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *term; 1285064b4e82SJin Yao 1286064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 128735ac0cadSArnaldo Carvalho de Melo if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1288064b4e82SJin Yao return term->val.percore; 1289064b4e82SJin Yao } 1290064b4e82SJin Yao 1291064b4e82SJin Yao return false; 1292064b4e82SJin Yao } 1293064b4e82SJin Yao 1294c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 129536adec85SJiri Olsa struct list_head *list, char *name, 12963cdc5c2cSKan Liang struct list_head *head_config, 129781a4e31fSIan Rogers bool auto_merge_stats, void *loc_) 12985f537a26SJiri Olsa { 12995f537a26SJiri Olsa struct perf_event_attr attr; 130046441bdcSMatt Fleming struct perf_pmu_info info; 13015f537a26SJiri Olsa struct perf_pmu *pmu; 130232dcd021SJiri Olsa struct evsel *evsel; 1303333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 130481a4e31fSIan Rogers YYLTYPE *loc = loc_; 1305930a2e29SJiri Olsa LIST_HEAD(config_terms); 13065f537a26SJiri Olsa 13071eaf496eSIan Rogers pmu = parse_state->fake_pmu ?: perf_pmus__find(name); 1308f034fc50SAdrian Hunter 1309f034fc50SAdrian Hunter if (verbose > 1 && !(pmu && pmu->selectable)) { 13104ac22b48SIan Rogers fprintf(stderr, "Attempting to add event pmu '%s' with '", 13114ac22b48SIan Rogers name); 13124ac22b48SIan Rogers if (head_config) { 13134ac22b48SIan Rogers struct parse_events_term *term; 13144ac22b48SIan Rogers 13154ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 13164ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 13174ac22b48SIan Rogers } 13184ac22b48SIan Rogers } 13194ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 13204ac22b48SIan Rogers } 13214ac22b48SIan Rogers 1322333b5665SAndi Kleen if (!pmu) { 1323448d732cSIan Rogers char *err_str; 1324448d732cSIan Rogers 1325448d732cSIan Rogers if (asprintf(&err_str, 1326333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1327448d732cSIan Rogers name) >= 0) 132881a4e31fSIan Rogers parse_events_error__handle(err, loc->first_column, err_str, NULL); 13295f537a26SJiri Olsa return -EINVAL; 1330333b5665SAndi Kleen } 133170c90e4aSIan Rogers if (head_config) 133270c90e4aSIan Rogers fix_raw(head_config, pmu); 13335f537a26SJiri Olsa 1334dc0a6202SAdrian Hunter if (pmu->default_config) { 1335dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1336dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1337dc0a6202SAdrian Hunter } else { 13385f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1339dc0a6202SAdrian Hunter } 1340c9aeb2e9SIan Rogers attr.type = pmu->type; 13415f537a26SJiri Olsa 1342ad962273SAdrian Hunter if (!head_config) { 13432b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, 13442b62b3a6SIan Rogers /*init_attr=*/true, /*name=*/NULL, 13452b62b3a6SIan Rogers /*metric_id=*/NULL, pmu, 13462b62b3a6SIan Rogers /*config_terms=*/NULL, auto_merge_stats, 13472b62b3a6SIan Rogers /*cpu_list=*/NULL); 1348cae256aeSIan Rogers return evsel ? 0 : -ENOMEM; 1349ad962273SAdrian Hunter } 1350ad962273SAdrian Hunter 1351*9d31cb93SIan Rogers if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info, err)) 1352a6146d50SZheng Yan return -EINVAL; 1353a6146d50SZheng Yan 13544ac22b48SIan Rogers if (verbose > 1) { 13554ac22b48SIan Rogers fprintf(stderr, "After aliases, add event pmu '%s' with '", 13564ac22b48SIan Rogers name); 13574ac22b48SIan Rogers if (head_config) { 13584ac22b48SIan Rogers struct parse_events_term *term; 13594ac22b48SIan Rogers 13604ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 13614ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 13624ac22b48SIan Rogers } 13634ac22b48SIan Rogers } 13644ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 13654ac22b48SIan Rogers } 13664ac22b48SIan Rogers 13675f537a26SJiri Olsa /* 13685f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 13695f537a26SJiri Olsa * return value when called with fail == 0 ;) 13705f537a26SJiri Olsa */ 13715d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1372c056ba6aSJiri Olsa return -EINVAL; 13735f537a26SJiri Olsa 1374930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1375930a2e29SJiri Olsa return -ENOMEM; 1376930a2e29SJiri Olsa 1377a1ac7de6SAdrian Hunter /* 1378a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1379a1ac7de6SAdrian Hunter * changed by the user. 1380a1ac7de6SAdrian Hunter */ 1381a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1382a1ac7de6SAdrian Hunter return -ENOMEM; 1383a1ac7de6SAdrian Hunter 1384387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 1385a7d212fcSAdrian Hunter free_config_terms(&config_terms); 13865f537a26SJiri Olsa return -EINVAL; 138738f2c422SIan Rogers } 13885f537a26SJiri Olsa 13892b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true, 13902b62b3a6SIan Rogers get_config_name(head_config), 13912b62b3a6SIan Rogers get_config_metric_id(head_config), pmu, 13922b62b3a6SIan Rogers &config_terms, auto_merge_stats, /*cpu_list=*/NULL); 139333321a06SIan Rogers if (!evsel) 139433321a06SIan Rogers return -ENOMEM; 139533321a06SIan Rogers 139612279429SJin Yao if (evsel->name) 139712279429SJin Yao evsel->use_config_name = true; 139812279429SJin Yao 139933321a06SIan Rogers evsel->percore = config_term_percore(&evsel->config_terms); 140033321a06SIan Rogers 140133321a06SIan Rogers if (parse_state->fake_pmu) 140233321a06SIan Rogers return 0; 140333321a06SIan Rogers 1404b194c9cdSIan Rogers free((char *)evsel->unit); 1405b194c9cdSIan Rogers evsel->unit = strdup(info.unit); 140646441bdcSMatt Fleming evsel->scale = info.scale; 1407044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 14081d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 140933321a06SIan Rogers return 0; 14105f537a26SJiri Olsa } 14115f537a26SJiri Olsa 14125d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 1413fb081153SIan Rogers char *str, struct list_head *head, 141481a4e31fSIan Rogers struct list_head **listp, void *loc_) 14152073ad33SAndi Kleen { 14162073ad33SAndi Kleen struct parse_events_term *term; 1417fb081153SIan Rogers struct list_head *list = NULL; 141891c9923aSZhengjun Xing struct list_head *orig_head = NULL; 14192073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 142081a4e31fSIan Rogers YYLTYPE *loc = loc_; 14212073ad33SAndi Kleen int ok = 0; 1422fb081153SIan Rogers char *config; 14232073ad33SAndi Kleen 14242073ad33SAndi Kleen *listp = NULL; 1425fb081153SIan Rogers 1426fb081153SIan Rogers if (!head) { 1427fb081153SIan Rogers head = malloc(sizeof(struct list_head)); 1428fb081153SIan Rogers if (!head) 1429fb081153SIan Rogers goto out_err; 1430fb081153SIan Rogers 1431fb081153SIan Rogers INIT_LIST_HEAD(head); 1432fb081153SIan Rogers } 1433fb081153SIan Rogers config = strdup(str); 1434fb081153SIan Rogers if (!config) 1435fb081153SIan Rogers goto out_err; 1436fb081153SIan Rogers 1437fb081153SIan Rogers if (parse_events_term__num(&term, 1438fb081153SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 143966c9598bSNamhyung Kim config, 1, false, NULL, 1440fb081153SIan Rogers NULL) < 0) { 1441fb081153SIan Rogers free(config); 1442fb081153SIan Rogers goto out_err; 1443fb081153SIan Rogers } 1444fb081153SIan Rogers list_add_tail(&term->list, head); 1445fb081153SIan Rogers 14462073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 14472073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 14482073ad33SAndi Kleen if (!list) 1449fb081153SIan Rogers goto out_err; 1450fb081153SIan Rogers 14512073ad33SAndi Kleen INIT_LIST_HEAD(list); 1452fb081153SIan Rogers 14531eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu)) != NULL) { 145452c7b4d3SIan Rogers bool auto_merge_stats; 14552073ad33SAndi Kleen 1456411ad22eSIan Rogers if (parse_events__filter_pmu(parse_state, pmu)) 1457411ad22eSIan Rogers continue; 1458411ad22eSIan Rogers 1459c3245d20SIan Rogers if (!perf_pmu__have_event(pmu, str)) 1460c3245d20SIan Rogers continue; 146152c7b4d3SIan Rogers 1462c3245d20SIan Rogers auto_merge_stats = perf_pmu__auto_merge_stats(pmu); 146391c9923aSZhengjun Xing parse_events_copy_term_list(head, &orig_head); 1464c3245d20SIan Rogers if (!parse_events_add_pmu(parse_state, list, pmu->name, 1465c3245d20SIan Rogers orig_head, auto_merge_stats, loc)) { 1466c3245d20SIan Rogers pr_debug("%s -> %s/%s/\n", str, pmu->name, str); 14672073ad33SAndi Kleen ok++; 14682073ad33SAndi Kleen } 146991c9923aSZhengjun Xing parse_events_terms__delete(orig_head); 1470fb081153SIan Rogers } 1471b4a7276cSJohn Garry 1472b4a7276cSJohn Garry if (parse_state->fake_pmu) { 1473b4a7276cSJohn Garry if (!parse_events_add_pmu(parse_state, list, str, head, 147481a4e31fSIan Rogers /*auto_merge_stats=*/true, loc)) { 1475b4a7276cSJohn Garry pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str); 1476b4a7276cSJohn Garry ok++; 1477b4a7276cSJohn Garry } 1478b4a7276cSJohn Garry } 1479b4a7276cSJohn Garry 1480fb081153SIan Rogers out_err: 1481fb081153SIan Rogers if (ok) 1482fb081153SIan Rogers *listp = list; 1483fb081153SIan Rogers else 1484fb081153SIan Rogers free(list); 14852073ad33SAndi Kleen 14862073ad33SAndi Kleen parse_events_terms__delete(head); 1487fb081153SIan Rogers return ok ? 0 : -1; 14882073ad33SAndi Kleen } 14892073ad33SAndi Kleen 14906a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 14916a4bb04cSJiri Olsa char *event_mod) 149289efb029SJiri Olsa { 14936a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 14946a4bb04cSJiri Olsa } 14956a4bb04cSJiri Olsa 1496347c2f0aSIan Rogers void parse_events__set_leader(char *name, struct list_head *list) 14976a4bb04cSJiri Olsa { 149832dcd021SJiri Olsa struct evsel *leader; 14996a4bb04cSJiri Olsa 1500854f7363SWang Nan if (list_empty(list)) { 1501854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1502854f7363SWang Nan return; 1503854f7363SWang Nan } 1504854f7363SWang Nan 1505347c2f0aSIan Rogers leader = list_first_entry(list, struct evsel, core.node); 1506ecdcf630SIan Rogers __perf_evlist__set_leader(list, &leader->core); 15074bb311b2SIan Rogers leader->group_name = name; 150889efb029SJiri Olsa } 150989efb029SJiri Olsa 1510c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 15115d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 15125d7be90eSJiri Olsa struct list_head *list_all) 15135d7be90eSJiri Olsa { 15145d7be90eSJiri Olsa /* 15155d7be90eSJiri Olsa * Called for single event definition. Update the 151689efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 15175d7be90eSJiri Olsa * list, for next event definition. 15185d7be90eSJiri Olsa */ 15195d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1520b847cbdcSJiri Olsa free(list_event); 15215d7be90eSJiri Olsa } 15225d7be90eSJiri Olsa 1523f5b1135bSJiri Olsa struct event_modifier { 1524f5b1135bSJiri Olsa int eu; 1525f5b1135bSJiri Olsa int ek; 1526f5b1135bSJiri Olsa int eh; 1527f5b1135bSJiri Olsa int eH; 1528f5b1135bSJiri Olsa int eG; 1529a1e12da4SJiri Olsa int eI; 1530f5b1135bSJiri Olsa int precise; 15317f94af7aSJiri Olsa int precise_max; 1532f5b1135bSJiri Olsa int exclude_GH; 15333c176311SJiri Olsa int sample_read; 1534e9a7c414SMichael Ellerman int pinned; 15355a5dfe4bSAndi Kleen int weak; 15360997a266SAndi Kleen int exclusive; 153701bd8efcSSong Liu int bpf_counter; 1538f5b1135bSJiri Olsa }; 153961c45981SPaul Mackerras 1540f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 154132dcd021SJiri Olsa struct evsel *evsel) 1542f5b1135bSJiri Olsa { 15431fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 15441fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 15451fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 15461fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 15471fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 15481fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 15491fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 15507f94af7aSJiri Olsa int precise_max = 0; 15513c176311SJiri Olsa int sample_read = 0; 15521fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 15530997a266SAndi Kleen int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1554f5b1135bSJiri Olsa 1555f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1556f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 15575a5dfe4bSAndi Kleen int weak = 0; 155801bd8efcSSong Liu int bpf_counter = 0; 1559f5b1135bSJiri Olsa 1560f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1561ceb53fbfSIngo Molnar 156261c45981SPaul Mackerras while (*str) { 1563ab608344SPeter Zijlstra if (*str == 'u') { 1564ab608344SPeter Zijlstra if (!exclude) 1565ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 1566943b69acSJin Yao if (!exclude_GH && !perf_guest) 1567943b69acSJin Yao eG = 1; 156861c45981SPaul Mackerras eu = 0; 1569ab608344SPeter Zijlstra } else if (*str == 'k') { 1570ab608344SPeter Zijlstra if (!exclude) 1571ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 157261c45981SPaul Mackerras ek = 0; 1573ab608344SPeter Zijlstra } else if (*str == 'h') { 1574ab608344SPeter Zijlstra if (!exclude) 1575ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 157661c45981SPaul Mackerras eh = 0; 157799320cc8SJoerg Roedel } else if (*str == 'G') { 157899320cc8SJoerg Roedel if (!exclude_GH) 157999320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 158099320cc8SJoerg Roedel eG = 0; 158199320cc8SJoerg Roedel } else if (*str == 'H') { 158299320cc8SJoerg Roedel if (!exclude_GH) 158399320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 158499320cc8SJoerg Roedel eH = 0; 1585a1e12da4SJiri Olsa } else if (*str == 'I') { 1586a1e12da4SJiri Olsa eI = 1; 1587ab608344SPeter Zijlstra } else if (*str == 'p') { 1588ab608344SPeter Zijlstra precise++; 15891342798cSDavid Ahern /* use of precise requires exclude_guest */ 15901342798cSDavid Ahern if (!exclude_GH) 15911342798cSDavid Ahern eG = 1; 15927f94af7aSJiri Olsa } else if (*str == 'P') { 15937f94af7aSJiri Olsa precise_max = 1; 15943c176311SJiri Olsa } else if (*str == 'S') { 15953c176311SJiri Olsa sample_read = 1; 1596e9a7c414SMichael Ellerman } else if (*str == 'D') { 1597e9a7c414SMichael Ellerman pinned = 1; 15980997a266SAndi Kleen } else if (*str == 'e') { 15990997a266SAndi Kleen exclusive = 1; 16005a5dfe4bSAndi Kleen } else if (*str == 'W') { 16015a5dfe4bSAndi Kleen weak = 1; 160201bd8efcSSong Liu } else if (*str == 'b') { 160301bd8efcSSong Liu bpf_counter = 1; 1604ab608344SPeter Zijlstra } else 160561c45981SPaul Mackerras break; 1606ab608344SPeter Zijlstra 160761c45981SPaul Mackerras ++str; 160861c45981SPaul Mackerras } 160974d5b588SJaswinder Singh Rajput 161086470930SIngo Molnar /* 161189812fc8SJiri Olsa * precise ip: 161289812fc8SJiri Olsa * 161389812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 161489812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 161589812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 161689812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 161789812fc8SJiri Olsa * 161889812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 161986470930SIngo Molnar */ 162089812fc8SJiri Olsa if (precise > 3) 162189812fc8SJiri Olsa return -EINVAL; 162286470930SIngo Molnar 1623f5b1135bSJiri Olsa mod->eu = eu; 1624f5b1135bSJiri Olsa mod->ek = ek; 1625f5b1135bSJiri Olsa mod->eh = eh; 1626f5b1135bSJiri Olsa mod->eH = eH; 1627f5b1135bSJiri Olsa mod->eG = eG; 1628a1e12da4SJiri Olsa mod->eI = eI; 1629f5b1135bSJiri Olsa mod->precise = precise; 16307f94af7aSJiri Olsa mod->precise_max = precise_max; 1631f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 16323c176311SJiri Olsa mod->sample_read = sample_read; 1633e9a7c414SMichael Ellerman mod->pinned = pinned; 16345a5dfe4bSAndi Kleen mod->weak = weak; 163501bd8efcSSong Liu mod->bpf_counter = bpf_counter; 16360997a266SAndi Kleen mod->exclusive = exclusive; 1637e9a7c414SMichael Ellerman 1638f5b1135bSJiri Olsa return 0; 1639f5b1135bSJiri Olsa } 1640f5b1135bSJiri Olsa 1641534123f4SJiri Olsa /* 1642534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1643534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1644534123f4SJiri Olsa */ 1645534123f4SJiri Olsa static int check_modifier(char *str) 1646534123f4SJiri Olsa { 1647534123f4SJiri Olsa char *p = str; 1648534123f4SJiri Olsa 1649534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 165001bd8efcSSong Liu if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1651534123f4SJiri Olsa return -1; 1652534123f4SJiri Olsa 1653534123f4SJiri Olsa while (*p) { 1654534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1655534123f4SJiri Olsa return -1; 1656534123f4SJiri Olsa p++; 1657534123f4SJiri Olsa } 1658534123f4SJiri Olsa 1659534123f4SJiri Olsa return 0; 1660534123f4SJiri Olsa } 1661534123f4SJiri Olsa 1662f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1663f5b1135bSJiri Olsa { 166432dcd021SJiri Olsa struct evsel *evsel; 1665f5b1135bSJiri Olsa struct event_modifier mod; 1666f5b1135bSJiri Olsa 1667f5b1135bSJiri Olsa if (str == NULL) 1668f5b1135bSJiri Olsa return 0; 1669f5b1135bSJiri Olsa 1670534123f4SJiri Olsa if (check_modifier(str)) 1671534123f4SJiri Olsa return -EINVAL; 1672534123f4SJiri Olsa 1673f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1674f5b1135bSJiri Olsa return -EINVAL; 1675f5b1135bSJiri Olsa 1676e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1677f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1678f5b1135bSJiri Olsa return -EINVAL; 1679f5b1135bSJiri Olsa 16801fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 16811fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 16821fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 16831fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 16841fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 16851fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 16861fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 1687f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 16883c176311SJiri Olsa evsel->sample_read = mod.sample_read; 16897f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 16905a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 169101bd8efcSSong Liu evsel->bpf_counter = mod.bpf_counter; 1692e9a7c414SMichael Ellerman 16930997a266SAndi Kleen if (evsel__is_group_leader(evsel)) { 16941fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 16950997a266SAndi Kleen evsel->core.attr.exclusive = mod.exclusive; 16960997a266SAndi Kleen } 1697ceb53fbfSIngo Molnar } 169886470930SIngo Molnar 169989812fc8SJiri Olsa return 0; 170086470930SIngo Molnar } 170186470930SIngo Molnar 17028e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name) 1703ac2ba9f3SRobert Richter { 170432dcd021SJiri Olsa struct evsel *evsel; 1705ac2ba9f3SRobert Richter 1706e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 17079462e4deSIan Rogers if (!evsel->name) { 1708ac2ba9f3SRobert Richter evsel->name = strdup(name); 17099462e4deSIan Rogers if (!evsel->name) 17109462e4deSIan Rogers return -ENOMEM; 17119462e4deSIan Rogers } 1712ac2ba9f3SRobert Richter } 1713ac2ba9f3SRobert Richter 1714ac2ba9f3SRobert Richter return 0; 1715ac2ba9f3SRobert Richter } 1716ac2ba9f3SRobert Richter 17171244a327SJiri Olsa static int parse_events__scanner(const char *str, 17181244a327SJiri Olsa struct parse_events_state *parse_state) 1719ac20de6fSZheng Yan { 1720ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1721ac20de6fSZheng Yan void *scanner; 1722ac20de6fSZheng Yan int ret; 1723ac20de6fSZheng Yan 17241244a327SJiri Olsa ret = parse_events_lex_init_extra(parse_state, &scanner); 1725ac20de6fSZheng Yan if (ret) 1726ac20de6fSZheng Yan return ret; 1727ac20de6fSZheng Yan 1728ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1729ac20de6fSZheng Yan 1730ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1731ac20de6fSZheng Yan parse_events_debug = 1; 1732ae762641SJiri Olsa parse_events_set_debug(1, scanner); 1733ac20de6fSZheng Yan #endif 17345d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 1735ac20de6fSZheng Yan 1736ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1737ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1738ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1739ac20de6fSZheng Yan return ret; 1740ac20de6fSZheng Yan } 1741ac20de6fSZheng Yan 174290e2b22dSJiri Olsa /* 174390e2b22dSJiri Olsa * parse event config string, return a list of event terms. 174490e2b22dSJiri Olsa */ 174590e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 174690e2b22dSJiri Olsa { 17475d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 174890e2b22dSJiri Olsa .terms = NULL, 17491244a327SJiri Olsa .stoken = PE_START_TERMS, 175090e2b22dSJiri Olsa }; 175190e2b22dSJiri Olsa int ret; 175290e2b22dSJiri Olsa 17531244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 17544929e95aSJiri Olsa 175590e2b22dSJiri Olsa if (!ret) { 17565d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 17575d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 175890e2b22dSJiri Olsa return 0; 175990e2b22dSJiri Olsa } 176090e2b22dSJiri Olsa 17615d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 176290e2b22dSJiri Olsa return ret; 176390e2b22dSJiri Olsa } 176490e2b22dSJiri Olsa 1765a90cc5a9SIan Rogers static int evsel__compute_group_pmu_name(struct evsel *evsel, 1766a90cc5a9SIan Rogers const struct list_head *head) 1767a90cc5a9SIan Rogers { 1768a90cc5a9SIan Rogers struct evsel *leader = evsel__leader(evsel); 1769a90cc5a9SIan Rogers struct evsel *pos; 1770251aa040SIan Rogers const char *group_pmu_name; 1771251aa040SIan Rogers struct perf_pmu *pmu = evsel__find_pmu(evsel); 1772a90cc5a9SIan Rogers 1773251aa040SIan Rogers if (!pmu) { 1774251aa040SIan Rogers /* 1775251aa040SIan Rogers * For PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE types the PMU 1776251aa040SIan Rogers * is a core PMU, but in heterogeneous systems this is 1777251aa040SIan Rogers * unknown. For now pick the first core PMU. 1778251aa040SIan Rogers */ 1779251aa040SIan Rogers pmu = perf_pmus__scan_core(NULL); 1780251aa040SIan Rogers } 1781251aa040SIan Rogers if (!pmu) { 1782bc06026dSYang Jihong pr_debug("No PMU found for '%s'\n", evsel__name(evsel)); 1783251aa040SIan Rogers return -EINVAL; 1784251aa040SIan Rogers } 1785251aa040SIan Rogers group_pmu_name = pmu->name; 1786a90cc5a9SIan Rogers /* 1787a90cc5a9SIan Rogers * Software events may be in a group with other uncore PMU events. Use 1788a90cc5a9SIan Rogers * the pmu_name of the first non-software event to avoid breaking the 1789a90cc5a9SIan Rogers * software event out of the group. 1790a90cc5a9SIan Rogers * 1791a90cc5a9SIan Rogers * Aux event leaders, like intel_pt, expect a group with events from 1792a90cc5a9SIan Rogers * other PMUs, so substitute the AUX event's PMU in this case. 1793a90cc5a9SIan Rogers */ 1794251aa040SIan Rogers if (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader)) { 1795251aa040SIan Rogers struct perf_pmu *leader_pmu = evsel__find_pmu(leader); 1796251aa040SIan Rogers 1797251aa040SIan Rogers if (!leader_pmu) { 1798251aa040SIan Rogers /* As with determining pmu above. */ 1799251aa040SIan Rogers leader_pmu = perf_pmus__scan_core(NULL); 1800251aa040SIan Rogers } 1801a90cc5a9SIan Rogers /* 1802a90cc5a9SIan Rogers * Starting with the leader, find the first event with a named 1803251aa040SIan Rogers * non-software PMU. for_each_group_(member|evsel) isn't used as 1804251aa040SIan Rogers * the list isn't yet sorted putting evsel's in the same group 1805251aa040SIan Rogers * together. 1806a90cc5a9SIan Rogers */ 1807251aa040SIan Rogers if (leader_pmu && !perf_pmu__is_software(leader_pmu)) { 1808251aa040SIan Rogers group_pmu_name = leader_pmu->name; 1809a90cc5a9SIan Rogers } else if (leader->core.nr_members > 1) { 1810a90cc5a9SIan Rogers list_for_each_entry(pos, head, core.node) { 1811251aa040SIan Rogers struct perf_pmu *pos_pmu; 1812251aa040SIan Rogers 1813251aa040SIan Rogers if (pos == leader || evsel__leader(pos) != leader) 1814251aa040SIan Rogers continue; 1815251aa040SIan Rogers pos_pmu = evsel__find_pmu(pos); 1816251aa040SIan Rogers if (!pos_pmu) { 1817251aa040SIan Rogers /* As with determining pmu above. */ 1818251aa040SIan Rogers pos_pmu = perf_pmus__scan_core(NULL); 1819251aa040SIan Rogers } 1820251aa040SIan Rogers if (pos_pmu && !perf_pmu__is_software(pos_pmu)) { 1821251aa040SIan Rogers group_pmu_name = pos_pmu->name; 1822a90cc5a9SIan Rogers break; 1823a90cc5a9SIan Rogers } 1824a90cc5a9SIan Rogers } 1825a90cc5a9SIan Rogers } 1826a90cc5a9SIan Rogers } 1827251aa040SIan Rogers /* Assign the actual name taking care that the fake PMU lacks a name. */ 1828251aa040SIan Rogers evsel->group_pmu_name = strdup(group_pmu_name ?: "fake"); 1829a90cc5a9SIan Rogers return evsel->group_pmu_name ? 0 : -ENOMEM; 1830a90cc5a9SIan Rogers } 1831a90cc5a9SIan Rogers 1832347c2f0aSIan Rogers __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs) 1833347c2f0aSIan Rogers { 1834347c2f0aSIan Rogers /* Order by insertion index. */ 1835347c2f0aSIan Rogers return lhs->core.idx - rhs->core.idx; 1836347c2f0aSIan Rogers } 1837347c2f0aSIan Rogers 1838b161f25fSIan Rogers static int evlist__cmp(void *_fg_idx, const struct list_head *l, const struct list_head *r) 1839347c2f0aSIan Rogers { 1840347c2f0aSIan Rogers const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node); 1841347c2f0aSIan Rogers const struct evsel *lhs = container_of(lhs_core, struct evsel, core); 1842347c2f0aSIan Rogers const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node); 1843347c2f0aSIan Rogers const struct evsel *rhs = container_of(rhs_core, struct evsel, core); 1844b161f25fSIan Rogers int *force_grouped_idx = _fg_idx; 1845b161f25fSIan Rogers int lhs_sort_idx, rhs_sort_idx, ret; 1846347c2f0aSIan Rogers const char *lhs_pmu_name, *rhs_pmu_name; 1847b161f25fSIan Rogers bool lhs_has_group, rhs_has_group; 1848347c2f0aSIan Rogers 1849347c2f0aSIan Rogers /* 1850347c2f0aSIan Rogers * First sort by grouping/leader. Read the leader idx only if the evsel 1851a90cc5a9SIan Rogers * is part of a group, by default ungrouped events will be sorted 1852a90cc5a9SIan Rogers * relative to grouped events based on where the first ungrouped event 1853a90cc5a9SIan Rogers * occurs. If both events don't have a group we want to fall-through to 1854a90cc5a9SIan Rogers * the arch specific sorting, that can reorder and fix things like 1855a90cc5a9SIan Rogers * Intel's topdown events. 1856347c2f0aSIan Rogers */ 1857327daf34SIan Rogers if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) { 1858327daf34SIan Rogers lhs_has_group = true; 1859b161f25fSIan Rogers lhs_sort_idx = lhs_core->leader->idx; 1860b161f25fSIan Rogers } else { 1861b161f25fSIan Rogers lhs_has_group = false; 1862b161f25fSIan Rogers lhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(lhs) 1863b161f25fSIan Rogers ? *force_grouped_idx 1864b161f25fSIan Rogers : lhs_core->idx; 1865327daf34SIan Rogers } 1866327daf34SIan Rogers if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) { 1867327daf34SIan Rogers rhs_has_group = true; 1868b161f25fSIan Rogers rhs_sort_idx = rhs_core->leader->idx; 1869b161f25fSIan Rogers } else { 1870b161f25fSIan Rogers rhs_has_group = false; 1871b161f25fSIan Rogers rhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(rhs) 1872b161f25fSIan Rogers ? *force_grouped_idx 1873b161f25fSIan Rogers : rhs_core->idx; 1874327daf34SIan Rogers } 1875347c2f0aSIan Rogers 1876b161f25fSIan Rogers if (lhs_sort_idx != rhs_sort_idx) 1877b161f25fSIan Rogers return lhs_sort_idx - rhs_sort_idx; 1878347c2f0aSIan Rogers 1879327daf34SIan Rogers /* Group by PMU if there is a group. Groups can't span PMUs. */ 1880327daf34SIan Rogers if (lhs_has_group && rhs_has_group) { 1881a90cc5a9SIan Rogers lhs_pmu_name = lhs->group_pmu_name; 1882a90cc5a9SIan Rogers rhs_pmu_name = rhs->group_pmu_name; 1883347c2f0aSIan Rogers ret = strcmp(lhs_pmu_name, rhs_pmu_name); 1884347c2f0aSIan Rogers if (ret) 1885347c2f0aSIan Rogers return ret; 1886327daf34SIan Rogers } 1887347c2f0aSIan Rogers 1888347c2f0aSIan Rogers /* Architecture specific sorting. */ 1889347c2f0aSIan Rogers return arch_evlist__cmp(lhs, rhs); 1890347c2f0aSIan Rogers } 1891347c2f0aSIan Rogers 1892a90cc5a9SIan Rogers static int parse_events__sort_events_and_fix_groups(struct list_head *list) 1893347c2f0aSIan Rogers { 1894b161f25fSIan Rogers int idx = 0, force_grouped_idx = -1; 1895347c2f0aSIan Rogers struct evsel *pos, *cur_leader = NULL; 1896347c2f0aSIan Rogers struct perf_evsel *cur_leaders_grp = NULL; 18975c49b6c3SIan Rogers bool idx_changed = false, cur_leader_force_grouped = false; 1898a4c7d7c5SIan Rogers int orig_num_leaders = 0, num_leaders = 0; 1899a90cc5a9SIan Rogers int ret; 1900347c2f0aSIan Rogers 1901347c2f0aSIan Rogers /* 1902347c2f0aSIan Rogers * Compute index to insert ungrouped events at. Place them where the 1903347c2f0aSIan Rogers * first ungrouped event appears. 1904347c2f0aSIan Rogers */ 1905347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 1906347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 1907347c2f0aSIan Rogers 1908a90cc5a9SIan Rogers ret = evsel__compute_group_pmu_name(pos, list); 1909a90cc5a9SIan Rogers if (ret) 1910a90cc5a9SIan Rogers return ret; 1911a90cc5a9SIan Rogers 1912a4c7d7c5SIan Rogers if (pos == pos_leader) 1913a4c7d7c5SIan Rogers orig_num_leaders++; 1914347c2f0aSIan Rogers 1915a4c7d7c5SIan Rogers /* 1916a4c7d7c5SIan Rogers * Ensure indexes are sequential, in particular for multiple 1917a4c7d7c5SIan Rogers * event lists being merged. The indexes are used to detect when 1918a4c7d7c5SIan Rogers * the user order is modified. 1919a4c7d7c5SIan Rogers */ 1920a4c7d7c5SIan Rogers pos->core.idx = idx++; 1921a4c7d7c5SIan Rogers 1922b161f25fSIan Rogers /* Remember an index to sort all forced grouped events together to. */ 1923b161f25fSIan Rogers if (force_grouped_idx == -1 && pos == pos_leader && pos->core.nr_members < 2 && 1924b161f25fSIan Rogers arch_evsel__must_be_in_group(pos)) 1925b161f25fSIan Rogers force_grouped_idx = pos->core.idx; 1926347c2f0aSIan Rogers } 1927347c2f0aSIan Rogers 1928347c2f0aSIan Rogers /* Sort events. */ 1929b161f25fSIan Rogers list_sort(&force_grouped_idx, list, evlist__cmp); 1930347c2f0aSIan Rogers 1931347c2f0aSIan Rogers /* 1932347c2f0aSIan Rogers * Recompute groups, splitting for PMUs and adding groups for events 1933347c2f0aSIan Rogers * that require them. 1934347c2f0aSIan Rogers */ 1935347c2f0aSIan Rogers idx = 0; 1936347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 1937347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 1938a90cc5a9SIan Rogers const char *pos_pmu_name = pos->group_pmu_name; 1939e8d38345SIan Rogers const char *cur_leader_pmu_name; 1940b161f25fSIan Rogers bool pos_force_grouped = force_grouped_idx != -1 && 1941b161f25fSIan Rogers arch_evsel__must_be_in_group(pos); 1942347c2f0aSIan Rogers 1943347c2f0aSIan Rogers /* Reset index and nr_members. */ 1944a4c7d7c5SIan Rogers if (pos->core.idx != idx) 1945a4c7d7c5SIan Rogers idx_changed = true; 1946347c2f0aSIan Rogers pos->core.idx = idx++; 1947347c2f0aSIan Rogers pos->core.nr_members = 0; 1948347c2f0aSIan Rogers 1949347c2f0aSIan Rogers /* 1950347c2f0aSIan Rogers * Set the group leader respecting the given groupings and that 1951347c2f0aSIan Rogers * groups can't span PMUs. 1952347c2f0aSIan Rogers */ 1953347c2f0aSIan Rogers if (!cur_leader) 1954347c2f0aSIan Rogers cur_leader = pos; 1955347c2f0aSIan Rogers 1956a90cc5a9SIan Rogers cur_leader_pmu_name = cur_leader->group_pmu_name; 19575c49b6c3SIan Rogers if ((cur_leaders_grp != pos->core.leader && 19585c49b6c3SIan Rogers (!pos_force_grouped || !cur_leader_force_grouped)) || 1959347c2f0aSIan Rogers strcmp(cur_leader_pmu_name, pos_pmu_name)) { 1960347c2f0aSIan Rogers /* Event is for a different group/PMU than last. */ 1961347c2f0aSIan Rogers cur_leader = pos; 1962347c2f0aSIan Rogers /* 1963347c2f0aSIan Rogers * Remember the leader's group before it is overwritten, 1964347c2f0aSIan Rogers * so that later events match as being in the same 1965347c2f0aSIan Rogers * group. 1966347c2f0aSIan Rogers */ 1967347c2f0aSIan Rogers cur_leaders_grp = pos->core.leader; 1968347c2f0aSIan Rogers /* 19695c49b6c3SIan Rogers * Avoid forcing events into groups with events that 19705c49b6c3SIan Rogers * don't need to be in the group. 1971347c2f0aSIan Rogers */ 19725c49b6c3SIan Rogers cur_leader_force_grouped = pos_force_grouped; 1973347c2f0aSIan Rogers } 1974e8d38345SIan Rogers if (pos_leader != cur_leader) { 1975e8d38345SIan Rogers /* The leader changed so update it. */ 1976347c2f0aSIan Rogers evsel__set_leader(pos, cur_leader); 1977347c2f0aSIan Rogers } 1978347c2f0aSIan Rogers } 1979347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 1980a4c7d7c5SIan Rogers struct evsel *pos_leader = evsel__leader(pos); 1981a4c7d7c5SIan Rogers 1982a4c7d7c5SIan Rogers if (pos == pos_leader) 1983a4c7d7c5SIan Rogers num_leaders++; 1984a4c7d7c5SIan Rogers pos_leader->core.nr_members++; 1985347c2f0aSIan Rogers } 1986a90cc5a9SIan Rogers return (idx_changed || num_leaders != orig_num_leaders) ? 1 : 0; 1987347c2f0aSIan Rogers } 1988347c2f0aSIan Rogers 1989411ad22eSIan Rogers int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter, 1990a4c7d7c5SIan Rogers struct parse_events_error *err, struct perf_pmu *fake_pmu, 1991a4c7d7c5SIan Rogers bool warn_if_reordered) 199286470930SIngo Molnar { 19935d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 19945d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 19956484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 1996b39b8393SJiri Olsa .error = err, 19971244a327SJiri Olsa .stoken = PE_START_EVENTS, 19983bf91aa5SArnaldo Carvalho de Melo .fake_pmu = fake_pmu, 1999411ad22eSIan Rogers .pmu_filter = pmu_filter, 20006fd1e519SIan Rogers .match_legacy_cache_terms = true, 200146010ab2SJiri Olsa }; 2002a90cc5a9SIan Rogers int ret, ret2; 200386470930SIngo Molnar 20041244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 200515bfd2ccSWang Nan 20068e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2007c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2008854f7363SWang Nan return -1; 2009854f7363SWang Nan } 2010854f7363SWang Nan 2011a90cc5a9SIan Rogers ret2 = parse_events__sort_events_and_fix_groups(&parse_state.list); 2012a90cc5a9SIan Rogers if (ret2 < 0) 2013a90cc5a9SIan Rogers return ret; 2014a90cc5a9SIan Rogers 2015a90cc5a9SIan Rogers if (ret2 && warn_if_reordered && !parse_state.wild_card_pmus) 2016a4c7d7c5SIan Rogers pr_warning("WARNING: events were regrouped to match PMUs\n"); 2017347c2f0aSIan Rogers 20188e8714c3SIan Rogers /* 20198e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 20208e8714c3SIan Rogers */ 2021e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &parse_state.list); 20228e8714c3SIan Rogers 20238e8714c3SIan Rogers if (!ret) { 20248e8714c3SIan Rogers struct evsel *last; 20258e8714c3SIan Rogers 2026515dbe48SJiri Olsa last = evlist__last(evlist); 202715bfd2ccSWang Nan last->cmdline_group_boundary = true; 202815bfd2ccSWang Nan 202986470930SIngo Molnar return 0; 203086470930SIngo Molnar } 203186470930SIngo Molnar 20325d7be90eSJiri Olsa /* 20335d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2034c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 20355d7be90eSJiri Olsa * need to bother. 20365d7be90eSJiri Olsa */ 203789812fc8SJiri Olsa return ret; 203889812fc8SJiri Olsa } 203989812fc8SJiri Olsa 2040806731a9SAdrian Hunter int parse_event(struct evlist *evlist, const char *str) 2041806731a9SAdrian Hunter { 2042806731a9SAdrian Hunter struct parse_events_error err; 2043806731a9SAdrian Hunter int ret; 2044806731a9SAdrian Hunter 2045806731a9SAdrian Hunter parse_events_error__init(&err); 2046806731a9SAdrian Hunter ret = parse_events(evlist, str, &err); 2047806731a9SAdrian Hunter parse_events_error__exit(&err); 2048806731a9SAdrian Hunter return ret; 2049806731a9SAdrian Hunter } 2050806731a9SAdrian Hunter 205107eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err) 205207eafd4eSIan Rogers { 205307eafd4eSIan Rogers bzero(err, sizeof(*err)); 205407eafd4eSIan Rogers } 205507eafd4eSIan Rogers 205607eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err) 205707eafd4eSIan Rogers { 205807eafd4eSIan Rogers zfree(&err->str); 205907eafd4eSIan Rogers zfree(&err->help); 206007eafd4eSIan Rogers zfree(&err->first_str); 206107eafd4eSIan Rogers zfree(&err->first_help); 206207eafd4eSIan Rogers } 206307eafd4eSIan Rogers 20646c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx, 20656c191289SIan Rogers char *str, char *help) 20666c191289SIan Rogers { 2067806731a9SAdrian Hunter if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n")) 20682e828582SAdrian Hunter goto out_free; 20696c191289SIan Rogers switch (err->num_errors) { 20706c191289SIan Rogers case 0: 20716c191289SIan Rogers err->idx = idx; 20726c191289SIan Rogers err->str = str; 20736c191289SIan Rogers err->help = help; 20746c191289SIan Rogers break; 20756c191289SIan Rogers case 1: 20766c191289SIan Rogers err->first_idx = err->idx; 20776c191289SIan Rogers err->idx = idx; 20786c191289SIan Rogers err->first_str = err->str; 20796c191289SIan Rogers err->str = str; 20806c191289SIan Rogers err->first_help = err->help; 20816c191289SIan Rogers err->help = help; 20826c191289SIan Rogers break; 20836c191289SIan Rogers default: 20846c191289SIan Rogers pr_debug("Multiple errors dropping message: %s (%s)\n", 20856c191289SIan Rogers err->str, err->help); 20866c191289SIan Rogers free(err->str); 20876c191289SIan Rogers err->str = str; 20886c191289SIan Rogers free(err->help); 20896c191289SIan Rogers err->help = help; 20906c191289SIan Rogers break; 20916c191289SIan Rogers } 20926c191289SIan Rogers err->num_errors++; 20932e828582SAdrian Hunter return; 20942e828582SAdrian Hunter 20952e828582SAdrian Hunter out_free: 20962e828582SAdrian Hunter free(str); 20972e828582SAdrian Hunter free(help); 20986c191289SIan Rogers } 20996c191289SIan Rogers 2100b39b8393SJiri Olsa #define MAX_WIDTH 1000 2101b39b8393SJiri Olsa static int get_term_width(void) 2102b39b8393SJiri Olsa { 2103b39b8393SJiri Olsa struct winsize ws; 2104b39b8393SJiri Olsa 2105b39b8393SJiri Olsa get_term_dimensions(&ws); 2106b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2107b39b8393SJiri Olsa } 2108b39b8393SJiri Olsa 21096c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str, 2110a910e466SIan Rogers const char *err_help, const char *event) 2111b39b8393SJiri Olsa { 2112b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2113b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2114b39b8393SJiri Olsa char *buf = (char *) event; 2115b39b8393SJiri Olsa int idx = 0; 2116a910e466SIan Rogers if (err_str) { 2117b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2118b39b8393SJiri Olsa int width = get_term_width() - 2; 2119b39b8393SJiri Olsa int len_event = strlen(event); 2120b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2121b39b8393SJiri Olsa 2122b39b8393SJiri Olsa /* 2123b39b8393SJiri Olsa * Maximum error index indent, we will cut 2124b39b8393SJiri Olsa * the event string if it's bigger. 2125b39b8393SJiri Olsa */ 2126141b2d31SAdrian Hunter int max_err_idx = 13; 2127b39b8393SJiri Olsa 2128b39b8393SJiri Olsa /* 2129b39b8393SJiri Olsa * Let's be specific with the message when 2130b39b8393SJiri Olsa * we have the precise error. 2131b39b8393SJiri Olsa */ 2132b39b8393SJiri Olsa str = "event syntax error: "; 2133b39b8393SJiri Olsa len_str = strlen(str); 2134b39b8393SJiri Olsa max_len = width - len_str; 2135b39b8393SJiri Olsa 2136b39b8393SJiri Olsa buf = _buf; 2137b39b8393SJiri Olsa 2138bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2139a910e466SIan Rogers if (err_idx > max_err_idx) 2140a910e466SIan Rogers cut = err_idx - max_err_idx; 2141b39b8393SJiri Olsa 2142b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2143b39b8393SJiri Olsa 2144b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2145b39b8393SJiri Olsa if (cut) 2146b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2147b39b8393SJiri Olsa 2148b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2149b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2150b39b8393SJiri Olsa buf[max_len] = 0; 2151b39b8393SJiri Olsa } 2152b39b8393SJiri Olsa 2153a910e466SIan Rogers idx = len_str + err_idx - cut; 2154b39b8393SJiri Olsa } 2155b39b8393SJiri Olsa 2156b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2157b39b8393SJiri Olsa if (idx) { 2158a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2159a910e466SIan Rogers if (err_help) 2160a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2161a910e466SIan Rogers } 2162a910e466SIan Rogers } 2163a910e466SIan Rogers 21646c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err, 2165a910e466SIan Rogers const char *event) 2166a910e466SIan Rogers { 2167a910e466SIan Rogers if (!err->num_errors) 2168a910e466SIan Rogers return; 2169a910e466SIan Rogers 21706c191289SIan Rogers __parse_events_error__print(err->idx, err->str, err->help, event); 2171a910e466SIan Rogers 2172a910e466SIan Rogers if (err->num_errors > 1) { 2173a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 21746c191289SIan Rogers __parse_events_error__print(err->first_idx, err->first_str, 2175a910e466SIan Rogers err->first_help, event); 2176b39b8393SJiri Olsa } 2177b39b8393SJiri Olsa } 2178b39b8393SJiri Olsa 2179b39b8393SJiri Olsa #undef MAX_WIDTH 2180b39b8393SJiri Olsa 2181f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 21821d037ca1SIrina Tirdea int unset __maybe_unused) 2183f120f9d5SJiri Olsa { 2184411ad22eSIan Rogers struct parse_events_option_args *args = opt->value; 2185a910e466SIan Rogers struct parse_events_error err; 2186a910e466SIan Rogers int ret; 2187a910e466SIan Rogers 218807eafd4eSIan Rogers parse_events_error__init(&err); 2189411ad22eSIan Rogers ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err, 2190411ad22eSIan Rogers /*fake_pmu=*/NULL, /*warn_if_reordered=*/true); 21919175ce1fSAndi Kleen 2192333b5665SAndi Kleen if (ret) { 21936c191289SIan Rogers parse_events_error__print(&err, str); 2194333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2195333b5665SAndi Kleen } 219607eafd4eSIan Rogers parse_events_error__exit(&err); 2197b39b8393SJiri Olsa 21989175ce1fSAndi Kleen return ret; 2199f120f9d5SJiri Olsa } 2200f120f9d5SJiri Olsa 2201d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2202d0abbc3cSArnaldo Carvalho de Melo { 2203411ad22eSIan Rogers struct parse_events_option_args *args = opt->value; 2204d0abbc3cSArnaldo Carvalho de Melo int ret; 2205d0abbc3cSArnaldo Carvalho de Melo 2206411ad22eSIan Rogers if (*args->evlistp == NULL) { 2207411ad22eSIan Rogers *args->evlistp = evlist__new(); 2208d0abbc3cSArnaldo Carvalho de Melo 2209411ad22eSIan Rogers if (*args->evlistp == NULL) { 2210d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2211d0abbc3cSArnaldo Carvalho de Melo return -1; 2212d0abbc3cSArnaldo Carvalho de Melo } 2213d0abbc3cSArnaldo Carvalho de Melo } 2214d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2215d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2216411ad22eSIan Rogers evlist__delete(*args->evlistp); 2217411ad22eSIan Rogers *args->evlistp = NULL; 2218d0abbc3cSArnaldo Carvalho de Melo } 2219d0abbc3cSArnaldo Carvalho de Melo 2220d0abbc3cSArnaldo Carvalho de Melo return ret; 2221d0abbc3cSArnaldo Carvalho de Melo } 2222d0abbc3cSArnaldo Carvalho de Melo 22234ba1faa1SWang Nan static int 222463503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 222532dcd021SJiri Olsa int (*func)(struct evsel *evsel, 22264ba1faa1SWang Nan const void *arg), 22274ba1faa1SWang Nan const void *arg) 2228c171b552SLi Zefan { 222932dcd021SJiri Olsa struct evsel *last = NULL; 22304ba1faa1SWang Nan int err; 2231c171b552SLi Zefan 2232854f7363SWang Nan /* 2233854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2234854f7363SWang Nan * error when it found last == NULL. 2235854f7363SWang Nan * 2236854f7363SWang Nan * So no need to WARN here, let *func do this. 2237854f7363SWang Nan */ 22386484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2239515dbe48SJiri Olsa last = evlist__last(evlist); 224069aad6f1SArnaldo Carvalho de Melo 224115bfd2ccSWang Nan do { 22424ba1faa1SWang Nan err = (*func)(last, arg); 22434ba1faa1SWang Nan if (err) 2244c171b552SLi Zefan return -1; 22454ba1faa1SWang Nan if (!last) 22464ba1faa1SWang Nan return 0; 2247c171b552SLi Zefan 2248ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 224915bfd2ccSWang Nan return 0; 2250b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 225115bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 225215bfd2ccSWang Nan 2253c171b552SLi Zefan return 0; 2254c171b552SLi Zefan } 2255c171b552SLi Zefan 225632dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 22574ba1faa1SWang Nan { 22584ba1faa1SWang Nan const char *str = arg; 22591e857484SMathieu Poirier bool found = false; 22601e857484SMathieu Poirier int nr_addr_filters = 0; 22611e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 22624ba1faa1SWang Nan 226349836f78SJack Henschel if (evsel == NULL) { 226449836f78SJack Henschel fprintf(stderr, 226549836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 226649836f78SJack Henschel return -1; 226749836f78SJack Henschel } 22684ba1faa1SWang Nan 22691fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2270ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, str) < 0) { 22714ba1faa1SWang Nan fprintf(stderr, 22724ba1faa1SWang Nan "not enough memory to hold filter string\n"); 22734ba1faa1SWang Nan return -1; 22744ba1faa1SWang Nan } 22754ba1faa1SWang Nan 22764ba1faa1SWang Nan return 0; 22774ba1faa1SWang Nan } 22784ba1faa1SWang Nan 22791eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu)) != NULL) 22801fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 22811e857484SMathieu Poirier found = true; 22821e857484SMathieu Poirier break; 22831e857484SMathieu Poirier } 22841e857484SMathieu Poirier 22851e857484SMathieu Poirier if (found) 22861e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 22871e857484SMathieu Poirier "%d", &nr_addr_filters); 22881e857484SMathieu Poirier 2289d180aa56SNamhyung Kim if (!nr_addr_filters) 2290d180aa56SNamhyung Kim return perf_bpf_filter__parse(&evsel->bpf_filters, str); 22911e857484SMathieu Poirier 2292ad681adfSArnaldo Carvalho de Melo if (evsel__append_addr_filter(evsel, str) < 0) { 22931e857484SMathieu Poirier fprintf(stderr, 22941e857484SMathieu Poirier "not enough memory to hold filter string\n"); 22951e857484SMathieu Poirier return -1; 22961e857484SMathieu Poirier } 22971e857484SMathieu Poirier 22981e857484SMathieu Poirier return 0; 22991e857484SMathieu Poirier } 23001e857484SMathieu Poirier 23014ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 23024ba1faa1SWang Nan int unset __maybe_unused) 23034ba1faa1SWang Nan { 230463503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 23054ba1faa1SWang Nan 23064ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 23074ba1faa1SWang Nan (const void *)str); 23084ba1faa1SWang Nan } 23094ba1faa1SWang Nan 231032dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 23114ba1faa1SWang Nan const void *arg __maybe_unused) 23124ba1faa1SWang Nan { 23134ba1faa1SWang Nan char new_filter[64]; 23144ba1faa1SWang Nan 23151fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 23164ba1faa1SWang Nan fprintf(stderr, 23174ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 23184ba1faa1SWang Nan return -1; 23194ba1faa1SWang Nan } 23204ba1faa1SWang Nan 23214ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 23224ba1faa1SWang Nan 2323ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, new_filter) < 0) { 23244ba1faa1SWang Nan fprintf(stderr, 23254ba1faa1SWang Nan "not enough memory to hold filter string\n"); 23264ba1faa1SWang Nan return -1; 23274ba1faa1SWang Nan } 23284ba1faa1SWang Nan 23294ba1faa1SWang Nan return 0; 23304ba1faa1SWang Nan } 23314ba1faa1SWang Nan 23324ba1faa1SWang Nan int exclude_perf(const struct option *opt, 23334ba1faa1SWang Nan const char *arg __maybe_unused, 23344ba1faa1SWang Nan int unset __maybe_unused) 23354ba1faa1SWang Nan { 233663503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 23374ba1faa1SWang Nan 23384ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 23394ba1faa1SWang Nan NULL); 23404ba1faa1SWang Nan } 23414ba1faa1SWang Nan 23426cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 23438f707d84SJiri Olsa { 234416fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 23458f707d84SJiri Olsa } 23468f707d84SJiri Olsa 234767b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 234867b49b38SJiri Olsa struct parse_events_term *temp, 234967b49b38SJiri Olsa char *str, u64 num) 23508f707d84SJiri Olsa { 23516cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 23528f707d84SJiri Olsa 235367b49b38SJiri Olsa term = malloc(sizeof(*term)); 23548f707d84SJiri Olsa if (!term) 23558f707d84SJiri Olsa return -ENOMEM; 23568f707d84SJiri Olsa 235767b49b38SJiri Olsa *term = *temp; 23588f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2359c2f1ceadSAndi Kleen term->weak = false; 23608f707d84SJiri Olsa 236167b49b38SJiri Olsa switch (term->type_val) { 23628f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 23638f707d84SJiri Olsa term->val.num = num; 23648f707d84SJiri Olsa break; 23658f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 23668f707d84SJiri Olsa term->val.str = str; 23678f707d84SJiri Olsa break; 23688f707d84SJiri Olsa default: 23694be8be6bSAdrian Hunter free(term); 23708f707d84SJiri Olsa return -EINVAL; 23718f707d84SJiri Olsa } 23728f707d84SJiri Olsa 23738f707d84SJiri Olsa *_term = term; 23748f707d84SJiri Olsa return 0; 23758f707d84SJiri Olsa } 23768f707d84SJiri Olsa 23776cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2378cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 237999e7138eSJiri Olsa bool no_value, 2380bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 238116fa7e82SJiri Olsa { 2382bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2383bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2384bb78ce7dSAdrian Hunter 238567b49b38SJiri Olsa struct parse_events_term temp = { 238667b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 238767b49b38SJiri Olsa .type_term = type_term, 23884f9d4f8aSJohn Garry .config = config ? : strdup(config_term_names[type_term]), 238999e7138eSJiri Olsa .no_value = no_value, 239067b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 239167b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 239267b49b38SJiri Olsa }; 239367b49b38SJiri Olsa 239467b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 239516fa7e82SJiri Olsa } 239616fa7e82SJiri Olsa 23976cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2398cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2399bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 240016fa7e82SJiri Olsa { 2401bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2402bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2403bb78ce7dSAdrian Hunter 240467b49b38SJiri Olsa struct parse_events_term temp = { 240567b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 240667b49b38SJiri Olsa .type_term = type_term, 240767b49b38SJiri Olsa .config = config, 240867b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 240967b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 241067b49b38SJiri Olsa }; 241167b49b38SJiri Olsa 241267b49b38SJiri Olsa return new_term(term, &temp, str, 0); 241316fa7e82SJiri Olsa } 241416fa7e82SJiri Olsa 2415e831f3ccSIan Rogers int parse_events_term__term(struct parse_events_term **term, 2416e831f3ccSIan Rogers int term_lhs, int term_rhs, 2417e831f3ccSIan Rogers void *loc_term, void *loc_val) 2418e831f3ccSIan Rogers { 2419e831f3ccSIan Rogers return parse_events_term__str(term, term_lhs, NULL, 2420e831f3ccSIan Rogers strdup(config_term_names[term_rhs]), 2421e831f3ccSIan Rogers loc_term, loc_val); 2422e831f3ccSIan Rogers } 2423e831f3ccSIan Rogers 24246cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 24256cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2426a6146d50SZheng Yan { 2427b6645a72SIan Rogers char *str; 242867b49b38SJiri Olsa struct parse_events_term temp = { 242967b49b38SJiri Olsa .type_val = term->type_val, 243067b49b38SJiri Olsa .type_term = term->type_term, 2431b6645a72SIan Rogers .config = NULL, 243267b49b38SJiri Olsa .err_term = term->err_term, 243367b49b38SJiri Olsa .err_val = term->err_val, 243467b49b38SJiri Olsa }; 243567b49b38SJiri Olsa 2436b6645a72SIan Rogers if (term->config) { 2437b6645a72SIan Rogers temp.config = strdup(term->config); 2438b6645a72SIan Rogers if (!temp.config) 2439b6645a72SIan Rogers return -ENOMEM; 2440b6645a72SIan Rogers } 2441b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 2442b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 2443b6645a72SIan Rogers 2444b6645a72SIan Rogers str = strdup(term->val.str); 2445b6645a72SIan Rogers if (!str) 2446b6645a72SIan Rogers return -ENOMEM; 2447b6645a72SIan Rogers return new_term(new, &temp, str, 0); 2448a6146d50SZheng Yan } 2449a6146d50SZheng Yan 24501dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 24511dc92556SIan Rogers { 24521dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 24531dc92556SIan Rogers zfree(&term->val.str); 24541dc92556SIan Rogers 24551dc92556SIan Rogers zfree(&term->config); 24561dc92556SIan Rogers free(term); 24571dc92556SIan Rogers } 24581dc92556SIan Rogers 24598255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 24608255718fSAndi Kleen struct list_head **new) 24618255718fSAndi Kleen { 24628255718fSAndi Kleen struct parse_events_term *term, *n; 24638255718fSAndi Kleen int ret; 24648255718fSAndi Kleen 24658255718fSAndi Kleen if (!old) { 24668255718fSAndi Kleen *new = NULL; 24678255718fSAndi Kleen return 0; 24688255718fSAndi Kleen } 24698255718fSAndi Kleen 24708255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 24718255718fSAndi Kleen if (!*new) 24728255718fSAndi Kleen return -ENOMEM; 24738255718fSAndi Kleen INIT_LIST_HEAD(*new); 24748255718fSAndi Kleen 24758255718fSAndi Kleen list_for_each_entry (term, old, list) { 24768255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 24778255718fSAndi Kleen if (ret) 24788255718fSAndi Kleen return ret; 24798255718fSAndi Kleen list_add_tail(&n->list, *new); 24808255718fSAndi Kleen } 24818255718fSAndi Kleen return 0; 24828255718fSAndi Kleen } 24838255718fSAndi Kleen 2484fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 24858f707d84SJiri Olsa { 24866cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 24878f707d84SJiri Olsa 2488a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 2489a8adfcebSWang Nan list_del_init(&term->list); 24901dc92556SIan Rogers parse_events_term__delete(term); 24918f707d84SJiri Olsa } 2492a8adfcebSWang Nan } 2493b39b8393SJiri Olsa 24942146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2495fc0a2c1dSArnaldo Carvalho de Melo { 24962146afc6SArnaldo Carvalho de Melo if (!terms) 24972146afc6SArnaldo Carvalho de Melo return; 2498fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2499d20a5f2bSWang Nan free(terms); 2500fc0a2c1dSArnaldo Carvalho de Melo } 2501fc0a2c1dSArnaldo Carvalho de Melo 25025d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2503b39b8393SJiri Olsa int idx, const char *str) 2504b39b8393SJiri Olsa { 2505448d732cSIan Rogers if (!parse_state->error) 2506a6ced2beSAdrian Hunter return; 2507448d732cSIan Rogers 25086c191289SIan Rogers parse_events_error__handle(parse_state->error, idx, strdup(str), NULL); 2509b39b8393SJiri Olsa } 2510ffeb883eSHe Kuang 251117cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 251217cb5f84SWang Nan { 251317cb5f84SWang Nan int i; 251417cb5f84SWang Nan bool first = true; 251517cb5f84SWang Nan 251617cb5f84SWang Nan buf[0] = '\0'; 251717cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 251817cb5f84SWang Nan const char *name = config_term_names[i]; 251917cb5f84SWang Nan 25201669e509SWang Nan if (!config_term_avail(i, NULL)) 25211669e509SWang Nan continue; 252217cb5f84SWang Nan if (!name) 252317cb5f84SWang Nan continue; 252417cb5f84SWang Nan if (name[0] == '<') 252517cb5f84SWang Nan continue; 252617cb5f84SWang Nan 252717cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 252817cb5f84SWang Nan return; 252917cb5f84SWang Nan 253017cb5f84SWang Nan if (!first) 253117cb5f84SWang Nan strcat(buf, ","); 253217cb5f84SWang Nan else 253317cb5f84SWang Nan first = false; 253417cb5f84SWang Nan strcat(buf, name); 253517cb5f84SWang Nan } 253617cb5f84SWang Nan } 253717cb5f84SWang Nan 2538ffeb883eSHe Kuang /* 2539ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2540ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2541ffeb883eSHe Kuang */ 2542ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2543ffeb883eSHe Kuang { 2544ffeb883eSHe Kuang char *str; 2545626a6b78SWang Nan /* "no-overwrite" is the longest name */ 254617cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2547626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2548ffeb883eSHe Kuang 254917cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2550ffeb883eSHe Kuang /* valid terms */ 2551ffeb883eSHe Kuang if (additional_terms) { 255226dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 255326dee028SWang Nan additional_terms, static_terms) < 0) 2554ffeb883eSHe Kuang goto fail; 2555ffeb883eSHe Kuang } else { 255626dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2557ffeb883eSHe Kuang goto fail; 2558ffeb883eSHe Kuang } 2559ffeb883eSHe Kuang return str; 2560ffeb883eSHe Kuang 2561ffeb883eSHe Kuang fail: 2562ffeb883eSHe Kuang return NULL; 2563ffeb883eSHe Kuang } 2564