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" 1784c86ca1SWang Nan #include "bpf-loader.h" 186e81c74cSMasami Hiramatsu #include "debug.h" 19592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 209c3516d1SJiri Olsa #include <perf/cpumap.h> 21ac20de6fSZheng Yan #include "parse-events-bison.h" 2289812fc8SJiri Olsa #include "parse-events-flex.h" 235f537a26SJiri Olsa #include "pmu.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" 289b7c7728SIan Rogers #include "perf.h" 299cbfa2f6SJin Yao #include "util/parse-events-hybrid.h" 305e4edd1fSJin Yao #include "util/pmu-hybrid.h" 31*d180aa56SNamhyung Kim #include "util/bpf-filter.h" 329b7c7728SIan Rogers #include "tracepoint.h" 3371c86cdaSZhengjun Xing #include "thread_map.h" 3489812fc8SJiri Olsa 3589812fc8SJiri Olsa #define MAX_NAME_LEN 100 3686470930SIngo Molnar 379b7c7728SIan Rogers struct perf_pmu_event_symbol { 389b7c7728SIan Rogers char *symbol; 399b7c7728SIan Rogers enum perf_pmu_event_symbol_type type; 409b7c7728SIan Rogers }; 419b7c7728SIan Rogers 4282ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4382ba1f2fSJiri Olsa extern int parse_events_debug; 4482ba1f2fSJiri Olsa #endif 455d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 46e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 47e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 485e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 495e4edd1fSJin Yao const char *str, char *pmu_name, 505e4edd1fSJin Yao struct list_head *list); 51bcd3279fSFrederic Weisbecker 52dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 53dcb4e102SKan Liang /* 54dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 55dcb4e102SKan Liang * 0 means not initialized and ready to init 56dcb4e102SKan Liang * -1 means failed to init, don't try anymore 57dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 58dcb4e102SKan Liang */ 59dcb4e102SKan Liang static int perf_pmu_events_list_num; 60dcb4e102SKan Liang 61705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 621dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 631dc12760SJiri Olsa .symbol = "cpu-cycles", 641dc12760SJiri Olsa .alias = "cycles", 651dc12760SJiri Olsa }, 661dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 671dc12760SJiri Olsa .symbol = "instructions", 681dc12760SJiri Olsa .alias = "", 691dc12760SJiri Olsa }, 701dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 711dc12760SJiri Olsa .symbol = "cache-references", 721dc12760SJiri Olsa .alias = "", 731dc12760SJiri Olsa }, 741dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 751dc12760SJiri Olsa .symbol = "cache-misses", 761dc12760SJiri Olsa .alias = "", 771dc12760SJiri Olsa }, 781dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 791dc12760SJiri Olsa .symbol = "branch-instructions", 801dc12760SJiri Olsa .alias = "branches", 811dc12760SJiri Olsa }, 821dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 831dc12760SJiri Olsa .symbol = "branch-misses", 841dc12760SJiri Olsa .alias = "", 851dc12760SJiri Olsa }, 861dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 871dc12760SJiri Olsa .symbol = "bus-cycles", 881dc12760SJiri Olsa .alias = "", 891dc12760SJiri Olsa }, 901dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 911dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 921dc12760SJiri Olsa .alias = "idle-cycles-frontend", 931dc12760SJiri Olsa }, 941dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 951dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 961dc12760SJiri Olsa .alias = "idle-cycles-backend", 971dc12760SJiri Olsa }, 981dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 991dc12760SJiri Olsa .symbol = "ref-cycles", 1001dc12760SJiri Olsa .alias = "", 1011dc12760SJiri Olsa }, 1021dc12760SJiri Olsa }; 10386470930SIngo Molnar 104705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 1051dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1061dc12760SJiri Olsa .symbol = "cpu-clock", 1071dc12760SJiri Olsa .alias = "", 1081dc12760SJiri Olsa }, 1091dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1101dc12760SJiri Olsa .symbol = "task-clock", 1111dc12760SJiri Olsa .alias = "", 1121dc12760SJiri Olsa }, 1131dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1141dc12760SJiri Olsa .symbol = "page-faults", 1151dc12760SJiri Olsa .alias = "faults", 1161dc12760SJiri Olsa }, 1171dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1181dc12760SJiri Olsa .symbol = "context-switches", 1191dc12760SJiri Olsa .alias = "cs", 1201dc12760SJiri Olsa }, 1211dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1221dc12760SJiri Olsa .symbol = "cpu-migrations", 1231dc12760SJiri Olsa .alias = "migrations", 1241dc12760SJiri Olsa }, 1251dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1261dc12760SJiri Olsa .symbol = "minor-faults", 1271dc12760SJiri Olsa .alias = "", 1281dc12760SJiri Olsa }, 1291dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1301dc12760SJiri Olsa .symbol = "major-faults", 1311dc12760SJiri Olsa .alias = "", 1321dc12760SJiri Olsa }, 1331dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1341dc12760SJiri Olsa .symbol = "alignment-faults", 1351dc12760SJiri Olsa .alias = "", 1361dc12760SJiri Olsa }, 1371dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1381dc12760SJiri Olsa .symbol = "emulation-faults", 1391dc12760SJiri Olsa .alias = "", 1401dc12760SJiri Olsa }, 141d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 142d22d1a2aSAdrian Hunter .symbol = "dummy", 143d22d1a2aSAdrian Hunter .alias = "", 144d22d1a2aSAdrian Hunter }, 145bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 146bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 147bae9cc41SArnaldo Carvalho de Melo .alias = "", 148bae9cc41SArnaldo Carvalho de Melo }, 149fb6c79d7SNamhyung Kim [PERF_COUNT_SW_CGROUP_SWITCHES] = { 150fb6c79d7SNamhyung Kim .symbol = "cgroup-switches", 151fb6c79d7SNamhyung Kim .alias = "", 152fb6c79d7SNamhyung Kim }, 15386470930SIngo Molnar }; 15486470930SIngo Molnar 15571c86cdaSZhengjun Xing bool is_event_supported(u8 type, u64 config) 15671c86cdaSZhengjun Xing { 15771c86cdaSZhengjun Xing bool ret = true; 15871c86cdaSZhengjun Xing int open_return; 15971c86cdaSZhengjun Xing struct evsel *evsel; 16071c86cdaSZhengjun Xing struct perf_event_attr attr = { 16171c86cdaSZhengjun Xing .type = type, 16271c86cdaSZhengjun Xing .config = config, 16371c86cdaSZhengjun Xing .disabled = 1, 16471c86cdaSZhengjun Xing }; 16571c86cdaSZhengjun Xing struct perf_thread_map *tmap = thread_map__new_by_tid(0); 16671c86cdaSZhengjun Xing 16771c86cdaSZhengjun Xing if (tmap == NULL) 16871c86cdaSZhengjun Xing return false; 16971c86cdaSZhengjun Xing 17071c86cdaSZhengjun Xing evsel = evsel__new(&attr); 17171c86cdaSZhengjun Xing if (evsel) { 17271c86cdaSZhengjun Xing open_return = evsel__open(evsel, NULL, tmap); 17371c86cdaSZhengjun Xing ret = open_return >= 0; 17471c86cdaSZhengjun Xing 17571c86cdaSZhengjun Xing if (open_return == -EACCES) { 17671c86cdaSZhengjun Xing /* 17771c86cdaSZhengjun Xing * This happens if the paranoid value 17871c86cdaSZhengjun Xing * /proc/sys/kernel/perf_event_paranoid is set to 2 17971c86cdaSZhengjun Xing * Re-run with exclude_kernel set; we don't do that 18071c86cdaSZhengjun Xing * by default as some ARM machines do not support it. 18171c86cdaSZhengjun Xing * 18271c86cdaSZhengjun Xing */ 18371c86cdaSZhengjun Xing evsel->core.attr.exclude_kernel = 1; 18471c86cdaSZhengjun Xing ret = evsel__open(evsel, NULL, tmap) >= 0; 18571c86cdaSZhengjun Xing } 18671c86cdaSZhengjun Xing evsel__delete(evsel); 18771c86cdaSZhengjun Xing } 18871c86cdaSZhengjun Xing 18971c86cdaSZhengjun Xing perf_thread_map__put(tmap); 19071c86cdaSZhengjun Xing return ret; 19171c86cdaSZhengjun Xing } 19271c86cdaSZhengjun Xing 1931424dc96SDavid Ahern const char *event_type(int type) 1941424dc96SDavid Ahern { 1951424dc96SDavid Ahern switch (type) { 1961424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 1971424dc96SDavid Ahern return "hardware"; 1981424dc96SDavid Ahern 1991424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2001424dc96SDavid Ahern return "software"; 2011424dc96SDavid Ahern 2021424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2031424dc96SDavid Ahern return "tracepoint"; 2041424dc96SDavid Ahern 2051424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2061424dc96SDavid Ahern return "hardware-cache"; 2071424dc96SDavid Ahern 2081424dc96SDavid Ahern default: 2091424dc96SDavid Ahern break; 2101424dc96SDavid Ahern } 2111424dc96SDavid Ahern 2121424dc96SDavid Ahern return "unknown"; 2131424dc96SDavid Ahern } 2141424dc96SDavid Ahern 2152b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term) 216e814fdddSWang Nan { 217e814fdddSWang Nan struct parse_events_term *term; 218e814fdddSWang Nan 219e814fdddSWang Nan if (!head_terms) 220e814fdddSWang Nan return NULL; 221e814fdddSWang Nan 222e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 2232b62b3a6SIan Rogers if (term->type_term == type_term) 224e814fdddSWang Nan return term->val.str; 225e814fdddSWang Nan 226e814fdddSWang Nan return NULL; 227e814fdddSWang Nan } 2287ae92e74SYan, Zheng 2292b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms) 2302b62b3a6SIan Rogers { 2312b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID); 2322b62b3a6SIan Rogers } 2332b62b3a6SIan Rogers 2342b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms) 2352b62b3a6SIan Rogers { 2362b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); 2372b62b3a6SIan Rogers } 2382b62b3a6SIan Rogers 23932dcd021SJiri Olsa static struct evsel * 240410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 2417ae92e74SYan, Zheng struct perf_event_attr *attr, 24270943490SStephane Eranian bool init_attr, 2432b62b3a6SIan Rogers const char *name, const char *metric_id, struct perf_pmu *pmu, 244f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 245f0fbb114SAndi Kleen const char *cpu_list) 24689812fc8SJiri Olsa { 24732dcd021SJiri Olsa struct evsel *evsel; 2485d680be3SNamhyung Kim struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 2499c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 25089812fc8SJiri Olsa 251e552b7beSRob Herring if (pmu) 252e552b7beSRob Herring perf_pmu__warn_invalid_formats(pmu); 253e552b7beSRob Herring 254e4064776SJin Yao if (pmu && attr->type == PERF_TYPE_RAW) 255e4064776SJin Yao perf_pmu__warn_invalid_config(pmu, attr->config, name); 256e4064776SJin Yao 25770943490SStephane Eranian if (init_attr) 25889812fc8SJiri Olsa event_attr_init(attr); 25989812fc8SJiri Olsa 2608f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attr, *idx); 2610f1b550eSNamhyung Kim if (!evsel) { 2620f1b550eSNamhyung Kim perf_cpu_map__put(cpus); 263410136f5SStephane Eranian return NULL; 2640f1b550eSNamhyung Kim } 26589812fc8SJiri Olsa 26675fc5ae5STaeung Song (*idx)++; 2675d680be3SNamhyung Kim evsel->core.cpus = cpus; 268fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 269d3345fecSAdrian Hunter evsel->core.requires_cpu = pmu ? pmu->is_uncore : false; 27063ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 271f7400262SNamhyung Kim evsel->pmu = pmu; 272f30a79b0SJiri Olsa 2739db1763cSArnaldo Carvalho de Melo if (name) 27489812fc8SJiri Olsa evsel->name = strdup(name); 275930a2e29SJiri Olsa 2762b62b3a6SIan Rogers if (metric_id) 2772b62b3a6SIan Rogers evsel->metric_id = strdup(metric_id); 2782b62b3a6SIan Rogers 279930a2e29SJiri Olsa if (config_terms) 28099fc5941SAdrian Hunter list_splice_init(config_terms, &evsel->config_terms); 281930a2e29SJiri Olsa 28270943490SStephane Eranian if (list) 283b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 28470943490SStephane Eranian 285410136f5SStephane Eranian return evsel; 28689812fc8SJiri Olsa } 28789812fc8SJiri Olsa 28870943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 2892b62b3a6SIan Rogers const char *name, const char *metric_id, 2902b62b3a6SIan Rogers struct perf_pmu *pmu) 29170943490SStephane Eranian { 2922b62b3a6SIan Rogers return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name, 2932b62b3a6SIan Rogers metric_id, pmu, /*config_terms=*/NULL, 2942b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL); 29570943490SStephane Eranian } 29670943490SStephane Eranian 297c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 2988e8bbfb3SIan Rogers struct perf_event_attr *attr, const char *name, 2992b62b3a6SIan Rogers const char *metric_id, struct list_head *config_terms) 3007ae92e74SYan, Zheng { 3012b62b3a6SIan Rogers return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, 3022b62b3a6SIan Rogers /*pmu=*/NULL, config_terms, 3032b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM; 304f0fbb114SAndi Kleen } 305f0fbb114SAndi Kleen 306f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 307f0fbb114SAndi Kleen enum perf_tool_event tool_event) 308f0fbb114SAndi Kleen { 30932dcd021SJiri Olsa struct evsel *evsel; 310f0fbb114SAndi Kleen struct perf_event_attr attr = { 311f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 312f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 313f0fbb114SAndi Kleen }; 314f0fbb114SAndi Kleen 3152b62b3a6SIan Rogers evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL, 3162b62b3a6SIan Rogers /*metric_id=*/NULL, /*pmu=*/NULL, 3172b62b3a6SIan Rogers /*config_terms=*/NULL, /*auto_merge_stats=*/false, 3182b62b3a6SIan Rogers /*cpu_list=*/"0"); 319f0fbb114SAndi Kleen if (!evsel) 320f0fbb114SAndi Kleen return -ENOMEM; 321f0fbb114SAndi Kleen evsel->tool_event = tool_event; 322b03b89b3SFlorian Fischer if (tool_event == PERF_TOOL_DURATION_TIME 323b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_USER_TIME 324b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_SYSTEM_TIME) { 325b194c9cdSIan Rogers free((char *)evsel->unit); 326b194c9cdSIan Rogers evsel->unit = strdup("ns"); 327b194c9cdSIan Rogers } 328f0fbb114SAndi Kleen return 0; 3297ae92e74SYan, Zheng } 3307ae92e74SYan, Zheng 331545a96c9SIan Rogers static int parse_aliases(char *str, const char *const names[][EVSEL__MAX_ALIASES], int size) 33286470930SIngo Molnar { 33386470930SIngo Molnar int i, j; 33461c45981SPaul Mackerras int n, longest = -1; 33586470930SIngo Molnar 33686470930SIngo Molnar for (i = 0; i < size; i++) { 337c64e85e1SArnaldo Carvalho de Melo for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 33861c45981SPaul Mackerras n = strlen(names[i][j]); 33989812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 34061c45981SPaul Mackerras longest = n; 34161c45981SPaul Mackerras } 34289812fc8SJiri Olsa if (longest > 0) 34386470930SIngo Molnar return i; 34486470930SIngo Molnar } 34586470930SIngo Molnar 3468953645fSIngo Molnar return -1; 34786470930SIngo Molnar } 34886470930SIngo Molnar 34943d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 35043d0b978SWang Nan struct parse_events_term *term, 35143d0b978SWang Nan struct parse_events_error *err); 35243d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 35343d0b978SWang Nan struct parse_events_term *term, 35443d0b978SWang Nan struct parse_events_error *err); 35543d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 35643d0b978SWang Nan struct list_head *head, 35743d0b978SWang Nan struct parse_events_error *err, 35843d0b978SWang Nan config_term_func_t config_term); 35943d0b978SWang Nan 360c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 36143d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3622c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 363c93afadcSJin Yao struct list_head *head_config, 364c93afadcSJin Yao struct parse_events_state *parse_state) 36586470930SIngo Molnar { 36689812fc8SJiri Olsa struct perf_event_attr attr; 36743d0b978SWang Nan LIST_HEAD(config_terms); 3688e8bbfb3SIan Rogers char name[MAX_NAME_LEN]; 3692b62b3a6SIan Rogers const char *config_name, *metric_id; 37061c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 37189812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 37230def61fSJin Yao int i, n, ret; 37330def61fSJin Yao bool hybrid; 37486470930SIngo Molnar 37586470930SIngo Molnar /* 37686470930SIngo Molnar * No fallback - if we cannot get a clear cache type 37786470930SIngo Molnar * then bail out: 37886470930SIngo Molnar */ 379c64e85e1SArnaldo Carvalho de Melo cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX); 38086470930SIngo Molnar if (cache_type == -1) 38189812fc8SJiri Olsa return -EINVAL; 38286470930SIngo Molnar 38343d0b978SWang Nan config_name = get_config_name(head_config); 38489812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 38589812fc8SJiri Olsa 38689812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 38789812fc8SJiri Olsa char *str = op_result[i]; 38889812fc8SJiri Olsa 389275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 39061c45981SPaul Mackerras 39161c45981SPaul Mackerras if (cache_op == -1) { 392c64e85e1SArnaldo Carvalho de Melo cache_op = parse_aliases(str, evsel__hw_cache_op, 39361c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 39461c45981SPaul Mackerras if (cache_op >= 0) { 395c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(cache_type, cache_op)) 39689812fc8SJiri Olsa return -EINVAL; 39761c45981SPaul Mackerras continue; 39861c45981SPaul Mackerras } 39961c45981SPaul Mackerras } 40061c45981SPaul Mackerras 40161c45981SPaul Mackerras if (cache_result == -1) { 402c64e85e1SArnaldo Carvalho de Melo cache_result = parse_aliases(str, evsel__hw_cache_result, 40361c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 40461c45981SPaul Mackerras if (cache_result >= 0) 40561c45981SPaul Mackerras continue; 40661c45981SPaul Mackerras } 40761c45981SPaul Mackerras } 40861c45981SPaul Mackerras 40986470930SIngo Molnar /* 41086470930SIngo Molnar * Fall back to reads: 41186470930SIngo Molnar */ 4128953645fSIngo Molnar if (cache_op == -1) 4138953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 41486470930SIngo Molnar 41586470930SIngo Molnar /* 41686470930SIngo Molnar * Fall back to accesses: 41786470930SIngo Molnar */ 41886470930SIngo Molnar if (cache_result == -1) 41986470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 42086470930SIngo Molnar 42189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 42289812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 42389812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 42443d0b978SWang Nan 42543d0b978SWang Nan if (head_config) { 4262c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 42743d0b978SWang Nan config_term_common)) 42843d0b978SWang Nan return -EINVAL; 42943d0b978SWang Nan 43043d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 43143d0b978SWang Nan return -ENOMEM; 43243d0b978SWang Nan } 43330def61fSJin Yao 4342b62b3a6SIan Rogers metric_id = get_config_metric_id(head_config); 43530def61fSJin Yao ret = parse_events__add_cache_hybrid(list, idx, &attr, 4362b62b3a6SIan Rogers config_name ? : name, 4372b62b3a6SIan Rogers metric_id, 4382b62b3a6SIan Rogers &config_terms, 439c93afadcSJin Yao &hybrid, parse_state); 44030def61fSJin Yao if (hybrid) 44199fc5941SAdrian Hunter goto out_free_terms; 44230def61fSJin Yao 4432b62b3a6SIan Rogers ret = add_event(list, idx, &attr, config_name ? : name, metric_id, 4442b62b3a6SIan Rogers &config_terms); 44599fc5941SAdrian Hunter out_free_terms: 44699fc5941SAdrian Hunter free_config_terms(&config_terms); 44799fc5941SAdrian Hunter return ret; 44886470930SIngo Molnar } 44986470930SIngo Molnar 450378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 451272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4528c619d6aSWang Nan const char *sys, const char *name) 45319658171SJiri Olsa { 4544584f084SIan Rogers const char *str; 45519658171SJiri Olsa char help[BUFSIZ]; 45619658171SJiri Olsa 457ec183d22SAdrian Hunter if (!e) 458ec183d22SAdrian Hunter return; 459ec183d22SAdrian Hunter 46019658171SJiri Olsa /* 46119658171SJiri Olsa * We get error directly from syscall errno ( > 0), 46219658171SJiri Olsa * or from encoded pointer's error ( < 0). 46319658171SJiri Olsa */ 46419658171SJiri Olsa err = abs(err); 46519658171SJiri Olsa 46619658171SJiri Olsa switch (err) { 46719658171SJiri Olsa case EACCES: 4684584f084SIan Rogers str = "can't access trace events"; 46919658171SJiri Olsa break; 47019658171SJiri Olsa case ENOENT: 4714584f084SIan Rogers str = "unknown tracepoint"; 47219658171SJiri Olsa break; 47319658171SJiri Olsa default: 4744584f084SIan Rogers str = "failed to add tracepoint"; 47519658171SJiri Olsa break; 47619658171SJiri Olsa } 47719658171SJiri Olsa 47819658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 4796c191289SIan Rogers parse_events_error__handle(e, 0, strdup(str), strdup(help)); 48019658171SJiri Olsa } 48119658171SJiri Olsa 482c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4838c619d6aSWang Nan const char *sys_name, const char *evt_name, 484272ed29aSJiri Olsa struct parse_events_error *err, 485e637d177SHe Kuang struct list_head *head_config) 486bcd3279fSFrederic Weisbecker { 4878f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 488bcd3279fSFrederic Weisbecker 48919658171SJiri Olsa if (IS_ERR(evsel)) { 490272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4918dd2a131SJiri Olsa return PTR_ERR(evsel); 49219658171SJiri Olsa } 493bcd3279fSFrederic Weisbecker 494e637d177SHe Kuang if (head_config) { 495e637d177SHe Kuang LIST_HEAD(config_terms); 496e637d177SHe Kuang 497e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 498e637d177SHe Kuang return -ENOMEM; 499e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 500e637d177SHe Kuang } 501e637d177SHe Kuang 502b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 50382fe1c29SArnaldo Carvalho de Melo return 0; 504bcd3279fSFrederic Weisbecker } 505bcd3279fSFrederic Weisbecker 506c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5078c619d6aSWang Nan const char *sys_name, const char *evt_name, 508272ed29aSJiri Olsa struct parse_events_error *err, 509e637d177SHe Kuang struct list_head *head_config) 510bcd3279fSFrederic Weisbecker { 51125a7d914SArnaldo Carvalho de Melo char *evt_path; 512bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 513bcd3279fSFrederic Weisbecker DIR *evt_dir; 51427bf90bfSJiri Olsa int ret = 0, found = 0; 515bcd3279fSFrederic Weisbecker 51625a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 51725a7d914SArnaldo Carvalho de Melo if (!evt_path) { 51825a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 51925a7d914SArnaldo Carvalho de Melo return -1; 52025a7d914SArnaldo Carvalho de Melo } 521bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 522bcd3279fSFrederic Weisbecker if (!evt_dir) { 52325a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 524272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 52589812fc8SJiri Olsa return -1; 526bcd3279fSFrederic Weisbecker } 527bcd3279fSFrederic Weisbecker 52889812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 529bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 530bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 531bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 532bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 533bcd3279fSFrederic Weisbecker continue; 534bcd3279fSFrederic Weisbecker 53589812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 536fb1d2edfSMasami Hiramatsu continue; 537fb1d2edfSMasami Hiramatsu 53827bf90bfSJiri Olsa found++; 53927bf90bfSJiri Olsa 540e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 541272ed29aSJiri Olsa err, head_config); 542bcd3279fSFrederic Weisbecker } 543bcd3279fSFrederic Weisbecker 54427bf90bfSJiri Olsa if (!found) { 54527bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 54627bf90bfSJiri Olsa ret = -1; 54727bf90bfSJiri Olsa } 54827bf90bfSJiri Olsa 54925a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 5500bd3f084SJiri Olsa closedir(evt_dir); 55189812fc8SJiri Olsa return ret; 552bcd3279fSFrederic Weisbecker } 553bcd3279fSFrederic Weisbecker 554c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5558c619d6aSWang Nan const char *sys_name, const char *evt_name, 556272ed29aSJiri Olsa struct parse_events_error *err, 557e637d177SHe Kuang struct list_head *head_config) 558f35488f9SJiri Olsa { 559f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 560e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 561272ed29aSJiri Olsa err, head_config) : 562e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 563272ed29aSJiri Olsa err, head_config); 564f35488f9SJiri Olsa } 565f35488f9SJiri Olsa 566c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5678c619d6aSWang Nan const char *sys_name, const char *evt_name, 568272ed29aSJiri Olsa struct parse_events_error *err, 569e637d177SHe Kuang struct list_head *head_config) 570f35488f9SJiri Olsa { 571f35488f9SJiri Olsa struct dirent *events_ent; 572f35488f9SJiri Olsa DIR *events_dir; 573f35488f9SJiri Olsa int ret = 0; 574f35488f9SJiri Olsa 5757014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 576f35488f9SJiri Olsa if (!events_dir) { 577272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 578f35488f9SJiri Olsa return -1; 579f35488f9SJiri Olsa } 580f35488f9SJiri Olsa 581f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 582f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 583f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 584f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 585f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 586f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 587f35488f9SJiri Olsa continue; 588f35488f9SJiri Olsa 589f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 590f35488f9SJiri Olsa continue; 591f35488f9SJiri Olsa 592f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 593272ed29aSJiri Olsa evt_name, err, head_config); 594f35488f9SJiri Olsa } 595f35488f9SJiri Olsa 596f35488f9SJiri Olsa closedir(events_dir); 597f35488f9SJiri Olsa return ret; 598f35488f9SJiri Olsa } 599378ef0f5SIan Rogers #endif /* HAVE_LIBTRACEEVENT */ 600f35488f9SJiri Olsa 601c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT 6024edf30e3SWang Nan struct __add_bpf_event_param { 6035d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6044edf30e3SWang Nan struct list_head *list; 60595088a59SWang Nan struct list_head *head_config; 6064edf30e3SWang Nan }; 6074edf30e3SWang Nan 608af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 6094edf30e3SWang Nan void *_param) 6104edf30e3SWang Nan { 6114edf30e3SWang Nan LIST_HEAD(new_evsels); 6124edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6135d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6144edf30e3SWang Nan struct list_head *list = param->list; 61532dcd021SJiri Olsa struct evsel *pos; 6164edf30e3SWang Nan int err; 6172620b7e3SArnaldo Carvalho de Melo /* 6182620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 6192620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 6202620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 6212620b7e3SArnaldo Carvalho de Melo * 6222620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 6232620b7e3SArnaldo Carvalho de Melo */ 6242620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 6252620b7e3SArnaldo Carvalho de Melo return 0; 6264edf30e3SWang Nan 6274edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 628cd102d70SWang Nan group, event, fd); 6294edf30e3SWang Nan 6305d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6315d369a75SArnaldo Carvalho de Melo event, parse_state->error, 63295088a59SWang Nan param->head_config); 6334edf30e3SWang Nan if (err) { 63432dcd021SJiri Olsa struct evsel *evsel, *tmp; 6354edf30e3SWang Nan 6364edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 637cd102d70SWang Nan group, event); 638b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 639b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 6405eb2dd2aSJiri Olsa evsel__delete(evsel); 6414edf30e3SWang Nan } 6424edf30e3SWang Nan return err; 6434edf30e3SWang Nan } 644cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6454edf30e3SWang Nan 646b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 6471f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 648cd102d70SWang Nan group, event, pos); 6491f45b1d4SWang Nan pos->bpf_fd = fd; 650af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 6511f45b1d4SWang Nan } 6524edf30e3SWang Nan list_splice(&new_evsels, list); 6534edf30e3SWang Nan return 0; 6544edf30e3SWang Nan } 6554edf30e3SWang Nan 6565d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 65784c86ca1SWang Nan struct list_head *list, 65895088a59SWang Nan struct bpf_object *obj, 65995088a59SWang Nan struct list_head *head_config) 66084c86ca1SWang Nan { 66184c86ca1SWang Nan int err; 66284c86ca1SWang Nan char errbuf[BUFSIZ]; 6635d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 664aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 66584c86ca1SWang Nan 66684c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 66784c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 66884c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 66984c86ca1SWang Nan err = -EINVAL; 67084c86ca1SWang Nan goto errout; 67184c86ca1SWang Nan } 67284c86ca1SWang Nan 67384c86ca1SWang Nan /* 674aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 675aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 676aa3abf30SWang Nan * created when failure. 677aa3abf30SWang Nan */ 678aa3abf30SWang Nan if (!registered_unprobe_atexit) { 679aa3abf30SWang Nan atexit(bpf__clear); 680aa3abf30SWang Nan registered_unprobe_atexit = true; 681aa3abf30SWang Nan } 682aa3abf30SWang Nan 683aa3abf30SWang Nan err = bpf__probe(obj); 684aa3abf30SWang Nan if (err) { 685aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 686aa3abf30SWang Nan goto errout; 687aa3abf30SWang Nan } 688aa3abf30SWang Nan 6891e5e3ee8SWang Nan err = bpf__load(obj); 6901e5e3ee8SWang Nan if (err) { 6911e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6921e5e3ee8SWang Nan goto errout; 6931e5e3ee8SWang Nan } 6941e5e3ee8SWang Nan 695cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6964edf30e3SWang Nan if (err) { 6974edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6984edf30e3SWang Nan "Attach events in BPF object failed"); 6994edf30e3SWang Nan goto errout; 7004edf30e3SWang Nan } 7014edf30e3SWang Nan 7024edf30e3SWang Nan return 0; 70384c86ca1SWang Nan errout: 7046c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 7055f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 70684c86ca1SWang Nan return err; 70784c86ca1SWang Nan } 70884c86ca1SWang Nan 709a34f3be7SWang Nan static int 7105d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 711a34f3be7SWang Nan struct bpf_object *obj, 712a34f3be7SWang Nan struct list_head *head_config) 713a34f3be7SWang Nan { 714a34f3be7SWang Nan struct parse_events_term *term; 715a34f3be7SWang Nan int error_pos; 716a34f3be7SWang Nan 717a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 718a34f3be7SWang Nan return 0; 719a34f3be7SWang Nan 720a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 721a34f3be7SWang Nan int err; 722a34f3be7SWang Nan 723a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 7246c191289SIan Rogers parse_events_error__handle(parse_state->error, term->err_term, 7255f634c8eSIan Rogers strdup("Invalid config term for BPF object"), 7265f634c8eSIan Rogers NULL); 727a34f3be7SWang Nan return -EINVAL; 728a34f3be7SWang Nan } 729a34f3be7SWang Nan 7305d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 731a34f3be7SWang Nan if (err) { 7325f634c8eSIan Rogers char errbuf[BUFSIZ]; 7335f634c8eSIan Rogers int idx; 7345f634c8eSIan Rogers 7355d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 736a34f3be7SWang Nan &error_pos, err, errbuf, 737a34f3be7SWang Nan sizeof(errbuf)); 7385f634c8eSIan Rogers 7395f634c8eSIan Rogers if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 7405f634c8eSIan Rogers idx = term->err_val; 7415f634c8eSIan Rogers else 7425f634c8eSIan Rogers idx = term->err_term + error_pos; 7435f634c8eSIan Rogers 7446c191289SIan Rogers parse_events_error__handle(parse_state->error, idx, 7455f634c8eSIan Rogers strdup(errbuf), 7465f634c8eSIan Rogers strdup( 7477630b3e2SWang Nan "Hint:\tValid config terms:\n" 748e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 749e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7507630b3e2SWang Nan "\n" 751e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 7525f634c8eSIan Rogers " \t(add -v to see detail)")); 753a34f3be7SWang Nan return err; 754a34f3be7SWang Nan } 755a34f3be7SWang Nan } 756a34f3be7SWang Nan return 0; 757a34f3be7SWang Nan } 758a34f3be7SWang Nan 75995088a59SWang Nan /* 76095088a59SWang Nan * Split config terms: 76195088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 76295088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 76395088a59SWang Nan * events in bpf.c. 76495088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 76595088a59SWang Nan * with parse_events_config_bpf. 76695088a59SWang Nan * 76795088a59SWang Nan * Move object config terms from the first list to obj_head_config. 76895088a59SWang Nan */ 76995088a59SWang Nan static void 77095088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 77195088a59SWang Nan struct list_head *obj_head_config) 77295088a59SWang Nan { 77395088a59SWang Nan struct parse_events_term *term, *temp; 77495088a59SWang Nan 77595088a59SWang Nan /* 7764d39c89fSIngo Molnar * Currently, all possible user config term 77795088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 7784d39c89fSIngo Molnar * happens to be a good flag. 77995088a59SWang Nan * 78095088a59SWang Nan * See parse_events_config_bpf() and 78195088a59SWang Nan * config_term_tracepoint(). 78295088a59SWang Nan */ 78395088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 78495088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 78595088a59SWang Nan list_move_tail(&term->list, obj_head_config); 78695088a59SWang Nan } 78795088a59SWang Nan 7885d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 78984c86ca1SWang Nan struct list_head *list, 790d509db04SWang Nan char *bpf_file_name, 791a34f3be7SWang Nan bool source, 792a34f3be7SWang Nan struct list_head *head_config) 79384c86ca1SWang Nan { 794a34f3be7SWang Nan int err; 79595088a59SWang Nan struct bpf_object *obj; 79695088a59SWang Nan LIST_HEAD(obj_head_config); 79795088a59SWang Nan 79895088a59SWang Nan if (head_config) 79995088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 80084c86ca1SWang Nan 801d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8026371ca3bSWang Nan if (IS_ERR(obj)) { 80384c86ca1SWang Nan char errbuf[BUFSIZ]; 80484c86ca1SWang Nan 8056371ca3bSWang Nan err = PTR_ERR(obj); 80684c86ca1SWang Nan 80784c86ca1SWang Nan if (err == -ENOTSUP) 80884c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 80984c86ca1SWang Nan "BPF support is not compiled"); 81084c86ca1SWang Nan else 811d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 812d3e0ce39SWang Nan source, 813d3e0ce39SWang Nan -err, errbuf, 814d3e0ce39SWang Nan sizeof(errbuf)); 81584c86ca1SWang Nan 8166c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 8175f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 81884c86ca1SWang Nan return err; 81984c86ca1SWang Nan } 82084c86ca1SWang Nan 8215d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 822a34f3be7SWang Nan if (err) 823a34f3be7SWang Nan return err; 8245d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 82595088a59SWang Nan 82695088a59SWang Nan /* 82795088a59SWang Nan * Caller doesn't know anything about obj_head_config, 8284d39c89fSIngo Molnar * so combine them together again before returning. 82995088a59SWang Nan */ 83095088a59SWang Nan if (head_config) 83195088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 83295088a59SWang Nan return err; 83384c86ca1SWang Nan } 834c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT 835c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 836c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 837c18cf78dSArnaldo Carvalho de Melo struct bpf_object *obj __maybe_unused, 838c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 839c18cf78dSArnaldo Carvalho de Melo { 8406c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 841c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 842c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 843c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 844c18cf78dSArnaldo Carvalho de Melo } 845c18cf78dSArnaldo Carvalho de Melo 846c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 847c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 848c18cf78dSArnaldo Carvalho de Melo char *bpf_file_name __maybe_unused, 849c18cf78dSArnaldo Carvalho de Melo bool source __maybe_unused, 850c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 851c18cf78dSArnaldo Carvalho de Melo { 8526c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 853c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 854c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 855c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 856c18cf78dSArnaldo Carvalho de Melo } 857c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT 85884c86ca1SWang Nan 85989812fc8SJiri Olsa static int 86089812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8611b290d67SFrederic Weisbecker { 8621b290d67SFrederic Weisbecker int i; 8631b290d67SFrederic Weisbecker 8641b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 86589812fc8SJiri Olsa if (!type || !type[i]) 8661b290d67SFrederic Weisbecker break; 8671b290d67SFrederic Weisbecker 8687582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8697582732fSJiri Olsa do { \ 8707582732fSJiri Olsa if (attr->bp_type & bit) \ 8717582732fSJiri Olsa return -EINVAL; \ 8727582732fSJiri Olsa else \ 8737582732fSJiri Olsa attr->bp_type |= bit; \ 8747582732fSJiri Olsa } while (0) 8757582732fSJiri Olsa 8761b290d67SFrederic Weisbecker switch (type[i]) { 8771b290d67SFrederic Weisbecker case 'r': 8787582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8791b290d67SFrederic Weisbecker break; 8801b290d67SFrederic Weisbecker case 'w': 8817582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8821b290d67SFrederic Weisbecker break; 8831b290d67SFrederic Weisbecker case 'x': 8847582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8851b290d67SFrederic Weisbecker break; 8861b290d67SFrederic Weisbecker default: 88789812fc8SJiri Olsa return -EINVAL; 8881b290d67SFrederic Weisbecker } 8891b290d67SFrederic Weisbecker } 89089812fc8SJiri Olsa 8917582732fSJiri Olsa #undef CHECK_SET_TYPE 8927582732fSJiri Olsa 8931b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8941b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8951b290d67SFrederic Weisbecker 89689812fc8SJiri Olsa return 0; 8971b290d67SFrederic Weisbecker } 8981b290d67SFrederic Weisbecker 899c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 900aa98d848SIan Rogers u64 addr, char *type, u64 len) 9011b290d67SFrederic Weisbecker { 90289812fc8SJiri Olsa struct perf_event_attr attr; 9031b290d67SFrederic Weisbecker 90489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 905aa98d848SIan Rogers attr.bp_addr = addr; 9061b290d67SFrederic Weisbecker 90789812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 90889812fc8SJiri Olsa return -EINVAL; 9091b290d67SFrederic Weisbecker 9103741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 9113741eb9fSJacob Shin if (!len) { 91289812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 9133741eb9fSJacob Shin len = sizeof(long); 914aa59a485SFrederic Weisbecker else 9153741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 9163741eb9fSJacob Shin } 9173741eb9fSJacob Shin 9183741eb9fSJacob Shin attr.bp_len = len; 919aa59a485SFrederic Weisbecker 92089812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 9214a841d65SJovi Zhang attr.sample_period = 1; 9221b290d67SFrederic Weisbecker 9232b62b3a6SIan Rogers return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL, 9242b62b3a6SIan Rogers /*config_terms=*/NULL); 9251b290d67SFrederic Weisbecker } 9261b290d67SFrederic Weisbecker 9273b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 9283b0e371cSJiri Olsa struct parse_events_error *err, 9293b0e371cSJiri Olsa int type) 9303b0e371cSJiri Olsa { 9313b0e371cSJiri Olsa if (type == term->type_val) 9323b0e371cSJiri Olsa return 0; 9333b0e371cSJiri Olsa 9343b0e371cSJiri Olsa if (err) { 9356c191289SIan Rogers parse_events_error__handle(err, term->err_val, 936448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 937448d732cSIan Rogers ? strdup("expected numeric value") 938448d732cSIan Rogers : strdup("expected string value"), 939448d732cSIan Rogers NULL); 9403b0e371cSJiri Olsa } 9413b0e371cSJiri Olsa return -EINVAL; 9423b0e371cSJiri Olsa } 9433b0e371cSJiri Olsa 94417cb5f84SWang Nan /* 94517cb5f84SWang Nan * Update according to parse-events.l 94617cb5f84SWang Nan */ 94717cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 94817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 94917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 95017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 95117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 95217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 95317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 95417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 95517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 95617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 95717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 95817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 95917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 96017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 961792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 9622fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 963626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 964626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 965dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 966064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 9671b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 968eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 9692b62b3a6SIan Rogers [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id", 97017cb5f84SWang Nan }; 97117cb5f84SWang Nan 9721669e509SWang Nan static bool config_term_shrinked; 9731669e509SWang Nan 9741669e509SWang Nan static bool 9751669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9761669e509SWang Nan { 977448d732cSIan Rogers char *err_str; 978448d732cSIan Rogers 9791669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9806c191289SIan Rogers parse_events_error__handle(err, -1, 981448d732cSIan Rogers strdup("Invalid term_type"), NULL); 9821669e509SWang Nan return false; 9831669e509SWang Nan } 9841669e509SWang Nan if (!config_term_shrinked) 9851669e509SWang Nan return true; 9861669e509SWang Nan 9871669e509SWang Nan switch (term_type) { 9881669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9891669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9901669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9911669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 9922b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 99306835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 994064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 9951669e509SWang Nan return true; 9961669e509SWang Nan default: 9971669e509SWang Nan if (!err) 9981669e509SWang Nan return false; 9991669e509SWang Nan 10001669e509SWang Nan /* term_type is validated so indexing is safe */ 1001448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 1002448d732cSIan Rogers config_term_names[term_type]) >= 0) 10036c191289SIan Rogers parse_events_error__handle(err, -1, err_str, NULL); 10041669e509SWang Nan return false; 10051669e509SWang Nan } 10061669e509SWang Nan } 10071669e509SWang Nan 10081669e509SWang Nan void parse_events__shrink_config_terms(void) 10091669e509SWang Nan { 10101669e509SWang Nan config_term_shrinked = true; 10111669e509SWang Nan } 10121669e509SWang Nan 10130b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 10143b0e371cSJiri Olsa struct parse_events_term *term, 10153b0e371cSJiri Olsa struct parse_events_error *err) 10168f707d84SJiri Olsa { 101716fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 101816fa7e82SJiri Olsa do { \ 10193b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 102016fa7e82SJiri Olsa return -EINVAL; \ 102116fa7e82SJiri Olsa } while (0) 102216fa7e82SJiri Olsa 102316fa7e82SJiri Olsa switch (term->type_term) { 10248f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 102516fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10268f707d84SJiri Olsa attr->config = term->val.num; 10278f707d84SJiri Olsa break; 10288f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 102916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10308f707d84SJiri Olsa attr->config1 = term->val.num; 10318f707d84SJiri Olsa break; 10328f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 103316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10348f707d84SJiri Olsa attr->config2 = term->val.num; 10358f707d84SJiri Olsa break; 10368f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 103716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10388f707d84SJiri Olsa break; 103909af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 104009af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 104109af2a55SNamhyung Kim break; 10428f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1043ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1044ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1045448d732cSIan Rogers parse_branch_str(term->val.str, 1046448d732cSIan Rogers &attr->branch_sample_type)) { 10476c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1048448d732cSIan Rogers strdup("invalid branch sample type"), 1049448d732cSIan Rogers NULL); 1050ac12f676SAndi Kleen return -EINVAL; 1051ac12f676SAndi Kleen } 10528f707d84SJiri Olsa break; 105332067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 105432067712SKan Liang CHECK_TYPE_VAL(NUM); 105532067712SKan Liang if (term->val.num > 1) { 10566c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1057448d732cSIan Rogers strdup("expected 0 or 1"), 1058448d732cSIan Rogers NULL); 105932067712SKan Liang return -EINVAL; 106032067712SKan Liang } 106132067712SKan Liang break; 1062d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1063d457c963SKan Liang CHECK_TYPE_VAL(STR); 1064d457c963SKan Liang break; 1065d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1066d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1067d457c963SKan Liang break; 1068374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1069374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1070374ce938SWang Nan break; 1071374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1072374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1073374ce938SWang Nan break; 1074626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1075626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1076626a6b78SWang Nan break; 1077626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1078626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1079626a6b78SWang Nan break; 10806b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10816b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10826b5fc39bSJiri Olsa break; 10832b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 10842b62b3a6SIan Rogers CHECK_TYPE_VAL(STR); 10852b62b3a6SIan Rogers break; 1086792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1087792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1088792d48b4SArnaldo Carvalho de Melo break; 10892fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 10902fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 10912fda5adaSArnaldo Carvalho de Melo break; 1092064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1093064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1094064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 10956c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1096448d732cSIan Rogers strdup("expected 0 or 1"), 1097448d732cSIan Rogers NULL); 1098064b4e82SJin Yao return -EINVAL; 1099064b4e82SJin Yao } 1100064b4e82SJin Yao break; 11011b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 11021b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 11031b992154SAdrian Hunter break; 1104eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1105eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 1106eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 11076c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1108eb7a52d4SAdrian Hunter strdup("too big"), 1109eb7a52d4SAdrian Hunter NULL); 1110eb7a52d4SAdrian Hunter return -EINVAL; 1111eb7a52d4SAdrian Hunter } 1112eb7a52d4SAdrian Hunter break; 11138f707d84SJiri Olsa default: 11146c191289SIan Rogers parse_events_error__handle(err, term->err_term, 1115448d732cSIan Rogers strdup("unknown term"), 1116448d732cSIan Rogers parse_events_formats_error_string(NULL)); 11178f707d84SJiri Olsa return -EINVAL; 11188f707d84SJiri Olsa } 111916fa7e82SJiri Olsa 11201669e509SWang Nan /* 11214d39c89fSIngo Molnar * Check term availability after basic checking so 11221669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 11231669e509SWang Nan * 11244d39c89fSIngo Molnar * If check availability at the entry of this function, 11251669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 11261669e509SWang Nan * if an invalid config term is provided for legacy events 11271669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 11281669e509SWang Nan */ 11291669e509SWang Nan if (!config_term_avail(term->type_term, err)) 11301669e509SWang Nan return -EINVAL; 11318f707d84SJiri Olsa return 0; 113216fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 11338f707d84SJiri Olsa } 11348f707d84SJiri Olsa 11350b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 11360b8891a8SHe Kuang struct parse_events_term *term, 11370b8891a8SHe Kuang struct parse_events_error *err) 11380b8891a8SHe Kuang { 1139dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1140dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 11410b8891a8SHe Kuang /* 11420b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 11430b8891a8SHe Kuang * at this point what type they need to have. 11440b8891a8SHe Kuang */ 11450b8891a8SHe Kuang return 0; 11460b8891a8SHe Kuang else 11470b8891a8SHe Kuang return config_term_common(attr, term, err); 11480b8891a8SHe Kuang } 11490b8891a8SHe Kuang 1150378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1151e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1152e637d177SHe Kuang struct parse_events_term *term, 1153e637d177SHe Kuang struct parse_events_error *err) 1154e637d177SHe Kuang { 1155e637d177SHe Kuang switch (term->type_term) { 1156e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1157e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1158374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1159374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1160792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 11612fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1162626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1163626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 11641b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1165eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1166e637d177SHe Kuang return config_term_common(attr, term, err); 1167e637d177SHe Kuang default: 1168e637d177SHe Kuang if (err) { 11696c191289SIan Rogers parse_events_error__handle(err, term->err_term, 1170448d732cSIan Rogers strdup("unknown term"), 1171448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1172e637d177SHe Kuang } 1173e637d177SHe Kuang return -EINVAL; 1174e637d177SHe Kuang } 1175e637d177SHe Kuang 1176e637d177SHe Kuang return 0; 1177e637d177SHe Kuang } 1178378ef0f5SIan Rogers #endif 1179e637d177SHe Kuang 11808f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 11813b0e371cSJiri Olsa struct list_head *head, 11820b8891a8SHe Kuang struct parse_events_error *err, 11830b8891a8SHe Kuang config_term_func_t config_term) 11848f707d84SJiri Olsa { 11856cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 11868f707d84SJiri Olsa 11878f707d84SJiri Olsa list_for_each_entry(term, head, list) 11883b0e371cSJiri Olsa if (config_term(attr, term, err)) 11898f707d84SJiri Olsa return -EINVAL; 11908f707d84SJiri Olsa 11918f707d84SJiri Olsa return 0; 11928f707d84SJiri Olsa } 11938f707d84SJiri Olsa 1194930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1195930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1196930a2e29SJiri Olsa { 119705e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 119835ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *__t; \ 1199930a2e29SJiri Olsa \ 1200930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1201930a2e29SJiri Olsa if (!__t) \ 1202930a2e29SJiri Olsa return -ENOMEM; \ 1203930a2e29SJiri Olsa \ 1204930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 120535ac0cadSArnaldo Carvalho de Melo __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 120605e54e23SIan Rogers __t->weak = __weak; \ 1207e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1208e884602bSLeo Yan 120905e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1210e884602bSLeo Yan do { \ 121105e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1212e884602bSLeo Yan __t->val.__name = __val; \ 1213e884602bSLeo Yan } while (0) 1214e884602bSLeo Yan 121505e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1216e884602bSLeo Yan do { \ 121705e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 12183220fb8dSLeo Yan __t->val.str = strdup(__val); \ 12193220fb8dSLeo Yan if (!__t->val.str) { \ 12203220fb8dSLeo Yan zfree(&__t); \ 12213220fb8dSLeo Yan return -ENOMEM; \ 12223220fb8dSLeo Yan } \ 12233220fb8dSLeo Yan __t->free_str = true; \ 1224930a2e29SJiri Olsa } while (0) 1225930a2e29SJiri Olsa 1226930a2e29SJiri Olsa struct parse_events_term *term; 1227930a2e29SJiri Olsa 1228930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1229930a2e29SJiri Olsa switch (term->type_term) { 1230ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 123105e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 123232067712SKan Liang break; 123309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 123405e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 123509af2a55SNamhyung Kim break; 123632067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 123705e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 123832067712SKan Liang break; 1239d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 124005e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1241d457c963SKan Liang break; 1242ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 124305e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1244ac12f676SAndi Kleen break; 1245d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1246e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 124705e54e23SIan Rogers term->val.num, term->weak); 1248d457c963SKan Liang break; 1249374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1250e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 125105e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1252374ce938SWang Nan break; 1253374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1254e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 125505e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1256374ce938SWang Nan break; 1257792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1258e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 125905e54e23SIan Rogers term->val.num, term->weak); 1260792d48b4SArnaldo Carvalho de Melo break; 12612fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1262e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 126305e54e23SIan Rogers term->val.num, term->weak); 12642fda5adaSArnaldo Carvalho de Melo break; 1265626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1266e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 126705e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1268626a6b78SWang Nan break; 1269626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1270e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 127105e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1272626a6b78SWang Nan break; 1273dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 127405e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1275dd60fba7SMathieu Poirier break; 1276064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1277e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 127805e54e23SIan Rogers term->val.num ? true : false, term->weak); 1279064b4e82SJin Yao break; 12801b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1281e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 128205e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 12831b992154SAdrian Hunter break; 1284eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1285e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 128605e54e23SIan Rogers term->val.num, term->weak); 1287eb7a52d4SAdrian Hunter break; 1288930a2e29SJiri Olsa default: 1289930a2e29SJiri Olsa break; 1290930a2e29SJiri Olsa } 1291930a2e29SJiri Olsa } 1292a1ac7de6SAdrian Hunter return 0; 1293a1ac7de6SAdrian Hunter } 1294a1ac7de6SAdrian Hunter 1295a1ac7de6SAdrian Hunter /* 129635ac0cadSArnaldo Carvalho de Melo * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1297a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1298a1ac7de6SAdrian Hunter */ 1299a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1300a1ac7de6SAdrian Hunter struct list_head *head_terms) 1301a1ac7de6SAdrian Hunter { 1302a1ac7de6SAdrian Hunter struct parse_events_term *term; 1303a1ac7de6SAdrian Hunter u64 bits = 0; 1304a1ac7de6SAdrian Hunter int type; 1305a1ac7de6SAdrian Hunter 1306a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1307a1ac7de6SAdrian Hunter switch (term->type_term) { 1308a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 1309a1ac7de6SAdrian Hunter type = perf_pmu__format_type(&pmu->format, term->config); 1310a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1311a1ac7de6SAdrian Hunter continue; 1312a1ac7de6SAdrian Hunter bits |= perf_pmu__format_bits(&pmu->format, term->config); 1313a1ac7de6SAdrian Hunter break; 1314a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1315a1ac7de6SAdrian Hunter bits = ~(u64)0; 1316a1ac7de6SAdrian Hunter break; 1317a1ac7de6SAdrian Hunter default: 1318a1ac7de6SAdrian Hunter break; 1319a1ac7de6SAdrian Hunter } 1320a1ac7de6SAdrian Hunter } 1321a1ac7de6SAdrian Hunter 1322a1ac7de6SAdrian Hunter if (bits) 132305e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1324a1ac7de6SAdrian Hunter 1325a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1326930a2e29SJiri Olsa return 0; 1327930a2e29SJiri Olsa } 1328930a2e29SJiri Olsa 1329e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 13308c619d6aSWang Nan const char *sys, const char *event, 1331272ed29aSJiri Olsa struct parse_events_error *err, 1332e637d177SHe Kuang struct list_head *head_config) 1333e637d177SHe Kuang { 1334378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1335e637d177SHe Kuang if (head_config) { 1336e637d177SHe Kuang struct perf_event_attr attr; 1337e637d177SHe Kuang 1338272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1339e637d177SHe Kuang config_term_tracepoint)) 1340e637d177SHe Kuang return -EINVAL; 1341e637d177SHe Kuang } 1342e637d177SHe Kuang 1343e637d177SHe Kuang if (strpbrk(sys, "*?")) 1344e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1345272ed29aSJiri Olsa err, head_config); 1346e637d177SHe Kuang else 1347e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1348272ed29aSJiri Olsa err, head_config); 1349378ef0f5SIan Rogers #else 1350378ef0f5SIan Rogers (void)list; 1351378ef0f5SIan Rogers (void)idx; 1352378ef0f5SIan Rogers (void)sys; 1353378ef0f5SIan Rogers (void)event; 1354378ef0f5SIan Rogers (void)head_config; 1355378ef0f5SIan Rogers parse_events_error__handle(err, 0, strdup("unsupported tracepoint"), 1356378ef0f5SIan Rogers strdup("libtraceevent is necessary for tracepoint support")); 1357378ef0f5SIan Rogers return -1; 1358378ef0f5SIan Rogers #endif 1359e637d177SHe Kuang } 1360e637d177SHe Kuang 13615d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 136287d650beSJiri Olsa struct list_head *list, 1363b527bab5SRobert Richter u32 type, u64 config, 13648f707d84SJiri Olsa struct list_head *head_config) 136574d5b588SJaswinder Singh Rajput { 136689812fc8SJiri Olsa struct perf_event_attr attr; 1367930a2e29SJiri Olsa LIST_HEAD(config_terms); 13682b62b3a6SIan Rogers const char *name, *metric_id; 13699cbfa2f6SJin Yao bool hybrid; 13709cbfa2f6SJin Yao int ret; 137174d5b588SJaswinder Singh Rajput 137289812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 137389812fc8SJiri Olsa attr.type = type; 137489812fc8SJiri Olsa attr.config = config; 13758f707d84SJiri Olsa 1376930a2e29SJiri Olsa if (head_config) { 13775d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 13780b8891a8SHe Kuang config_term_common)) 13798f707d84SJiri Olsa return -EINVAL; 13808f707d84SJiri Olsa 1381930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1382930a2e29SJiri Olsa return -ENOMEM; 1383930a2e29SJiri Olsa } 1384930a2e29SJiri Olsa 13852b62b3a6SIan Rogers name = get_config_name(head_config); 13862b62b3a6SIan Rogers metric_id = get_config_metric_id(head_config); 13879cbfa2f6SJin Yao ret = parse_events__add_numeric_hybrid(parse_state, list, &attr, 13882b62b3a6SIan Rogers name, metric_id, 13899cbfa2f6SJin Yao &config_terms, &hybrid); 13909cbfa2f6SJin Yao if (hybrid) 139199fc5941SAdrian Hunter goto out_free_terms; 13929cbfa2f6SJin Yao 13932b62b3a6SIan Rogers ret = add_event(list, &parse_state->idx, &attr, name, metric_id, 13942b62b3a6SIan Rogers &config_terms); 139599fc5941SAdrian Hunter out_free_terms: 139699fc5941SAdrian Hunter free_config_terms(&config_terms); 139799fc5941SAdrian Hunter return ret; 1398b908debdSIngo Molnar } 1399b908debdSIngo Molnar 1400f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1401f0fbb114SAndi Kleen struct list_head *list, 14028228e936SIan Rogers int tool_event) 1403f0fbb114SAndi Kleen { 1404f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1405f0fbb114SAndi Kleen } 1406f0fbb114SAndi Kleen 1407064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1408064b4e82SJin Yao { 140935ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *term; 1410064b4e82SJin Yao 1411064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 141235ac0cadSArnaldo Carvalho de Melo if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1413064b4e82SJin Yao return term->val.percore; 1414064b4e82SJin Yao } 1415064b4e82SJin Yao 1416064b4e82SJin Yao return false; 1417064b4e82SJin Yao } 1418064b4e82SJin Yao 14195e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state, 14205e4edd1fSJin Yao struct list_head *list, char *name, 14215e4edd1fSJin Yao struct list_head *head_config) 14225e4edd1fSJin Yao { 14235e4edd1fSJin Yao struct parse_events_term *term; 14245e4edd1fSJin Yao int ret = -1; 14255e4edd1fSJin Yao 14265e4edd1fSJin Yao if (parse_state->fake_pmu || !head_config || list_empty(head_config) || 14275e4edd1fSJin Yao !perf_pmu__is_hybrid(name)) { 14285e4edd1fSJin Yao return -1; 14295e4edd1fSJin Yao } 14305e4edd1fSJin Yao 14315e4edd1fSJin Yao /* 14325e4edd1fSJin Yao * More than one term in list. 14335e4edd1fSJin Yao */ 14345e4edd1fSJin Yao if (head_config->next && head_config->next->next != head_config) 14355e4edd1fSJin Yao return -1; 14365e4edd1fSJin Yao 14375e4edd1fSJin Yao term = list_first_entry(head_config, struct parse_events_term, list); 14385e4edd1fSJin Yao if (term && term->config && strcmp(term->config, "event")) { 14395e4edd1fSJin Yao ret = parse_events__with_hybrid_pmu(parse_state, term->config, 14405e4edd1fSJin Yao name, list); 14415e4edd1fSJin Yao } 14425e4edd1fSJin Yao 14435e4edd1fSJin Yao return ret; 14445e4edd1fSJin Yao } 14455e4edd1fSJin Yao 1446c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 144736adec85SJiri Olsa struct list_head *list, char *name, 14483cdc5c2cSKan Liang struct list_head *head_config, 1449e733f87eSIan Rogers bool auto_merge_stats) 14505f537a26SJiri Olsa { 14515f537a26SJiri Olsa struct perf_event_attr attr; 145246441bdcSMatt Fleming struct perf_pmu_info info; 14535f537a26SJiri Olsa struct perf_pmu *pmu; 145432dcd021SJiri Olsa struct evsel *evsel; 1455333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 1456930a2e29SJiri Olsa LIST_HEAD(config_terms); 14575f537a26SJiri Olsa 1458f034fc50SAdrian Hunter pmu = parse_state->fake_pmu ?: perf_pmu__find(name); 1459f034fc50SAdrian Hunter 1460f034fc50SAdrian Hunter if (verbose > 1 && !(pmu && pmu->selectable)) { 14614ac22b48SIan Rogers fprintf(stderr, "Attempting to add event pmu '%s' with '", 14624ac22b48SIan Rogers name); 14634ac22b48SIan Rogers if (head_config) { 14644ac22b48SIan Rogers struct parse_events_term *term; 14654ac22b48SIan Rogers 14664ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 14674ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 14684ac22b48SIan Rogers } 14694ac22b48SIan Rogers } 14704ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 14714ac22b48SIan Rogers } 14724ac22b48SIan Rogers 1473333b5665SAndi Kleen if (!pmu) { 1474448d732cSIan Rogers char *err_str; 1475448d732cSIan Rogers 1476448d732cSIan Rogers if (asprintf(&err_str, 1477333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1478448d732cSIan Rogers name) >= 0) 14796c191289SIan Rogers parse_events_error__handle(err, 0, err_str, NULL); 14805f537a26SJiri Olsa return -EINVAL; 1481333b5665SAndi Kleen } 14825f537a26SJiri Olsa 1483dc0a6202SAdrian Hunter if (pmu->default_config) { 1484dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1485dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1486dc0a6202SAdrian Hunter } else { 14875f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1488dc0a6202SAdrian Hunter } 14895f537a26SJiri Olsa 1490ad962273SAdrian Hunter if (!head_config) { 1491ad962273SAdrian Hunter attr.type = pmu->type; 14922b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, 14932b62b3a6SIan Rogers /*init_attr=*/true, /*name=*/NULL, 14942b62b3a6SIan Rogers /*metric_id=*/NULL, pmu, 14952b62b3a6SIan Rogers /*config_terms=*/NULL, auto_merge_stats, 14962b62b3a6SIan Rogers /*cpu_list=*/NULL); 14978c5421c0SAgustin Vega-Frias if (evsel) { 1498d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 14998c5421c0SAgustin Vega-Frias return 0; 15008c5421c0SAgustin Vega-Frias } else { 15018c5421c0SAgustin Vega-Frias return -ENOMEM; 15028c5421c0SAgustin Vega-Frias } 1503ad962273SAdrian Hunter } 1504ad962273SAdrian Hunter 1505387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) 1506a6146d50SZheng Yan return -EINVAL; 1507a6146d50SZheng Yan 15084ac22b48SIan Rogers if (verbose > 1) { 15094ac22b48SIan Rogers fprintf(stderr, "After aliases, add event pmu '%s' with '", 15104ac22b48SIan Rogers name); 15114ac22b48SIan Rogers if (head_config) { 15124ac22b48SIan Rogers struct parse_events_term *term; 15134ac22b48SIan Rogers 15144ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15154ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15164ac22b48SIan Rogers } 15174ac22b48SIan Rogers } 15184ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15194ac22b48SIan Rogers } 15204ac22b48SIan Rogers 15215f537a26SJiri Olsa /* 15225f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 15235f537a26SJiri Olsa * return value when called with fail == 0 ;) 15245f537a26SJiri Olsa */ 15255d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1526c056ba6aSJiri Olsa return -EINVAL; 15275f537a26SJiri Olsa 1528930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1529930a2e29SJiri Olsa return -ENOMEM; 1530930a2e29SJiri Olsa 1531a1ac7de6SAdrian Hunter /* 1532a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1533a1ac7de6SAdrian Hunter * changed by the user. 1534a1ac7de6SAdrian Hunter */ 1535a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1536a1ac7de6SAdrian Hunter return -ENOMEM; 1537a1ac7de6SAdrian Hunter 15385e4edd1fSJin Yao if (!parse_events__inside_hybrid_pmu(parse_state, list, name, 15395e4edd1fSJin Yao head_config)) { 15405e4edd1fSJin Yao return 0; 15415e4edd1fSJin Yao } 15425e4edd1fSJin Yao 1543387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 1544a7d212fcSAdrian Hunter free_config_terms(&config_terms); 15455f537a26SJiri Olsa return -EINVAL; 154638f2c422SIan Rogers } 15475f537a26SJiri Olsa 15482b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true, 15492b62b3a6SIan Rogers get_config_name(head_config), 15502b62b3a6SIan Rogers get_config_metric_id(head_config), pmu, 15512b62b3a6SIan Rogers &config_terms, auto_merge_stats, /*cpu_list=*/NULL); 155233321a06SIan Rogers if (!evsel) 155333321a06SIan Rogers return -ENOMEM; 155433321a06SIan Rogers 155512279429SJin Yao if (evsel->name) 155612279429SJin Yao evsel->use_config_name = true; 155712279429SJin Yao 155833321a06SIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 155933321a06SIan Rogers evsel->percore = config_term_percore(&evsel->config_terms); 156033321a06SIan Rogers 156133321a06SIan Rogers if (parse_state->fake_pmu) 156233321a06SIan Rogers return 0; 156333321a06SIan Rogers 1564b194c9cdSIan Rogers free((char *)evsel->unit); 1565b194c9cdSIan Rogers evsel->unit = strdup(info.unit); 156646441bdcSMatt Fleming evsel->scale = info.scale; 1567044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 15681d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 156933321a06SIan Rogers return 0; 15705f537a26SJiri Olsa } 15715f537a26SJiri Olsa 15725d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 1573fb081153SIan Rogers char *str, struct list_head *head, 1574fb081153SIan Rogers struct list_head **listp) 15752073ad33SAndi Kleen { 15762073ad33SAndi Kleen struct parse_events_term *term; 1577fb081153SIan Rogers struct list_head *list = NULL; 157891c9923aSZhengjun Xing struct list_head *orig_head = NULL; 15792073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 15802073ad33SAndi Kleen int ok = 0; 1581fb081153SIan Rogers char *config; 15822073ad33SAndi Kleen 15832073ad33SAndi Kleen *listp = NULL; 1584fb081153SIan Rogers 1585fb081153SIan Rogers if (!head) { 1586fb081153SIan Rogers head = malloc(sizeof(struct list_head)); 1587fb081153SIan Rogers if (!head) 1588fb081153SIan Rogers goto out_err; 1589fb081153SIan Rogers 1590fb081153SIan Rogers INIT_LIST_HEAD(head); 1591fb081153SIan Rogers } 1592fb081153SIan Rogers config = strdup(str); 1593fb081153SIan Rogers if (!config) 1594fb081153SIan Rogers goto out_err; 1595fb081153SIan Rogers 1596fb081153SIan Rogers if (parse_events_term__num(&term, 1597fb081153SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 1598fb081153SIan Rogers config, 1, false, &config, 1599fb081153SIan Rogers NULL) < 0) { 1600fb081153SIan Rogers free(config); 1601fb081153SIan Rogers goto out_err; 1602fb081153SIan Rogers } 1603fb081153SIan Rogers list_add_tail(&term->list, head); 1604fb081153SIan Rogers 16052073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 16062073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 16072073ad33SAndi Kleen if (!list) 1608fb081153SIan Rogers goto out_err; 1609fb081153SIan Rogers 16102073ad33SAndi Kleen INIT_LIST_HEAD(list); 1611fb081153SIan Rogers 16122073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 16132073ad33SAndi Kleen struct perf_pmu_alias *alias; 16142073ad33SAndi Kleen 16152073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 16162073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 161791c9923aSZhengjun Xing parse_events_copy_term_list(head, &orig_head); 1618c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 161991c9923aSZhengjun Xing pmu->name, orig_head, 1620e733f87eSIan Rogers /*auto_merge_stats=*/true)) { 1621d4953f7eSIan Rogers pr_debug("%s -> %s/%s/\n", str, 16222073ad33SAndi Kleen pmu->name, alias->str); 16232073ad33SAndi Kleen ok++; 16242073ad33SAndi Kleen } 162591c9923aSZhengjun Xing parse_events_terms__delete(orig_head); 1626fb081153SIan Rogers } 1627fb081153SIan Rogers } 1628fb081153SIan Rogers } 1629b4a7276cSJohn Garry 1630b4a7276cSJohn Garry if (parse_state->fake_pmu) { 1631b4a7276cSJohn Garry if (!parse_events_add_pmu(parse_state, list, str, head, 1632e733f87eSIan Rogers /*auto_merge_stats=*/true)) { 1633b4a7276cSJohn Garry pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str); 1634b4a7276cSJohn Garry ok++; 1635b4a7276cSJohn Garry } 1636b4a7276cSJohn Garry } 1637b4a7276cSJohn Garry 1638fb081153SIan Rogers out_err: 1639fb081153SIan Rogers if (ok) 1640fb081153SIan Rogers *listp = list; 1641fb081153SIan Rogers else 1642fb081153SIan Rogers free(list); 16432073ad33SAndi Kleen 16442073ad33SAndi Kleen parse_events_terms__delete(head); 1645fb081153SIan Rogers return ok ? 0 : -1; 16462073ad33SAndi Kleen } 16472073ad33SAndi Kleen 16486a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 16496a4bb04cSJiri Olsa char *event_mod) 165089efb029SJiri Olsa { 16516a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 16526a4bb04cSJiri Olsa } 16536a4bb04cSJiri Olsa 1654347c2f0aSIan Rogers void parse_events__set_leader(char *name, struct list_head *list) 16556a4bb04cSJiri Olsa { 165632dcd021SJiri Olsa struct evsel *leader; 16576a4bb04cSJiri Olsa 1658854f7363SWang Nan if (list_empty(list)) { 1659854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1660854f7363SWang Nan return; 1661854f7363SWang Nan } 1662854f7363SWang Nan 1663347c2f0aSIan Rogers leader = list_first_entry(list, struct evsel, core.node); 1664ecdcf630SIan Rogers __perf_evlist__set_leader(list, &leader->core); 16654bb311b2SIan Rogers leader->group_name = name; 166689efb029SJiri Olsa } 166789efb029SJiri Olsa 1668c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 16695d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 16705d7be90eSJiri Olsa struct list_head *list_all) 16715d7be90eSJiri Olsa { 16725d7be90eSJiri Olsa /* 16735d7be90eSJiri Olsa * Called for single event definition. Update the 167489efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 16755d7be90eSJiri Olsa * list, for next event definition. 16765d7be90eSJiri Olsa */ 16775d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1678b847cbdcSJiri Olsa free(list_event); 16795d7be90eSJiri Olsa } 16805d7be90eSJiri Olsa 1681f5b1135bSJiri Olsa struct event_modifier { 1682f5b1135bSJiri Olsa int eu; 1683f5b1135bSJiri Olsa int ek; 1684f5b1135bSJiri Olsa int eh; 1685f5b1135bSJiri Olsa int eH; 1686f5b1135bSJiri Olsa int eG; 1687a1e12da4SJiri Olsa int eI; 1688f5b1135bSJiri Olsa int precise; 16897f94af7aSJiri Olsa int precise_max; 1690f5b1135bSJiri Olsa int exclude_GH; 16913c176311SJiri Olsa int sample_read; 1692e9a7c414SMichael Ellerman int pinned; 16935a5dfe4bSAndi Kleen int weak; 16940997a266SAndi Kleen int exclusive; 169501bd8efcSSong Liu int bpf_counter; 1696f5b1135bSJiri Olsa }; 169761c45981SPaul Mackerras 1698f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 169932dcd021SJiri Olsa struct evsel *evsel) 1700f5b1135bSJiri Olsa { 17011fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 17021fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 17031fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 17041fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 17051fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 17061fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 17071fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 17087f94af7aSJiri Olsa int precise_max = 0; 17093c176311SJiri Olsa int sample_read = 0; 17101fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 17110997a266SAndi Kleen int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1712f5b1135bSJiri Olsa 1713f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1714f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 17155a5dfe4bSAndi Kleen int weak = 0; 171601bd8efcSSong Liu int bpf_counter = 0; 1717f5b1135bSJiri Olsa 1718f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1719ceb53fbfSIngo Molnar 172061c45981SPaul Mackerras while (*str) { 1721ab608344SPeter Zijlstra if (*str == 'u') { 1722ab608344SPeter Zijlstra if (!exclude) 1723ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 1724943b69acSJin Yao if (!exclude_GH && !perf_guest) 1725943b69acSJin Yao eG = 1; 172661c45981SPaul Mackerras eu = 0; 1727ab608344SPeter Zijlstra } else if (*str == 'k') { 1728ab608344SPeter Zijlstra if (!exclude) 1729ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 173061c45981SPaul Mackerras ek = 0; 1731ab608344SPeter Zijlstra } else if (*str == 'h') { 1732ab608344SPeter Zijlstra if (!exclude) 1733ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 173461c45981SPaul Mackerras eh = 0; 173599320cc8SJoerg Roedel } else if (*str == 'G') { 173699320cc8SJoerg Roedel if (!exclude_GH) 173799320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 173899320cc8SJoerg Roedel eG = 0; 173999320cc8SJoerg Roedel } else if (*str == 'H') { 174099320cc8SJoerg Roedel if (!exclude_GH) 174199320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 174299320cc8SJoerg Roedel eH = 0; 1743a1e12da4SJiri Olsa } else if (*str == 'I') { 1744a1e12da4SJiri Olsa eI = 1; 1745ab608344SPeter Zijlstra } else if (*str == 'p') { 1746ab608344SPeter Zijlstra precise++; 17471342798cSDavid Ahern /* use of precise requires exclude_guest */ 17481342798cSDavid Ahern if (!exclude_GH) 17491342798cSDavid Ahern eG = 1; 17507f94af7aSJiri Olsa } else if (*str == 'P') { 17517f94af7aSJiri Olsa precise_max = 1; 17523c176311SJiri Olsa } else if (*str == 'S') { 17533c176311SJiri Olsa sample_read = 1; 1754e9a7c414SMichael Ellerman } else if (*str == 'D') { 1755e9a7c414SMichael Ellerman pinned = 1; 17560997a266SAndi Kleen } else if (*str == 'e') { 17570997a266SAndi Kleen exclusive = 1; 17585a5dfe4bSAndi Kleen } else if (*str == 'W') { 17595a5dfe4bSAndi Kleen weak = 1; 176001bd8efcSSong Liu } else if (*str == 'b') { 176101bd8efcSSong Liu bpf_counter = 1; 1762ab608344SPeter Zijlstra } else 176361c45981SPaul Mackerras break; 1764ab608344SPeter Zijlstra 176561c45981SPaul Mackerras ++str; 176661c45981SPaul Mackerras } 176774d5b588SJaswinder Singh Rajput 176886470930SIngo Molnar /* 176989812fc8SJiri Olsa * precise ip: 177089812fc8SJiri Olsa * 177189812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 177289812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 177389812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 177489812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 177589812fc8SJiri Olsa * 177689812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 177786470930SIngo Molnar */ 177889812fc8SJiri Olsa if (precise > 3) 177989812fc8SJiri Olsa return -EINVAL; 178086470930SIngo Molnar 1781f5b1135bSJiri Olsa mod->eu = eu; 1782f5b1135bSJiri Olsa mod->ek = ek; 1783f5b1135bSJiri Olsa mod->eh = eh; 1784f5b1135bSJiri Olsa mod->eH = eH; 1785f5b1135bSJiri Olsa mod->eG = eG; 1786a1e12da4SJiri Olsa mod->eI = eI; 1787f5b1135bSJiri Olsa mod->precise = precise; 17887f94af7aSJiri Olsa mod->precise_max = precise_max; 1789f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 17903c176311SJiri Olsa mod->sample_read = sample_read; 1791e9a7c414SMichael Ellerman mod->pinned = pinned; 17925a5dfe4bSAndi Kleen mod->weak = weak; 179301bd8efcSSong Liu mod->bpf_counter = bpf_counter; 17940997a266SAndi Kleen mod->exclusive = exclusive; 1795e9a7c414SMichael Ellerman 1796f5b1135bSJiri Olsa return 0; 1797f5b1135bSJiri Olsa } 1798f5b1135bSJiri Olsa 1799534123f4SJiri Olsa /* 1800534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1801534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1802534123f4SJiri Olsa */ 1803534123f4SJiri Olsa static int check_modifier(char *str) 1804534123f4SJiri Olsa { 1805534123f4SJiri Olsa char *p = str; 1806534123f4SJiri Olsa 1807534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 180801bd8efcSSong Liu if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1809534123f4SJiri Olsa return -1; 1810534123f4SJiri Olsa 1811534123f4SJiri Olsa while (*p) { 1812534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1813534123f4SJiri Olsa return -1; 1814534123f4SJiri Olsa p++; 1815534123f4SJiri Olsa } 1816534123f4SJiri Olsa 1817534123f4SJiri Olsa return 0; 1818534123f4SJiri Olsa } 1819534123f4SJiri Olsa 1820f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1821f5b1135bSJiri Olsa { 182232dcd021SJiri Olsa struct evsel *evsel; 1823f5b1135bSJiri Olsa struct event_modifier mod; 1824f5b1135bSJiri Olsa 1825f5b1135bSJiri Olsa if (str == NULL) 1826f5b1135bSJiri Olsa return 0; 1827f5b1135bSJiri Olsa 1828534123f4SJiri Olsa if (check_modifier(str)) 1829534123f4SJiri Olsa return -EINVAL; 1830534123f4SJiri Olsa 1831f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1832f5b1135bSJiri Olsa return -EINVAL; 1833f5b1135bSJiri Olsa 1834e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1835f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1836f5b1135bSJiri Olsa return -EINVAL; 1837f5b1135bSJiri Olsa 18381fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 18391fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 18401fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 18411fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 18421fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 18431fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 18441fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 1845f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 18463c176311SJiri Olsa evsel->sample_read = mod.sample_read; 18477f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 18485a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 184901bd8efcSSong Liu evsel->bpf_counter = mod.bpf_counter; 1850e9a7c414SMichael Ellerman 18510997a266SAndi Kleen if (evsel__is_group_leader(evsel)) { 18521fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 18530997a266SAndi Kleen evsel->core.attr.exclusive = mod.exclusive; 18540997a266SAndi Kleen } 1855ceb53fbfSIngo Molnar } 185686470930SIngo Molnar 185789812fc8SJiri Olsa return 0; 185886470930SIngo Molnar } 185986470930SIngo Molnar 18608e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name) 1861ac2ba9f3SRobert Richter { 186232dcd021SJiri Olsa struct evsel *evsel; 1863ac2ba9f3SRobert Richter 1864e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1865ac2ba9f3SRobert Richter if (!evsel->name) 1866ac2ba9f3SRobert Richter evsel->name = strdup(name); 1867ac2ba9f3SRobert Richter } 1868ac2ba9f3SRobert Richter 1869ac2ba9f3SRobert Richter return 0; 1870ac2ba9f3SRobert Richter } 1871ac2ba9f3SRobert Richter 1872dcb4e102SKan Liang static int 1873dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1874dcb4e102SKan Liang { 1875dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1876dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1877dcb4e102SKan Liang 1878e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1879dcb4e102SKan Liang } 1880dcb4e102SKan Liang 1881dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1882dcb4e102SKan Liang { 1883dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1884dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1885dcb4e102SKan Liang int i; 1886dcb4e102SKan Liang 1887dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1888dcb4e102SKan Liang p = perf_pmu_events_list + i; 1889360e071bSTaeung Song zfree(&p->symbol); 1890dcb4e102SKan Liang } 1891506fde11STaeung Song zfree(&perf_pmu_events_list); 1892dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1893dcb4e102SKan Liang } 1894dcb4e102SKan Liang } 1895dcb4e102SKan Liang 1896dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1897dcb4e102SKan Liang do { \ 1898dcb4e102SKan Liang p->symbol = str; \ 1899dcb4e102SKan Liang if (!p->symbol) \ 1900dcb4e102SKan Liang goto err; \ 1901dcb4e102SKan Liang p->type = stype; \ 1902dcb4e102SKan Liang } while (0) 1903dcb4e102SKan Liang 1904dcb4e102SKan Liang /* 1905dcb4e102SKan Liang * Read the pmu events list from sysfs 1906dcb4e102SKan Liang * Save it into perf_pmu_events_list 1907dcb4e102SKan Liang */ 1908dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1909dcb4e102SKan Liang { 1910dcb4e102SKan Liang 1911dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1912dcb4e102SKan Liang struct perf_pmu_alias *alias; 1913dcb4e102SKan Liang int len = 0; 1914dcb4e102SKan Liang 1915231bb2aaSAndi Kleen pmu = NULL; 1916231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1917dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1918864bc8c9SJohn Garry char *tmp = strchr(alias->name, '-'); 1919864bc8c9SJohn Garry 1920864bc8c9SJohn Garry if (tmp) { 1921864bc8c9SJohn Garry char *tmp2 = NULL; 1922864bc8c9SJohn Garry 1923864bc8c9SJohn Garry tmp2 = strchr(tmp + 1, '-'); 1924dcb4e102SKan Liang len++; 1925864bc8c9SJohn Garry if (tmp2) 1926864bc8c9SJohn Garry len++; 1927864bc8c9SJohn Garry } 1928864bc8c9SJohn Garry 1929dcb4e102SKan Liang len++; 1930dcb4e102SKan Liang } 1931231bb2aaSAndi Kleen } 1932231bb2aaSAndi Kleen 1933231bb2aaSAndi Kleen if (len == 0) { 1934231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1935231bb2aaSAndi Kleen return; 1936231bb2aaSAndi Kleen } 1937dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1938dcb4e102SKan Liang if (!perf_pmu_events_list) 1939dcb4e102SKan Liang return; 1940dcb4e102SKan Liang perf_pmu_events_list_num = len; 1941dcb4e102SKan Liang 1942dcb4e102SKan Liang len = 0; 1943231bb2aaSAndi Kleen pmu = NULL; 1944231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1945dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1946dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1947dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1948864bc8c9SJohn Garry char *tmp2 = NULL; 1949dcb4e102SKan Liang 1950864bc8c9SJohn Garry if (tmp) 1951864bc8c9SJohn Garry tmp2 = strchr(tmp + 1, '-'); 1952864bc8c9SJohn Garry if (tmp2) { 1953864bc8c9SJohn Garry SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1954864bc8c9SJohn Garry PMU_EVENT_SYMBOL_PREFIX); 1955864bc8c9SJohn Garry p++; 1956864bc8c9SJohn Garry tmp++; 1957864bc8c9SJohn Garry SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX); 1958864bc8c9SJohn Garry p++; 1959864bc8c9SJohn Garry SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2); 1960864bc8c9SJohn Garry len += 3; 1961864bc8c9SJohn Garry } else if (tmp) { 1962dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1963dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1964dcb4e102SKan Liang p++; 1965dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1966dcb4e102SKan Liang len += 2; 1967dcb4e102SKan Liang } else { 1968dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1969dcb4e102SKan Liang len++; 1970dcb4e102SKan Liang } 1971dcb4e102SKan Liang } 1972231bb2aaSAndi Kleen } 1973dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1974dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1975dcb4e102SKan Liang 1976dcb4e102SKan Liang return; 1977dcb4e102SKan Liang err: 1978dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1979dcb4e102SKan Liang } 1980dcb4e102SKan Liang 19814929e95aSJiri Olsa /* 19824929e95aSJiri Olsa * This function injects special term in 19834929e95aSJiri Olsa * perf_pmu_events_list so the test code 19844929e95aSJiri Olsa * can check on this functionality. 19854929e95aSJiri Olsa */ 19864929e95aSJiri Olsa int perf_pmu__test_parse_init(void) 19874929e95aSJiri Olsa { 1988b4a7276cSJohn Garry struct perf_pmu_event_symbol *list, *tmp, symbols[] = { 1989b4a7276cSJohn Garry {(char *)"read", PMU_EVENT_SYMBOL}, 1990b4a7276cSJohn Garry {(char *)"event", PMU_EVENT_SYMBOL_PREFIX}, 1991b4a7276cSJohn Garry {(char *)"two", PMU_EVENT_SYMBOL_SUFFIX}, 1992b4a7276cSJohn Garry {(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX}, 1993b4a7276cSJohn Garry {(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2}, 1994b4a7276cSJohn Garry }; 1995b4a7276cSJohn Garry unsigned long i, j; 19964929e95aSJiri Olsa 1997b4a7276cSJohn Garry tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols)); 19984929e95aSJiri Olsa if (!list) 19994929e95aSJiri Olsa return -ENOMEM; 20004929e95aSJiri Olsa 2001b4a7276cSJohn Garry for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) { 2002b4a7276cSJohn Garry tmp->type = symbols[i].type; 2003b4a7276cSJohn Garry tmp->symbol = strdup(symbols[i].symbol); 2004a7a72631SWeiguo Li if (!tmp->symbol) 2005b4a7276cSJohn Garry goto err_free; 20064929e95aSJiri Olsa } 20074929e95aSJiri Olsa 20084929e95aSJiri Olsa perf_pmu_events_list = list; 2009b4a7276cSJohn Garry perf_pmu_events_list_num = ARRAY_SIZE(symbols); 2010b4a7276cSJohn Garry 2011b4a7276cSJohn Garry qsort(perf_pmu_events_list, ARRAY_SIZE(symbols), 2012b4a7276cSJohn Garry sizeof(struct perf_pmu_event_symbol), comp_pmu); 20134929e95aSJiri Olsa return 0; 2014b4a7276cSJohn Garry 2015b4a7276cSJohn Garry err_free: 2016b4a7276cSJohn Garry for (j = 0, tmp = list; j < i; j++, tmp++) 2017b4a7276cSJohn Garry free(tmp->symbol); 2018b4a7276cSJohn Garry free(list); 2019b4a7276cSJohn Garry return -ENOMEM; 20204929e95aSJiri Olsa } 20214929e95aSJiri Olsa 2022dcb4e102SKan Liang enum perf_pmu_event_symbol_type 2023dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 2024dcb4e102SKan Liang { 2025dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 2026dcb4e102SKan Liang 2027dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 2028dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 2029dcb4e102SKan Liang perf_pmu__parse_init(); 2030dcb4e102SKan Liang /* 2031dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 2032dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 2033dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 2034dcb4e102SKan Liang */ 2035dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 2036dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 2037dcb4e102SKan Liang 2038dcb4e102SKan Liang p.symbol = strdup(name); 2039dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 2040dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 2041dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2042360e071bSTaeung Song zfree(&p.symbol); 2043dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 2044dcb4e102SKan Liang } 2045dcb4e102SKan Liang 20461244a327SJiri Olsa static int parse_events__scanner(const char *str, 20471244a327SJiri Olsa struct parse_events_state *parse_state) 2048ac20de6fSZheng Yan { 2049ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 2050ac20de6fSZheng Yan void *scanner; 2051ac20de6fSZheng Yan int ret; 2052ac20de6fSZheng Yan 20531244a327SJiri Olsa ret = parse_events_lex_init_extra(parse_state, &scanner); 2054ac20de6fSZheng Yan if (ret) 2055ac20de6fSZheng Yan return ret; 2056ac20de6fSZheng Yan 2057ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 2058ac20de6fSZheng Yan 2059ac20de6fSZheng Yan #ifdef PARSER_DEBUG 2060ac20de6fSZheng Yan parse_events_debug = 1; 2061ae762641SJiri Olsa parse_events_set_debug(1, scanner); 2062ac20de6fSZheng Yan #endif 20635d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 2064ac20de6fSZheng Yan 2065ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 2066ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 2067ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 2068ac20de6fSZheng Yan return ret; 2069ac20de6fSZheng Yan } 2070ac20de6fSZheng Yan 207190e2b22dSJiri Olsa /* 207290e2b22dSJiri Olsa * parse event config string, return a list of event terms. 207390e2b22dSJiri Olsa */ 207490e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 207590e2b22dSJiri Olsa { 20765d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 207790e2b22dSJiri Olsa .terms = NULL, 20781244a327SJiri Olsa .stoken = PE_START_TERMS, 207990e2b22dSJiri Olsa }; 208090e2b22dSJiri Olsa int ret; 208190e2b22dSJiri Olsa 20821244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 20834929e95aSJiri Olsa perf_pmu__parse_cleanup(); 20844929e95aSJiri Olsa 208590e2b22dSJiri Olsa if (!ret) { 20865d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 20875d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 208890e2b22dSJiri Olsa return 0; 208990e2b22dSJiri Olsa } 209090e2b22dSJiri Olsa 20915d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 209290e2b22dSJiri Olsa return ret; 209390e2b22dSJiri Olsa } 209490e2b22dSJiri Olsa 20955e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 20965e4edd1fSJin Yao const char *str, char *pmu_name, 20975e4edd1fSJin Yao struct list_head *list) 20985e4edd1fSJin Yao { 20995e4edd1fSJin Yao struct parse_events_state ps = { 21005e4edd1fSJin Yao .list = LIST_HEAD_INIT(ps.list), 21015e4edd1fSJin Yao .stoken = PE_START_EVENTS, 21025e4edd1fSJin Yao .hybrid_pmu_name = pmu_name, 21035e4edd1fSJin Yao .idx = parse_state->idx, 21045e4edd1fSJin Yao }; 21055e4edd1fSJin Yao int ret; 21065e4edd1fSJin Yao 21075e4edd1fSJin Yao ret = parse_events__scanner(str, &ps); 21085e4edd1fSJin Yao perf_pmu__parse_cleanup(); 21095e4edd1fSJin Yao 21105e4edd1fSJin Yao if (!ret) { 21115e4edd1fSJin Yao if (!list_empty(&ps.list)) { 21125e4edd1fSJin Yao list_splice(&ps.list, list); 21135e4edd1fSJin Yao parse_state->idx = ps.idx; 21145e4edd1fSJin Yao return 0; 21155e4edd1fSJin Yao } else 21165e4edd1fSJin Yao return -1; 21175e4edd1fSJin Yao } 21185e4edd1fSJin Yao 21195e4edd1fSJin Yao return ret; 21205e4edd1fSJin Yao } 21215e4edd1fSJin Yao 2122347c2f0aSIan Rogers __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs) 2123347c2f0aSIan Rogers { 2124347c2f0aSIan Rogers /* Order by insertion index. */ 2125347c2f0aSIan Rogers return lhs->core.idx - rhs->core.idx; 2126347c2f0aSIan Rogers } 2127347c2f0aSIan Rogers 2128347c2f0aSIan Rogers static int evlist__cmp(void *state, const struct list_head *l, const struct list_head *r) 2129347c2f0aSIan Rogers { 2130347c2f0aSIan Rogers const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node); 2131347c2f0aSIan Rogers const struct evsel *lhs = container_of(lhs_core, struct evsel, core); 2132347c2f0aSIan Rogers const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node); 2133347c2f0aSIan Rogers const struct evsel *rhs = container_of(rhs_core, struct evsel, core); 2134347c2f0aSIan Rogers int *leader_idx = state; 2135347c2f0aSIan Rogers int lhs_leader_idx = *leader_idx, rhs_leader_idx = *leader_idx, ret; 2136347c2f0aSIan Rogers const char *lhs_pmu_name, *rhs_pmu_name; 2137347c2f0aSIan Rogers 2138347c2f0aSIan Rogers /* 2139347c2f0aSIan Rogers * First sort by grouping/leader. Read the leader idx only if the evsel 2140347c2f0aSIan Rogers * is part of a group, as -1 indicates no group. 2141347c2f0aSIan Rogers */ 2142347c2f0aSIan Rogers if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) 2143347c2f0aSIan Rogers lhs_leader_idx = lhs_core->leader->idx; 2144347c2f0aSIan Rogers if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) 2145347c2f0aSIan Rogers rhs_leader_idx = rhs_core->leader->idx; 2146347c2f0aSIan Rogers 2147347c2f0aSIan Rogers if (lhs_leader_idx != rhs_leader_idx) 2148347c2f0aSIan Rogers return lhs_leader_idx - rhs_leader_idx; 2149347c2f0aSIan Rogers 2150347c2f0aSIan Rogers /* Group by PMU. Groups can't span PMUs. */ 2151347c2f0aSIan Rogers lhs_pmu_name = evsel__group_pmu_name(lhs); 2152347c2f0aSIan Rogers rhs_pmu_name = evsel__group_pmu_name(rhs); 2153347c2f0aSIan Rogers ret = strcmp(lhs_pmu_name, rhs_pmu_name); 2154347c2f0aSIan Rogers if (ret) 2155347c2f0aSIan Rogers return ret; 2156347c2f0aSIan Rogers 2157347c2f0aSIan Rogers /* Architecture specific sorting. */ 2158347c2f0aSIan Rogers return arch_evlist__cmp(lhs, rhs); 2159347c2f0aSIan Rogers } 2160347c2f0aSIan Rogers 2161a4c7d7c5SIan Rogers static bool parse_events__sort_events_and_fix_groups(struct list_head *list) 2162347c2f0aSIan Rogers { 2163a4c7d7c5SIan Rogers int idx = 0, unsorted_idx = -1; 2164347c2f0aSIan Rogers struct evsel *pos, *cur_leader = NULL; 2165347c2f0aSIan Rogers struct perf_evsel *cur_leaders_grp = NULL; 2166a4c7d7c5SIan Rogers bool idx_changed = false; 2167a4c7d7c5SIan Rogers int orig_num_leaders = 0, num_leaders = 0; 2168347c2f0aSIan Rogers 2169347c2f0aSIan Rogers /* 2170347c2f0aSIan Rogers * Compute index to insert ungrouped events at. Place them where the 2171347c2f0aSIan Rogers * first ungrouped event appears. 2172347c2f0aSIan Rogers */ 2173347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2174347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 2175347c2f0aSIan Rogers 2176a4c7d7c5SIan Rogers if (pos == pos_leader) 2177a4c7d7c5SIan Rogers orig_num_leaders++; 2178347c2f0aSIan Rogers 2179a4c7d7c5SIan Rogers /* 2180a4c7d7c5SIan Rogers * Ensure indexes are sequential, in particular for multiple 2181a4c7d7c5SIan Rogers * event lists being merged. The indexes are used to detect when 2182a4c7d7c5SIan Rogers * the user order is modified. 2183a4c7d7c5SIan Rogers */ 2184a4c7d7c5SIan Rogers pos->core.idx = idx++; 2185a4c7d7c5SIan Rogers 2186a4c7d7c5SIan Rogers if (unsorted_idx == -1 && pos == pos_leader && pos->core.nr_members < 2) 2187a4c7d7c5SIan Rogers unsorted_idx = pos->core.idx; 2188347c2f0aSIan Rogers } 2189347c2f0aSIan Rogers 2190347c2f0aSIan Rogers /* Sort events. */ 2191a4c7d7c5SIan Rogers list_sort(&unsorted_idx, list, evlist__cmp); 2192347c2f0aSIan Rogers 2193347c2f0aSIan Rogers /* 2194347c2f0aSIan Rogers * Recompute groups, splitting for PMUs and adding groups for events 2195347c2f0aSIan Rogers * that require them. 2196347c2f0aSIan Rogers */ 2197347c2f0aSIan Rogers idx = 0; 2198347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2199347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 2200347c2f0aSIan Rogers const char *pos_pmu_name = evsel__group_pmu_name(pos); 2201347c2f0aSIan Rogers const char *cur_leader_pmu_name, *pos_leader_pmu_name; 2202347c2f0aSIan Rogers bool force_grouped = arch_evsel__must_be_in_group(pos); 2203347c2f0aSIan Rogers 2204347c2f0aSIan Rogers /* Reset index and nr_members. */ 2205a4c7d7c5SIan Rogers if (pos->core.idx != idx) 2206a4c7d7c5SIan Rogers idx_changed = true; 2207347c2f0aSIan Rogers pos->core.idx = idx++; 2208347c2f0aSIan Rogers pos->core.nr_members = 0; 2209347c2f0aSIan Rogers 2210347c2f0aSIan Rogers /* 2211347c2f0aSIan Rogers * Set the group leader respecting the given groupings and that 2212347c2f0aSIan Rogers * groups can't span PMUs. 2213347c2f0aSIan Rogers */ 2214347c2f0aSIan Rogers if (!cur_leader) 2215347c2f0aSIan Rogers cur_leader = pos; 2216347c2f0aSIan Rogers 2217347c2f0aSIan Rogers cur_leader_pmu_name = evsel__group_pmu_name(cur_leader); 2218347c2f0aSIan Rogers if ((cur_leaders_grp != pos->core.leader && !force_grouped) || 2219347c2f0aSIan Rogers strcmp(cur_leader_pmu_name, pos_pmu_name)) { 2220347c2f0aSIan Rogers /* Event is for a different group/PMU than last. */ 2221347c2f0aSIan Rogers cur_leader = pos; 2222347c2f0aSIan Rogers /* 2223347c2f0aSIan Rogers * Remember the leader's group before it is overwritten, 2224347c2f0aSIan Rogers * so that later events match as being in the same 2225347c2f0aSIan Rogers * group. 2226347c2f0aSIan Rogers */ 2227347c2f0aSIan Rogers cur_leaders_grp = pos->core.leader; 2228347c2f0aSIan Rogers } 2229347c2f0aSIan Rogers pos_leader_pmu_name = evsel__group_pmu_name(pos_leader); 2230347c2f0aSIan Rogers if (strcmp(pos_leader_pmu_name, pos_pmu_name) || force_grouped) { 2231347c2f0aSIan Rogers /* 2232347c2f0aSIan Rogers * Event's PMU differs from its leader's. Groups can't 2233347c2f0aSIan Rogers * span PMUs, so update leader from the group/PMU 2234347c2f0aSIan Rogers * tracker. 2235347c2f0aSIan Rogers */ 2236347c2f0aSIan Rogers evsel__set_leader(pos, cur_leader); 2237347c2f0aSIan Rogers } 2238347c2f0aSIan Rogers } 2239347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2240a4c7d7c5SIan Rogers struct evsel *pos_leader = evsel__leader(pos); 2241a4c7d7c5SIan Rogers 2242a4c7d7c5SIan Rogers if (pos == pos_leader) 2243a4c7d7c5SIan Rogers num_leaders++; 2244a4c7d7c5SIan Rogers pos_leader->core.nr_members++; 2245347c2f0aSIan Rogers } 2246a4c7d7c5SIan Rogers return idx_changed || num_leaders != orig_num_leaders; 2247347c2f0aSIan Rogers } 2248347c2f0aSIan Rogers 22493bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str, 2250a4c7d7c5SIan Rogers struct parse_events_error *err, struct perf_pmu *fake_pmu, 2251a4c7d7c5SIan Rogers bool warn_if_reordered) 225286470930SIngo Molnar { 22535d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 22545d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 22556484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 2256b39b8393SJiri Olsa .error = err, 22577630b3e2SWang Nan .evlist = evlist, 22581244a327SJiri Olsa .stoken = PE_START_EVENTS, 22593bf91aa5SArnaldo Carvalho de Melo .fake_pmu = fake_pmu, 226046010ab2SJiri Olsa }; 226146010ab2SJiri Olsa int ret; 226286470930SIngo Molnar 22631244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 2264dcb4e102SKan Liang perf_pmu__parse_cleanup(); 226515bfd2ccSWang Nan 22668e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2267c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2268854f7363SWang Nan return -1; 2269854f7363SWang Nan } 2270854f7363SWang Nan 2271a4c7d7c5SIan Rogers if (parse_events__sort_events_and_fix_groups(&parse_state.list) && 2272a4c7d7c5SIan Rogers warn_if_reordered && !parse_state.wild_card_pmus) 2273a4c7d7c5SIan Rogers pr_warning("WARNING: events were regrouped to match PMUs\n"); 2274347c2f0aSIan Rogers 22758e8714c3SIan Rogers /* 22768e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 22778e8714c3SIan Rogers */ 2278e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &parse_state.list); 22798e8714c3SIan Rogers 22808e8714c3SIan Rogers if (!ret) { 22818e8714c3SIan Rogers struct evsel *last; 22828e8714c3SIan Rogers 2283515dbe48SJiri Olsa last = evlist__last(evlist); 228415bfd2ccSWang Nan last->cmdline_group_boundary = true; 228515bfd2ccSWang Nan 228686470930SIngo Molnar return 0; 228786470930SIngo Molnar } 228886470930SIngo Molnar 22895d7be90eSJiri Olsa /* 22905d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2291c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 22925d7be90eSJiri Olsa * need to bother. 22935d7be90eSJiri Olsa */ 229489812fc8SJiri Olsa return ret; 229589812fc8SJiri Olsa } 229689812fc8SJiri Olsa 2297806731a9SAdrian Hunter int parse_event(struct evlist *evlist, const char *str) 2298806731a9SAdrian Hunter { 2299806731a9SAdrian Hunter struct parse_events_error err; 2300806731a9SAdrian Hunter int ret; 2301806731a9SAdrian Hunter 2302806731a9SAdrian Hunter parse_events_error__init(&err); 2303806731a9SAdrian Hunter ret = parse_events(evlist, str, &err); 2304806731a9SAdrian Hunter parse_events_error__exit(&err); 2305806731a9SAdrian Hunter return ret; 2306806731a9SAdrian Hunter } 2307806731a9SAdrian Hunter 230807eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err) 230907eafd4eSIan Rogers { 231007eafd4eSIan Rogers bzero(err, sizeof(*err)); 231107eafd4eSIan Rogers } 231207eafd4eSIan Rogers 231307eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err) 231407eafd4eSIan Rogers { 231507eafd4eSIan Rogers zfree(&err->str); 231607eafd4eSIan Rogers zfree(&err->help); 231707eafd4eSIan Rogers zfree(&err->first_str); 231807eafd4eSIan Rogers zfree(&err->first_help); 231907eafd4eSIan Rogers } 232007eafd4eSIan Rogers 23216c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx, 23226c191289SIan Rogers char *str, char *help) 23236c191289SIan Rogers { 2324806731a9SAdrian Hunter if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n")) 23252e828582SAdrian Hunter goto out_free; 23266c191289SIan Rogers switch (err->num_errors) { 23276c191289SIan Rogers case 0: 23286c191289SIan Rogers err->idx = idx; 23296c191289SIan Rogers err->str = str; 23306c191289SIan Rogers err->help = help; 23316c191289SIan Rogers break; 23326c191289SIan Rogers case 1: 23336c191289SIan Rogers err->first_idx = err->idx; 23346c191289SIan Rogers err->idx = idx; 23356c191289SIan Rogers err->first_str = err->str; 23366c191289SIan Rogers err->str = str; 23376c191289SIan Rogers err->first_help = err->help; 23386c191289SIan Rogers err->help = help; 23396c191289SIan Rogers break; 23406c191289SIan Rogers default: 23416c191289SIan Rogers pr_debug("Multiple errors dropping message: %s (%s)\n", 23426c191289SIan Rogers err->str, err->help); 23436c191289SIan Rogers free(err->str); 23446c191289SIan Rogers err->str = str; 23456c191289SIan Rogers free(err->help); 23466c191289SIan Rogers err->help = help; 23476c191289SIan Rogers break; 23486c191289SIan Rogers } 23496c191289SIan Rogers err->num_errors++; 23502e828582SAdrian Hunter return; 23512e828582SAdrian Hunter 23522e828582SAdrian Hunter out_free: 23532e828582SAdrian Hunter free(str); 23542e828582SAdrian Hunter free(help); 23556c191289SIan Rogers } 23566c191289SIan Rogers 2357b39b8393SJiri Olsa #define MAX_WIDTH 1000 2358b39b8393SJiri Olsa static int get_term_width(void) 2359b39b8393SJiri Olsa { 2360b39b8393SJiri Olsa struct winsize ws; 2361b39b8393SJiri Olsa 2362b39b8393SJiri Olsa get_term_dimensions(&ws); 2363b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2364b39b8393SJiri Olsa } 2365b39b8393SJiri Olsa 23666c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str, 2367a910e466SIan Rogers const char *err_help, const char *event) 2368b39b8393SJiri Olsa { 2369b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2370b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2371b39b8393SJiri Olsa char *buf = (char *) event; 2372b39b8393SJiri Olsa int idx = 0; 2373a910e466SIan Rogers if (err_str) { 2374b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2375b39b8393SJiri Olsa int width = get_term_width() - 2; 2376b39b8393SJiri Olsa int len_event = strlen(event); 2377b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2378b39b8393SJiri Olsa 2379b39b8393SJiri Olsa /* 2380b39b8393SJiri Olsa * Maximum error index indent, we will cut 2381b39b8393SJiri Olsa * the event string if it's bigger. 2382b39b8393SJiri Olsa */ 2383141b2d31SAdrian Hunter int max_err_idx = 13; 2384b39b8393SJiri Olsa 2385b39b8393SJiri Olsa /* 2386b39b8393SJiri Olsa * Let's be specific with the message when 2387b39b8393SJiri Olsa * we have the precise error. 2388b39b8393SJiri Olsa */ 2389b39b8393SJiri Olsa str = "event syntax error: "; 2390b39b8393SJiri Olsa len_str = strlen(str); 2391b39b8393SJiri Olsa max_len = width - len_str; 2392b39b8393SJiri Olsa 2393b39b8393SJiri Olsa buf = _buf; 2394b39b8393SJiri Olsa 2395bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2396a910e466SIan Rogers if (err_idx > max_err_idx) 2397a910e466SIan Rogers cut = err_idx - max_err_idx; 2398b39b8393SJiri Olsa 2399b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2400b39b8393SJiri Olsa 2401b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2402b39b8393SJiri Olsa if (cut) 2403b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2404b39b8393SJiri Olsa 2405b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2406b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2407b39b8393SJiri Olsa buf[max_len] = 0; 2408b39b8393SJiri Olsa } 2409b39b8393SJiri Olsa 2410a910e466SIan Rogers idx = len_str + err_idx - cut; 2411b39b8393SJiri Olsa } 2412b39b8393SJiri Olsa 2413b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2414b39b8393SJiri Olsa if (idx) { 2415a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2416a910e466SIan Rogers if (err_help) 2417a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2418a910e466SIan Rogers } 2419a910e466SIan Rogers } 2420a910e466SIan Rogers 24216c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err, 2422a910e466SIan Rogers const char *event) 2423a910e466SIan Rogers { 2424a910e466SIan Rogers if (!err->num_errors) 2425a910e466SIan Rogers return; 2426a910e466SIan Rogers 24276c191289SIan Rogers __parse_events_error__print(err->idx, err->str, err->help, event); 2428a910e466SIan Rogers 2429a910e466SIan Rogers if (err->num_errors > 1) { 2430a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 24316c191289SIan Rogers __parse_events_error__print(err->first_idx, err->first_str, 2432a910e466SIan Rogers err->first_help, event); 2433b39b8393SJiri Olsa } 2434b39b8393SJiri Olsa } 2435b39b8393SJiri Olsa 2436b39b8393SJiri Olsa #undef MAX_WIDTH 2437b39b8393SJiri Olsa 2438f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 24391d037ca1SIrina Tirdea int unset __maybe_unused) 2440f120f9d5SJiri Olsa { 244163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2442a910e466SIan Rogers struct parse_events_error err; 2443a910e466SIan Rogers int ret; 2444a910e466SIan Rogers 244507eafd4eSIan Rogers parse_events_error__init(&err); 2446a910e466SIan Rogers ret = parse_events(evlist, str, &err); 24479175ce1fSAndi Kleen 2448333b5665SAndi Kleen if (ret) { 24496c191289SIan Rogers parse_events_error__print(&err, str); 2450333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2451333b5665SAndi Kleen } 245207eafd4eSIan Rogers parse_events_error__exit(&err); 2453b39b8393SJiri Olsa 24549175ce1fSAndi Kleen return ret; 2455f120f9d5SJiri Olsa } 2456f120f9d5SJiri Olsa 2457d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2458d0abbc3cSArnaldo Carvalho de Melo { 2459d0abbc3cSArnaldo Carvalho de Melo struct evlist **evlistp = opt->value; 2460d0abbc3cSArnaldo Carvalho de Melo int ret; 2461d0abbc3cSArnaldo Carvalho de Melo 2462d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2463d0abbc3cSArnaldo Carvalho de Melo *evlistp = evlist__new(); 2464d0abbc3cSArnaldo Carvalho de Melo 2465d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2466d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2467d0abbc3cSArnaldo Carvalho de Melo return -1; 2468d0abbc3cSArnaldo Carvalho de Melo } 2469d0abbc3cSArnaldo Carvalho de Melo } 2470d0abbc3cSArnaldo Carvalho de Melo 2471d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2472d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2473d0abbc3cSArnaldo Carvalho de Melo evlist__delete(*evlistp); 2474d0abbc3cSArnaldo Carvalho de Melo *evlistp = NULL; 2475d0abbc3cSArnaldo Carvalho de Melo } 2476d0abbc3cSArnaldo Carvalho de Melo 2477d0abbc3cSArnaldo Carvalho de Melo return ret; 2478d0abbc3cSArnaldo Carvalho de Melo } 2479d0abbc3cSArnaldo Carvalho de Melo 24804ba1faa1SWang Nan static int 248163503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 248232dcd021SJiri Olsa int (*func)(struct evsel *evsel, 24834ba1faa1SWang Nan const void *arg), 24844ba1faa1SWang Nan const void *arg) 2485c171b552SLi Zefan { 248632dcd021SJiri Olsa struct evsel *last = NULL; 24874ba1faa1SWang Nan int err; 2488c171b552SLi Zefan 2489854f7363SWang Nan /* 2490854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2491854f7363SWang Nan * error when it found last == NULL. 2492854f7363SWang Nan * 2493854f7363SWang Nan * So no need to WARN here, let *func do this. 2494854f7363SWang Nan */ 24956484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2496515dbe48SJiri Olsa last = evlist__last(evlist); 249769aad6f1SArnaldo Carvalho de Melo 249815bfd2ccSWang Nan do { 24994ba1faa1SWang Nan err = (*func)(last, arg); 25004ba1faa1SWang Nan if (err) 2501c171b552SLi Zefan return -1; 25024ba1faa1SWang Nan if (!last) 25034ba1faa1SWang Nan return 0; 2504c171b552SLi Zefan 2505ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 250615bfd2ccSWang Nan return 0; 2507b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 250815bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 250915bfd2ccSWang Nan 2510c171b552SLi Zefan return 0; 2511c171b552SLi Zefan } 2512c171b552SLi Zefan 251332dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 25144ba1faa1SWang Nan { 25154ba1faa1SWang Nan const char *str = arg; 25161e857484SMathieu Poirier bool found = false; 25171e857484SMathieu Poirier int nr_addr_filters = 0; 25181e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 25194ba1faa1SWang Nan 252049836f78SJack Henschel if (evsel == NULL) { 252149836f78SJack Henschel fprintf(stderr, 252249836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 252349836f78SJack Henschel return -1; 252449836f78SJack Henschel } 25254ba1faa1SWang Nan 25261fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2527ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, str) < 0) { 25284ba1faa1SWang Nan fprintf(stderr, 25294ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25304ba1faa1SWang Nan return -1; 25314ba1faa1SWang Nan } 25324ba1faa1SWang Nan 25334ba1faa1SWang Nan return 0; 25344ba1faa1SWang Nan } 25354ba1faa1SWang Nan 25361e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 25371fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 25381e857484SMathieu Poirier found = true; 25391e857484SMathieu Poirier break; 25401e857484SMathieu Poirier } 25411e857484SMathieu Poirier 25421e857484SMathieu Poirier if (found) 25431e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 25441e857484SMathieu Poirier "%d", &nr_addr_filters); 25451e857484SMathieu Poirier 2546*d180aa56SNamhyung Kim if (!nr_addr_filters) 2547*d180aa56SNamhyung Kim return perf_bpf_filter__parse(&evsel->bpf_filters, str); 25481e857484SMathieu Poirier 2549ad681adfSArnaldo Carvalho de Melo if (evsel__append_addr_filter(evsel, str) < 0) { 25501e857484SMathieu Poirier fprintf(stderr, 25511e857484SMathieu Poirier "not enough memory to hold filter string\n"); 25521e857484SMathieu Poirier return -1; 25531e857484SMathieu Poirier } 25541e857484SMathieu Poirier 25551e857484SMathieu Poirier return 0; 25561e857484SMathieu Poirier } 25571e857484SMathieu Poirier 25584ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 25594ba1faa1SWang Nan int unset __maybe_unused) 25604ba1faa1SWang Nan { 256163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25624ba1faa1SWang Nan 25634ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 25644ba1faa1SWang Nan (const void *)str); 25654ba1faa1SWang Nan } 25664ba1faa1SWang Nan 256732dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 25684ba1faa1SWang Nan const void *arg __maybe_unused) 25694ba1faa1SWang Nan { 25704ba1faa1SWang Nan char new_filter[64]; 25714ba1faa1SWang Nan 25721fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 25734ba1faa1SWang Nan fprintf(stderr, 25744ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 25754ba1faa1SWang Nan return -1; 25764ba1faa1SWang Nan } 25774ba1faa1SWang Nan 25784ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 25794ba1faa1SWang Nan 2580ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, new_filter) < 0) { 25814ba1faa1SWang Nan fprintf(stderr, 25824ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25834ba1faa1SWang Nan return -1; 25844ba1faa1SWang Nan } 25854ba1faa1SWang Nan 25864ba1faa1SWang Nan return 0; 25874ba1faa1SWang Nan } 25884ba1faa1SWang Nan 25894ba1faa1SWang Nan int exclude_perf(const struct option *opt, 25904ba1faa1SWang Nan const char *arg __maybe_unused, 25914ba1faa1SWang Nan int unset __maybe_unused) 25924ba1faa1SWang Nan { 259363503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25944ba1faa1SWang Nan 25954ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 25964ba1faa1SWang Nan NULL); 25974ba1faa1SWang Nan } 25984ba1faa1SWang Nan 25996cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 26008f707d84SJiri Olsa { 260116fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 26028f707d84SJiri Olsa } 26038f707d84SJiri Olsa 260467b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 260567b49b38SJiri Olsa struct parse_events_term *temp, 260667b49b38SJiri Olsa char *str, u64 num) 26078f707d84SJiri Olsa { 26086cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 26098f707d84SJiri Olsa 261067b49b38SJiri Olsa term = malloc(sizeof(*term)); 26118f707d84SJiri Olsa if (!term) 26128f707d84SJiri Olsa return -ENOMEM; 26138f707d84SJiri Olsa 261467b49b38SJiri Olsa *term = *temp; 26158f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2616c2f1ceadSAndi Kleen term->weak = false; 26178f707d84SJiri Olsa 261867b49b38SJiri Olsa switch (term->type_val) { 26198f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 26208f707d84SJiri Olsa term->val.num = num; 26218f707d84SJiri Olsa break; 26228f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 26238f707d84SJiri Olsa term->val.str = str; 26248f707d84SJiri Olsa break; 26258f707d84SJiri Olsa default: 26264be8be6bSAdrian Hunter free(term); 26278f707d84SJiri Olsa return -EINVAL; 26288f707d84SJiri Olsa } 26298f707d84SJiri Olsa 26308f707d84SJiri Olsa *_term = term; 26318f707d84SJiri Olsa return 0; 26328f707d84SJiri Olsa } 26338f707d84SJiri Olsa 26346cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2635cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 263699e7138eSJiri Olsa bool no_value, 2637bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 263816fa7e82SJiri Olsa { 2639bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2640bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2641bb78ce7dSAdrian Hunter 264267b49b38SJiri Olsa struct parse_events_term temp = { 264367b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 264467b49b38SJiri Olsa .type_term = type_term, 26454f9d4f8aSJohn Garry .config = config ? : strdup(config_term_names[type_term]), 264699e7138eSJiri Olsa .no_value = no_value, 264767b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 264867b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 264967b49b38SJiri Olsa }; 265067b49b38SJiri Olsa 265167b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 265216fa7e82SJiri Olsa } 265316fa7e82SJiri Olsa 26546cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2655cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2656bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 265716fa7e82SJiri Olsa { 2658bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2659bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2660bb78ce7dSAdrian Hunter 266167b49b38SJiri Olsa struct parse_events_term temp = { 266267b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 266367b49b38SJiri Olsa .type_term = type_term, 266467b49b38SJiri Olsa .config = config, 266567b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 266667b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 266767b49b38SJiri Olsa }; 266867b49b38SJiri Olsa 266967b49b38SJiri Olsa return new_term(term, &temp, str, 0); 267016fa7e82SJiri Olsa } 267116fa7e82SJiri Olsa 26726cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 26731d33d6dcSJiri Olsa char *config, unsigned idx) 26741d33d6dcSJiri Olsa { 26751d33d6dcSJiri Olsa struct event_symbol *sym; 2676b6645a72SIan Rogers char *str; 267767b49b38SJiri Olsa struct parse_events_term temp = { 267867b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 267967b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 2680b6645a72SIan Rogers .config = config, 268167b49b38SJiri Olsa }; 26821d33d6dcSJiri Olsa 2683b6645a72SIan Rogers if (!temp.config) { 2684b6645a72SIan Rogers temp.config = strdup("event"); 2685b6645a72SIan Rogers if (!temp.config) 2686b6645a72SIan Rogers return -ENOMEM; 2687b6645a72SIan Rogers } 26881d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 26891d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 26901d33d6dcSJiri Olsa 2691b6645a72SIan Rogers str = strdup(sym->symbol); 2692b6645a72SIan Rogers if (!str) 2693b6645a72SIan Rogers return -ENOMEM; 2694b6645a72SIan Rogers return new_term(term, &temp, str, 0); 26951d33d6dcSJiri Olsa } 26961d33d6dcSJiri Olsa 26976cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 26986cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2699a6146d50SZheng Yan { 2700b6645a72SIan Rogers char *str; 270167b49b38SJiri Olsa struct parse_events_term temp = { 270267b49b38SJiri Olsa .type_val = term->type_val, 270367b49b38SJiri Olsa .type_term = term->type_term, 2704b6645a72SIan Rogers .config = NULL, 270567b49b38SJiri Olsa .err_term = term->err_term, 270667b49b38SJiri Olsa .err_val = term->err_val, 270767b49b38SJiri Olsa }; 270867b49b38SJiri Olsa 2709b6645a72SIan Rogers if (term->config) { 2710b6645a72SIan Rogers temp.config = strdup(term->config); 2711b6645a72SIan Rogers if (!temp.config) 2712b6645a72SIan Rogers return -ENOMEM; 2713b6645a72SIan Rogers } 2714b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 2715b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 2716b6645a72SIan Rogers 2717b6645a72SIan Rogers str = strdup(term->val.str); 2718b6645a72SIan Rogers if (!str) 2719b6645a72SIan Rogers return -ENOMEM; 2720b6645a72SIan Rogers return new_term(new, &temp, str, 0); 2721a6146d50SZheng Yan } 2722a6146d50SZheng Yan 27231dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 27241dc92556SIan Rogers { 27251dc92556SIan Rogers if (term->array.nr_ranges) 27261dc92556SIan Rogers zfree(&term->array.ranges); 27271dc92556SIan Rogers 27281dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 27291dc92556SIan Rogers zfree(&term->val.str); 27301dc92556SIan Rogers 27311dc92556SIan Rogers zfree(&term->config); 27321dc92556SIan Rogers free(term); 27331dc92556SIan Rogers } 27341dc92556SIan Rogers 27358255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 27368255718fSAndi Kleen struct list_head **new) 27378255718fSAndi Kleen { 27388255718fSAndi Kleen struct parse_events_term *term, *n; 27398255718fSAndi Kleen int ret; 27408255718fSAndi Kleen 27418255718fSAndi Kleen if (!old) { 27428255718fSAndi Kleen *new = NULL; 27438255718fSAndi Kleen return 0; 27448255718fSAndi Kleen } 27458255718fSAndi Kleen 27468255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 27478255718fSAndi Kleen if (!*new) 27488255718fSAndi Kleen return -ENOMEM; 27498255718fSAndi Kleen INIT_LIST_HEAD(*new); 27508255718fSAndi Kleen 27518255718fSAndi Kleen list_for_each_entry (term, old, list) { 27528255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 27538255718fSAndi Kleen if (ret) 27548255718fSAndi Kleen return ret; 27558255718fSAndi Kleen list_add_tail(&n->list, *new); 27568255718fSAndi Kleen } 27578255718fSAndi Kleen return 0; 27588255718fSAndi Kleen } 27598255718fSAndi Kleen 2760fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 27618f707d84SJiri Olsa { 27626cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 27638f707d84SJiri Olsa 2764a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 2765a8adfcebSWang Nan list_del_init(&term->list); 27661dc92556SIan Rogers parse_events_term__delete(term); 27678f707d84SJiri Olsa } 2768a8adfcebSWang Nan } 2769b39b8393SJiri Olsa 27702146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2771fc0a2c1dSArnaldo Carvalho de Melo { 27722146afc6SArnaldo Carvalho de Melo if (!terms) 27732146afc6SArnaldo Carvalho de Melo return; 2774fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2775d20a5f2bSWang Nan free(terms); 2776fc0a2c1dSArnaldo Carvalho de Melo } 2777fc0a2c1dSArnaldo Carvalho de Melo 27782d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 27792d055bf2SWang Nan { 2780360e071bSTaeung Song zfree(&a->ranges); 27812d055bf2SWang Nan } 27822d055bf2SWang Nan 27835d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2784b39b8393SJiri Olsa int idx, const char *str) 2785b39b8393SJiri Olsa { 2786448d732cSIan Rogers if (!parse_state->error) 2787a6ced2beSAdrian Hunter return; 2788448d732cSIan Rogers 27896c191289SIan Rogers parse_events_error__handle(parse_state->error, idx, strdup(str), NULL); 2790b39b8393SJiri Olsa } 2791ffeb883eSHe Kuang 279217cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 279317cb5f84SWang Nan { 279417cb5f84SWang Nan int i; 279517cb5f84SWang Nan bool first = true; 279617cb5f84SWang Nan 279717cb5f84SWang Nan buf[0] = '\0'; 279817cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 279917cb5f84SWang Nan const char *name = config_term_names[i]; 280017cb5f84SWang Nan 28011669e509SWang Nan if (!config_term_avail(i, NULL)) 28021669e509SWang Nan continue; 280317cb5f84SWang Nan if (!name) 280417cb5f84SWang Nan continue; 280517cb5f84SWang Nan if (name[0] == '<') 280617cb5f84SWang Nan continue; 280717cb5f84SWang Nan 280817cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 280917cb5f84SWang Nan return; 281017cb5f84SWang Nan 281117cb5f84SWang Nan if (!first) 281217cb5f84SWang Nan strcat(buf, ","); 281317cb5f84SWang Nan else 281417cb5f84SWang Nan first = false; 281517cb5f84SWang Nan strcat(buf, name); 281617cb5f84SWang Nan } 281717cb5f84SWang Nan } 281817cb5f84SWang Nan 2819ffeb883eSHe Kuang /* 2820ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2821ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2822ffeb883eSHe Kuang */ 2823ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2824ffeb883eSHe Kuang { 2825ffeb883eSHe Kuang char *str; 2826626a6b78SWang Nan /* "no-overwrite" is the longest name */ 282717cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2828626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2829ffeb883eSHe Kuang 283017cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2831ffeb883eSHe Kuang /* valid terms */ 2832ffeb883eSHe Kuang if (additional_terms) { 283326dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 283426dee028SWang Nan additional_terms, static_terms) < 0) 2835ffeb883eSHe Kuang goto fail; 2836ffeb883eSHe Kuang } else { 283726dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2838ffeb883eSHe Kuang goto fail; 2839ffeb883eSHe Kuang } 2840ffeb883eSHe Kuang return str; 2841ffeb883eSHe Kuang 2842ffeb883eSHe Kuang fail: 2843ffeb883eSHe Kuang return NULL; 2844ffeb883eSHe Kuang } 28459cbfa2f6SJin Yao 28469cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx, 28479cbfa2f6SJin Yao struct perf_event_attr *attr, 28482b62b3a6SIan Rogers const char *name, 28492b62b3a6SIan Rogers const char *metric_id, 28502b62b3a6SIan Rogers struct perf_pmu *pmu, 28519cbfa2f6SJin Yao struct list_head *config_terms) 28529cbfa2f6SJin Yao { 28532b62b3a6SIan Rogers return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id, 28542b62b3a6SIan Rogers pmu, config_terms, /*auto_merge_stats=*/false, 28552b62b3a6SIan Rogers /*cpu_list=*/NULL); 28569cbfa2f6SJin Yao } 2857