1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 38dd2a131SJiri Olsa #include <linux/err.h> 4*347c2f0aSIan 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" 319b7c7728SIan Rogers #include "tracepoint.h" 3271c86cdaSZhengjun Xing #include "thread_map.h" 3389812fc8SJiri Olsa 3489812fc8SJiri Olsa #define MAX_NAME_LEN 100 3586470930SIngo Molnar 369b7c7728SIan Rogers struct perf_pmu_event_symbol { 379b7c7728SIan Rogers char *symbol; 389b7c7728SIan Rogers enum perf_pmu_event_symbol_type type; 399b7c7728SIan Rogers }; 409b7c7728SIan Rogers 4182ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4282ba1f2fSJiri Olsa extern int parse_events_debug; 4382ba1f2fSJiri Olsa #endif 445d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 45e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 46e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 475e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 485e4edd1fSJin Yao const char *str, char *pmu_name, 495e4edd1fSJin Yao struct list_head *list); 50bcd3279fSFrederic Weisbecker 51dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 52dcb4e102SKan Liang /* 53dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 54dcb4e102SKan Liang * 0 means not initialized and ready to init 55dcb4e102SKan Liang * -1 means failed to init, don't try anymore 56dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 57dcb4e102SKan Liang */ 58dcb4e102SKan Liang static int perf_pmu_events_list_num; 59dcb4e102SKan Liang 60705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 611dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 621dc12760SJiri Olsa .symbol = "cpu-cycles", 631dc12760SJiri Olsa .alias = "cycles", 641dc12760SJiri Olsa }, 651dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 661dc12760SJiri Olsa .symbol = "instructions", 671dc12760SJiri Olsa .alias = "", 681dc12760SJiri Olsa }, 691dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 701dc12760SJiri Olsa .symbol = "cache-references", 711dc12760SJiri Olsa .alias = "", 721dc12760SJiri Olsa }, 731dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 741dc12760SJiri Olsa .symbol = "cache-misses", 751dc12760SJiri Olsa .alias = "", 761dc12760SJiri Olsa }, 771dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 781dc12760SJiri Olsa .symbol = "branch-instructions", 791dc12760SJiri Olsa .alias = "branches", 801dc12760SJiri Olsa }, 811dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 821dc12760SJiri Olsa .symbol = "branch-misses", 831dc12760SJiri Olsa .alias = "", 841dc12760SJiri Olsa }, 851dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 861dc12760SJiri Olsa .symbol = "bus-cycles", 871dc12760SJiri Olsa .alias = "", 881dc12760SJiri Olsa }, 891dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 901dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 911dc12760SJiri Olsa .alias = "idle-cycles-frontend", 921dc12760SJiri Olsa }, 931dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 941dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 951dc12760SJiri Olsa .alias = "idle-cycles-backend", 961dc12760SJiri Olsa }, 971dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 981dc12760SJiri Olsa .symbol = "ref-cycles", 991dc12760SJiri Olsa .alias = "", 1001dc12760SJiri Olsa }, 1011dc12760SJiri Olsa }; 10286470930SIngo Molnar 103705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 1041dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1051dc12760SJiri Olsa .symbol = "cpu-clock", 1061dc12760SJiri Olsa .alias = "", 1071dc12760SJiri Olsa }, 1081dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1091dc12760SJiri Olsa .symbol = "task-clock", 1101dc12760SJiri Olsa .alias = "", 1111dc12760SJiri Olsa }, 1121dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1131dc12760SJiri Olsa .symbol = "page-faults", 1141dc12760SJiri Olsa .alias = "faults", 1151dc12760SJiri Olsa }, 1161dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1171dc12760SJiri Olsa .symbol = "context-switches", 1181dc12760SJiri Olsa .alias = "cs", 1191dc12760SJiri Olsa }, 1201dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1211dc12760SJiri Olsa .symbol = "cpu-migrations", 1221dc12760SJiri Olsa .alias = "migrations", 1231dc12760SJiri Olsa }, 1241dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1251dc12760SJiri Olsa .symbol = "minor-faults", 1261dc12760SJiri Olsa .alias = "", 1271dc12760SJiri Olsa }, 1281dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1291dc12760SJiri Olsa .symbol = "major-faults", 1301dc12760SJiri Olsa .alias = "", 1311dc12760SJiri Olsa }, 1321dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1331dc12760SJiri Olsa .symbol = "alignment-faults", 1341dc12760SJiri Olsa .alias = "", 1351dc12760SJiri Olsa }, 1361dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1371dc12760SJiri Olsa .symbol = "emulation-faults", 1381dc12760SJiri Olsa .alias = "", 1391dc12760SJiri Olsa }, 140d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 141d22d1a2aSAdrian Hunter .symbol = "dummy", 142d22d1a2aSAdrian Hunter .alias = "", 143d22d1a2aSAdrian Hunter }, 144bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 145bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 146bae9cc41SArnaldo Carvalho de Melo .alias = "", 147bae9cc41SArnaldo Carvalho de Melo }, 148fb6c79d7SNamhyung Kim [PERF_COUNT_SW_CGROUP_SWITCHES] = { 149fb6c79d7SNamhyung Kim .symbol = "cgroup-switches", 150fb6c79d7SNamhyung Kim .alias = "", 151fb6c79d7SNamhyung Kim }, 15286470930SIngo Molnar }; 15386470930SIngo Molnar 15471c86cdaSZhengjun Xing bool is_event_supported(u8 type, u64 config) 15571c86cdaSZhengjun Xing { 15671c86cdaSZhengjun Xing bool ret = true; 15771c86cdaSZhengjun Xing int open_return; 15871c86cdaSZhengjun Xing struct evsel *evsel; 15971c86cdaSZhengjun Xing struct perf_event_attr attr = { 16071c86cdaSZhengjun Xing .type = type, 16171c86cdaSZhengjun Xing .config = config, 16271c86cdaSZhengjun Xing .disabled = 1, 16371c86cdaSZhengjun Xing }; 16471c86cdaSZhengjun Xing struct perf_thread_map *tmap = thread_map__new_by_tid(0); 16571c86cdaSZhengjun Xing 16671c86cdaSZhengjun Xing if (tmap == NULL) 16771c86cdaSZhengjun Xing return false; 16871c86cdaSZhengjun Xing 16971c86cdaSZhengjun Xing evsel = evsel__new(&attr); 17071c86cdaSZhengjun Xing if (evsel) { 17171c86cdaSZhengjun Xing open_return = evsel__open(evsel, NULL, tmap); 17271c86cdaSZhengjun Xing ret = open_return >= 0; 17371c86cdaSZhengjun Xing 17471c86cdaSZhengjun Xing if (open_return == -EACCES) { 17571c86cdaSZhengjun Xing /* 17671c86cdaSZhengjun Xing * This happens if the paranoid value 17771c86cdaSZhengjun Xing * /proc/sys/kernel/perf_event_paranoid is set to 2 17871c86cdaSZhengjun Xing * Re-run with exclude_kernel set; we don't do that 17971c86cdaSZhengjun Xing * by default as some ARM machines do not support it. 18071c86cdaSZhengjun Xing * 18171c86cdaSZhengjun Xing */ 18271c86cdaSZhengjun Xing evsel->core.attr.exclude_kernel = 1; 18371c86cdaSZhengjun Xing ret = evsel__open(evsel, NULL, tmap) >= 0; 18471c86cdaSZhengjun Xing } 18571c86cdaSZhengjun Xing evsel__delete(evsel); 18671c86cdaSZhengjun Xing } 18771c86cdaSZhengjun Xing 18871c86cdaSZhengjun Xing perf_thread_map__put(tmap); 18971c86cdaSZhengjun Xing return ret; 19071c86cdaSZhengjun Xing } 19171c86cdaSZhengjun Xing 1921424dc96SDavid Ahern const char *event_type(int type) 1931424dc96SDavid Ahern { 1941424dc96SDavid Ahern switch (type) { 1951424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 1961424dc96SDavid Ahern return "hardware"; 1971424dc96SDavid Ahern 1981424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 1991424dc96SDavid Ahern return "software"; 2001424dc96SDavid Ahern 2011424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2021424dc96SDavid Ahern return "tracepoint"; 2031424dc96SDavid Ahern 2041424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2051424dc96SDavid Ahern return "hardware-cache"; 2061424dc96SDavid Ahern 2071424dc96SDavid Ahern default: 2081424dc96SDavid Ahern break; 2091424dc96SDavid Ahern } 2101424dc96SDavid Ahern 2111424dc96SDavid Ahern return "unknown"; 2121424dc96SDavid Ahern } 2131424dc96SDavid Ahern 2142b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term) 215e814fdddSWang Nan { 216e814fdddSWang Nan struct parse_events_term *term; 217e814fdddSWang Nan 218e814fdddSWang Nan if (!head_terms) 219e814fdddSWang Nan return NULL; 220e814fdddSWang Nan 221e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 2222b62b3a6SIan Rogers if (term->type_term == type_term) 223e814fdddSWang Nan return term->val.str; 224e814fdddSWang Nan 225e814fdddSWang Nan return NULL; 226e814fdddSWang Nan } 2277ae92e74SYan, Zheng 2282b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms) 2292b62b3a6SIan Rogers { 2302b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID); 2312b62b3a6SIan Rogers } 2322b62b3a6SIan Rogers 2332b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms) 2342b62b3a6SIan Rogers { 2352b62b3a6SIan Rogers return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); 2362b62b3a6SIan Rogers } 2372b62b3a6SIan Rogers 23832dcd021SJiri Olsa static struct evsel * 239410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 2407ae92e74SYan, Zheng struct perf_event_attr *attr, 24170943490SStephane Eranian bool init_attr, 2422b62b3a6SIan Rogers const char *name, const char *metric_id, struct perf_pmu *pmu, 243f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 244f0fbb114SAndi Kleen const char *cpu_list) 24589812fc8SJiri Olsa { 24632dcd021SJiri Olsa struct evsel *evsel; 2475d680be3SNamhyung Kim struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 2489c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 24989812fc8SJiri Olsa 250e552b7beSRob Herring if (pmu) 251e552b7beSRob Herring perf_pmu__warn_invalid_formats(pmu); 252e552b7beSRob Herring 253e4064776SJin Yao if (pmu && attr->type == PERF_TYPE_RAW) 254e4064776SJin Yao perf_pmu__warn_invalid_config(pmu, attr->config, name); 255e4064776SJin Yao 25670943490SStephane Eranian if (init_attr) 25789812fc8SJiri Olsa event_attr_init(attr); 25889812fc8SJiri Olsa 2598f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attr, *idx); 2600f1b550eSNamhyung Kim if (!evsel) { 2610f1b550eSNamhyung Kim perf_cpu_map__put(cpus); 262410136f5SStephane Eranian return NULL; 2630f1b550eSNamhyung Kim } 26489812fc8SJiri Olsa 26575fc5ae5STaeung Song (*idx)++; 2665d680be3SNamhyung Kim evsel->core.cpus = cpus; 267fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 268d3345fecSAdrian Hunter evsel->core.requires_cpu = pmu ? pmu->is_uncore : false; 26963ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 270f7400262SNamhyung Kim evsel->pmu = pmu; 271f30a79b0SJiri Olsa 2729db1763cSArnaldo Carvalho de Melo if (name) 27389812fc8SJiri Olsa evsel->name = strdup(name); 274930a2e29SJiri Olsa 2752b62b3a6SIan Rogers if (metric_id) 2762b62b3a6SIan Rogers evsel->metric_id = strdup(metric_id); 2772b62b3a6SIan Rogers 278930a2e29SJiri Olsa if (config_terms) 27999fc5941SAdrian Hunter list_splice_init(config_terms, &evsel->config_terms); 280930a2e29SJiri Olsa 28170943490SStephane Eranian if (list) 282b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 28370943490SStephane Eranian 284410136f5SStephane Eranian return evsel; 28589812fc8SJiri Olsa } 28689812fc8SJiri Olsa 28770943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 2882b62b3a6SIan Rogers const char *name, const char *metric_id, 2892b62b3a6SIan Rogers struct perf_pmu *pmu) 29070943490SStephane Eranian { 2912b62b3a6SIan Rogers return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name, 2922b62b3a6SIan Rogers metric_id, pmu, /*config_terms=*/NULL, 2932b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL); 29470943490SStephane Eranian } 29570943490SStephane Eranian 296c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 2978e8bbfb3SIan Rogers struct perf_event_attr *attr, const char *name, 2982b62b3a6SIan Rogers const char *metric_id, struct list_head *config_terms) 2997ae92e74SYan, Zheng { 3002b62b3a6SIan Rogers return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, 3012b62b3a6SIan Rogers /*pmu=*/NULL, config_terms, 3022b62b3a6SIan Rogers /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM; 303f0fbb114SAndi Kleen } 304f0fbb114SAndi Kleen 305f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 306f0fbb114SAndi Kleen enum perf_tool_event tool_event) 307f0fbb114SAndi Kleen { 30832dcd021SJiri Olsa struct evsel *evsel; 309f0fbb114SAndi Kleen struct perf_event_attr attr = { 310f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 311f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 312f0fbb114SAndi Kleen }; 313f0fbb114SAndi Kleen 3142b62b3a6SIan Rogers evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL, 3152b62b3a6SIan Rogers /*metric_id=*/NULL, /*pmu=*/NULL, 3162b62b3a6SIan Rogers /*config_terms=*/NULL, /*auto_merge_stats=*/false, 3172b62b3a6SIan Rogers /*cpu_list=*/"0"); 318f0fbb114SAndi Kleen if (!evsel) 319f0fbb114SAndi Kleen return -ENOMEM; 320f0fbb114SAndi Kleen evsel->tool_event = tool_event; 321b03b89b3SFlorian Fischer if (tool_event == PERF_TOOL_DURATION_TIME 322b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_USER_TIME 323b03b89b3SFlorian Fischer || tool_event == PERF_TOOL_SYSTEM_TIME) { 324b194c9cdSIan Rogers free((char *)evsel->unit); 325b194c9cdSIan Rogers evsel->unit = strdup("ns"); 326b194c9cdSIan Rogers } 327f0fbb114SAndi Kleen return 0; 3287ae92e74SYan, Zheng } 3297ae92e74SYan, Zheng 330545a96c9SIan Rogers static int parse_aliases(char *str, const char *const names[][EVSEL__MAX_ALIASES], int size) 33186470930SIngo Molnar { 33286470930SIngo Molnar int i, j; 33361c45981SPaul Mackerras int n, longest = -1; 33486470930SIngo Molnar 33586470930SIngo Molnar for (i = 0; i < size; i++) { 336c64e85e1SArnaldo Carvalho de Melo for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 33761c45981SPaul Mackerras n = strlen(names[i][j]); 33889812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 33961c45981SPaul Mackerras longest = n; 34061c45981SPaul Mackerras } 34189812fc8SJiri Olsa if (longest > 0) 34286470930SIngo Molnar return i; 34386470930SIngo Molnar } 34486470930SIngo Molnar 3458953645fSIngo Molnar return -1; 34686470930SIngo Molnar } 34786470930SIngo Molnar 34843d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 34943d0b978SWang Nan struct parse_events_term *term, 35043d0b978SWang Nan struct parse_events_error *err); 35143d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 35243d0b978SWang Nan struct parse_events_term *term, 35343d0b978SWang Nan struct parse_events_error *err); 35443d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 35543d0b978SWang Nan struct list_head *head, 35643d0b978SWang Nan struct parse_events_error *err, 35743d0b978SWang Nan config_term_func_t config_term); 35843d0b978SWang Nan 359c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 36043d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3612c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 362c93afadcSJin Yao struct list_head *head_config, 363c93afadcSJin Yao struct parse_events_state *parse_state) 36486470930SIngo Molnar { 36589812fc8SJiri Olsa struct perf_event_attr attr; 36643d0b978SWang Nan LIST_HEAD(config_terms); 3678e8bbfb3SIan Rogers char name[MAX_NAME_LEN]; 3682b62b3a6SIan Rogers const char *config_name, *metric_id; 36961c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 37089812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 37130def61fSJin Yao int i, n, ret; 37230def61fSJin Yao bool hybrid; 37386470930SIngo Molnar 37486470930SIngo Molnar /* 37586470930SIngo Molnar * No fallback - if we cannot get a clear cache type 37686470930SIngo Molnar * then bail out: 37786470930SIngo Molnar */ 378c64e85e1SArnaldo Carvalho de Melo cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX); 37986470930SIngo Molnar if (cache_type == -1) 38089812fc8SJiri Olsa return -EINVAL; 38186470930SIngo Molnar 38243d0b978SWang Nan config_name = get_config_name(head_config); 38389812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 38489812fc8SJiri Olsa 38589812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 38689812fc8SJiri Olsa char *str = op_result[i]; 38789812fc8SJiri Olsa 388275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 38961c45981SPaul Mackerras 39061c45981SPaul Mackerras if (cache_op == -1) { 391c64e85e1SArnaldo Carvalho de Melo cache_op = parse_aliases(str, evsel__hw_cache_op, 39261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 39361c45981SPaul Mackerras if (cache_op >= 0) { 394c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(cache_type, cache_op)) 39589812fc8SJiri Olsa return -EINVAL; 39661c45981SPaul Mackerras continue; 39761c45981SPaul Mackerras } 39861c45981SPaul Mackerras } 39961c45981SPaul Mackerras 40061c45981SPaul Mackerras if (cache_result == -1) { 401c64e85e1SArnaldo Carvalho de Melo cache_result = parse_aliases(str, evsel__hw_cache_result, 40261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 40361c45981SPaul Mackerras if (cache_result >= 0) 40461c45981SPaul Mackerras continue; 40561c45981SPaul Mackerras } 40661c45981SPaul Mackerras } 40761c45981SPaul Mackerras 40886470930SIngo Molnar /* 40986470930SIngo Molnar * Fall back to reads: 41086470930SIngo Molnar */ 4118953645fSIngo Molnar if (cache_op == -1) 4128953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 41386470930SIngo Molnar 41486470930SIngo Molnar /* 41586470930SIngo Molnar * Fall back to accesses: 41686470930SIngo Molnar */ 41786470930SIngo Molnar if (cache_result == -1) 41886470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 41986470930SIngo Molnar 42089812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 42189812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 42289812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 42343d0b978SWang Nan 42443d0b978SWang Nan if (head_config) { 4252c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 42643d0b978SWang Nan config_term_common)) 42743d0b978SWang Nan return -EINVAL; 42843d0b978SWang Nan 42943d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 43043d0b978SWang Nan return -ENOMEM; 43143d0b978SWang Nan } 43230def61fSJin Yao 4332b62b3a6SIan Rogers metric_id = get_config_metric_id(head_config); 43430def61fSJin Yao ret = parse_events__add_cache_hybrid(list, idx, &attr, 4352b62b3a6SIan Rogers config_name ? : name, 4362b62b3a6SIan Rogers metric_id, 4372b62b3a6SIan Rogers &config_terms, 438c93afadcSJin Yao &hybrid, parse_state); 43930def61fSJin Yao if (hybrid) 44099fc5941SAdrian Hunter goto out_free_terms; 44130def61fSJin Yao 4422b62b3a6SIan Rogers ret = add_event(list, idx, &attr, config_name ? : name, metric_id, 4432b62b3a6SIan Rogers &config_terms); 44499fc5941SAdrian Hunter out_free_terms: 44599fc5941SAdrian Hunter free_config_terms(&config_terms); 44699fc5941SAdrian Hunter return ret; 44786470930SIngo Molnar } 44886470930SIngo Molnar 449378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 450272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4518c619d6aSWang Nan const char *sys, const char *name) 45219658171SJiri Olsa { 4534584f084SIan Rogers const char *str; 45419658171SJiri Olsa char help[BUFSIZ]; 45519658171SJiri Olsa 456ec183d22SAdrian Hunter if (!e) 457ec183d22SAdrian Hunter return; 458ec183d22SAdrian Hunter 45919658171SJiri Olsa /* 46019658171SJiri Olsa * We get error directly from syscall errno ( > 0), 46119658171SJiri Olsa * or from encoded pointer's error ( < 0). 46219658171SJiri Olsa */ 46319658171SJiri Olsa err = abs(err); 46419658171SJiri Olsa 46519658171SJiri Olsa switch (err) { 46619658171SJiri Olsa case EACCES: 4674584f084SIan Rogers str = "can't access trace events"; 46819658171SJiri Olsa break; 46919658171SJiri Olsa case ENOENT: 4704584f084SIan Rogers str = "unknown tracepoint"; 47119658171SJiri Olsa break; 47219658171SJiri Olsa default: 4734584f084SIan Rogers str = "failed to add tracepoint"; 47419658171SJiri Olsa break; 47519658171SJiri Olsa } 47619658171SJiri Olsa 47719658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 4786c191289SIan Rogers parse_events_error__handle(e, 0, strdup(str), strdup(help)); 47919658171SJiri Olsa } 48019658171SJiri Olsa 481c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4828c619d6aSWang Nan const char *sys_name, const char *evt_name, 483272ed29aSJiri Olsa struct parse_events_error *err, 484e637d177SHe Kuang struct list_head *head_config) 485bcd3279fSFrederic Weisbecker { 4868f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 487bcd3279fSFrederic Weisbecker 48819658171SJiri Olsa if (IS_ERR(evsel)) { 489272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4908dd2a131SJiri Olsa return PTR_ERR(evsel); 49119658171SJiri Olsa } 492bcd3279fSFrederic Weisbecker 493e637d177SHe Kuang if (head_config) { 494e637d177SHe Kuang LIST_HEAD(config_terms); 495e637d177SHe Kuang 496e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 497e637d177SHe Kuang return -ENOMEM; 498e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 499e637d177SHe Kuang } 500e637d177SHe Kuang 501b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 50282fe1c29SArnaldo Carvalho de Melo return 0; 503bcd3279fSFrederic Weisbecker } 504bcd3279fSFrederic Weisbecker 505c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5068c619d6aSWang Nan const char *sys_name, const char *evt_name, 507272ed29aSJiri Olsa struct parse_events_error *err, 508e637d177SHe Kuang struct list_head *head_config) 509bcd3279fSFrederic Weisbecker { 51025a7d914SArnaldo Carvalho de Melo char *evt_path; 511bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 512bcd3279fSFrederic Weisbecker DIR *evt_dir; 51327bf90bfSJiri Olsa int ret = 0, found = 0; 514bcd3279fSFrederic Weisbecker 51525a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 51625a7d914SArnaldo Carvalho de Melo if (!evt_path) { 51725a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 51825a7d914SArnaldo Carvalho de Melo return -1; 51925a7d914SArnaldo Carvalho de Melo } 520bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 521bcd3279fSFrederic Weisbecker if (!evt_dir) { 52225a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 523272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 52489812fc8SJiri Olsa return -1; 525bcd3279fSFrederic Weisbecker } 526bcd3279fSFrederic Weisbecker 52789812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 528bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 529bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 530bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 531bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 532bcd3279fSFrederic Weisbecker continue; 533bcd3279fSFrederic Weisbecker 53489812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 535fb1d2edfSMasami Hiramatsu continue; 536fb1d2edfSMasami Hiramatsu 53727bf90bfSJiri Olsa found++; 53827bf90bfSJiri Olsa 539e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 540272ed29aSJiri Olsa err, head_config); 541bcd3279fSFrederic Weisbecker } 542bcd3279fSFrederic Weisbecker 54327bf90bfSJiri Olsa if (!found) { 54427bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 54527bf90bfSJiri Olsa ret = -1; 54627bf90bfSJiri Olsa } 54727bf90bfSJiri Olsa 54825a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 5490bd3f084SJiri Olsa closedir(evt_dir); 55089812fc8SJiri Olsa return ret; 551bcd3279fSFrederic Weisbecker } 552bcd3279fSFrederic Weisbecker 553c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5548c619d6aSWang Nan const char *sys_name, const char *evt_name, 555272ed29aSJiri Olsa struct parse_events_error *err, 556e637d177SHe Kuang struct list_head *head_config) 557f35488f9SJiri Olsa { 558f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 559e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 560272ed29aSJiri Olsa err, head_config) : 561e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 562272ed29aSJiri Olsa err, head_config); 563f35488f9SJiri Olsa } 564f35488f9SJiri Olsa 565c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5668c619d6aSWang Nan const char *sys_name, const char *evt_name, 567272ed29aSJiri Olsa struct parse_events_error *err, 568e637d177SHe Kuang struct list_head *head_config) 569f35488f9SJiri Olsa { 570f35488f9SJiri Olsa struct dirent *events_ent; 571f35488f9SJiri Olsa DIR *events_dir; 572f35488f9SJiri Olsa int ret = 0; 573f35488f9SJiri Olsa 5747014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 575f35488f9SJiri Olsa if (!events_dir) { 576272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 577f35488f9SJiri Olsa return -1; 578f35488f9SJiri Olsa } 579f35488f9SJiri Olsa 580f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 581f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 582f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 583f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 584f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 585f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 586f35488f9SJiri Olsa continue; 587f35488f9SJiri Olsa 588f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 589f35488f9SJiri Olsa continue; 590f35488f9SJiri Olsa 591f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 592272ed29aSJiri Olsa evt_name, err, head_config); 593f35488f9SJiri Olsa } 594f35488f9SJiri Olsa 595f35488f9SJiri Olsa closedir(events_dir); 596f35488f9SJiri Olsa return ret; 597f35488f9SJiri Olsa } 598378ef0f5SIan Rogers #endif /* HAVE_LIBTRACEEVENT */ 599f35488f9SJiri Olsa 600c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT 6014edf30e3SWang Nan struct __add_bpf_event_param { 6025d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6034edf30e3SWang Nan struct list_head *list; 60495088a59SWang Nan struct list_head *head_config; 6054edf30e3SWang Nan }; 6064edf30e3SWang Nan 607af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 6084edf30e3SWang Nan void *_param) 6094edf30e3SWang Nan { 6104edf30e3SWang Nan LIST_HEAD(new_evsels); 6114edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6125d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6134edf30e3SWang Nan struct list_head *list = param->list; 61432dcd021SJiri Olsa struct evsel *pos; 6154edf30e3SWang Nan int err; 6162620b7e3SArnaldo Carvalho de Melo /* 6172620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 6182620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 6192620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 6202620b7e3SArnaldo Carvalho de Melo * 6212620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 6222620b7e3SArnaldo Carvalho de Melo */ 6232620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 6242620b7e3SArnaldo Carvalho de Melo return 0; 6254edf30e3SWang Nan 6264edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 627cd102d70SWang Nan group, event, fd); 6284edf30e3SWang Nan 6295d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6305d369a75SArnaldo Carvalho de Melo event, parse_state->error, 63195088a59SWang Nan param->head_config); 6324edf30e3SWang Nan if (err) { 63332dcd021SJiri Olsa struct evsel *evsel, *tmp; 6344edf30e3SWang Nan 6354edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 636cd102d70SWang Nan group, event); 637b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 638b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 6395eb2dd2aSJiri Olsa evsel__delete(evsel); 6404edf30e3SWang Nan } 6414edf30e3SWang Nan return err; 6424edf30e3SWang Nan } 643cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6444edf30e3SWang Nan 645b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 6461f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 647cd102d70SWang Nan group, event, pos); 6481f45b1d4SWang Nan pos->bpf_fd = fd; 649af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 6501f45b1d4SWang Nan } 6514edf30e3SWang Nan list_splice(&new_evsels, list); 6524edf30e3SWang Nan return 0; 6534edf30e3SWang Nan } 6544edf30e3SWang Nan 6555d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 65684c86ca1SWang Nan struct list_head *list, 65795088a59SWang Nan struct bpf_object *obj, 65895088a59SWang Nan struct list_head *head_config) 65984c86ca1SWang Nan { 66084c86ca1SWang Nan int err; 66184c86ca1SWang Nan char errbuf[BUFSIZ]; 6625d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 663aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 66484c86ca1SWang Nan 66584c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 66684c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 66784c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 66884c86ca1SWang Nan err = -EINVAL; 66984c86ca1SWang Nan goto errout; 67084c86ca1SWang Nan } 67184c86ca1SWang Nan 67284c86ca1SWang Nan /* 673aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 674aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 675aa3abf30SWang Nan * created when failure. 676aa3abf30SWang Nan */ 677aa3abf30SWang Nan if (!registered_unprobe_atexit) { 678aa3abf30SWang Nan atexit(bpf__clear); 679aa3abf30SWang Nan registered_unprobe_atexit = true; 680aa3abf30SWang Nan } 681aa3abf30SWang Nan 682aa3abf30SWang Nan err = bpf__probe(obj); 683aa3abf30SWang Nan if (err) { 684aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 685aa3abf30SWang Nan goto errout; 686aa3abf30SWang Nan } 687aa3abf30SWang Nan 6881e5e3ee8SWang Nan err = bpf__load(obj); 6891e5e3ee8SWang Nan if (err) { 6901e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6911e5e3ee8SWang Nan goto errout; 6921e5e3ee8SWang Nan } 6931e5e3ee8SWang Nan 694cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6954edf30e3SWang Nan if (err) { 6964edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6974edf30e3SWang Nan "Attach events in BPF object failed"); 6984edf30e3SWang Nan goto errout; 6994edf30e3SWang Nan } 7004edf30e3SWang Nan 7014edf30e3SWang Nan return 0; 70284c86ca1SWang Nan errout: 7036c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 7045f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 70584c86ca1SWang Nan return err; 70684c86ca1SWang Nan } 70784c86ca1SWang Nan 708a34f3be7SWang Nan static int 7095d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 710a34f3be7SWang Nan struct bpf_object *obj, 711a34f3be7SWang Nan struct list_head *head_config) 712a34f3be7SWang Nan { 713a34f3be7SWang Nan struct parse_events_term *term; 714a34f3be7SWang Nan int error_pos; 715a34f3be7SWang Nan 716a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 717a34f3be7SWang Nan return 0; 718a34f3be7SWang Nan 719a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 720a34f3be7SWang Nan int err; 721a34f3be7SWang Nan 722a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 7236c191289SIan Rogers parse_events_error__handle(parse_state->error, term->err_term, 7245f634c8eSIan Rogers strdup("Invalid config term for BPF object"), 7255f634c8eSIan Rogers NULL); 726a34f3be7SWang Nan return -EINVAL; 727a34f3be7SWang Nan } 728a34f3be7SWang Nan 7295d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 730a34f3be7SWang Nan if (err) { 7315f634c8eSIan Rogers char errbuf[BUFSIZ]; 7325f634c8eSIan Rogers int idx; 7335f634c8eSIan Rogers 7345d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 735a34f3be7SWang Nan &error_pos, err, errbuf, 736a34f3be7SWang Nan sizeof(errbuf)); 7375f634c8eSIan Rogers 7385f634c8eSIan Rogers if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 7395f634c8eSIan Rogers idx = term->err_val; 7405f634c8eSIan Rogers else 7415f634c8eSIan Rogers idx = term->err_term + error_pos; 7425f634c8eSIan Rogers 7436c191289SIan Rogers parse_events_error__handle(parse_state->error, idx, 7445f634c8eSIan Rogers strdup(errbuf), 7455f634c8eSIan Rogers strdup( 7467630b3e2SWang Nan "Hint:\tValid config terms:\n" 747e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 748e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7497630b3e2SWang Nan "\n" 750e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 7515f634c8eSIan Rogers " \t(add -v to see detail)")); 752a34f3be7SWang Nan return err; 753a34f3be7SWang Nan } 754a34f3be7SWang Nan } 755a34f3be7SWang Nan return 0; 756a34f3be7SWang Nan } 757a34f3be7SWang Nan 75895088a59SWang Nan /* 75995088a59SWang Nan * Split config terms: 76095088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 76195088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 76295088a59SWang Nan * events in bpf.c. 76395088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 76495088a59SWang Nan * with parse_events_config_bpf. 76595088a59SWang Nan * 76695088a59SWang Nan * Move object config terms from the first list to obj_head_config. 76795088a59SWang Nan */ 76895088a59SWang Nan static void 76995088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 77095088a59SWang Nan struct list_head *obj_head_config) 77195088a59SWang Nan { 77295088a59SWang Nan struct parse_events_term *term, *temp; 77395088a59SWang Nan 77495088a59SWang Nan /* 7754d39c89fSIngo Molnar * Currently, all possible user config term 77695088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 7774d39c89fSIngo Molnar * happens to be a good flag. 77895088a59SWang Nan * 77995088a59SWang Nan * See parse_events_config_bpf() and 78095088a59SWang Nan * config_term_tracepoint(). 78195088a59SWang Nan */ 78295088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 78395088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 78495088a59SWang Nan list_move_tail(&term->list, obj_head_config); 78595088a59SWang Nan } 78695088a59SWang Nan 7875d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 78884c86ca1SWang Nan struct list_head *list, 789d509db04SWang Nan char *bpf_file_name, 790a34f3be7SWang Nan bool source, 791a34f3be7SWang Nan struct list_head *head_config) 79284c86ca1SWang Nan { 793a34f3be7SWang Nan int err; 79495088a59SWang Nan struct bpf_object *obj; 79595088a59SWang Nan LIST_HEAD(obj_head_config); 79695088a59SWang Nan 79795088a59SWang Nan if (head_config) 79895088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 79984c86ca1SWang Nan 800d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8016371ca3bSWang Nan if (IS_ERR(obj)) { 80284c86ca1SWang Nan char errbuf[BUFSIZ]; 80384c86ca1SWang Nan 8046371ca3bSWang Nan err = PTR_ERR(obj); 80584c86ca1SWang Nan 80684c86ca1SWang Nan if (err == -ENOTSUP) 80784c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 80884c86ca1SWang Nan "BPF support is not compiled"); 80984c86ca1SWang Nan else 810d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 811d3e0ce39SWang Nan source, 812d3e0ce39SWang Nan -err, errbuf, 813d3e0ce39SWang Nan sizeof(errbuf)); 81484c86ca1SWang Nan 8156c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 8165f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 81784c86ca1SWang Nan return err; 81884c86ca1SWang Nan } 81984c86ca1SWang Nan 8205d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 821a34f3be7SWang Nan if (err) 822a34f3be7SWang Nan return err; 8235d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 82495088a59SWang Nan 82595088a59SWang Nan /* 82695088a59SWang Nan * Caller doesn't know anything about obj_head_config, 8274d39c89fSIngo Molnar * so combine them together again before returning. 82895088a59SWang Nan */ 82995088a59SWang Nan if (head_config) 83095088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 83195088a59SWang Nan return err; 83284c86ca1SWang Nan } 833c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT 834c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 835c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 836c18cf78dSArnaldo Carvalho de Melo struct bpf_object *obj __maybe_unused, 837c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 838c18cf78dSArnaldo Carvalho de Melo { 8396c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 840c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 841c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 842c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 843c18cf78dSArnaldo Carvalho de Melo } 844c18cf78dSArnaldo Carvalho de Melo 845c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 846c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 847c18cf78dSArnaldo Carvalho de Melo char *bpf_file_name __maybe_unused, 848c18cf78dSArnaldo Carvalho de Melo bool source __maybe_unused, 849c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 850c18cf78dSArnaldo Carvalho de Melo { 8516c191289SIan Rogers parse_events_error__handle(parse_state->error, 0, 852c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 853c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 854c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 855c18cf78dSArnaldo Carvalho de Melo } 856c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT 85784c86ca1SWang Nan 85889812fc8SJiri Olsa static int 85989812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8601b290d67SFrederic Weisbecker { 8611b290d67SFrederic Weisbecker int i; 8621b290d67SFrederic Weisbecker 8631b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 86489812fc8SJiri Olsa if (!type || !type[i]) 8651b290d67SFrederic Weisbecker break; 8661b290d67SFrederic Weisbecker 8677582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8687582732fSJiri Olsa do { \ 8697582732fSJiri Olsa if (attr->bp_type & bit) \ 8707582732fSJiri Olsa return -EINVAL; \ 8717582732fSJiri Olsa else \ 8727582732fSJiri Olsa attr->bp_type |= bit; \ 8737582732fSJiri Olsa } while (0) 8747582732fSJiri Olsa 8751b290d67SFrederic Weisbecker switch (type[i]) { 8761b290d67SFrederic Weisbecker case 'r': 8777582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8781b290d67SFrederic Weisbecker break; 8791b290d67SFrederic Weisbecker case 'w': 8807582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8811b290d67SFrederic Weisbecker break; 8821b290d67SFrederic Weisbecker case 'x': 8837582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8841b290d67SFrederic Weisbecker break; 8851b290d67SFrederic Weisbecker default: 88689812fc8SJiri Olsa return -EINVAL; 8871b290d67SFrederic Weisbecker } 8881b290d67SFrederic Weisbecker } 88989812fc8SJiri Olsa 8907582732fSJiri Olsa #undef CHECK_SET_TYPE 8917582732fSJiri Olsa 8921b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8931b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8941b290d67SFrederic Weisbecker 89589812fc8SJiri Olsa return 0; 8961b290d67SFrederic Weisbecker } 8971b290d67SFrederic Weisbecker 898c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 899aa98d848SIan Rogers u64 addr, char *type, u64 len) 9001b290d67SFrederic Weisbecker { 90189812fc8SJiri Olsa struct perf_event_attr attr; 9021b290d67SFrederic Weisbecker 90389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 904aa98d848SIan Rogers attr.bp_addr = addr; 9051b290d67SFrederic Weisbecker 90689812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 90789812fc8SJiri Olsa return -EINVAL; 9081b290d67SFrederic Weisbecker 9093741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 9103741eb9fSJacob Shin if (!len) { 91189812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 9123741eb9fSJacob Shin len = sizeof(long); 913aa59a485SFrederic Weisbecker else 9143741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 9153741eb9fSJacob Shin } 9163741eb9fSJacob Shin 9173741eb9fSJacob Shin attr.bp_len = len; 918aa59a485SFrederic Weisbecker 91989812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 9204a841d65SJovi Zhang attr.sample_period = 1; 9211b290d67SFrederic Weisbecker 9222b62b3a6SIan Rogers return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL, 9232b62b3a6SIan Rogers /*config_terms=*/NULL); 9241b290d67SFrederic Weisbecker } 9251b290d67SFrederic Weisbecker 9263b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 9273b0e371cSJiri Olsa struct parse_events_error *err, 9283b0e371cSJiri Olsa int type) 9293b0e371cSJiri Olsa { 9303b0e371cSJiri Olsa if (type == term->type_val) 9313b0e371cSJiri Olsa return 0; 9323b0e371cSJiri Olsa 9333b0e371cSJiri Olsa if (err) { 9346c191289SIan Rogers parse_events_error__handle(err, term->err_val, 935448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 936448d732cSIan Rogers ? strdup("expected numeric value") 937448d732cSIan Rogers : strdup("expected string value"), 938448d732cSIan Rogers NULL); 9393b0e371cSJiri Olsa } 9403b0e371cSJiri Olsa return -EINVAL; 9413b0e371cSJiri Olsa } 9423b0e371cSJiri Olsa 94317cb5f84SWang Nan /* 94417cb5f84SWang Nan * Update according to parse-events.l 94517cb5f84SWang Nan */ 94617cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 94717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 94817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 94917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 95017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 95117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 95217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 95317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 95417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 95517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 95617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 95717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 95817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 95917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 960792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 9612fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 962626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 963626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 964dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 965064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 9661b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 967eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 9682b62b3a6SIan Rogers [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id", 96917cb5f84SWang Nan }; 97017cb5f84SWang Nan 9711669e509SWang Nan static bool config_term_shrinked; 9721669e509SWang Nan 9731669e509SWang Nan static bool 9741669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9751669e509SWang Nan { 976448d732cSIan Rogers char *err_str; 977448d732cSIan Rogers 9781669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9796c191289SIan Rogers parse_events_error__handle(err, -1, 980448d732cSIan Rogers strdup("Invalid term_type"), NULL); 9811669e509SWang Nan return false; 9821669e509SWang Nan } 9831669e509SWang Nan if (!config_term_shrinked) 9841669e509SWang Nan return true; 9851669e509SWang Nan 9861669e509SWang Nan switch (term_type) { 9871669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9881669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9891669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9901669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 9912b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 99206835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 993064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 9941669e509SWang Nan return true; 9951669e509SWang Nan default: 9961669e509SWang Nan if (!err) 9971669e509SWang Nan return false; 9981669e509SWang Nan 9991669e509SWang Nan /* term_type is validated so indexing is safe */ 1000448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 1001448d732cSIan Rogers config_term_names[term_type]) >= 0) 10026c191289SIan Rogers parse_events_error__handle(err, -1, err_str, NULL); 10031669e509SWang Nan return false; 10041669e509SWang Nan } 10051669e509SWang Nan } 10061669e509SWang Nan 10071669e509SWang Nan void parse_events__shrink_config_terms(void) 10081669e509SWang Nan { 10091669e509SWang Nan config_term_shrinked = true; 10101669e509SWang Nan } 10111669e509SWang Nan 10120b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 10133b0e371cSJiri Olsa struct parse_events_term *term, 10143b0e371cSJiri Olsa struct parse_events_error *err) 10158f707d84SJiri Olsa { 101616fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 101716fa7e82SJiri Olsa do { \ 10183b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 101916fa7e82SJiri Olsa return -EINVAL; \ 102016fa7e82SJiri Olsa } while (0) 102116fa7e82SJiri Olsa 102216fa7e82SJiri Olsa switch (term->type_term) { 10238f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 102416fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10258f707d84SJiri Olsa attr->config = term->val.num; 10268f707d84SJiri Olsa break; 10278f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 102816fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10298f707d84SJiri Olsa attr->config1 = term->val.num; 10308f707d84SJiri Olsa break; 10318f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 103216fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10338f707d84SJiri Olsa attr->config2 = term->val.num; 10348f707d84SJiri Olsa break; 10358f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 103616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10378f707d84SJiri Olsa break; 103809af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 103909af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 104009af2a55SNamhyung Kim break; 10418f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1042ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1043ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1044448d732cSIan Rogers parse_branch_str(term->val.str, 1045448d732cSIan Rogers &attr->branch_sample_type)) { 10466c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1047448d732cSIan Rogers strdup("invalid branch sample type"), 1048448d732cSIan Rogers NULL); 1049ac12f676SAndi Kleen return -EINVAL; 1050ac12f676SAndi Kleen } 10518f707d84SJiri Olsa break; 105232067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 105332067712SKan Liang CHECK_TYPE_VAL(NUM); 105432067712SKan Liang if (term->val.num > 1) { 10556c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1056448d732cSIan Rogers strdup("expected 0 or 1"), 1057448d732cSIan Rogers NULL); 105832067712SKan Liang return -EINVAL; 105932067712SKan Liang } 106032067712SKan Liang break; 1061d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1062d457c963SKan Liang CHECK_TYPE_VAL(STR); 1063d457c963SKan Liang break; 1064d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1065d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1066d457c963SKan Liang break; 1067374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1068374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1069374ce938SWang Nan break; 1070374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1071374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1072374ce938SWang Nan break; 1073626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1074626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1075626a6b78SWang Nan break; 1076626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1077626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1078626a6b78SWang Nan break; 10796b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10806b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10816b5fc39bSJiri Olsa break; 10822b62b3a6SIan Rogers case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 10832b62b3a6SIan Rogers CHECK_TYPE_VAL(STR); 10842b62b3a6SIan Rogers break; 1085792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1086792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1087792d48b4SArnaldo Carvalho de Melo break; 10882fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 10892fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 10902fda5adaSArnaldo Carvalho de Melo break; 1091064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1092064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1093064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 10946c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1095448d732cSIan Rogers strdup("expected 0 or 1"), 1096448d732cSIan Rogers NULL); 1097064b4e82SJin Yao return -EINVAL; 1098064b4e82SJin Yao } 1099064b4e82SJin Yao break; 11001b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 11011b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 11021b992154SAdrian Hunter break; 1103eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1104eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 1105eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 11066c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1107eb7a52d4SAdrian Hunter strdup("too big"), 1108eb7a52d4SAdrian Hunter NULL); 1109eb7a52d4SAdrian Hunter return -EINVAL; 1110eb7a52d4SAdrian Hunter } 1111eb7a52d4SAdrian Hunter break; 11128f707d84SJiri Olsa default: 11136c191289SIan Rogers parse_events_error__handle(err, term->err_term, 1114448d732cSIan Rogers strdup("unknown term"), 1115448d732cSIan Rogers parse_events_formats_error_string(NULL)); 11168f707d84SJiri Olsa return -EINVAL; 11178f707d84SJiri Olsa } 111816fa7e82SJiri Olsa 11191669e509SWang Nan /* 11204d39c89fSIngo Molnar * Check term availability after basic checking so 11211669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 11221669e509SWang Nan * 11234d39c89fSIngo Molnar * If check availability at the entry of this function, 11241669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 11251669e509SWang Nan * if an invalid config term is provided for legacy events 11261669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 11271669e509SWang Nan */ 11281669e509SWang Nan if (!config_term_avail(term->type_term, err)) 11291669e509SWang Nan return -EINVAL; 11308f707d84SJiri Olsa return 0; 113116fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 11328f707d84SJiri Olsa } 11338f707d84SJiri Olsa 11340b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 11350b8891a8SHe Kuang struct parse_events_term *term, 11360b8891a8SHe Kuang struct parse_events_error *err) 11370b8891a8SHe Kuang { 1138dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1139dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 11400b8891a8SHe Kuang /* 11410b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 11420b8891a8SHe Kuang * at this point what type they need to have. 11430b8891a8SHe Kuang */ 11440b8891a8SHe Kuang return 0; 11450b8891a8SHe Kuang else 11460b8891a8SHe Kuang return config_term_common(attr, term, err); 11470b8891a8SHe Kuang } 11480b8891a8SHe Kuang 1149378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1150e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1151e637d177SHe Kuang struct parse_events_term *term, 1152e637d177SHe Kuang struct parse_events_error *err) 1153e637d177SHe Kuang { 1154e637d177SHe Kuang switch (term->type_term) { 1155e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1156e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1157374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1158374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1159792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 11602fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1161626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1162626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 11631b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1164eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1165e637d177SHe Kuang return config_term_common(attr, term, err); 1166e637d177SHe Kuang default: 1167e637d177SHe Kuang if (err) { 11686c191289SIan Rogers parse_events_error__handle(err, term->err_term, 1169448d732cSIan Rogers strdup("unknown term"), 1170448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1171e637d177SHe Kuang } 1172e637d177SHe Kuang return -EINVAL; 1173e637d177SHe Kuang } 1174e637d177SHe Kuang 1175e637d177SHe Kuang return 0; 1176e637d177SHe Kuang } 1177378ef0f5SIan Rogers #endif 1178e637d177SHe Kuang 11798f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 11803b0e371cSJiri Olsa struct list_head *head, 11810b8891a8SHe Kuang struct parse_events_error *err, 11820b8891a8SHe Kuang config_term_func_t config_term) 11838f707d84SJiri Olsa { 11846cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 11858f707d84SJiri Olsa 11868f707d84SJiri Olsa list_for_each_entry(term, head, list) 11873b0e371cSJiri Olsa if (config_term(attr, term, err)) 11888f707d84SJiri Olsa return -EINVAL; 11898f707d84SJiri Olsa 11908f707d84SJiri Olsa return 0; 11918f707d84SJiri Olsa } 11928f707d84SJiri Olsa 1193930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1194930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1195930a2e29SJiri Olsa { 119605e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 119735ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *__t; \ 1198930a2e29SJiri Olsa \ 1199930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1200930a2e29SJiri Olsa if (!__t) \ 1201930a2e29SJiri Olsa return -ENOMEM; \ 1202930a2e29SJiri Olsa \ 1203930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 120435ac0cadSArnaldo Carvalho de Melo __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 120505e54e23SIan Rogers __t->weak = __weak; \ 1206e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1207e884602bSLeo Yan 120805e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1209e884602bSLeo Yan do { \ 121005e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1211e884602bSLeo Yan __t->val.__name = __val; \ 1212e884602bSLeo Yan } while (0) 1213e884602bSLeo Yan 121405e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1215e884602bSLeo Yan do { \ 121605e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 12173220fb8dSLeo Yan __t->val.str = strdup(__val); \ 12183220fb8dSLeo Yan if (!__t->val.str) { \ 12193220fb8dSLeo Yan zfree(&__t); \ 12203220fb8dSLeo Yan return -ENOMEM; \ 12213220fb8dSLeo Yan } \ 12223220fb8dSLeo Yan __t->free_str = true; \ 1223930a2e29SJiri Olsa } while (0) 1224930a2e29SJiri Olsa 1225930a2e29SJiri Olsa struct parse_events_term *term; 1226930a2e29SJiri Olsa 1227930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1228930a2e29SJiri Olsa switch (term->type_term) { 1229ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 123005e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 123132067712SKan Liang break; 123209af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 123305e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 123409af2a55SNamhyung Kim break; 123532067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 123605e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 123732067712SKan Liang break; 1238d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 123905e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1240d457c963SKan Liang break; 1241ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 124205e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1243ac12f676SAndi Kleen break; 1244d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1245e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 124605e54e23SIan Rogers term->val.num, term->weak); 1247d457c963SKan Liang break; 1248374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1249e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 125005e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1251374ce938SWang Nan break; 1252374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1253e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 125405e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1255374ce938SWang Nan break; 1256792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1257e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 125805e54e23SIan Rogers term->val.num, term->weak); 1259792d48b4SArnaldo Carvalho de Melo break; 12602fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1261e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 126205e54e23SIan Rogers term->val.num, term->weak); 12632fda5adaSArnaldo Carvalho de Melo break; 1264626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1265e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 126605e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1267626a6b78SWang Nan break; 1268626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1269e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 127005e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1271626a6b78SWang Nan break; 1272dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 127305e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1274dd60fba7SMathieu Poirier break; 1275064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1276e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 127705e54e23SIan Rogers term->val.num ? true : false, term->weak); 1278064b4e82SJin Yao break; 12791b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1280e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 128105e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 12821b992154SAdrian Hunter break; 1283eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1284e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 128505e54e23SIan Rogers term->val.num, term->weak); 1286eb7a52d4SAdrian Hunter break; 1287930a2e29SJiri Olsa default: 1288930a2e29SJiri Olsa break; 1289930a2e29SJiri Olsa } 1290930a2e29SJiri Olsa } 1291a1ac7de6SAdrian Hunter return 0; 1292a1ac7de6SAdrian Hunter } 1293a1ac7de6SAdrian Hunter 1294a1ac7de6SAdrian Hunter /* 129535ac0cadSArnaldo Carvalho de Melo * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1296a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1297a1ac7de6SAdrian Hunter */ 1298a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1299a1ac7de6SAdrian Hunter struct list_head *head_terms) 1300a1ac7de6SAdrian Hunter { 1301a1ac7de6SAdrian Hunter struct parse_events_term *term; 1302a1ac7de6SAdrian Hunter u64 bits = 0; 1303a1ac7de6SAdrian Hunter int type; 1304a1ac7de6SAdrian Hunter 1305a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1306a1ac7de6SAdrian Hunter switch (term->type_term) { 1307a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 1308a1ac7de6SAdrian Hunter type = perf_pmu__format_type(&pmu->format, term->config); 1309a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1310a1ac7de6SAdrian Hunter continue; 1311a1ac7de6SAdrian Hunter bits |= perf_pmu__format_bits(&pmu->format, term->config); 1312a1ac7de6SAdrian Hunter break; 1313a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1314a1ac7de6SAdrian Hunter bits = ~(u64)0; 1315a1ac7de6SAdrian Hunter break; 1316a1ac7de6SAdrian Hunter default: 1317a1ac7de6SAdrian Hunter break; 1318a1ac7de6SAdrian Hunter } 1319a1ac7de6SAdrian Hunter } 1320a1ac7de6SAdrian Hunter 1321a1ac7de6SAdrian Hunter if (bits) 132205e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1323a1ac7de6SAdrian Hunter 1324a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1325930a2e29SJiri Olsa return 0; 1326930a2e29SJiri Olsa } 1327930a2e29SJiri Olsa 1328e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 13298c619d6aSWang Nan const char *sys, const char *event, 1330272ed29aSJiri Olsa struct parse_events_error *err, 1331e637d177SHe Kuang struct list_head *head_config) 1332e637d177SHe Kuang { 1333378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1334e637d177SHe Kuang if (head_config) { 1335e637d177SHe Kuang struct perf_event_attr attr; 1336e637d177SHe Kuang 1337272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1338e637d177SHe Kuang config_term_tracepoint)) 1339e637d177SHe Kuang return -EINVAL; 1340e637d177SHe Kuang } 1341e637d177SHe Kuang 1342e637d177SHe Kuang if (strpbrk(sys, "*?")) 1343e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1344272ed29aSJiri Olsa err, head_config); 1345e637d177SHe Kuang else 1346e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1347272ed29aSJiri Olsa err, head_config); 1348378ef0f5SIan Rogers #else 1349378ef0f5SIan Rogers (void)list; 1350378ef0f5SIan Rogers (void)idx; 1351378ef0f5SIan Rogers (void)sys; 1352378ef0f5SIan Rogers (void)event; 1353378ef0f5SIan Rogers (void)head_config; 1354378ef0f5SIan Rogers parse_events_error__handle(err, 0, strdup("unsupported tracepoint"), 1355378ef0f5SIan Rogers strdup("libtraceevent is necessary for tracepoint support")); 1356378ef0f5SIan Rogers return -1; 1357378ef0f5SIan Rogers #endif 1358e637d177SHe Kuang } 1359e637d177SHe Kuang 13605d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 136187d650beSJiri Olsa struct list_head *list, 1362b527bab5SRobert Richter u32 type, u64 config, 13638f707d84SJiri Olsa struct list_head *head_config) 136474d5b588SJaswinder Singh Rajput { 136589812fc8SJiri Olsa struct perf_event_attr attr; 1366930a2e29SJiri Olsa LIST_HEAD(config_terms); 13672b62b3a6SIan Rogers const char *name, *metric_id; 13689cbfa2f6SJin Yao bool hybrid; 13699cbfa2f6SJin Yao int ret; 137074d5b588SJaswinder Singh Rajput 137189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 137289812fc8SJiri Olsa attr.type = type; 137389812fc8SJiri Olsa attr.config = config; 13748f707d84SJiri Olsa 1375930a2e29SJiri Olsa if (head_config) { 13765d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 13770b8891a8SHe Kuang config_term_common)) 13788f707d84SJiri Olsa return -EINVAL; 13798f707d84SJiri Olsa 1380930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1381930a2e29SJiri Olsa return -ENOMEM; 1382930a2e29SJiri Olsa } 1383930a2e29SJiri Olsa 13842b62b3a6SIan Rogers name = get_config_name(head_config); 13852b62b3a6SIan Rogers metric_id = get_config_metric_id(head_config); 13869cbfa2f6SJin Yao ret = parse_events__add_numeric_hybrid(parse_state, list, &attr, 13872b62b3a6SIan Rogers name, metric_id, 13889cbfa2f6SJin Yao &config_terms, &hybrid); 13899cbfa2f6SJin Yao if (hybrid) 139099fc5941SAdrian Hunter goto out_free_terms; 13919cbfa2f6SJin Yao 13922b62b3a6SIan Rogers ret = add_event(list, &parse_state->idx, &attr, name, metric_id, 13932b62b3a6SIan Rogers &config_terms); 139499fc5941SAdrian Hunter out_free_terms: 139599fc5941SAdrian Hunter free_config_terms(&config_terms); 139699fc5941SAdrian Hunter return ret; 1397b908debdSIngo Molnar } 1398b908debdSIngo Molnar 1399f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1400f0fbb114SAndi Kleen struct list_head *list, 14018228e936SIan Rogers int tool_event) 1402f0fbb114SAndi Kleen { 1403f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1404f0fbb114SAndi Kleen } 1405f0fbb114SAndi Kleen 1406064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1407064b4e82SJin Yao { 140835ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *term; 1409064b4e82SJin Yao 1410064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 141135ac0cadSArnaldo Carvalho de Melo if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1412064b4e82SJin Yao return term->val.percore; 1413064b4e82SJin Yao } 1414064b4e82SJin Yao 1415064b4e82SJin Yao return false; 1416064b4e82SJin Yao } 1417064b4e82SJin Yao 14185e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state, 14195e4edd1fSJin Yao struct list_head *list, char *name, 14205e4edd1fSJin Yao struct list_head *head_config) 14215e4edd1fSJin Yao { 14225e4edd1fSJin Yao struct parse_events_term *term; 14235e4edd1fSJin Yao int ret = -1; 14245e4edd1fSJin Yao 14255e4edd1fSJin Yao if (parse_state->fake_pmu || !head_config || list_empty(head_config) || 14265e4edd1fSJin Yao !perf_pmu__is_hybrid(name)) { 14275e4edd1fSJin Yao return -1; 14285e4edd1fSJin Yao } 14295e4edd1fSJin Yao 14305e4edd1fSJin Yao /* 14315e4edd1fSJin Yao * More than one term in list. 14325e4edd1fSJin Yao */ 14335e4edd1fSJin Yao if (head_config->next && head_config->next->next != head_config) 14345e4edd1fSJin Yao return -1; 14355e4edd1fSJin Yao 14365e4edd1fSJin Yao term = list_first_entry(head_config, struct parse_events_term, list); 14375e4edd1fSJin Yao if (term && term->config && strcmp(term->config, "event")) { 14385e4edd1fSJin Yao ret = parse_events__with_hybrid_pmu(parse_state, term->config, 14395e4edd1fSJin Yao name, list); 14405e4edd1fSJin Yao } 14415e4edd1fSJin Yao 14425e4edd1fSJin Yao return ret; 14435e4edd1fSJin Yao } 14445e4edd1fSJin Yao 1445c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 144636adec85SJiri Olsa struct list_head *list, char *name, 14473cdc5c2cSKan Liang struct list_head *head_config, 14483cdc5c2cSKan Liang bool auto_merge_stats, 14493cdc5c2cSKan Liang bool use_alias) 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; 14563cdc5c2cSKan Liang bool use_uncore_alias; 1457930a2e29SJiri Olsa LIST_HEAD(config_terms); 14585f537a26SJiri Olsa 1459f034fc50SAdrian Hunter pmu = parse_state->fake_pmu ?: perf_pmu__find(name); 1460f034fc50SAdrian Hunter 1461f034fc50SAdrian Hunter if (verbose > 1 && !(pmu && pmu->selectable)) { 14624ac22b48SIan Rogers fprintf(stderr, "Attempting to add event pmu '%s' with '", 14634ac22b48SIan Rogers name); 14644ac22b48SIan Rogers if (head_config) { 14654ac22b48SIan Rogers struct parse_events_term *term; 14664ac22b48SIan Rogers 14674ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 14684ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 14694ac22b48SIan Rogers } 14704ac22b48SIan Rogers } 14714ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 14724ac22b48SIan Rogers } 14734ac22b48SIan Rogers 1474333b5665SAndi Kleen if (!pmu) { 1475448d732cSIan Rogers char *err_str; 1476448d732cSIan Rogers 1477448d732cSIan Rogers if (asprintf(&err_str, 1478333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1479448d732cSIan Rogers name) >= 0) 14806c191289SIan Rogers parse_events_error__handle(err, 0, err_str, NULL); 14815f537a26SJiri Olsa return -EINVAL; 1482333b5665SAndi Kleen } 14835f537a26SJiri Olsa 1484dc0a6202SAdrian Hunter if (pmu->default_config) { 1485dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1486dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1487dc0a6202SAdrian Hunter } else { 14885f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1489dc0a6202SAdrian Hunter } 14905f537a26SJiri Olsa 14913cdc5c2cSKan Liang use_uncore_alias = (pmu->is_uncore && use_alias); 14923cdc5c2cSKan Liang 1493ad962273SAdrian Hunter if (!head_config) { 1494ad962273SAdrian Hunter attr.type = pmu->type; 14952b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, 14962b62b3a6SIan Rogers /*init_attr=*/true, /*name=*/NULL, 14972b62b3a6SIan Rogers /*metric_id=*/NULL, pmu, 14982b62b3a6SIan Rogers /*config_terms=*/NULL, auto_merge_stats, 14992b62b3a6SIan Rogers /*cpu_list=*/NULL); 15008c5421c0SAgustin Vega-Frias if (evsel) { 1501d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 15023cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 15038c5421c0SAgustin Vega-Frias return 0; 15048c5421c0SAgustin Vega-Frias } else { 15058c5421c0SAgustin Vega-Frias return -ENOMEM; 15068c5421c0SAgustin Vega-Frias } 1507ad962273SAdrian Hunter } 1508ad962273SAdrian Hunter 1509387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) 1510a6146d50SZheng Yan return -EINVAL; 1511a6146d50SZheng Yan 15124ac22b48SIan Rogers if (verbose > 1) { 15134ac22b48SIan Rogers fprintf(stderr, "After aliases, add event pmu '%s' with '", 15144ac22b48SIan Rogers name); 15154ac22b48SIan Rogers if (head_config) { 15164ac22b48SIan Rogers struct parse_events_term *term; 15174ac22b48SIan Rogers 15184ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15194ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15204ac22b48SIan Rogers } 15214ac22b48SIan Rogers } 15224ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15234ac22b48SIan Rogers } 15244ac22b48SIan Rogers 15255f537a26SJiri Olsa /* 15265f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 15275f537a26SJiri Olsa * return value when called with fail == 0 ;) 15285f537a26SJiri Olsa */ 15295d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1530c056ba6aSJiri Olsa return -EINVAL; 15315f537a26SJiri Olsa 1532930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1533930a2e29SJiri Olsa return -ENOMEM; 1534930a2e29SJiri Olsa 1535a1ac7de6SAdrian Hunter /* 1536a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1537a1ac7de6SAdrian Hunter * changed by the user. 1538a1ac7de6SAdrian Hunter */ 1539a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1540a1ac7de6SAdrian Hunter return -ENOMEM; 1541a1ac7de6SAdrian Hunter 15425e4edd1fSJin Yao if (!parse_events__inside_hybrid_pmu(parse_state, list, name, 15435e4edd1fSJin Yao head_config)) { 15445e4edd1fSJin Yao return 0; 15455e4edd1fSJin Yao } 15465e4edd1fSJin Yao 1547387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 1548a7d212fcSAdrian Hunter free_config_terms(&config_terms); 15495f537a26SJiri Olsa return -EINVAL; 155038f2c422SIan Rogers } 15515f537a26SJiri Olsa 15522b62b3a6SIan Rogers evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true, 15532b62b3a6SIan Rogers get_config_name(head_config), 15542b62b3a6SIan Rogers get_config_metric_id(head_config), pmu, 15552b62b3a6SIan Rogers &config_terms, auto_merge_stats, /*cpu_list=*/NULL); 155633321a06SIan Rogers if (!evsel) 155733321a06SIan Rogers return -ENOMEM; 155833321a06SIan Rogers 155912279429SJin Yao if (evsel->name) 156012279429SJin Yao evsel->use_config_name = true; 156112279429SJin Yao 156233321a06SIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 156333321a06SIan Rogers evsel->use_uncore_alias = use_uncore_alias; 156433321a06SIan Rogers evsel->percore = config_term_percore(&evsel->config_terms); 156533321a06SIan Rogers 156633321a06SIan Rogers if (parse_state->fake_pmu) 156733321a06SIan Rogers return 0; 156833321a06SIan Rogers 1569b194c9cdSIan Rogers free((char *)evsel->unit); 1570b194c9cdSIan Rogers evsel->unit = strdup(info.unit); 157146441bdcSMatt Fleming evsel->scale = info.scale; 1572044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 15731d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 157433321a06SIan Rogers return 0; 15755f537a26SJiri Olsa } 15765f537a26SJiri Olsa 15775d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 1578fb081153SIan Rogers char *str, struct list_head *head, 1579fb081153SIan Rogers struct list_head **listp) 15802073ad33SAndi Kleen { 15812073ad33SAndi Kleen struct parse_events_term *term; 1582fb081153SIan Rogers struct list_head *list = NULL; 158391c9923aSZhengjun Xing struct list_head *orig_head = NULL; 15842073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 15852073ad33SAndi Kleen int ok = 0; 1586fb081153SIan Rogers char *config; 15872073ad33SAndi Kleen 15882073ad33SAndi Kleen *listp = NULL; 1589fb081153SIan Rogers 1590fb081153SIan Rogers if (!head) { 1591fb081153SIan Rogers head = malloc(sizeof(struct list_head)); 1592fb081153SIan Rogers if (!head) 1593fb081153SIan Rogers goto out_err; 1594fb081153SIan Rogers 1595fb081153SIan Rogers INIT_LIST_HEAD(head); 1596fb081153SIan Rogers } 1597fb081153SIan Rogers config = strdup(str); 1598fb081153SIan Rogers if (!config) 1599fb081153SIan Rogers goto out_err; 1600fb081153SIan Rogers 1601fb081153SIan Rogers if (parse_events_term__num(&term, 1602fb081153SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 1603fb081153SIan Rogers config, 1, false, &config, 1604fb081153SIan Rogers NULL) < 0) { 1605fb081153SIan Rogers free(config); 1606fb081153SIan Rogers goto out_err; 1607fb081153SIan Rogers } 1608fb081153SIan Rogers list_add_tail(&term->list, head); 1609fb081153SIan Rogers 16102073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 16112073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 16122073ad33SAndi Kleen if (!list) 1613fb081153SIan Rogers goto out_err; 1614fb081153SIan Rogers 16152073ad33SAndi Kleen INIT_LIST_HEAD(list); 1616fb081153SIan Rogers 16172073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 16182073ad33SAndi Kleen struct perf_pmu_alias *alias; 16192073ad33SAndi Kleen 16202073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 16212073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 162291c9923aSZhengjun Xing parse_events_copy_term_list(head, &orig_head); 1623c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 162491c9923aSZhengjun Xing pmu->name, orig_head, 16253cdc5c2cSKan Liang true, true)) { 1626d4953f7eSIan Rogers pr_debug("%s -> %s/%s/\n", str, 16272073ad33SAndi Kleen pmu->name, alias->str); 16282073ad33SAndi Kleen ok++; 16292073ad33SAndi Kleen } 163091c9923aSZhengjun Xing parse_events_terms__delete(orig_head); 1631fb081153SIan Rogers } 1632fb081153SIan Rogers } 1633fb081153SIan Rogers } 1634b4a7276cSJohn Garry 1635b4a7276cSJohn Garry if (parse_state->fake_pmu) { 1636b4a7276cSJohn Garry if (!parse_events_add_pmu(parse_state, list, str, head, 1637b4a7276cSJohn Garry true, true)) { 1638b4a7276cSJohn Garry pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str); 1639b4a7276cSJohn Garry ok++; 1640b4a7276cSJohn Garry } 1641b4a7276cSJohn Garry } 1642b4a7276cSJohn Garry 1643fb081153SIan Rogers out_err: 1644fb081153SIan Rogers if (ok) 1645fb081153SIan Rogers *listp = list; 1646fb081153SIan Rogers else 1647fb081153SIan Rogers free(list); 16482073ad33SAndi Kleen 16492073ad33SAndi Kleen parse_events_terms__delete(head); 1650fb081153SIan Rogers return ok ? 0 : -1; 16512073ad33SAndi Kleen } 16522073ad33SAndi Kleen 16536a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 16546a4bb04cSJiri Olsa char *event_mod) 165589efb029SJiri Olsa { 16566a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 16576a4bb04cSJiri Olsa } 16586a4bb04cSJiri Olsa 1659*347c2f0aSIan Rogers void parse_events__set_leader(char *name, struct list_head *list) 16606a4bb04cSJiri Olsa { 166132dcd021SJiri Olsa struct evsel *leader; 16626a4bb04cSJiri Olsa 1663854f7363SWang Nan if (list_empty(list)) { 1664854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1665854f7363SWang Nan return; 1666854f7363SWang Nan } 1667854f7363SWang Nan 1668*347c2f0aSIan Rogers leader = list_first_entry(list, struct evsel, core.node); 1669ecdcf630SIan Rogers __perf_evlist__set_leader(list, &leader->core); 16704bb311b2SIan Rogers leader->group_name = name; 167189efb029SJiri Olsa } 167289efb029SJiri Olsa 1673c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 16745d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 16755d7be90eSJiri Olsa struct list_head *list_all) 16765d7be90eSJiri Olsa { 16775d7be90eSJiri Olsa /* 16785d7be90eSJiri Olsa * Called for single event definition. Update the 167989efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 16805d7be90eSJiri Olsa * list, for next event definition. 16815d7be90eSJiri Olsa */ 16825d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1683b847cbdcSJiri Olsa free(list_event); 16845d7be90eSJiri Olsa } 16855d7be90eSJiri Olsa 1686f5b1135bSJiri Olsa struct event_modifier { 1687f5b1135bSJiri Olsa int eu; 1688f5b1135bSJiri Olsa int ek; 1689f5b1135bSJiri Olsa int eh; 1690f5b1135bSJiri Olsa int eH; 1691f5b1135bSJiri Olsa int eG; 1692a1e12da4SJiri Olsa int eI; 1693f5b1135bSJiri Olsa int precise; 16947f94af7aSJiri Olsa int precise_max; 1695f5b1135bSJiri Olsa int exclude_GH; 16963c176311SJiri Olsa int sample_read; 1697e9a7c414SMichael Ellerman int pinned; 16985a5dfe4bSAndi Kleen int weak; 16990997a266SAndi Kleen int exclusive; 170001bd8efcSSong Liu int bpf_counter; 1701f5b1135bSJiri Olsa }; 170261c45981SPaul Mackerras 1703f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 170432dcd021SJiri Olsa struct evsel *evsel) 1705f5b1135bSJiri Olsa { 17061fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 17071fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 17081fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 17091fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 17101fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 17111fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 17121fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 17137f94af7aSJiri Olsa int precise_max = 0; 17143c176311SJiri Olsa int sample_read = 0; 17151fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 17160997a266SAndi Kleen int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1717f5b1135bSJiri Olsa 1718f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1719f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 17205a5dfe4bSAndi Kleen int weak = 0; 172101bd8efcSSong Liu int bpf_counter = 0; 1722f5b1135bSJiri Olsa 1723f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1724ceb53fbfSIngo Molnar 172561c45981SPaul Mackerras while (*str) { 1726ab608344SPeter Zijlstra if (*str == 'u') { 1727ab608344SPeter Zijlstra if (!exclude) 1728ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 1729943b69acSJin Yao if (!exclude_GH && !perf_guest) 1730943b69acSJin Yao eG = 1; 173161c45981SPaul Mackerras eu = 0; 1732ab608344SPeter Zijlstra } else if (*str == 'k') { 1733ab608344SPeter Zijlstra if (!exclude) 1734ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 173561c45981SPaul Mackerras ek = 0; 1736ab608344SPeter Zijlstra } else if (*str == 'h') { 1737ab608344SPeter Zijlstra if (!exclude) 1738ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 173961c45981SPaul Mackerras eh = 0; 174099320cc8SJoerg Roedel } else if (*str == 'G') { 174199320cc8SJoerg Roedel if (!exclude_GH) 174299320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 174399320cc8SJoerg Roedel eG = 0; 174499320cc8SJoerg Roedel } else if (*str == 'H') { 174599320cc8SJoerg Roedel if (!exclude_GH) 174699320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 174799320cc8SJoerg Roedel eH = 0; 1748a1e12da4SJiri Olsa } else if (*str == 'I') { 1749a1e12da4SJiri Olsa eI = 1; 1750ab608344SPeter Zijlstra } else if (*str == 'p') { 1751ab608344SPeter Zijlstra precise++; 17521342798cSDavid Ahern /* use of precise requires exclude_guest */ 17531342798cSDavid Ahern if (!exclude_GH) 17541342798cSDavid Ahern eG = 1; 17557f94af7aSJiri Olsa } else if (*str == 'P') { 17567f94af7aSJiri Olsa precise_max = 1; 17573c176311SJiri Olsa } else if (*str == 'S') { 17583c176311SJiri Olsa sample_read = 1; 1759e9a7c414SMichael Ellerman } else if (*str == 'D') { 1760e9a7c414SMichael Ellerman pinned = 1; 17610997a266SAndi Kleen } else if (*str == 'e') { 17620997a266SAndi Kleen exclusive = 1; 17635a5dfe4bSAndi Kleen } else if (*str == 'W') { 17645a5dfe4bSAndi Kleen weak = 1; 176501bd8efcSSong Liu } else if (*str == 'b') { 176601bd8efcSSong Liu bpf_counter = 1; 1767ab608344SPeter Zijlstra } else 176861c45981SPaul Mackerras break; 1769ab608344SPeter Zijlstra 177061c45981SPaul Mackerras ++str; 177161c45981SPaul Mackerras } 177274d5b588SJaswinder Singh Rajput 177386470930SIngo Molnar /* 177489812fc8SJiri Olsa * precise ip: 177589812fc8SJiri Olsa * 177689812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 177789812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 177889812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 177989812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 178089812fc8SJiri Olsa * 178189812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 178286470930SIngo Molnar */ 178389812fc8SJiri Olsa if (precise > 3) 178489812fc8SJiri Olsa return -EINVAL; 178586470930SIngo Molnar 1786f5b1135bSJiri Olsa mod->eu = eu; 1787f5b1135bSJiri Olsa mod->ek = ek; 1788f5b1135bSJiri Olsa mod->eh = eh; 1789f5b1135bSJiri Olsa mod->eH = eH; 1790f5b1135bSJiri Olsa mod->eG = eG; 1791a1e12da4SJiri Olsa mod->eI = eI; 1792f5b1135bSJiri Olsa mod->precise = precise; 17937f94af7aSJiri Olsa mod->precise_max = precise_max; 1794f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 17953c176311SJiri Olsa mod->sample_read = sample_read; 1796e9a7c414SMichael Ellerman mod->pinned = pinned; 17975a5dfe4bSAndi Kleen mod->weak = weak; 179801bd8efcSSong Liu mod->bpf_counter = bpf_counter; 17990997a266SAndi Kleen mod->exclusive = exclusive; 1800e9a7c414SMichael Ellerman 1801f5b1135bSJiri Olsa return 0; 1802f5b1135bSJiri Olsa } 1803f5b1135bSJiri Olsa 1804534123f4SJiri Olsa /* 1805534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1806534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1807534123f4SJiri Olsa */ 1808534123f4SJiri Olsa static int check_modifier(char *str) 1809534123f4SJiri Olsa { 1810534123f4SJiri Olsa char *p = str; 1811534123f4SJiri Olsa 1812534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 181301bd8efcSSong Liu if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1814534123f4SJiri Olsa return -1; 1815534123f4SJiri Olsa 1816534123f4SJiri Olsa while (*p) { 1817534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1818534123f4SJiri Olsa return -1; 1819534123f4SJiri Olsa p++; 1820534123f4SJiri Olsa } 1821534123f4SJiri Olsa 1822534123f4SJiri Olsa return 0; 1823534123f4SJiri Olsa } 1824534123f4SJiri Olsa 1825f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1826f5b1135bSJiri Olsa { 182732dcd021SJiri Olsa struct evsel *evsel; 1828f5b1135bSJiri Olsa struct event_modifier mod; 1829f5b1135bSJiri Olsa 1830f5b1135bSJiri Olsa if (str == NULL) 1831f5b1135bSJiri Olsa return 0; 1832f5b1135bSJiri Olsa 1833534123f4SJiri Olsa if (check_modifier(str)) 1834534123f4SJiri Olsa return -EINVAL; 1835534123f4SJiri Olsa 1836f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1837f5b1135bSJiri Olsa return -EINVAL; 1838f5b1135bSJiri Olsa 1839e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1840f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1841f5b1135bSJiri Olsa return -EINVAL; 1842f5b1135bSJiri Olsa 18431fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 18441fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 18451fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 18461fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 18471fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 18481fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 18491fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 1850f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 18513c176311SJiri Olsa evsel->sample_read = mod.sample_read; 18527f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 18535a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 185401bd8efcSSong Liu evsel->bpf_counter = mod.bpf_counter; 1855e9a7c414SMichael Ellerman 18560997a266SAndi Kleen if (evsel__is_group_leader(evsel)) { 18571fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 18580997a266SAndi Kleen evsel->core.attr.exclusive = mod.exclusive; 18590997a266SAndi Kleen } 1860ceb53fbfSIngo Molnar } 186186470930SIngo Molnar 186289812fc8SJiri Olsa return 0; 186386470930SIngo Molnar } 186486470930SIngo Molnar 18658e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name) 1866ac2ba9f3SRobert Richter { 186732dcd021SJiri Olsa struct evsel *evsel; 1868ac2ba9f3SRobert Richter 1869e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1870ac2ba9f3SRobert Richter if (!evsel->name) 1871ac2ba9f3SRobert Richter evsel->name = strdup(name); 1872ac2ba9f3SRobert Richter } 1873ac2ba9f3SRobert Richter 1874ac2ba9f3SRobert Richter return 0; 1875ac2ba9f3SRobert Richter } 1876ac2ba9f3SRobert Richter 1877dcb4e102SKan Liang static int 1878dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1879dcb4e102SKan Liang { 1880dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1881dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1882dcb4e102SKan Liang 1883e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1884dcb4e102SKan Liang } 1885dcb4e102SKan Liang 1886dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1887dcb4e102SKan Liang { 1888dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1889dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1890dcb4e102SKan Liang int i; 1891dcb4e102SKan Liang 1892dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1893dcb4e102SKan Liang p = perf_pmu_events_list + i; 1894360e071bSTaeung Song zfree(&p->symbol); 1895dcb4e102SKan Liang } 1896506fde11STaeung Song zfree(&perf_pmu_events_list); 1897dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1898dcb4e102SKan Liang } 1899dcb4e102SKan Liang } 1900dcb4e102SKan Liang 1901dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1902dcb4e102SKan Liang do { \ 1903dcb4e102SKan Liang p->symbol = str; \ 1904dcb4e102SKan Liang if (!p->symbol) \ 1905dcb4e102SKan Liang goto err; \ 1906dcb4e102SKan Liang p->type = stype; \ 1907dcb4e102SKan Liang } while (0) 1908dcb4e102SKan Liang 1909dcb4e102SKan Liang /* 1910dcb4e102SKan Liang * Read the pmu events list from sysfs 1911dcb4e102SKan Liang * Save it into perf_pmu_events_list 1912dcb4e102SKan Liang */ 1913dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1914dcb4e102SKan Liang { 1915dcb4e102SKan Liang 1916dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1917dcb4e102SKan Liang struct perf_pmu_alias *alias; 1918dcb4e102SKan Liang int len = 0; 1919dcb4e102SKan Liang 1920231bb2aaSAndi Kleen pmu = NULL; 1921231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1922dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1923864bc8c9SJohn Garry char *tmp = strchr(alias->name, '-'); 1924864bc8c9SJohn Garry 1925864bc8c9SJohn Garry if (tmp) { 1926864bc8c9SJohn Garry char *tmp2 = NULL; 1927864bc8c9SJohn Garry 1928864bc8c9SJohn Garry tmp2 = strchr(tmp + 1, '-'); 1929dcb4e102SKan Liang len++; 1930864bc8c9SJohn Garry if (tmp2) 1931864bc8c9SJohn Garry len++; 1932864bc8c9SJohn Garry } 1933864bc8c9SJohn Garry 1934dcb4e102SKan Liang len++; 1935dcb4e102SKan Liang } 1936231bb2aaSAndi Kleen } 1937231bb2aaSAndi Kleen 1938231bb2aaSAndi Kleen if (len == 0) { 1939231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1940231bb2aaSAndi Kleen return; 1941231bb2aaSAndi Kleen } 1942dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1943dcb4e102SKan Liang if (!perf_pmu_events_list) 1944dcb4e102SKan Liang return; 1945dcb4e102SKan Liang perf_pmu_events_list_num = len; 1946dcb4e102SKan Liang 1947dcb4e102SKan Liang len = 0; 1948231bb2aaSAndi Kleen pmu = NULL; 1949231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1950dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1951dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1952dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1953864bc8c9SJohn Garry char *tmp2 = NULL; 1954dcb4e102SKan Liang 1955864bc8c9SJohn Garry if (tmp) 1956864bc8c9SJohn Garry tmp2 = strchr(tmp + 1, '-'); 1957864bc8c9SJohn Garry if (tmp2) { 1958864bc8c9SJohn Garry SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1959864bc8c9SJohn Garry PMU_EVENT_SYMBOL_PREFIX); 1960864bc8c9SJohn Garry p++; 1961864bc8c9SJohn Garry tmp++; 1962864bc8c9SJohn Garry SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX); 1963864bc8c9SJohn Garry p++; 1964864bc8c9SJohn Garry SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2); 1965864bc8c9SJohn Garry len += 3; 1966864bc8c9SJohn Garry } else if (tmp) { 1967dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1968dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1969dcb4e102SKan Liang p++; 1970dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1971dcb4e102SKan Liang len += 2; 1972dcb4e102SKan Liang } else { 1973dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1974dcb4e102SKan Liang len++; 1975dcb4e102SKan Liang } 1976dcb4e102SKan Liang } 1977231bb2aaSAndi Kleen } 1978dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1979dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1980dcb4e102SKan Liang 1981dcb4e102SKan Liang return; 1982dcb4e102SKan Liang err: 1983dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1984dcb4e102SKan Liang } 1985dcb4e102SKan Liang 19864929e95aSJiri Olsa /* 19874929e95aSJiri Olsa * This function injects special term in 19884929e95aSJiri Olsa * perf_pmu_events_list so the test code 19894929e95aSJiri Olsa * can check on this functionality. 19904929e95aSJiri Olsa */ 19914929e95aSJiri Olsa int perf_pmu__test_parse_init(void) 19924929e95aSJiri Olsa { 1993b4a7276cSJohn Garry struct perf_pmu_event_symbol *list, *tmp, symbols[] = { 1994b4a7276cSJohn Garry {(char *)"read", PMU_EVENT_SYMBOL}, 1995b4a7276cSJohn Garry {(char *)"event", PMU_EVENT_SYMBOL_PREFIX}, 1996b4a7276cSJohn Garry {(char *)"two", PMU_EVENT_SYMBOL_SUFFIX}, 1997b4a7276cSJohn Garry {(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX}, 1998b4a7276cSJohn Garry {(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2}, 1999b4a7276cSJohn Garry }; 2000b4a7276cSJohn Garry unsigned long i, j; 20014929e95aSJiri Olsa 2002b4a7276cSJohn Garry tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols)); 20034929e95aSJiri Olsa if (!list) 20044929e95aSJiri Olsa return -ENOMEM; 20054929e95aSJiri Olsa 2006b4a7276cSJohn Garry for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) { 2007b4a7276cSJohn Garry tmp->type = symbols[i].type; 2008b4a7276cSJohn Garry tmp->symbol = strdup(symbols[i].symbol); 2009a7a72631SWeiguo Li if (!tmp->symbol) 2010b4a7276cSJohn Garry goto err_free; 20114929e95aSJiri Olsa } 20124929e95aSJiri Olsa 20134929e95aSJiri Olsa perf_pmu_events_list = list; 2014b4a7276cSJohn Garry perf_pmu_events_list_num = ARRAY_SIZE(symbols); 2015b4a7276cSJohn Garry 2016b4a7276cSJohn Garry qsort(perf_pmu_events_list, ARRAY_SIZE(symbols), 2017b4a7276cSJohn Garry sizeof(struct perf_pmu_event_symbol), comp_pmu); 20184929e95aSJiri Olsa return 0; 2019b4a7276cSJohn Garry 2020b4a7276cSJohn Garry err_free: 2021b4a7276cSJohn Garry for (j = 0, tmp = list; j < i; j++, tmp++) 2022b4a7276cSJohn Garry free(tmp->symbol); 2023b4a7276cSJohn Garry free(list); 2024b4a7276cSJohn Garry return -ENOMEM; 20254929e95aSJiri Olsa } 20264929e95aSJiri Olsa 2027dcb4e102SKan Liang enum perf_pmu_event_symbol_type 2028dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 2029dcb4e102SKan Liang { 2030dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 2031dcb4e102SKan Liang 2032dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 2033dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 2034dcb4e102SKan Liang perf_pmu__parse_init(); 2035dcb4e102SKan Liang /* 2036dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 2037dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 2038dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 2039dcb4e102SKan Liang */ 2040dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 2041dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 2042dcb4e102SKan Liang 2043dcb4e102SKan Liang p.symbol = strdup(name); 2044dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 2045dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 2046dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2047360e071bSTaeung Song zfree(&p.symbol); 2048dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 2049dcb4e102SKan Liang } 2050dcb4e102SKan Liang 20511244a327SJiri Olsa static int parse_events__scanner(const char *str, 20521244a327SJiri Olsa struct parse_events_state *parse_state) 2053ac20de6fSZheng Yan { 2054ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 2055ac20de6fSZheng Yan void *scanner; 2056ac20de6fSZheng Yan int ret; 2057ac20de6fSZheng Yan 20581244a327SJiri Olsa ret = parse_events_lex_init_extra(parse_state, &scanner); 2059ac20de6fSZheng Yan if (ret) 2060ac20de6fSZheng Yan return ret; 2061ac20de6fSZheng Yan 2062ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 2063ac20de6fSZheng Yan 2064ac20de6fSZheng Yan #ifdef PARSER_DEBUG 2065ac20de6fSZheng Yan parse_events_debug = 1; 2066ae762641SJiri Olsa parse_events_set_debug(1, scanner); 2067ac20de6fSZheng Yan #endif 20685d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 2069ac20de6fSZheng Yan 2070ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 2071ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 2072ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 2073ac20de6fSZheng Yan return ret; 2074ac20de6fSZheng Yan } 2075ac20de6fSZheng Yan 207690e2b22dSJiri Olsa /* 207790e2b22dSJiri Olsa * parse event config string, return a list of event terms. 207890e2b22dSJiri Olsa */ 207990e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 208090e2b22dSJiri Olsa { 20815d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 208290e2b22dSJiri Olsa .terms = NULL, 20831244a327SJiri Olsa .stoken = PE_START_TERMS, 208490e2b22dSJiri Olsa }; 208590e2b22dSJiri Olsa int ret; 208690e2b22dSJiri Olsa 20871244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 20884929e95aSJiri Olsa perf_pmu__parse_cleanup(); 20894929e95aSJiri Olsa 209090e2b22dSJiri Olsa if (!ret) { 20915d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 20925d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 209390e2b22dSJiri Olsa return 0; 209490e2b22dSJiri Olsa } 209590e2b22dSJiri Olsa 20965d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 209790e2b22dSJiri Olsa return ret; 209890e2b22dSJiri Olsa } 209990e2b22dSJiri Olsa 21005e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 21015e4edd1fSJin Yao const char *str, char *pmu_name, 21025e4edd1fSJin Yao struct list_head *list) 21035e4edd1fSJin Yao { 21045e4edd1fSJin Yao struct parse_events_state ps = { 21055e4edd1fSJin Yao .list = LIST_HEAD_INIT(ps.list), 21065e4edd1fSJin Yao .stoken = PE_START_EVENTS, 21075e4edd1fSJin Yao .hybrid_pmu_name = pmu_name, 21085e4edd1fSJin Yao .idx = parse_state->idx, 21095e4edd1fSJin Yao }; 21105e4edd1fSJin Yao int ret; 21115e4edd1fSJin Yao 21125e4edd1fSJin Yao ret = parse_events__scanner(str, &ps); 21135e4edd1fSJin Yao perf_pmu__parse_cleanup(); 21145e4edd1fSJin Yao 21155e4edd1fSJin Yao if (!ret) { 21165e4edd1fSJin Yao if (!list_empty(&ps.list)) { 21175e4edd1fSJin Yao list_splice(&ps.list, list); 21185e4edd1fSJin Yao parse_state->idx = ps.idx; 21195e4edd1fSJin Yao return 0; 21205e4edd1fSJin Yao } else 21215e4edd1fSJin Yao return -1; 21225e4edd1fSJin Yao } 21235e4edd1fSJin Yao 21245e4edd1fSJin Yao return ret; 21255e4edd1fSJin Yao } 21265e4edd1fSJin Yao 2127*347c2f0aSIan Rogers __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs) 2128*347c2f0aSIan Rogers { 2129*347c2f0aSIan Rogers /* Order by insertion index. */ 2130*347c2f0aSIan Rogers return lhs->core.idx - rhs->core.idx; 2131*347c2f0aSIan Rogers } 2132*347c2f0aSIan Rogers 2133*347c2f0aSIan Rogers static int evlist__cmp(void *state, const struct list_head *l, const struct list_head *r) 2134*347c2f0aSIan Rogers { 2135*347c2f0aSIan Rogers const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node); 2136*347c2f0aSIan Rogers const struct evsel *lhs = container_of(lhs_core, struct evsel, core); 2137*347c2f0aSIan Rogers const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node); 2138*347c2f0aSIan Rogers const struct evsel *rhs = container_of(rhs_core, struct evsel, core); 2139*347c2f0aSIan Rogers int *leader_idx = state; 2140*347c2f0aSIan Rogers int lhs_leader_idx = *leader_idx, rhs_leader_idx = *leader_idx, ret; 2141*347c2f0aSIan Rogers const char *lhs_pmu_name, *rhs_pmu_name; 2142*347c2f0aSIan Rogers 2143*347c2f0aSIan Rogers /* 2144*347c2f0aSIan Rogers * First sort by grouping/leader. Read the leader idx only if the evsel 2145*347c2f0aSIan Rogers * is part of a group, as -1 indicates no group. 2146*347c2f0aSIan Rogers */ 2147*347c2f0aSIan Rogers if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) 2148*347c2f0aSIan Rogers lhs_leader_idx = lhs_core->leader->idx; 2149*347c2f0aSIan Rogers if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) 2150*347c2f0aSIan Rogers rhs_leader_idx = rhs_core->leader->idx; 2151*347c2f0aSIan Rogers 2152*347c2f0aSIan Rogers if (lhs_leader_idx != rhs_leader_idx) 2153*347c2f0aSIan Rogers return lhs_leader_idx - rhs_leader_idx; 2154*347c2f0aSIan Rogers 2155*347c2f0aSIan Rogers /* Group by PMU. Groups can't span PMUs. */ 2156*347c2f0aSIan Rogers lhs_pmu_name = evsel__group_pmu_name(lhs); 2157*347c2f0aSIan Rogers rhs_pmu_name = evsel__group_pmu_name(rhs); 2158*347c2f0aSIan Rogers ret = strcmp(lhs_pmu_name, rhs_pmu_name); 2159*347c2f0aSIan Rogers if (ret) 2160*347c2f0aSIan Rogers return ret; 2161*347c2f0aSIan Rogers 2162*347c2f0aSIan Rogers /* Architecture specific sorting. */ 2163*347c2f0aSIan Rogers return arch_evlist__cmp(lhs, rhs); 2164*347c2f0aSIan Rogers } 2165*347c2f0aSIan Rogers 2166*347c2f0aSIan Rogers static void parse_events__sort_events_and_fix_groups(struct list_head *list) 2167*347c2f0aSIan Rogers { 2168*347c2f0aSIan Rogers int idx = -1; 2169*347c2f0aSIan Rogers struct evsel *pos, *cur_leader = NULL; 2170*347c2f0aSIan Rogers struct perf_evsel *cur_leaders_grp = NULL; 2171*347c2f0aSIan Rogers 2172*347c2f0aSIan Rogers /* 2173*347c2f0aSIan Rogers * Compute index to insert ungrouped events at. Place them where the 2174*347c2f0aSIan Rogers * first ungrouped event appears. 2175*347c2f0aSIan Rogers */ 2176*347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2177*347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 2178*347c2f0aSIan Rogers 2179*347c2f0aSIan Rogers if (pos != pos_leader || pos->core.nr_members > 1) 2180*347c2f0aSIan Rogers continue; 2181*347c2f0aSIan Rogers 2182*347c2f0aSIan Rogers idx = pos->core.idx; 2183*347c2f0aSIan Rogers break; 2184*347c2f0aSIan Rogers } 2185*347c2f0aSIan Rogers 2186*347c2f0aSIan Rogers /* Sort events. */ 2187*347c2f0aSIan Rogers list_sort(&idx, list, evlist__cmp); 2188*347c2f0aSIan Rogers 2189*347c2f0aSIan Rogers /* 2190*347c2f0aSIan Rogers * Recompute groups, splitting for PMUs and adding groups for events 2191*347c2f0aSIan Rogers * that require them. 2192*347c2f0aSIan Rogers */ 2193*347c2f0aSIan Rogers idx = 0; 2194*347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2195*347c2f0aSIan Rogers const struct evsel *pos_leader = evsel__leader(pos); 2196*347c2f0aSIan Rogers const char *pos_pmu_name = evsel__group_pmu_name(pos); 2197*347c2f0aSIan Rogers const char *cur_leader_pmu_name, *pos_leader_pmu_name; 2198*347c2f0aSIan Rogers bool force_grouped = arch_evsel__must_be_in_group(pos); 2199*347c2f0aSIan Rogers 2200*347c2f0aSIan Rogers /* Reset index and nr_members. */ 2201*347c2f0aSIan Rogers pos->core.idx = idx++; 2202*347c2f0aSIan Rogers pos->core.nr_members = 0; 2203*347c2f0aSIan Rogers 2204*347c2f0aSIan Rogers /* 2205*347c2f0aSIan Rogers * Set the group leader respecting the given groupings and that 2206*347c2f0aSIan Rogers * groups can't span PMUs. 2207*347c2f0aSIan Rogers */ 2208*347c2f0aSIan Rogers if (!cur_leader) 2209*347c2f0aSIan Rogers cur_leader = pos; 2210*347c2f0aSIan Rogers 2211*347c2f0aSIan Rogers cur_leader_pmu_name = evsel__group_pmu_name(cur_leader); 2212*347c2f0aSIan Rogers if ((cur_leaders_grp != pos->core.leader && !force_grouped) || 2213*347c2f0aSIan Rogers strcmp(cur_leader_pmu_name, pos_pmu_name)) { 2214*347c2f0aSIan Rogers /* Event is for a different group/PMU than last. */ 2215*347c2f0aSIan Rogers cur_leader = pos; 2216*347c2f0aSIan Rogers /* 2217*347c2f0aSIan Rogers * Remember the leader's group before it is overwritten, 2218*347c2f0aSIan Rogers * so that later events match as being in the same 2219*347c2f0aSIan Rogers * group. 2220*347c2f0aSIan Rogers */ 2221*347c2f0aSIan Rogers cur_leaders_grp = pos->core.leader; 2222*347c2f0aSIan Rogers } 2223*347c2f0aSIan Rogers pos_leader_pmu_name = evsel__group_pmu_name(pos_leader); 2224*347c2f0aSIan Rogers if (strcmp(pos_leader_pmu_name, pos_pmu_name) || force_grouped) { 2225*347c2f0aSIan Rogers /* 2226*347c2f0aSIan Rogers * Event's PMU differs from its leader's. Groups can't 2227*347c2f0aSIan Rogers * span PMUs, so update leader from the group/PMU 2228*347c2f0aSIan Rogers * tracker. 2229*347c2f0aSIan Rogers */ 2230*347c2f0aSIan Rogers evsel__set_leader(pos, cur_leader); 2231*347c2f0aSIan Rogers } 2232*347c2f0aSIan Rogers } 2233*347c2f0aSIan Rogers list_for_each_entry(pos, list, core.node) { 2234*347c2f0aSIan Rogers pos->core.leader->nr_members++; 2235*347c2f0aSIan Rogers } 2236*347c2f0aSIan Rogers } 2237*347c2f0aSIan Rogers 22383bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str, 22393bf91aa5SArnaldo Carvalho de Melo struct parse_events_error *err, struct perf_pmu *fake_pmu) 224086470930SIngo Molnar { 22415d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 22425d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 22436484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 2244b39b8393SJiri Olsa .error = err, 22457630b3e2SWang Nan .evlist = evlist, 22461244a327SJiri Olsa .stoken = PE_START_EVENTS, 22473bf91aa5SArnaldo Carvalho de Melo .fake_pmu = fake_pmu, 224846010ab2SJiri Olsa }; 224946010ab2SJiri Olsa int ret; 225086470930SIngo Molnar 22511244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 2252dcb4e102SKan Liang perf_pmu__parse_cleanup(); 225315bfd2ccSWang Nan 22548e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2255c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2256854f7363SWang Nan return -1; 2257854f7363SWang Nan } 2258854f7363SWang Nan 2259*347c2f0aSIan Rogers parse_events__sort_events_and_fix_groups(&parse_state.list); 2260*347c2f0aSIan Rogers 22618e8714c3SIan Rogers /* 22628e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 22638e8714c3SIan Rogers */ 2264e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &parse_state.list); 22658e8714c3SIan Rogers 22668e8714c3SIan Rogers if (!ret) { 22678e8714c3SIan Rogers struct evsel *last; 22688e8714c3SIan Rogers 22693a683120SJiri Olsa evlist->core.nr_groups += parse_state.nr_groups; 2270515dbe48SJiri Olsa last = evlist__last(evlist); 227115bfd2ccSWang Nan last->cmdline_group_boundary = true; 227215bfd2ccSWang Nan 227386470930SIngo Molnar return 0; 227486470930SIngo Molnar } 227586470930SIngo Molnar 22765d7be90eSJiri Olsa /* 22775d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2278c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 22795d7be90eSJiri Olsa * need to bother. 22805d7be90eSJiri Olsa */ 228189812fc8SJiri Olsa return ret; 228289812fc8SJiri Olsa } 228389812fc8SJiri Olsa 2284806731a9SAdrian Hunter int parse_event(struct evlist *evlist, const char *str) 2285806731a9SAdrian Hunter { 2286806731a9SAdrian Hunter struct parse_events_error err; 2287806731a9SAdrian Hunter int ret; 2288806731a9SAdrian Hunter 2289806731a9SAdrian Hunter parse_events_error__init(&err); 2290806731a9SAdrian Hunter ret = parse_events(evlist, str, &err); 2291806731a9SAdrian Hunter parse_events_error__exit(&err); 2292806731a9SAdrian Hunter return ret; 2293806731a9SAdrian Hunter } 2294806731a9SAdrian Hunter 229507eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err) 229607eafd4eSIan Rogers { 229707eafd4eSIan Rogers bzero(err, sizeof(*err)); 229807eafd4eSIan Rogers } 229907eafd4eSIan Rogers 230007eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err) 230107eafd4eSIan Rogers { 230207eafd4eSIan Rogers zfree(&err->str); 230307eafd4eSIan Rogers zfree(&err->help); 230407eafd4eSIan Rogers zfree(&err->first_str); 230507eafd4eSIan Rogers zfree(&err->first_help); 230607eafd4eSIan Rogers } 230707eafd4eSIan Rogers 23086c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx, 23096c191289SIan Rogers char *str, char *help) 23106c191289SIan Rogers { 2311806731a9SAdrian Hunter if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n")) 23122e828582SAdrian Hunter goto out_free; 23136c191289SIan Rogers switch (err->num_errors) { 23146c191289SIan Rogers case 0: 23156c191289SIan Rogers err->idx = idx; 23166c191289SIan Rogers err->str = str; 23176c191289SIan Rogers err->help = help; 23186c191289SIan Rogers break; 23196c191289SIan Rogers case 1: 23206c191289SIan Rogers err->first_idx = err->idx; 23216c191289SIan Rogers err->idx = idx; 23226c191289SIan Rogers err->first_str = err->str; 23236c191289SIan Rogers err->str = str; 23246c191289SIan Rogers err->first_help = err->help; 23256c191289SIan Rogers err->help = help; 23266c191289SIan Rogers break; 23276c191289SIan Rogers default: 23286c191289SIan Rogers pr_debug("Multiple errors dropping message: %s (%s)\n", 23296c191289SIan Rogers err->str, err->help); 23306c191289SIan Rogers free(err->str); 23316c191289SIan Rogers err->str = str; 23326c191289SIan Rogers free(err->help); 23336c191289SIan Rogers err->help = help; 23346c191289SIan Rogers break; 23356c191289SIan Rogers } 23366c191289SIan Rogers err->num_errors++; 23372e828582SAdrian Hunter return; 23382e828582SAdrian Hunter 23392e828582SAdrian Hunter out_free: 23402e828582SAdrian Hunter free(str); 23412e828582SAdrian Hunter free(help); 23426c191289SIan Rogers } 23436c191289SIan Rogers 2344b39b8393SJiri Olsa #define MAX_WIDTH 1000 2345b39b8393SJiri Olsa static int get_term_width(void) 2346b39b8393SJiri Olsa { 2347b39b8393SJiri Olsa struct winsize ws; 2348b39b8393SJiri Olsa 2349b39b8393SJiri Olsa get_term_dimensions(&ws); 2350b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2351b39b8393SJiri Olsa } 2352b39b8393SJiri Olsa 23536c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str, 2354a910e466SIan Rogers const char *err_help, const char *event) 2355b39b8393SJiri Olsa { 2356b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2357b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2358b39b8393SJiri Olsa char *buf = (char *) event; 2359b39b8393SJiri Olsa int idx = 0; 2360a910e466SIan Rogers if (err_str) { 2361b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2362b39b8393SJiri Olsa int width = get_term_width() - 2; 2363b39b8393SJiri Olsa int len_event = strlen(event); 2364b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2365b39b8393SJiri Olsa 2366b39b8393SJiri Olsa /* 2367b39b8393SJiri Olsa * Maximum error index indent, we will cut 2368b39b8393SJiri Olsa * the event string if it's bigger. 2369b39b8393SJiri Olsa */ 2370141b2d31SAdrian Hunter int max_err_idx = 13; 2371b39b8393SJiri Olsa 2372b39b8393SJiri Olsa /* 2373b39b8393SJiri Olsa * Let's be specific with the message when 2374b39b8393SJiri Olsa * we have the precise error. 2375b39b8393SJiri Olsa */ 2376b39b8393SJiri Olsa str = "event syntax error: "; 2377b39b8393SJiri Olsa len_str = strlen(str); 2378b39b8393SJiri Olsa max_len = width - len_str; 2379b39b8393SJiri Olsa 2380b39b8393SJiri Olsa buf = _buf; 2381b39b8393SJiri Olsa 2382bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2383a910e466SIan Rogers if (err_idx > max_err_idx) 2384a910e466SIan Rogers cut = err_idx - max_err_idx; 2385b39b8393SJiri Olsa 2386b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2387b39b8393SJiri Olsa 2388b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2389b39b8393SJiri Olsa if (cut) 2390b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2391b39b8393SJiri Olsa 2392b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2393b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2394b39b8393SJiri Olsa buf[max_len] = 0; 2395b39b8393SJiri Olsa } 2396b39b8393SJiri Olsa 2397a910e466SIan Rogers idx = len_str + err_idx - cut; 2398b39b8393SJiri Olsa } 2399b39b8393SJiri Olsa 2400b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2401b39b8393SJiri Olsa if (idx) { 2402a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2403a910e466SIan Rogers if (err_help) 2404a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2405a910e466SIan Rogers } 2406a910e466SIan Rogers } 2407a910e466SIan Rogers 24086c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err, 2409a910e466SIan Rogers const char *event) 2410a910e466SIan Rogers { 2411a910e466SIan Rogers if (!err->num_errors) 2412a910e466SIan Rogers return; 2413a910e466SIan Rogers 24146c191289SIan Rogers __parse_events_error__print(err->idx, err->str, err->help, event); 2415a910e466SIan Rogers 2416a910e466SIan Rogers if (err->num_errors > 1) { 2417a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 24186c191289SIan Rogers __parse_events_error__print(err->first_idx, err->first_str, 2419a910e466SIan Rogers err->first_help, event); 2420b39b8393SJiri Olsa } 2421b39b8393SJiri Olsa } 2422b39b8393SJiri Olsa 2423b39b8393SJiri Olsa #undef MAX_WIDTH 2424b39b8393SJiri Olsa 2425f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 24261d037ca1SIrina Tirdea int unset __maybe_unused) 2427f120f9d5SJiri Olsa { 242863503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2429a910e466SIan Rogers struct parse_events_error err; 2430a910e466SIan Rogers int ret; 2431a910e466SIan Rogers 243207eafd4eSIan Rogers parse_events_error__init(&err); 2433a910e466SIan Rogers ret = parse_events(evlist, str, &err); 24349175ce1fSAndi Kleen 2435333b5665SAndi Kleen if (ret) { 24366c191289SIan Rogers parse_events_error__print(&err, str); 2437333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2438333b5665SAndi Kleen } 243907eafd4eSIan Rogers parse_events_error__exit(&err); 2440b39b8393SJiri Olsa 24419175ce1fSAndi Kleen return ret; 2442f120f9d5SJiri Olsa } 2443f120f9d5SJiri Olsa 2444d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2445d0abbc3cSArnaldo Carvalho de Melo { 2446d0abbc3cSArnaldo Carvalho de Melo struct evlist **evlistp = opt->value; 2447d0abbc3cSArnaldo Carvalho de Melo int ret; 2448d0abbc3cSArnaldo Carvalho de Melo 2449d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2450d0abbc3cSArnaldo Carvalho de Melo *evlistp = evlist__new(); 2451d0abbc3cSArnaldo Carvalho de Melo 2452d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2453d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2454d0abbc3cSArnaldo Carvalho de Melo return -1; 2455d0abbc3cSArnaldo Carvalho de Melo } 2456d0abbc3cSArnaldo Carvalho de Melo } 2457d0abbc3cSArnaldo Carvalho de Melo 2458d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2459d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2460d0abbc3cSArnaldo Carvalho de Melo evlist__delete(*evlistp); 2461d0abbc3cSArnaldo Carvalho de Melo *evlistp = NULL; 2462d0abbc3cSArnaldo Carvalho de Melo } 2463d0abbc3cSArnaldo Carvalho de Melo 2464d0abbc3cSArnaldo Carvalho de Melo return ret; 2465d0abbc3cSArnaldo Carvalho de Melo } 2466d0abbc3cSArnaldo Carvalho de Melo 24674ba1faa1SWang Nan static int 246863503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 246932dcd021SJiri Olsa int (*func)(struct evsel *evsel, 24704ba1faa1SWang Nan const void *arg), 24714ba1faa1SWang Nan const void *arg) 2472c171b552SLi Zefan { 247332dcd021SJiri Olsa struct evsel *last = NULL; 24744ba1faa1SWang Nan int err; 2475c171b552SLi Zefan 2476854f7363SWang Nan /* 2477854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2478854f7363SWang Nan * error when it found last == NULL. 2479854f7363SWang Nan * 2480854f7363SWang Nan * So no need to WARN here, let *func do this. 2481854f7363SWang Nan */ 24826484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2483515dbe48SJiri Olsa last = evlist__last(evlist); 248469aad6f1SArnaldo Carvalho de Melo 248515bfd2ccSWang Nan do { 24864ba1faa1SWang Nan err = (*func)(last, arg); 24874ba1faa1SWang Nan if (err) 2488c171b552SLi Zefan return -1; 24894ba1faa1SWang Nan if (!last) 24904ba1faa1SWang Nan return 0; 2491c171b552SLi Zefan 2492ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 249315bfd2ccSWang Nan return 0; 2494b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 249515bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 249615bfd2ccSWang Nan 2497c171b552SLi Zefan return 0; 2498c171b552SLi Zefan } 2499c171b552SLi Zefan 250032dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 25014ba1faa1SWang Nan { 25024ba1faa1SWang Nan const char *str = arg; 25031e857484SMathieu Poirier bool found = false; 25041e857484SMathieu Poirier int nr_addr_filters = 0; 25051e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 25064ba1faa1SWang Nan 250749836f78SJack Henschel if (evsel == NULL) { 250849836f78SJack Henschel fprintf(stderr, 250949836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 251049836f78SJack Henschel return -1; 251149836f78SJack Henschel } 25124ba1faa1SWang Nan 25131fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2514ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, str) < 0) { 25154ba1faa1SWang Nan fprintf(stderr, 25164ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25174ba1faa1SWang Nan return -1; 25184ba1faa1SWang Nan } 25194ba1faa1SWang Nan 25204ba1faa1SWang Nan return 0; 25214ba1faa1SWang Nan } 25224ba1faa1SWang Nan 25231e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 25241fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 25251e857484SMathieu Poirier found = true; 25261e857484SMathieu Poirier break; 25271e857484SMathieu Poirier } 25281e857484SMathieu Poirier 25291e857484SMathieu Poirier if (found) 25301e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 25311e857484SMathieu Poirier "%d", &nr_addr_filters); 25321e857484SMathieu Poirier 253349836f78SJack Henschel if (!nr_addr_filters) { 253449836f78SJack Henschel fprintf(stderr, 253549836f78SJack Henschel "This CPU does not support address filtering\n"); 253649836f78SJack Henschel return -1; 253749836f78SJack Henschel } 25381e857484SMathieu Poirier 2539ad681adfSArnaldo Carvalho de Melo if (evsel__append_addr_filter(evsel, str) < 0) { 25401e857484SMathieu Poirier fprintf(stderr, 25411e857484SMathieu Poirier "not enough memory to hold filter string\n"); 25421e857484SMathieu Poirier return -1; 25431e857484SMathieu Poirier } 25441e857484SMathieu Poirier 25451e857484SMathieu Poirier return 0; 25461e857484SMathieu Poirier } 25471e857484SMathieu Poirier 25484ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 25494ba1faa1SWang Nan int unset __maybe_unused) 25504ba1faa1SWang Nan { 255163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25524ba1faa1SWang Nan 25534ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 25544ba1faa1SWang Nan (const void *)str); 25554ba1faa1SWang Nan } 25564ba1faa1SWang Nan 255732dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 25584ba1faa1SWang Nan const void *arg __maybe_unused) 25594ba1faa1SWang Nan { 25604ba1faa1SWang Nan char new_filter[64]; 25614ba1faa1SWang Nan 25621fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 25634ba1faa1SWang Nan fprintf(stderr, 25644ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 25654ba1faa1SWang Nan return -1; 25664ba1faa1SWang Nan } 25674ba1faa1SWang Nan 25684ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 25694ba1faa1SWang Nan 2570ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, new_filter) < 0) { 25714ba1faa1SWang Nan fprintf(stderr, 25724ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25734ba1faa1SWang Nan return -1; 25744ba1faa1SWang Nan } 25754ba1faa1SWang Nan 25764ba1faa1SWang Nan return 0; 25774ba1faa1SWang Nan } 25784ba1faa1SWang Nan 25794ba1faa1SWang Nan int exclude_perf(const struct option *opt, 25804ba1faa1SWang Nan const char *arg __maybe_unused, 25814ba1faa1SWang Nan int unset __maybe_unused) 25824ba1faa1SWang Nan { 258363503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25844ba1faa1SWang Nan 25854ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 25864ba1faa1SWang Nan NULL); 25874ba1faa1SWang Nan } 25884ba1faa1SWang Nan 25896cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 25908f707d84SJiri Olsa { 259116fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 25928f707d84SJiri Olsa } 25938f707d84SJiri Olsa 259467b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 259567b49b38SJiri Olsa struct parse_events_term *temp, 259667b49b38SJiri Olsa char *str, u64 num) 25978f707d84SJiri Olsa { 25986cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 25998f707d84SJiri Olsa 260067b49b38SJiri Olsa term = malloc(sizeof(*term)); 26018f707d84SJiri Olsa if (!term) 26028f707d84SJiri Olsa return -ENOMEM; 26038f707d84SJiri Olsa 260467b49b38SJiri Olsa *term = *temp; 26058f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2606c2f1ceadSAndi Kleen term->weak = false; 26078f707d84SJiri Olsa 260867b49b38SJiri Olsa switch (term->type_val) { 26098f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 26108f707d84SJiri Olsa term->val.num = num; 26118f707d84SJiri Olsa break; 26128f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 26138f707d84SJiri Olsa term->val.str = str; 26148f707d84SJiri Olsa break; 26158f707d84SJiri Olsa default: 26164be8be6bSAdrian Hunter free(term); 26178f707d84SJiri Olsa return -EINVAL; 26188f707d84SJiri Olsa } 26198f707d84SJiri Olsa 26208f707d84SJiri Olsa *_term = term; 26218f707d84SJiri Olsa return 0; 26228f707d84SJiri Olsa } 26238f707d84SJiri Olsa 26246cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2625cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 262699e7138eSJiri Olsa bool no_value, 2627bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 262816fa7e82SJiri Olsa { 2629bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2630bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2631bb78ce7dSAdrian Hunter 263267b49b38SJiri Olsa struct parse_events_term temp = { 263367b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 263467b49b38SJiri Olsa .type_term = type_term, 26354f9d4f8aSJohn Garry .config = config ? : strdup(config_term_names[type_term]), 263699e7138eSJiri Olsa .no_value = no_value, 263767b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 263867b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 263967b49b38SJiri Olsa }; 264067b49b38SJiri Olsa 264167b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 264216fa7e82SJiri Olsa } 264316fa7e82SJiri Olsa 26446cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2645cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2646bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 264716fa7e82SJiri Olsa { 2648bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2649bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2650bb78ce7dSAdrian Hunter 265167b49b38SJiri Olsa struct parse_events_term temp = { 265267b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 265367b49b38SJiri Olsa .type_term = type_term, 265467b49b38SJiri Olsa .config = config, 265567b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 265667b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 265767b49b38SJiri Olsa }; 265867b49b38SJiri Olsa 265967b49b38SJiri Olsa return new_term(term, &temp, str, 0); 266016fa7e82SJiri Olsa } 266116fa7e82SJiri Olsa 26626cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 26631d33d6dcSJiri Olsa char *config, unsigned idx) 26641d33d6dcSJiri Olsa { 26651d33d6dcSJiri Olsa struct event_symbol *sym; 2666b6645a72SIan Rogers char *str; 266767b49b38SJiri Olsa struct parse_events_term temp = { 266867b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 266967b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 2670b6645a72SIan Rogers .config = config, 267167b49b38SJiri Olsa }; 26721d33d6dcSJiri Olsa 2673b6645a72SIan Rogers if (!temp.config) { 2674b6645a72SIan Rogers temp.config = strdup("event"); 2675b6645a72SIan Rogers if (!temp.config) 2676b6645a72SIan Rogers return -ENOMEM; 2677b6645a72SIan Rogers } 26781d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 26791d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 26801d33d6dcSJiri Olsa 2681b6645a72SIan Rogers str = strdup(sym->symbol); 2682b6645a72SIan Rogers if (!str) 2683b6645a72SIan Rogers return -ENOMEM; 2684b6645a72SIan Rogers return new_term(term, &temp, str, 0); 26851d33d6dcSJiri Olsa } 26861d33d6dcSJiri Olsa 26876cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 26886cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2689a6146d50SZheng Yan { 2690b6645a72SIan Rogers char *str; 269167b49b38SJiri Olsa struct parse_events_term temp = { 269267b49b38SJiri Olsa .type_val = term->type_val, 269367b49b38SJiri Olsa .type_term = term->type_term, 2694b6645a72SIan Rogers .config = NULL, 269567b49b38SJiri Olsa .err_term = term->err_term, 269667b49b38SJiri Olsa .err_val = term->err_val, 269767b49b38SJiri Olsa }; 269867b49b38SJiri Olsa 2699b6645a72SIan Rogers if (term->config) { 2700b6645a72SIan Rogers temp.config = strdup(term->config); 2701b6645a72SIan Rogers if (!temp.config) 2702b6645a72SIan Rogers return -ENOMEM; 2703b6645a72SIan Rogers } 2704b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 2705b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 2706b6645a72SIan Rogers 2707b6645a72SIan Rogers str = strdup(term->val.str); 2708b6645a72SIan Rogers if (!str) 2709b6645a72SIan Rogers return -ENOMEM; 2710b6645a72SIan Rogers return new_term(new, &temp, str, 0); 2711a6146d50SZheng Yan } 2712a6146d50SZheng Yan 27131dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 27141dc92556SIan Rogers { 27151dc92556SIan Rogers if (term->array.nr_ranges) 27161dc92556SIan Rogers zfree(&term->array.ranges); 27171dc92556SIan Rogers 27181dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 27191dc92556SIan Rogers zfree(&term->val.str); 27201dc92556SIan Rogers 27211dc92556SIan Rogers zfree(&term->config); 27221dc92556SIan Rogers free(term); 27231dc92556SIan Rogers } 27241dc92556SIan Rogers 27258255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 27268255718fSAndi Kleen struct list_head **new) 27278255718fSAndi Kleen { 27288255718fSAndi Kleen struct parse_events_term *term, *n; 27298255718fSAndi Kleen int ret; 27308255718fSAndi Kleen 27318255718fSAndi Kleen if (!old) { 27328255718fSAndi Kleen *new = NULL; 27338255718fSAndi Kleen return 0; 27348255718fSAndi Kleen } 27358255718fSAndi Kleen 27368255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 27378255718fSAndi Kleen if (!*new) 27388255718fSAndi Kleen return -ENOMEM; 27398255718fSAndi Kleen INIT_LIST_HEAD(*new); 27408255718fSAndi Kleen 27418255718fSAndi Kleen list_for_each_entry (term, old, list) { 27428255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 27438255718fSAndi Kleen if (ret) 27448255718fSAndi Kleen return ret; 27458255718fSAndi Kleen list_add_tail(&n->list, *new); 27468255718fSAndi Kleen } 27478255718fSAndi Kleen return 0; 27488255718fSAndi Kleen } 27498255718fSAndi Kleen 2750fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 27518f707d84SJiri Olsa { 27526cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 27538f707d84SJiri Olsa 2754a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 2755a8adfcebSWang Nan list_del_init(&term->list); 27561dc92556SIan Rogers parse_events_term__delete(term); 27578f707d84SJiri Olsa } 2758a8adfcebSWang Nan } 2759b39b8393SJiri Olsa 27602146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2761fc0a2c1dSArnaldo Carvalho de Melo { 27622146afc6SArnaldo Carvalho de Melo if (!terms) 27632146afc6SArnaldo Carvalho de Melo return; 2764fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2765d20a5f2bSWang Nan free(terms); 2766fc0a2c1dSArnaldo Carvalho de Melo } 2767fc0a2c1dSArnaldo Carvalho de Melo 27682d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 27692d055bf2SWang Nan { 2770360e071bSTaeung Song zfree(&a->ranges); 27712d055bf2SWang Nan } 27722d055bf2SWang Nan 27735d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2774b39b8393SJiri Olsa int idx, const char *str) 2775b39b8393SJiri Olsa { 2776448d732cSIan Rogers if (!parse_state->error) 2777a6ced2beSAdrian Hunter return; 2778448d732cSIan Rogers 27796c191289SIan Rogers parse_events_error__handle(parse_state->error, idx, strdup(str), NULL); 2780b39b8393SJiri Olsa } 2781ffeb883eSHe Kuang 278217cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 278317cb5f84SWang Nan { 278417cb5f84SWang Nan int i; 278517cb5f84SWang Nan bool first = true; 278617cb5f84SWang Nan 278717cb5f84SWang Nan buf[0] = '\0'; 278817cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 278917cb5f84SWang Nan const char *name = config_term_names[i]; 279017cb5f84SWang Nan 27911669e509SWang Nan if (!config_term_avail(i, NULL)) 27921669e509SWang Nan continue; 279317cb5f84SWang Nan if (!name) 279417cb5f84SWang Nan continue; 279517cb5f84SWang Nan if (name[0] == '<') 279617cb5f84SWang Nan continue; 279717cb5f84SWang Nan 279817cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 279917cb5f84SWang Nan return; 280017cb5f84SWang Nan 280117cb5f84SWang Nan if (!first) 280217cb5f84SWang Nan strcat(buf, ","); 280317cb5f84SWang Nan else 280417cb5f84SWang Nan first = false; 280517cb5f84SWang Nan strcat(buf, name); 280617cb5f84SWang Nan } 280717cb5f84SWang Nan } 280817cb5f84SWang Nan 2809ffeb883eSHe Kuang /* 2810ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2811ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2812ffeb883eSHe Kuang */ 2813ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2814ffeb883eSHe Kuang { 2815ffeb883eSHe Kuang char *str; 2816626a6b78SWang Nan /* "no-overwrite" is the longest name */ 281717cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2818626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2819ffeb883eSHe Kuang 282017cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2821ffeb883eSHe Kuang /* valid terms */ 2822ffeb883eSHe Kuang if (additional_terms) { 282326dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 282426dee028SWang Nan additional_terms, static_terms) < 0) 2825ffeb883eSHe Kuang goto fail; 2826ffeb883eSHe Kuang } else { 282726dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2828ffeb883eSHe Kuang goto fail; 2829ffeb883eSHe Kuang } 2830ffeb883eSHe Kuang return str; 2831ffeb883eSHe Kuang 2832ffeb883eSHe Kuang fail: 2833ffeb883eSHe Kuang return NULL; 2834ffeb883eSHe Kuang } 28359cbfa2f6SJin Yao 28369cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx, 28379cbfa2f6SJin Yao struct perf_event_attr *attr, 28382b62b3a6SIan Rogers const char *name, 28392b62b3a6SIan Rogers const char *metric_id, 28402b62b3a6SIan Rogers struct perf_pmu *pmu, 28419cbfa2f6SJin Yao struct list_head *config_terms) 28429cbfa2f6SJin Yao { 28432b62b3a6SIan Rogers return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id, 28442b62b3a6SIan Rogers pmu, config_terms, /*auto_merge_stats=*/false, 28452b62b3a6SIan Rogers /*cpu_list=*/NULL); 28469cbfa2f6SJin Yao } 2847