1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 38dd2a131SJiri Olsa #include <linux/err.h> 47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 576b31a29SArnaldo Carvalho de Melo #include <dirent.h> 6a43783aeSArnaldo Carvalho de Melo #include <errno.h> 786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h> 9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h> 10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 11391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 12b0742e90SArnaldo Carvalho de Melo #include "term.h" 134a3cec84SArnaldo Carvalho de Melo #include "build-id.h" 14361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 16fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h> 174b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1886470930SIngo Molnar #include "parse-events.h" 194b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 20a067558eSArnaldo Carvalho de Melo #include "string2.h" 218ec20b17SArnaldo Carvalho de Melo #include "strlist.h" 225aab621bSArnaldo Carvalho de Melo #include "symbol.h" 238755a8f2SArjan van de Ven #include "header.h" 2484c86ca1SWang Nan #include "bpf-loader.h" 256e81c74cSMasami Hiramatsu #include "debug.h" 26592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 279c3516d1SJiri Olsa #include <perf/cpumap.h> 28ac20de6fSZheng Yan #include "parse-events-bison.h" 291244a327SJiri Olsa #define YY_EXTRA_TYPE void* 3089812fc8SJiri Olsa #include "parse-events-flex.h" 315f537a26SJiri Olsa #include "pmu.h" 32b41f1cecSNamhyung Kim #include "thread_map.h" 3340218daeSMasami Hiramatsu #include "probe-file.h" 34b39b8393SJiri Olsa #include "asm/bug.h" 35ac12f676SAndi Kleen #include "util/parse-branch-options.h" 3671b0acceSAndi Kleen #include "metricgroup.h" 3795be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h" 38252a2fdcSArnaldo Carvalho de Melo #include "util/event.h" 3970943490SStephane Eranian #include "util/pfm.h" 409cbfa2f6SJin Yao #include "util/parse-events-hybrid.h" 41*5e4edd1fSJin Yao #include "util/pmu-hybrid.h" 42943b69acSJin Yao #include "perf.h" 4389812fc8SJiri Olsa 4489812fc8SJiri Olsa #define MAX_NAME_LEN 100 4586470930SIngo Molnar 4682ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4782ba1f2fSJiri Olsa extern int parse_events_debug; 4882ba1f2fSJiri Olsa #endif 495d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 50e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 51e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 52*5e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 53*5e4edd1fSJin Yao const char *str, char *pmu_name, 54*5e4edd1fSJin Yao struct list_head *list); 55bcd3279fSFrederic Weisbecker 56dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 57dcb4e102SKan Liang /* 58dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 59dcb4e102SKan Liang * 0 means not initialized and ready to init 60dcb4e102SKan Liang * -1 means failed to init, don't try anymore 61dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 62dcb4e102SKan Liang */ 63dcb4e102SKan Liang static int perf_pmu_events_list_num; 64dcb4e102SKan Liang 65705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 661dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 671dc12760SJiri Olsa .symbol = "cpu-cycles", 681dc12760SJiri Olsa .alias = "cycles", 691dc12760SJiri Olsa }, 701dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 711dc12760SJiri Olsa .symbol = "instructions", 721dc12760SJiri Olsa .alias = "", 731dc12760SJiri Olsa }, 741dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 751dc12760SJiri Olsa .symbol = "cache-references", 761dc12760SJiri Olsa .alias = "", 771dc12760SJiri Olsa }, 781dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 791dc12760SJiri Olsa .symbol = "cache-misses", 801dc12760SJiri Olsa .alias = "", 811dc12760SJiri Olsa }, 821dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 831dc12760SJiri Olsa .symbol = "branch-instructions", 841dc12760SJiri Olsa .alias = "branches", 851dc12760SJiri Olsa }, 861dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 871dc12760SJiri Olsa .symbol = "branch-misses", 881dc12760SJiri Olsa .alias = "", 891dc12760SJiri Olsa }, 901dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 911dc12760SJiri Olsa .symbol = "bus-cycles", 921dc12760SJiri Olsa .alias = "", 931dc12760SJiri Olsa }, 941dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 951dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 961dc12760SJiri Olsa .alias = "idle-cycles-frontend", 971dc12760SJiri Olsa }, 981dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 991dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 1001dc12760SJiri Olsa .alias = "idle-cycles-backend", 1011dc12760SJiri Olsa }, 1021dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 1031dc12760SJiri Olsa .symbol = "ref-cycles", 1041dc12760SJiri Olsa .alias = "", 1051dc12760SJiri Olsa }, 1061dc12760SJiri Olsa }; 10786470930SIngo Molnar 108705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 1091dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1101dc12760SJiri Olsa .symbol = "cpu-clock", 1111dc12760SJiri Olsa .alias = "", 1121dc12760SJiri Olsa }, 1131dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1141dc12760SJiri Olsa .symbol = "task-clock", 1151dc12760SJiri Olsa .alias = "", 1161dc12760SJiri Olsa }, 1171dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1181dc12760SJiri Olsa .symbol = "page-faults", 1191dc12760SJiri Olsa .alias = "faults", 1201dc12760SJiri Olsa }, 1211dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1221dc12760SJiri Olsa .symbol = "context-switches", 1231dc12760SJiri Olsa .alias = "cs", 1241dc12760SJiri Olsa }, 1251dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1261dc12760SJiri Olsa .symbol = "cpu-migrations", 1271dc12760SJiri Olsa .alias = "migrations", 1281dc12760SJiri Olsa }, 1291dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1301dc12760SJiri Olsa .symbol = "minor-faults", 1311dc12760SJiri Olsa .alias = "", 1321dc12760SJiri Olsa }, 1331dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1341dc12760SJiri Olsa .symbol = "major-faults", 1351dc12760SJiri Olsa .alias = "", 1361dc12760SJiri Olsa }, 1371dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1381dc12760SJiri Olsa .symbol = "alignment-faults", 1391dc12760SJiri Olsa .alias = "", 1401dc12760SJiri Olsa }, 1411dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1421dc12760SJiri Olsa .symbol = "emulation-faults", 1431dc12760SJiri Olsa .alias = "", 1441dc12760SJiri Olsa }, 145d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 146d22d1a2aSAdrian Hunter .symbol = "dummy", 147d22d1a2aSAdrian Hunter .alias = "", 148d22d1a2aSAdrian Hunter }, 149bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 150bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 151bae9cc41SArnaldo Carvalho de Melo .alias = "", 152bae9cc41SArnaldo Carvalho de Melo }, 15386470930SIngo Molnar }; 15486470930SIngo Molnar 155cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 156cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 15786470930SIngo Molnar 158cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 159cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 160cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 161cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 16286470930SIngo Molnar 16322a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 16422a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 16522a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 16622a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 16722a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 168f6bdafefSJason Baron 169c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) 170ae07b63fSPeter Zijlstra { 171ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 172ae07b63fSPeter Zijlstra int fd; 173ae07b63fSPeter Zijlstra 174c02cab22SArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name); 175ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 176ae07b63fSPeter Zijlstra if (fd < 0) 177ae07b63fSPeter Zijlstra return -EINVAL; 178ae07b63fSPeter Zijlstra close(fd); 179ae07b63fSPeter Zijlstra 180ae07b63fSPeter Zijlstra return 0; 181ae07b63fSPeter Zijlstra } 182ae07b63fSPeter Zijlstra 183c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent) \ 18422a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 18522a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 18622a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 18722a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 188c02cab22SArnaldo Carvalho de Melo (!tp_event_has_id(dir_path, evt_dirent))) 189f6bdafefSJason Baron 190270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 191f6bdafefSJason Baron 192448d732cSIan Rogers void parse_events__handle_error(struct parse_events_error *err, int idx, 193448d732cSIan Rogers char *str, char *help) 194448d732cSIan Rogers { 195448d732cSIan Rogers if (WARN(!str, "WARNING: failed to provide error string\n")) { 196448d732cSIan Rogers free(help); 197448d732cSIan Rogers return; 198448d732cSIan Rogers } 199a910e466SIan Rogers switch (err->num_errors) { 200a910e466SIan Rogers case 0: 201448d732cSIan Rogers err->idx = idx; 202a910e466SIan Rogers err->str = str; 203a910e466SIan Rogers err->help = help; 204a910e466SIan Rogers break; 205a910e466SIan Rogers case 1: 206a910e466SIan Rogers err->first_idx = err->idx; 207a910e466SIan Rogers err->idx = idx; 208a910e466SIan Rogers err->first_str = err->str; 209a910e466SIan Rogers err->str = str; 210a910e466SIan Rogers err->first_help = err->help; 211a910e466SIan Rogers err->help = help; 212a910e466SIan Rogers break; 213a910e466SIan Rogers default: 2144ac22b48SIan Rogers pr_debug("Multiple errors dropping message: %s (%s)\n", 2154ac22b48SIan Rogers err->str, err->help); 216448d732cSIan Rogers free(err->str); 217448d732cSIan Rogers err->str = str; 218448d732cSIan Rogers free(err->help); 219448d732cSIan Rogers err->help = help; 220a910e466SIan Rogers break; 221a910e466SIan Rogers } 222a910e466SIan Rogers err->num_errors++; 223448d732cSIan Rogers } 224f6bdafefSJason Baron 2251ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 226f6bdafefSJason Baron { 2271ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 228f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 22922a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2308aa8a7c8SStephane Eranian char id_buf[24]; 231725b1368SEric Dumazet int fd; 232f6bdafefSJason Baron u64 id; 233f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 23425a7d914SArnaldo Carvalho de Melo char *dir_path; 235f6bdafefSJason Baron 2367014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 237f6bdafefSJason Baron if (!sys_dir) 238725b1368SEric Dumazet return NULL; 239f6bdafefSJason Baron 24022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 24125a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 24225a7d914SArnaldo Carvalho de Melo if (!dir_path) 24325a7d914SArnaldo Carvalho de Melo continue; 244725b1368SEric Dumazet evt_dir = opendir(dir_path); 245725b1368SEric Dumazet if (!evt_dir) 24625a7d914SArnaldo Carvalho de Melo goto next; 247725b1368SEric Dumazet 248c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 249725b1368SEric Dumazet 25077f18153SJiri Olsa scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 25122a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 252725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 253f6bdafefSJason Baron if (fd < 0) 254f6bdafefSJason Baron continue; 255f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 256f6bdafefSJason Baron close(fd); 257f6bdafefSJason Baron continue; 258f6bdafefSJason Baron } 259f6bdafefSJason Baron close(fd); 260f6bdafefSJason Baron id = atoll(id_buf); 261f6bdafefSJason Baron if (id == config) { 26225a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 263f6bdafefSJason Baron closedir(evt_dir); 264f6bdafefSJason Baron closedir(sys_dir); 26559b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2665aa365f2STaeung Song if (!path) 2675aa365f2STaeung Song return NULL; 2687125f204SArnaldo Carvalho de Melo if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) { 2691ef2ed10SFrederic Weisbecker free(path); 2701ef2ed10SFrederic Weisbecker return NULL; 2711ef2ed10SFrederic Weisbecker } 2727125f204SArnaldo Carvalho de Melo if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) { 27374cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2741ef2ed10SFrederic Weisbecker free(path); 2751ef2ed10SFrederic Weisbecker return NULL; 2761ef2ed10SFrederic Weisbecker } 2771ef2ed10SFrederic Weisbecker return path; 278f6bdafefSJason Baron } 279f6bdafefSJason Baron } 280f6bdafefSJason Baron closedir(evt_dir); 28125a7d914SArnaldo Carvalho de Melo next: 28225a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 283f6bdafefSJason Baron } 284f6bdafefSJason Baron 285f6bdafefSJason Baron closedir(sys_dir); 2861ef2ed10SFrederic Weisbecker return NULL; 2871ef2ed10SFrederic Weisbecker } 2881ef2ed10SFrederic Weisbecker 289e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 290e7c93f09SNamhyung Kim { 291e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 292e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 293e7c93f09SNamhyung Kim 294e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 295e7c93f09SNamhyung Kim free(path); 296e7c93f09SNamhyung Kim return NULL; 297e7c93f09SNamhyung Kim } 298e7c93f09SNamhyung Kim 299e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 300e7c93f09SNamhyung Kim path->name = strdup(str+1); 301e7c93f09SNamhyung Kim 302e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 30374cf249dSArnaldo Carvalho de Melo zfree(&path->system); 30474cf249dSArnaldo Carvalho de Melo zfree(&path->name); 305506fde11STaeung Song zfree(&path); 306e7c93f09SNamhyung Kim } 307e7c93f09SNamhyung Kim 308e7c93f09SNamhyung Kim return path; 309e7c93f09SNamhyung Kim } 310e7c93f09SNamhyung Kim 3111424dc96SDavid Ahern const char *event_type(int type) 3121424dc96SDavid Ahern { 3131424dc96SDavid Ahern switch (type) { 3141424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 3151424dc96SDavid Ahern return "hardware"; 3161424dc96SDavid Ahern 3171424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 3181424dc96SDavid Ahern return "software"; 3191424dc96SDavid Ahern 3201424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 3211424dc96SDavid Ahern return "tracepoint"; 3221424dc96SDavid Ahern 3231424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 3241424dc96SDavid Ahern return "hardware-cache"; 3251424dc96SDavid Ahern 3261424dc96SDavid Ahern default: 3271424dc96SDavid Ahern break; 3281424dc96SDavid Ahern } 3291424dc96SDavid Ahern 3301424dc96SDavid Ahern return "unknown"; 3311424dc96SDavid Ahern } 3321424dc96SDavid Ahern 333e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 334e814fdddSWang Nan { 335e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 336e814fdddSWang Nan } 3377ae92e74SYan, Zheng 338e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 339e814fdddSWang Nan { 340e814fdddSWang Nan struct parse_events_term *term; 341e814fdddSWang Nan 342e814fdddSWang Nan if (!head_terms) 343e814fdddSWang Nan return NULL; 344e814fdddSWang Nan 345e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 346e814fdddSWang Nan if (parse_events__is_name_term(term)) 347e814fdddSWang Nan return term->val.str; 348e814fdddSWang Nan 349e814fdddSWang Nan return NULL; 350e814fdddSWang Nan } 3517ae92e74SYan, Zheng 35232dcd021SJiri Olsa static struct evsel * 353410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3547ae92e74SYan, Zheng struct perf_event_attr *attr, 35570943490SStephane Eranian bool init_attr, 35666ec1191SMark Rutland char *name, struct perf_pmu *pmu, 357f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 358f0fbb114SAndi Kleen const char *cpu_list) 35989812fc8SJiri Olsa { 36032dcd021SJiri Olsa struct evsel *evsel; 3615d680be3SNamhyung Kim struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 3629c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 36389812fc8SJiri Olsa 364e4064776SJin Yao if (pmu && attr->type == PERF_TYPE_RAW) 365e4064776SJin Yao perf_pmu__warn_invalid_config(pmu, attr->config, name); 366e4064776SJin Yao 36770943490SStephane Eranian if (init_attr) 36889812fc8SJiri Olsa event_attr_init(attr); 36989812fc8SJiri Olsa 3708f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attr, *idx); 3710f1b550eSNamhyung Kim if (!evsel) { 3720f1b550eSNamhyung Kim perf_cpu_map__put(cpus); 373410136f5SStephane Eranian return NULL; 3740f1b550eSNamhyung Kim } 37589812fc8SJiri Olsa 37675fc5ae5STaeung Song (*idx)++; 3775d680be3SNamhyung Kim evsel->core.cpus = cpus; 378fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 379648b5af3SJiri Olsa evsel->core.system_wide = pmu ? pmu->is_uncore : false; 38063ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 381f30a79b0SJiri Olsa 3829db1763cSArnaldo Carvalho de Melo if (name) 38389812fc8SJiri Olsa evsel->name = strdup(name); 384930a2e29SJiri Olsa 385930a2e29SJiri Olsa if (config_terms) 386930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 387930a2e29SJiri Olsa 38870943490SStephane Eranian if (list) 389b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 39070943490SStephane Eranian 391410136f5SStephane Eranian return evsel; 39289812fc8SJiri Olsa } 39389812fc8SJiri Olsa 39470943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 39570943490SStephane Eranian char *name, struct perf_pmu *pmu) 39670943490SStephane Eranian { 39770943490SStephane Eranian return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false, 39870943490SStephane Eranian NULL); 39970943490SStephane Eranian } 40070943490SStephane Eranian 401c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 402930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 403930a2e29SJiri Olsa struct list_head *config_terms) 4047ae92e74SYan, Zheng { 40570943490SStephane Eranian return __add_event(list, idx, attr, true, name, NULL, config_terms, 40670943490SStephane Eranian false, NULL) ? 0 : -ENOMEM; 407f0fbb114SAndi Kleen } 408f0fbb114SAndi Kleen 409f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 410f0fbb114SAndi Kleen enum perf_tool_event tool_event) 411f0fbb114SAndi Kleen { 41232dcd021SJiri Olsa struct evsel *evsel; 413f0fbb114SAndi Kleen struct perf_event_attr attr = { 414f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 415f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 416f0fbb114SAndi Kleen }; 417f0fbb114SAndi Kleen 41870943490SStephane Eranian evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false, 41970943490SStephane Eranian "0"); 420f0fbb114SAndi Kleen if (!evsel) 421f0fbb114SAndi Kleen return -ENOMEM; 422f0fbb114SAndi Kleen evsel->tool_event = tool_event; 423f0fbb114SAndi Kleen if (tool_event == PERF_TOOL_DURATION_TIME) 424b12eea5aSNamhyung Kim evsel->unit = "ns"; 425f0fbb114SAndi Kleen return 0; 4267ae92e74SYan, Zheng } 4277ae92e74SYan, Zheng 428c64e85e1SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size) 42986470930SIngo Molnar { 43086470930SIngo Molnar int i, j; 43161c45981SPaul Mackerras int n, longest = -1; 43286470930SIngo Molnar 43386470930SIngo Molnar for (i = 0; i < size; i++) { 434c64e85e1SArnaldo Carvalho de Melo for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 43561c45981SPaul Mackerras n = strlen(names[i][j]); 43689812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 43761c45981SPaul Mackerras longest = n; 43861c45981SPaul Mackerras } 43989812fc8SJiri Olsa if (longest > 0) 44086470930SIngo Molnar return i; 44186470930SIngo Molnar } 44286470930SIngo Molnar 4438953645fSIngo Molnar return -1; 44486470930SIngo Molnar } 44586470930SIngo Molnar 44643d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 44743d0b978SWang Nan struct parse_events_term *term, 44843d0b978SWang Nan struct parse_events_error *err); 44943d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 45043d0b978SWang Nan struct parse_events_term *term, 45143d0b978SWang Nan struct parse_events_error *err); 45243d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 45343d0b978SWang Nan struct list_head *head, 45443d0b978SWang Nan struct parse_events_error *err, 45543d0b978SWang Nan config_term_func_t config_term); 45643d0b978SWang Nan 457c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 45843d0b978SWang Nan char *type, char *op_result1, char *op_result2, 4592c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 460c93afadcSJin Yao struct list_head *head_config, 461c93afadcSJin Yao struct parse_events_state *parse_state) 46286470930SIngo Molnar { 46389812fc8SJiri Olsa struct perf_event_attr attr; 46443d0b978SWang Nan LIST_HEAD(config_terms); 46543d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 46661c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 46789812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 46830def61fSJin Yao int i, n, ret; 46930def61fSJin Yao bool hybrid; 47086470930SIngo Molnar 47186470930SIngo Molnar /* 47286470930SIngo Molnar * No fallback - if we cannot get a clear cache type 47386470930SIngo Molnar * then bail out: 47486470930SIngo Molnar */ 475c64e85e1SArnaldo Carvalho de Melo cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX); 47686470930SIngo Molnar if (cache_type == -1) 47789812fc8SJiri Olsa return -EINVAL; 47886470930SIngo Molnar 47943d0b978SWang Nan config_name = get_config_name(head_config); 48089812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 48189812fc8SJiri Olsa 48289812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 48389812fc8SJiri Olsa char *str = op_result[i]; 48489812fc8SJiri Olsa 485275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 48661c45981SPaul Mackerras 48761c45981SPaul Mackerras if (cache_op == -1) { 488c64e85e1SArnaldo Carvalho de Melo cache_op = parse_aliases(str, evsel__hw_cache_op, 48961c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 49061c45981SPaul Mackerras if (cache_op >= 0) { 491c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(cache_type, cache_op)) 49289812fc8SJiri Olsa return -EINVAL; 49361c45981SPaul Mackerras continue; 49461c45981SPaul Mackerras } 49561c45981SPaul Mackerras } 49661c45981SPaul Mackerras 49761c45981SPaul Mackerras if (cache_result == -1) { 498c64e85e1SArnaldo Carvalho de Melo cache_result = parse_aliases(str, evsel__hw_cache_result, 49961c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 50061c45981SPaul Mackerras if (cache_result >= 0) 50161c45981SPaul Mackerras continue; 50261c45981SPaul Mackerras } 50361c45981SPaul Mackerras } 50461c45981SPaul Mackerras 50586470930SIngo Molnar /* 50686470930SIngo Molnar * Fall back to reads: 50786470930SIngo Molnar */ 5088953645fSIngo Molnar if (cache_op == -1) 5098953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 51086470930SIngo Molnar 51186470930SIngo Molnar /* 51286470930SIngo Molnar * Fall back to accesses: 51386470930SIngo Molnar */ 51486470930SIngo Molnar if (cache_result == -1) 51586470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 51686470930SIngo Molnar 51789812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 51889812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 51989812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 52043d0b978SWang Nan 52143d0b978SWang Nan if (head_config) { 5222c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 52343d0b978SWang Nan config_term_common)) 52443d0b978SWang Nan return -EINVAL; 52543d0b978SWang Nan 52643d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 52743d0b978SWang Nan return -ENOMEM; 52843d0b978SWang Nan } 52930def61fSJin Yao 53030def61fSJin Yao ret = parse_events__add_cache_hybrid(list, idx, &attr, 53130def61fSJin Yao config_name ? : name, &config_terms, 532c93afadcSJin Yao &hybrid, parse_state); 53330def61fSJin Yao if (hybrid) 53430def61fSJin Yao return ret; 53530def61fSJin Yao 53643d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 53786470930SIngo Molnar } 53886470930SIngo Molnar 539272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 5408c619d6aSWang Nan const char *sys, const char *name) 54119658171SJiri Olsa { 5424584f084SIan Rogers const char *str; 54319658171SJiri Olsa char help[BUFSIZ]; 54419658171SJiri Olsa 545ec183d22SAdrian Hunter if (!e) 546ec183d22SAdrian Hunter return; 547ec183d22SAdrian Hunter 54819658171SJiri Olsa /* 54919658171SJiri Olsa * We get error directly from syscall errno ( > 0), 55019658171SJiri Olsa * or from encoded pointer's error ( < 0). 55119658171SJiri Olsa */ 55219658171SJiri Olsa err = abs(err); 55319658171SJiri Olsa 55419658171SJiri Olsa switch (err) { 55519658171SJiri Olsa case EACCES: 5564584f084SIan Rogers str = "can't access trace events"; 55719658171SJiri Olsa break; 55819658171SJiri Olsa case ENOENT: 5594584f084SIan Rogers str = "unknown tracepoint"; 56019658171SJiri Olsa break; 56119658171SJiri Olsa default: 5624584f084SIan Rogers str = "failed to add tracepoint"; 56319658171SJiri Olsa break; 56419658171SJiri Olsa } 56519658171SJiri Olsa 56619658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 5674584f084SIan Rogers parse_events__handle_error(e, 0, strdup(str), strdup(help)); 56819658171SJiri Olsa } 56919658171SJiri Olsa 570c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 5718c619d6aSWang Nan const char *sys_name, const char *evt_name, 572272ed29aSJiri Olsa struct parse_events_error *err, 573e637d177SHe Kuang struct list_head *head_config) 574bcd3279fSFrederic Weisbecker { 5758f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 576bcd3279fSFrederic Weisbecker 57719658171SJiri Olsa if (IS_ERR(evsel)) { 578272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 5798dd2a131SJiri Olsa return PTR_ERR(evsel); 58019658171SJiri Olsa } 581bcd3279fSFrederic Weisbecker 582e637d177SHe Kuang if (head_config) { 583e637d177SHe Kuang LIST_HEAD(config_terms); 584e637d177SHe Kuang 585e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 586e637d177SHe Kuang return -ENOMEM; 587e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 588e637d177SHe Kuang } 589e637d177SHe Kuang 590b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 59182fe1c29SArnaldo Carvalho de Melo return 0; 592bcd3279fSFrederic Weisbecker } 593bcd3279fSFrederic Weisbecker 594c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5958c619d6aSWang Nan const char *sys_name, const char *evt_name, 596272ed29aSJiri Olsa struct parse_events_error *err, 597e637d177SHe Kuang struct list_head *head_config) 598bcd3279fSFrederic Weisbecker { 59925a7d914SArnaldo Carvalho de Melo char *evt_path; 600bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 601bcd3279fSFrederic Weisbecker DIR *evt_dir; 60227bf90bfSJiri Olsa int ret = 0, found = 0; 603bcd3279fSFrederic Weisbecker 60425a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 60525a7d914SArnaldo Carvalho de Melo if (!evt_path) { 60625a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 60725a7d914SArnaldo Carvalho de Melo return -1; 60825a7d914SArnaldo Carvalho de Melo } 609bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 610bcd3279fSFrederic Weisbecker if (!evt_dir) { 61125a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 612272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 61389812fc8SJiri Olsa return -1; 614bcd3279fSFrederic Weisbecker } 615bcd3279fSFrederic Weisbecker 61689812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 617bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 618bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 619bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 620bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 621bcd3279fSFrederic Weisbecker continue; 622bcd3279fSFrederic Weisbecker 62389812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 624fb1d2edfSMasami Hiramatsu continue; 625fb1d2edfSMasami Hiramatsu 62627bf90bfSJiri Olsa found++; 62727bf90bfSJiri Olsa 628e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 629272ed29aSJiri Olsa err, head_config); 630bcd3279fSFrederic Weisbecker } 631bcd3279fSFrederic Weisbecker 63227bf90bfSJiri Olsa if (!found) { 63327bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 63427bf90bfSJiri Olsa ret = -1; 63527bf90bfSJiri Olsa } 63627bf90bfSJiri Olsa 63725a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 6380bd3f084SJiri Olsa closedir(evt_dir); 63989812fc8SJiri Olsa return ret; 640bcd3279fSFrederic Weisbecker } 641bcd3279fSFrederic Weisbecker 642c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 6438c619d6aSWang Nan const char *sys_name, const char *evt_name, 644272ed29aSJiri Olsa struct parse_events_error *err, 645e637d177SHe Kuang struct list_head *head_config) 646f35488f9SJiri Olsa { 647f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 648e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 649272ed29aSJiri Olsa err, head_config) : 650e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 651272ed29aSJiri Olsa err, head_config); 652f35488f9SJiri Olsa } 653f35488f9SJiri Olsa 654c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 6558c619d6aSWang Nan const char *sys_name, const char *evt_name, 656272ed29aSJiri Olsa struct parse_events_error *err, 657e637d177SHe Kuang struct list_head *head_config) 658f35488f9SJiri Olsa { 659f35488f9SJiri Olsa struct dirent *events_ent; 660f35488f9SJiri Olsa DIR *events_dir; 661f35488f9SJiri Olsa int ret = 0; 662f35488f9SJiri Olsa 6637014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 664f35488f9SJiri Olsa if (!events_dir) { 665272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 666f35488f9SJiri Olsa return -1; 667f35488f9SJiri Olsa } 668f35488f9SJiri Olsa 669f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 670f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 671f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 672f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 673f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 674f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 675f35488f9SJiri Olsa continue; 676f35488f9SJiri Olsa 677f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 678f35488f9SJiri Olsa continue; 679f35488f9SJiri Olsa 680f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 681272ed29aSJiri Olsa evt_name, err, head_config); 682f35488f9SJiri Olsa } 683f35488f9SJiri Olsa 684f35488f9SJiri Olsa closedir(events_dir); 685f35488f9SJiri Olsa return ret; 686f35488f9SJiri Olsa } 687f35488f9SJiri Olsa 688c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT 6894edf30e3SWang Nan struct __add_bpf_event_param { 6905d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6914edf30e3SWang Nan struct list_head *list; 69295088a59SWang Nan struct list_head *head_config; 6934edf30e3SWang Nan }; 6944edf30e3SWang Nan 695af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 6964edf30e3SWang Nan void *_param) 6974edf30e3SWang Nan { 6984edf30e3SWang Nan LIST_HEAD(new_evsels); 6994edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 7005d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 7014edf30e3SWang Nan struct list_head *list = param->list; 70232dcd021SJiri Olsa struct evsel *pos; 7034edf30e3SWang Nan int err; 7042620b7e3SArnaldo Carvalho de Melo /* 7052620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 7062620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 7072620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 7082620b7e3SArnaldo Carvalho de Melo * 7092620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 7102620b7e3SArnaldo Carvalho de Melo */ 7112620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 7122620b7e3SArnaldo Carvalho de Melo return 0; 7134edf30e3SWang Nan 7144edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 715cd102d70SWang Nan group, event, fd); 7164edf30e3SWang Nan 7175d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 7185d369a75SArnaldo Carvalho de Melo event, parse_state->error, 71995088a59SWang Nan param->head_config); 7204edf30e3SWang Nan if (err) { 72132dcd021SJiri Olsa struct evsel *evsel, *tmp; 7224edf30e3SWang Nan 7234edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 724cd102d70SWang Nan group, event); 725b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 726b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 7275eb2dd2aSJiri Olsa evsel__delete(evsel); 7284edf30e3SWang Nan } 7294edf30e3SWang Nan return err; 7304edf30e3SWang Nan } 731cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 7324edf30e3SWang Nan 733b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 7341f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 735cd102d70SWang Nan group, event, pos); 7361f45b1d4SWang Nan pos->bpf_fd = fd; 737af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 7381f45b1d4SWang Nan } 7394edf30e3SWang Nan list_splice(&new_evsels, list); 7404edf30e3SWang Nan return 0; 7414edf30e3SWang Nan } 7424edf30e3SWang Nan 7435d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 74484c86ca1SWang Nan struct list_head *list, 74595088a59SWang Nan struct bpf_object *obj, 74695088a59SWang Nan struct list_head *head_config) 74784c86ca1SWang Nan { 74884c86ca1SWang Nan int err; 74984c86ca1SWang Nan char errbuf[BUFSIZ]; 7505d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 751aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 75284c86ca1SWang Nan 75384c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 75484c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 75584c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 75684c86ca1SWang Nan err = -EINVAL; 75784c86ca1SWang Nan goto errout; 75884c86ca1SWang Nan } 75984c86ca1SWang Nan 76084c86ca1SWang Nan /* 761aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 762aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 763aa3abf30SWang Nan * created when failure. 764aa3abf30SWang Nan */ 765aa3abf30SWang Nan if (!registered_unprobe_atexit) { 766aa3abf30SWang Nan atexit(bpf__clear); 767aa3abf30SWang Nan registered_unprobe_atexit = true; 768aa3abf30SWang Nan } 769aa3abf30SWang Nan 770aa3abf30SWang Nan err = bpf__probe(obj); 771aa3abf30SWang Nan if (err) { 772aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 773aa3abf30SWang Nan goto errout; 774aa3abf30SWang Nan } 775aa3abf30SWang Nan 7761e5e3ee8SWang Nan err = bpf__load(obj); 7771e5e3ee8SWang Nan if (err) { 7781e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 7791e5e3ee8SWang Nan goto errout; 7801e5e3ee8SWang Nan } 7811e5e3ee8SWang Nan 782cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 7834edf30e3SWang Nan if (err) { 7844edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 7854edf30e3SWang Nan "Attach events in BPF object failed"); 7864edf30e3SWang Nan goto errout; 7874edf30e3SWang Nan } 7884edf30e3SWang Nan 7894edf30e3SWang Nan return 0; 79084c86ca1SWang Nan errout: 7915f634c8eSIan Rogers parse_events__handle_error(parse_state->error, 0, 7925f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 79384c86ca1SWang Nan return err; 79484c86ca1SWang Nan } 79584c86ca1SWang Nan 796a34f3be7SWang Nan static int 7975d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 798a34f3be7SWang Nan struct bpf_object *obj, 799a34f3be7SWang Nan struct list_head *head_config) 800a34f3be7SWang Nan { 801a34f3be7SWang Nan struct parse_events_term *term; 802a34f3be7SWang Nan int error_pos; 803a34f3be7SWang Nan 804a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 805a34f3be7SWang Nan return 0; 806a34f3be7SWang Nan 807a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 808a34f3be7SWang Nan int err; 809a34f3be7SWang Nan 810a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 8115f634c8eSIan Rogers parse_events__handle_error(parse_state->error, term->err_term, 8125f634c8eSIan Rogers strdup("Invalid config term for BPF object"), 8135f634c8eSIan Rogers NULL); 814a34f3be7SWang Nan return -EINVAL; 815a34f3be7SWang Nan } 816a34f3be7SWang Nan 8175d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 818a34f3be7SWang Nan if (err) { 8195f634c8eSIan Rogers char errbuf[BUFSIZ]; 8205f634c8eSIan Rogers int idx; 8215f634c8eSIan Rogers 8225d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 823a34f3be7SWang Nan &error_pos, err, errbuf, 824a34f3be7SWang Nan sizeof(errbuf)); 8255f634c8eSIan Rogers 8265f634c8eSIan Rogers if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 8275f634c8eSIan Rogers idx = term->err_val; 8285f634c8eSIan Rogers else 8295f634c8eSIan Rogers idx = term->err_term + error_pos; 8305f634c8eSIan Rogers 8315f634c8eSIan Rogers parse_events__handle_error(parse_state->error, idx, 8325f634c8eSIan Rogers strdup(errbuf), 8335f634c8eSIan Rogers strdup( 8347630b3e2SWang Nan "Hint:\tValid config terms:\n" 835e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 836e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 8377630b3e2SWang Nan "\n" 838e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 8395f634c8eSIan Rogers " \t(add -v to see detail)")); 840a34f3be7SWang Nan return err; 841a34f3be7SWang Nan } 842a34f3be7SWang Nan } 843a34f3be7SWang Nan return 0; 844a34f3be7SWang Nan } 845a34f3be7SWang Nan 84695088a59SWang Nan /* 84795088a59SWang Nan * Split config terms: 84895088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 84995088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 85095088a59SWang Nan * events in bpf.c. 85195088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 85295088a59SWang Nan * with parse_events_config_bpf. 85395088a59SWang Nan * 85495088a59SWang Nan * Move object config terms from the first list to obj_head_config. 85595088a59SWang Nan */ 85695088a59SWang Nan static void 85795088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 85895088a59SWang Nan struct list_head *obj_head_config) 85995088a59SWang Nan { 86095088a59SWang Nan struct parse_events_term *term, *temp; 86195088a59SWang Nan 86295088a59SWang Nan /* 8634d39c89fSIngo Molnar * Currently, all possible user config term 86495088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 8654d39c89fSIngo Molnar * happens to be a good flag. 86695088a59SWang Nan * 86795088a59SWang Nan * See parse_events_config_bpf() and 86895088a59SWang Nan * config_term_tracepoint(). 86995088a59SWang Nan */ 87095088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 87195088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 87295088a59SWang Nan list_move_tail(&term->list, obj_head_config); 87395088a59SWang Nan } 87495088a59SWang Nan 8755d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 87684c86ca1SWang Nan struct list_head *list, 877d509db04SWang Nan char *bpf_file_name, 878a34f3be7SWang Nan bool source, 879a34f3be7SWang Nan struct list_head *head_config) 88084c86ca1SWang Nan { 881a34f3be7SWang Nan int err; 88295088a59SWang Nan struct bpf_object *obj; 88395088a59SWang Nan LIST_HEAD(obj_head_config); 88495088a59SWang Nan 88595088a59SWang Nan if (head_config) 88695088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 88784c86ca1SWang Nan 888d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8896371ca3bSWang Nan if (IS_ERR(obj)) { 89084c86ca1SWang Nan char errbuf[BUFSIZ]; 89184c86ca1SWang Nan 8926371ca3bSWang Nan err = PTR_ERR(obj); 89384c86ca1SWang Nan 89484c86ca1SWang Nan if (err == -ENOTSUP) 89584c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 89684c86ca1SWang Nan "BPF support is not compiled"); 89784c86ca1SWang Nan else 898d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 899d3e0ce39SWang Nan source, 900d3e0ce39SWang Nan -err, errbuf, 901d3e0ce39SWang Nan sizeof(errbuf)); 90284c86ca1SWang Nan 9035f634c8eSIan Rogers parse_events__handle_error(parse_state->error, 0, 9045f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 90584c86ca1SWang Nan return err; 90684c86ca1SWang Nan } 90784c86ca1SWang Nan 9085d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 909a34f3be7SWang Nan if (err) 910a34f3be7SWang Nan return err; 9115d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 91295088a59SWang Nan 91395088a59SWang Nan /* 91495088a59SWang Nan * Caller doesn't know anything about obj_head_config, 9154d39c89fSIngo Molnar * so combine them together again before returning. 91695088a59SWang Nan */ 91795088a59SWang Nan if (head_config) 91895088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 91995088a59SWang Nan return err; 92084c86ca1SWang Nan } 921c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT 922c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 923c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 924c18cf78dSArnaldo Carvalho de Melo struct bpf_object *obj __maybe_unused, 925c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 926c18cf78dSArnaldo Carvalho de Melo { 927c18cf78dSArnaldo Carvalho de Melo parse_events__handle_error(parse_state->error, 0, 928c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 929c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 930c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 931c18cf78dSArnaldo Carvalho de Melo } 932c18cf78dSArnaldo Carvalho de Melo 933c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 934c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 935c18cf78dSArnaldo Carvalho de Melo char *bpf_file_name __maybe_unused, 936c18cf78dSArnaldo Carvalho de Melo bool source __maybe_unused, 937c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 938c18cf78dSArnaldo Carvalho de Melo { 939c18cf78dSArnaldo Carvalho de Melo parse_events__handle_error(parse_state->error, 0, 940c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 941c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 942c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 943c18cf78dSArnaldo Carvalho de Melo } 944c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT 94584c86ca1SWang Nan 94689812fc8SJiri Olsa static int 94789812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 9481b290d67SFrederic Weisbecker { 9491b290d67SFrederic Weisbecker int i; 9501b290d67SFrederic Weisbecker 9511b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 95289812fc8SJiri Olsa if (!type || !type[i]) 9531b290d67SFrederic Weisbecker break; 9541b290d67SFrederic Weisbecker 9557582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 9567582732fSJiri Olsa do { \ 9577582732fSJiri Olsa if (attr->bp_type & bit) \ 9587582732fSJiri Olsa return -EINVAL; \ 9597582732fSJiri Olsa else \ 9607582732fSJiri Olsa attr->bp_type |= bit; \ 9617582732fSJiri Olsa } while (0) 9627582732fSJiri Olsa 9631b290d67SFrederic Weisbecker switch (type[i]) { 9641b290d67SFrederic Weisbecker case 'r': 9657582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 9661b290d67SFrederic Weisbecker break; 9671b290d67SFrederic Weisbecker case 'w': 9687582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 9691b290d67SFrederic Weisbecker break; 9701b290d67SFrederic Weisbecker case 'x': 9717582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 9721b290d67SFrederic Weisbecker break; 9731b290d67SFrederic Weisbecker default: 97489812fc8SJiri Olsa return -EINVAL; 9751b290d67SFrederic Weisbecker } 9761b290d67SFrederic Weisbecker } 97789812fc8SJiri Olsa 9787582732fSJiri Olsa #undef CHECK_SET_TYPE 9797582732fSJiri Olsa 9801b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 9811b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 9821b290d67SFrederic Weisbecker 98389812fc8SJiri Olsa return 0; 9841b290d67SFrederic Weisbecker } 9851b290d67SFrederic Weisbecker 986c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 987aa98d848SIan Rogers u64 addr, char *type, u64 len) 9881b290d67SFrederic Weisbecker { 98989812fc8SJiri Olsa struct perf_event_attr attr; 9901b290d67SFrederic Weisbecker 99189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 992aa98d848SIan Rogers attr.bp_addr = addr; 9931b290d67SFrederic Weisbecker 99489812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 99589812fc8SJiri Olsa return -EINVAL; 9961b290d67SFrederic Weisbecker 9973741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 9983741eb9fSJacob Shin if (!len) { 99989812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 10003741eb9fSJacob Shin len = sizeof(long); 1001aa59a485SFrederic Weisbecker else 10023741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 10033741eb9fSJacob Shin } 10043741eb9fSJacob Shin 10053741eb9fSJacob Shin attr.bp_len = len; 1006aa59a485SFrederic Weisbecker 100789812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 10084a841d65SJovi Zhang attr.sample_period = 1; 10091b290d67SFrederic Weisbecker 1010930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 10111b290d67SFrederic Weisbecker } 10121b290d67SFrederic Weisbecker 10133b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 10143b0e371cSJiri Olsa struct parse_events_error *err, 10153b0e371cSJiri Olsa int type) 10163b0e371cSJiri Olsa { 10173b0e371cSJiri Olsa if (type == term->type_val) 10183b0e371cSJiri Olsa return 0; 10193b0e371cSJiri Olsa 10203b0e371cSJiri Olsa if (err) { 1021448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1022448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 1023448d732cSIan Rogers ? strdup("expected numeric value") 1024448d732cSIan Rogers : strdup("expected string value"), 1025448d732cSIan Rogers NULL); 10263b0e371cSJiri Olsa } 10273b0e371cSJiri Olsa return -EINVAL; 10283b0e371cSJiri Olsa } 10293b0e371cSJiri Olsa 103017cb5f84SWang Nan /* 103117cb5f84SWang Nan * Update according to parse-events.l 103217cb5f84SWang Nan */ 103317cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 103417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 103517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 103617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 103717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 103817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 103917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 104017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 104117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 104217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 104317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 104417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 104517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 104617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 1047792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 10482fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 1049626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 1050626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 1051dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 1052064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 10531b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 1054eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 105517cb5f84SWang Nan }; 105617cb5f84SWang Nan 10571669e509SWang Nan static bool config_term_shrinked; 10581669e509SWang Nan 10591669e509SWang Nan static bool 10601669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 10611669e509SWang Nan { 1062448d732cSIan Rogers char *err_str; 1063448d732cSIan Rogers 10641669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 1065448d732cSIan Rogers parse_events__handle_error(err, -1, 1066448d732cSIan Rogers strdup("Invalid term_type"), NULL); 10671669e509SWang Nan return false; 10681669e509SWang Nan } 10691669e509SWang Nan if (!config_term_shrinked) 10701669e509SWang Nan return true; 10711669e509SWang Nan 10721669e509SWang Nan switch (term_type) { 10731669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 10741669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 10751669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 10761669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 107706835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1078064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 10791669e509SWang Nan return true; 10801669e509SWang Nan default: 10811669e509SWang Nan if (!err) 10821669e509SWang Nan return false; 10831669e509SWang Nan 10841669e509SWang Nan /* term_type is validated so indexing is safe */ 1085448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 1086448d732cSIan Rogers config_term_names[term_type]) >= 0) 1087448d732cSIan Rogers parse_events__handle_error(err, -1, err_str, NULL); 10881669e509SWang Nan return false; 10891669e509SWang Nan } 10901669e509SWang Nan } 10911669e509SWang Nan 10921669e509SWang Nan void parse_events__shrink_config_terms(void) 10931669e509SWang Nan { 10941669e509SWang Nan config_term_shrinked = true; 10951669e509SWang Nan } 10961669e509SWang Nan 10970b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 10983b0e371cSJiri Olsa struct parse_events_term *term, 10993b0e371cSJiri Olsa struct parse_events_error *err) 11008f707d84SJiri Olsa { 110116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 110216fa7e82SJiri Olsa do { \ 11033b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 110416fa7e82SJiri Olsa return -EINVAL; \ 110516fa7e82SJiri Olsa } while (0) 110616fa7e82SJiri Olsa 110716fa7e82SJiri Olsa switch (term->type_term) { 11088f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 110916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11108f707d84SJiri Olsa attr->config = term->val.num; 11118f707d84SJiri Olsa break; 11128f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 111316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11148f707d84SJiri Olsa attr->config1 = term->val.num; 11158f707d84SJiri Olsa break; 11168f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 111716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11188f707d84SJiri Olsa attr->config2 = term->val.num; 11198f707d84SJiri Olsa break; 11208f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 112116fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11228f707d84SJiri Olsa break; 112309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 112409af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 112509af2a55SNamhyung Kim break; 11268f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1127ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1128ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1129448d732cSIan Rogers parse_branch_str(term->val.str, 1130448d732cSIan Rogers &attr->branch_sample_type)) { 1131448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1132448d732cSIan Rogers strdup("invalid branch sample type"), 1133448d732cSIan Rogers NULL); 1134ac12f676SAndi Kleen return -EINVAL; 1135ac12f676SAndi Kleen } 11368f707d84SJiri Olsa break; 113732067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 113832067712SKan Liang CHECK_TYPE_VAL(NUM); 113932067712SKan Liang if (term->val.num > 1) { 1140448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1141448d732cSIan Rogers strdup("expected 0 or 1"), 1142448d732cSIan Rogers NULL); 114332067712SKan Liang return -EINVAL; 114432067712SKan Liang } 114532067712SKan Liang break; 1146d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1147d457c963SKan Liang CHECK_TYPE_VAL(STR); 1148d457c963SKan Liang break; 1149d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1150d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1151d457c963SKan Liang break; 1152374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1153374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1154374ce938SWang Nan break; 1155374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1156374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1157374ce938SWang Nan break; 1158626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1159626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1160626a6b78SWang Nan break; 1161626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1162626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1163626a6b78SWang Nan break; 11646b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 11656b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 11666b5fc39bSJiri Olsa break; 1167792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1168792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1169792d48b4SArnaldo Carvalho de Melo break; 11702fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 11712fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 11722fda5adaSArnaldo Carvalho de Melo break; 1173064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1174064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1175064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 1176448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1177448d732cSIan Rogers strdup("expected 0 or 1"), 1178448d732cSIan Rogers NULL); 1179064b4e82SJin Yao return -EINVAL; 1180064b4e82SJin Yao } 1181064b4e82SJin Yao break; 11821b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 11831b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 11841b992154SAdrian Hunter break; 1185eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1186eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 1187eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 1188eb7a52d4SAdrian Hunter parse_events__handle_error(err, term->err_val, 1189eb7a52d4SAdrian Hunter strdup("too big"), 1190eb7a52d4SAdrian Hunter NULL); 1191eb7a52d4SAdrian Hunter return -EINVAL; 1192eb7a52d4SAdrian Hunter } 1193eb7a52d4SAdrian Hunter break; 11948f707d84SJiri Olsa default: 1195448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1196448d732cSIan Rogers strdup("unknown term"), 1197448d732cSIan Rogers parse_events_formats_error_string(NULL)); 11988f707d84SJiri Olsa return -EINVAL; 11998f707d84SJiri Olsa } 120016fa7e82SJiri Olsa 12011669e509SWang Nan /* 12024d39c89fSIngo Molnar * Check term availability after basic checking so 12031669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 12041669e509SWang Nan * 12054d39c89fSIngo Molnar * If check availability at the entry of this function, 12061669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 12071669e509SWang Nan * if an invalid config term is provided for legacy events 12081669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 12091669e509SWang Nan */ 12101669e509SWang Nan if (!config_term_avail(term->type_term, err)) 12111669e509SWang Nan return -EINVAL; 12128f707d84SJiri Olsa return 0; 121316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 12148f707d84SJiri Olsa } 12158f707d84SJiri Olsa 12160b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 12170b8891a8SHe Kuang struct parse_events_term *term, 12180b8891a8SHe Kuang struct parse_events_error *err) 12190b8891a8SHe Kuang { 1220dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1221dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 12220b8891a8SHe Kuang /* 12230b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 12240b8891a8SHe Kuang * at this point what type they need to have. 12250b8891a8SHe Kuang */ 12260b8891a8SHe Kuang return 0; 12270b8891a8SHe Kuang else 12280b8891a8SHe Kuang return config_term_common(attr, term, err); 12290b8891a8SHe Kuang } 12300b8891a8SHe Kuang 1231e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1232e637d177SHe Kuang struct parse_events_term *term, 1233e637d177SHe Kuang struct parse_events_error *err) 1234e637d177SHe Kuang { 1235e637d177SHe Kuang switch (term->type_term) { 1236e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1237e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1238374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1239374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1240792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 12412fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1242626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1243626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 12441b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1245eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1246e637d177SHe Kuang return config_term_common(attr, term, err); 1247e637d177SHe Kuang default: 1248e637d177SHe Kuang if (err) { 1249448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1250448d732cSIan Rogers strdup("unknown term"), 1251448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1252e637d177SHe Kuang } 1253e637d177SHe Kuang return -EINVAL; 1254e637d177SHe Kuang } 1255e637d177SHe Kuang 1256e637d177SHe Kuang return 0; 1257e637d177SHe Kuang } 1258e637d177SHe Kuang 12598f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 12603b0e371cSJiri Olsa struct list_head *head, 12610b8891a8SHe Kuang struct parse_events_error *err, 12620b8891a8SHe Kuang config_term_func_t config_term) 12638f707d84SJiri Olsa { 12646cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 12658f707d84SJiri Olsa 12668f707d84SJiri Olsa list_for_each_entry(term, head, list) 12673b0e371cSJiri Olsa if (config_term(attr, term, err)) 12688f707d84SJiri Olsa return -EINVAL; 12698f707d84SJiri Olsa 12708f707d84SJiri Olsa return 0; 12718f707d84SJiri Olsa } 12728f707d84SJiri Olsa 1273930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1274930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1275930a2e29SJiri Olsa { 127605e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 127735ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *__t; \ 1278930a2e29SJiri Olsa \ 1279930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1280930a2e29SJiri Olsa if (!__t) \ 1281930a2e29SJiri Olsa return -ENOMEM; \ 1282930a2e29SJiri Olsa \ 1283930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 128435ac0cadSArnaldo Carvalho de Melo __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 128505e54e23SIan Rogers __t->weak = __weak; \ 1286e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1287e884602bSLeo Yan 128805e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1289e884602bSLeo Yan do { \ 129005e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1291e884602bSLeo Yan __t->val.__name = __val; \ 1292e884602bSLeo Yan } while (0) 1293e884602bSLeo Yan 129405e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1295e884602bSLeo Yan do { \ 129605e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 12973220fb8dSLeo Yan __t->val.str = strdup(__val); \ 12983220fb8dSLeo Yan if (!__t->val.str) { \ 12993220fb8dSLeo Yan zfree(&__t); \ 13003220fb8dSLeo Yan return -ENOMEM; \ 13013220fb8dSLeo Yan } \ 13023220fb8dSLeo Yan __t->free_str = true; \ 1303930a2e29SJiri Olsa } while (0) 1304930a2e29SJiri Olsa 1305930a2e29SJiri Olsa struct parse_events_term *term; 1306930a2e29SJiri Olsa 1307930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1308930a2e29SJiri Olsa switch (term->type_term) { 1309ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 131005e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 131132067712SKan Liang break; 131209af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 131305e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 131409af2a55SNamhyung Kim break; 131532067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 131605e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 131732067712SKan Liang break; 1318d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 131905e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1320d457c963SKan Liang break; 1321ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 132205e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1323ac12f676SAndi Kleen break; 1324d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1325e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 132605e54e23SIan Rogers term->val.num, term->weak); 1327d457c963SKan Liang break; 1328374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1329e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 133005e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1331374ce938SWang Nan break; 1332374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1333e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 133405e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1335374ce938SWang Nan break; 1336792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1337e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 133805e54e23SIan Rogers term->val.num, term->weak); 1339792d48b4SArnaldo Carvalho de Melo break; 13402fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1341e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 134205e54e23SIan Rogers term->val.num, term->weak); 13432fda5adaSArnaldo Carvalho de Melo break; 1344626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1345e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 134605e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1347626a6b78SWang Nan break; 1348626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1349e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 135005e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1351626a6b78SWang Nan break; 1352dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 135305e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1354dd60fba7SMathieu Poirier break; 1355064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1356e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 135705e54e23SIan Rogers term->val.num ? true : false, term->weak); 1358064b4e82SJin Yao break; 13591b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1360e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 136105e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 13621b992154SAdrian Hunter break; 1363eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1364e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 136505e54e23SIan Rogers term->val.num, term->weak); 1366eb7a52d4SAdrian Hunter break; 1367930a2e29SJiri Olsa default: 1368930a2e29SJiri Olsa break; 1369930a2e29SJiri Olsa } 1370930a2e29SJiri Olsa } 1371a1ac7de6SAdrian Hunter return 0; 1372a1ac7de6SAdrian Hunter } 1373a1ac7de6SAdrian Hunter 1374a1ac7de6SAdrian Hunter /* 137535ac0cadSArnaldo Carvalho de Melo * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1376a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1377a1ac7de6SAdrian Hunter */ 1378a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1379a1ac7de6SAdrian Hunter struct list_head *head_terms) 1380a1ac7de6SAdrian Hunter { 1381a1ac7de6SAdrian Hunter struct parse_events_term *term; 1382a1ac7de6SAdrian Hunter u64 bits = 0; 1383a1ac7de6SAdrian Hunter int type; 1384a1ac7de6SAdrian Hunter 1385a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1386a1ac7de6SAdrian Hunter switch (term->type_term) { 1387a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 1388a1ac7de6SAdrian Hunter type = perf_pmu__format_type(&pmu->format, term->config); 1389a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1390a1ac7de6SAdrian Hunter continue; 1391a1ac7de6SAdrian Hunter bits |= perf_pmu__format_bits(&pmu->format, term->config); 1392a1ac7de6SAdrian Hunter break; 1393a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1394a1ac7de6SAdrian Hunter bits = ~(u64)0; 1395a1ac7de6SAdrian Hunter break; 1396a1ac7de6SAdrian Hunter default: 1397a1ac7de6SAdrian Hunter break; 1398a1ac7de6SAdrian Hunter } 1399a1ac7de6SAdrian Hunter } 1400a1ac7de6SAdrian Hunter 1401a1ac7de6SAdrian Hunter if (bits) 140205e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1403a1ac7de6SAdrian Hunter 1404a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1405930a2e29SJiri Olsa return 0; 1406930a2e29SJiri Olsa } 1407930a2e29SJiri Olsa 1408e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 14098c619d6aSWang Nan const char *sys, const char *event, 1410272ed29aSJiri Olsa struct parse_events_error *err, 1411e637d177SHe Kuang struct list_head *head_config) 1412e637d177SHe Kuang { 1413e637d177SHe Kuang if (head_config) { 1414e637d177SHe Kuang struct perf_event_attr attr; 1415e637d177SHe Kuang 1416272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1417e637d177SHe Kuang config_term_tracepoint)) 1418e637d177SHe Kuang return -EINVAL; 1419e637d177SHe Kuang } 1420e637d177SHe Kuang 1421e637d177SHe Kuang if (strpbrk(sys, "*?")) 1422e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1423272ed29aSJiri Olsa err, head_config); 1424e637d177SHe Kuang else 1425e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1426272ed29aSJiri Olsa err, head_config); 1427e637d177SHe Kuang } 1428e637d177SHe Kuang 14295d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 143087d650beSJiri Olsa struct list_head *list, 1431b527bab5SRobert Richter u32 type, u64 config, 14328f707d84SJiri Olsa struct list_head *head_config) 143374d5b588SJaswinder Singh Rajput { 143489812fc8SJiri Olsa struct perf_event_attr attr; 1435930a2e29SJiri Olsa LIST_HEAD(config_terms); 14369cbfa2f6SJin Yao bool hybrid; 14379cbfa2f6SJin Yao int ret; 143874d5b588SJaswinder Singh Rajput 143989812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 144089812fc8SJiri Olsa attr.type = type; 144189812fc8SJiri Olsa attr.config = config; 14428f707d84SJiri Olsa 1443930a2e29SJiri Olsa if (head_config) { 14445d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 14450b8891a8SHe Kuang config_term_common)) 14468f707d84SJiri Olsa return -EINVAL; 14478f707d84SJiri Olsa 1448930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1449930a2e29SJiri Olsa return -ENOMEM; 1450930a2e29SJiri Olsa } 1451930a2e29SJiri Olsa 14529cbfa2f6SJin Yao ret = parse_events__add_numeric_hybrid(parse_state, list, &attr, 14539cbfa2f6SJin Yao get_config_name(head_config), 14549cbfa2f6SJin Yao &config_terms, &hybrid); 14559cbfa2f6SJin Yao if (hybrid) 14569cbfa2f6SJin Yao return ret; 14579cbfa2f6SJin Yao 14585d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 145910bf358aSWang Nan get_config_name(head_config), &config_terms); 1460b908debdSIngo Molnar } 1461b908debdSIngo Molnar 1462f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1463f0fbb114SAndi Kleen struct list_head *list, 1464f0fbb114SAndi Kleen enum perf_tool_event tool_event) 1465f0fbb114SAndi Kleen { 1466f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1467f0fbb114SAndi Kleen } 1468f0fbb114SAndi Kleen 1469064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1470064b4e82SJin Yao { 147135ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *term; 1472064b4e82SJin Yao 1473064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 147435ac0cadSArnaldo Carvalho de Melo if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1475064b4e82SJin Yao return term->val.percore; 1476064b4e82SJin Yao } 1477064b4e82SJin Yao 1478064b4e82SJin Yao return false; 1479064b4e82SJin Yao } 1480064b4e82SJin Yao 1481*5e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state, 1482*5e4edd1fSJin Yao struct list_head *list, char *name, 1483*5e4edd1fSJin Yao struct list_head *head_config) 1484*5e4edd1fSJin Yao { 1485*5e4edd1fSJin Yao struct parse_events_term *term; 1486*5e4edd1fSJin Yao int ret = -1; 1487*5e4edd1fSJin Yao 1488*5e4edd1fSJin Yao if (parse_state->fake_pmu || !head_config || list_empty(head_config) || 1489*5e4edd1fSJin Yao !perf_pmu__is_hybrid(name)) { 1490*5e4edd1fSJin Yao return -1; 1491*5e4edd1fSJin Yao } 1492*5e4edd1fSJin Yao 1493*5e4edd1fSJin Yao /* 1494*5e4edd1fSJin Yao * More than one term in list. 1495*5e4edd1fSJin Yao */ 1496*5e4edd1fSJin Yao if (head_config->next && head_config->next->next != head_config) 1497*5e4edd1fSJin Yao return -1; 1498*5e4edd1fSJin Yao 1499*5e4edd1fSJin Yao term = list_first_entry(head_config, struct parse_events_term, list); 1500*5e4edd1fSJin Yao if (term && term->config && strcmp(term->config, "event")) { 1501*5e4edd1fSJin Yao ret = parse_events__with_hybrid_pmu(parse_state, term->config, 1502*5e4edd1fSJin Yao name, list); 1503*5e4edd1fSJin Yao } 1504*5e4edd1fSJin Yao 1505*5e4edd1fSJin Yao return ret; 1506*5e4edd1fSJin Yao } 1507*5e4edd1fSJin Yao 1508c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 150936adec85SJiri Olsa struct list_head *list, char *name, 15103cdc5c2cSKan Liang struct list_head *head_config, 15113cdc5c2cSKan Liang bool auto_merge_stats, 15123cdc5c2cSKan Liang bool use_alias) 15135f537a26SJiri Olsa { 15145f537a26SJiri Olsa struct perf_event_attr attr; 151546441bdcSMatt Fleming struct perf_pmu_info info; 15165f537a26SJiri Olsa struct perf_pmu *pmu; 151732dcd021SJiri Olsa struct evsel *evsel; 1518333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 15193cdc5c2cSKan Liang bool use_uncore_alias; 1520930a2e29SJiri Olsa LIST_HEAD(config_terms); 15215f537a26SJiri Olsa 15224ac22b48SIan Rogers if (verbose > 1) { 15234ac22b48SIan Rogers fprintf(stderr, "Attempting to add event pmu '%s' with '", 15244ac22b48SIan Rogers name); 15254ac22b48SIan Rogers if (head_config) { 15264ac22b48SIan Rogers struct parse_events_term *term; 15274ac22b48SIan Rogers 15284ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15294ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15304ac22b48SIan Rogers } 15314ac22b48SIan Rogers } 15324ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15334ac22b48SIan Rogers } 15344ac22b48SIan Rogers 1535387ad33fSJiri Olsa pmu = parse_state->fake_pmu ?: perf_pmu__find(name); 1536333b5665SAndi Kleen if (!pmu) { 1537448d732cSIan Rogers char *err_str; 1538448d732cSIan Rogers 1539448d732cSIan Rogers if (asprintf(&err_str, 1540333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1541448d732cSIan Rogers name) >= 0) 1542a910e466SIan Rogers parse_events__handle_error(err, 0, err_str, NULL); 15435f537a26SJiri Olsa return -EINVAL; 1544333b5665SAndi Kleen } 15455f537a26SJiri Olsa 1546dc0a6202SAdrian Hunter if (pmu->default_config) { 1547dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1548dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1549dc0a6202SAdrian Hunter } else { 15505f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1551dc0a6202SAdrian Hunter } 15525f537a26SJiri Olsa 15533cdc5c2cSKan Liang use_uncore_alias = (pmu->is_uncore && use_alias); 15543cdc5c2cSKan Liang 1555ad962273SAdrian Hunter if (!head_config) { 1556ad962273SAdrian Hunter attr.type = pmu->type; 155770943490SStephane Eranian evsel = __add_event(list, &parse_state->idx, &attr, true, NULL, 155870943490SStephane Eranian pmu, NULL, auto_merge_stats, NULL); 15598c5421c0SAgustin Vega-Frias if (evsel) { 1560d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 15613cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 15628c5421c0SAgustin Vega-Frias return 0; 15638c5421c0SAgustin Vega-Frias } else { 15648c5421c0SAgustin Vega-Frias return -ENOMEM; 15658c5421c0SAgustin Vega-Frias } 1566ad962273SAdrian Hunter } 1567ad962273SAdrian Hunter 1568387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) 1569a6146d50SZheng Yan return -EINVAL; 1570a6146d50SZheng Yan 15714ac22b48SIan Rogers if (verbose > 1) { 15724ac22b48SIan Rogers fprintf(stderr, "After aliases, add event pmu '%s' with '", 15734ac22b48SIan Rogers name); 15744ac22b48SIan Rogers if (head_config) { 15754ac22b48SIan Rogers struct parse_events_term *term; 15764ac22b48SIan Rogers 15774ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15784ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15794ac22b48SIan Rogers } 15804ac22b48SIan Rogers } 15814ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15824ac22b48SIan Rogers } 15834ac22b48SIan Rogers 15845f537a26SJiri Olsa /* 15855f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 15865f537a26SJiri Olsa * return value when called with fail == 0 ;) 15875f537a26SJiri Olsa */ 15885d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1589c056ba6aSJiri Olsa return -EINVAL; 15905f537a26SJiri Olsa 1591930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1592930a2e29SJiri Olsa return -ENOMEM; 1593930a2e29SJiri Olsa 1594a1ac7de6SAdrian Hunter /* 1595a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1596a1ac7de6SAdrian Hunter * changed by the user. 1597a1ac7de6SAdrian Hunter */ 1598a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1599a1ac7de6SAdrian Hunter return -ENOMEM; 1600a1ac7de6SAdrian Hunter 1601*5e4edd1fSJin Yao if (!parse_events__inside_hybrid_pmu(parse_state, list, name, 1602*5e4edd1fSJin Yao head_config)) { 1603*5e4edd1fSJin Yao return 0; 1604*5e4edd1fSJin Yao } 1605*5e4edd1fSJin Yao 1606387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 160735ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *pos, *tmp; 160838f2c422SIan Rogers 160938f2c422SIan Rogers list_for_each_entry_safe(pos, tmp, &config_terms, list) { 161038f2c422SIan Rogers list_del_init(&pos->list); 16117fcdccd4SArnaldo Carvalho de Melo if (pos->free_str) 1612e8dfb818SIan Rogers zfree(&pos->val.str); 161338f2c422SIan Rogers free(pos); 161438f2c422SIan Rogers } 16155f537a26SJiri Olsa return -EINVAL; 161638f2c422SIan Rogers } 16175f537a26SJiri Olsa 161870943490SStephane Eranian evsel = __add_event(list, &parse_state->idx, &attr, true, 161966ec1191SMark Rutland get_config_name(head_config), pmu, 1620f0fbb114SAndi Kleen &config_terms, auto_merge_stats, NULL); 162133321a06SIan Rogers if (!evsel) 162233321a06SIan Rogers return -ENOMEM; 162333321a06SIan Rogers 162412279429SJin Yao if (evsel->name) 162512279429SJin Yao evsel->use_config_name = true; 162612279429SJin Yao 162733321a06SIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 162833321a06SIan Rogers evsel->use_uncore_alias = use_uncore_alias; 162933321a06SIan Rogers evsel->percore = config_term_percore(&evsel->config_terms); 163033321a06SIan Rogers 163133321a06SIan Rogers if (parse_state->fake_pmu) 163233321a06SIan Rogers return 0; 163333321a06SIan Rogers 163446441bdcSMatt Fleming evsel->unit = info.unit; 163546441bdcSMatt Fleming evsel->scale = info.scale; 1636044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 16371d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 163837932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 163996284814SAndi Kleen evsel->metric_name = info.metric_name; 164033321a06SIan Rogers return 0; 16415f537a26SJiri Olsa } 16425f537a26SJiri Olsa 16435d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 16442073ad33SAndi Kleen char *str, struct list_head **listp) 16452073ad33SAndi Kleen { 16462073ad33SAndi Kleen struct parse_events_term *term; 16472073ad33SAndi Kleen struct list_head *list; 16482073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 16492073ad33SAndi Kleen int ok = 0; 16502073ad33SAndi Kleen 16512073ad33SAndi Kleen *listp = NULL; 16522073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 16532073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 16542073ad33SAndi Kleen if (!list) 16552073ad33SAndi Kleen return -1; 16562073ad33SAndi Kleen INIT_LIST_HEAD(list); 16572073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 16582073ad33SAndi Kleen struct perf_pmu_alias *alias; 16592073ad33SAndi Kleen 16602073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 16612073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 1662b6645a72SIan Rogers struct list_head *head; 1663b6645a72SIan Rogers char *config; 1664b6645a72SIan Rogers 16652073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 16662073ad33SAndi Kleen if (!head) 16672073ad33SAndi Kleen return -1; 16682073ad33SAndi Kleen INIT_LIST_HEAD(head); 1669b6645a72SIan Rogers config = strdup(str); 1670b6645a72SIan Rogers if (!config) 16712073ad33SAndi Kleen return -1; 1672b6645a72SIan Rogers if (parse_events_term__num(&term, 1673b6645a72SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 1674b6645a72SIan Rogers config, 1, false, &config, 1675b6645a72SIan Rogers NULL) < 0) { 1676b6645a72SIan Rogers free(list); 1677b6645a72SIan Rogers free(config); 1678b6645a72SIan Rogers return -1; 1679b6645a72SIan Rogers } 16802073ad33SAndi Kleen list_add_tail(&term->list, head); 16812073ad33SAndi Kleen 1682c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 16833cdc5c2cSKan Liang pmu->name, head, 16843cdc5c2cSKan Liang true, true)) { 1685d4953f7eSIan Rogers pr_debug("%s -> %s/%s/\n", str, 16862073ad33SAndi Kleen pmu->name, alias->str); 16872073ad33SAndi Kleen ok++; 16882073ad33SAndi Kleen } 16892073ad33SAndi Kleen 16902073ad33SAndi Kleen parse_events_terms__delete(head); 16912073ad33SAndi Kleen } 16922073ad33SAndi Kleen } 16932073ad33SAndi Kleen } 1694b6645a72SIan Rogers if (!ok) { 1695b6645a72SIan Rogers free(list); 16962073ad33SAndi Kleen return -1; 1697b6645a72SIan Rogers } 16982073ad33SAndi Kleen *listp = list; 16992073ad33SAndi Kleen return 0; 17002073ad33SAndi Kleen } 17012073ad33SAndi Kleen 17026a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 17036a4bb04cSJiri Olsa char *event_mod) 170489efb029SJiri Olsa { 17056a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 17066a4bb04cSJiri Olsa } 17076a4bb04cSJiri Olsa 17083cdc5c2cSKan Liang /* 17093cdc5c2cSKan Liang * Check if the two uncore PMUs are from the same uncore block 17103cdc5c2cSKan Liang * The format of the uncore PMU name is uncore_#blockname_#pmuidx 17113cdc5c2cSKan Liang */ 17123cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b) 17133cdc5c2cSKan Liang { 17143cdc5c2cSKan Liang char *end_a, *end_b; 17153cdc5c2cSKan Liang 17163cdc5c2cSKan Liang end_a = strrchr(pmu_name_a, '_'); 17173cdc5c2cSKan Liang end_b = strrchr(pmu_name_b, '_'); 17183cdc5c2cSKan Liang 17193cdc5c2cSKan Liang if (!end_a || !end_b) 17203cdc5c2cSKan Liang return false; 17213cdc5c2cSKan Liang 17223cdc5c2cSKan Liang if ((end_a - pmu_name_a) != (end_b - pmu_name_b)) 17233cdc5c2cSKan Liang return false; 17243cdc5c2cSKan Liang 17253cdc5c2cSKan Liang return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0); 17263cdc5c2cSKan Liang } 17273cdc5c2cSKan Liang 17283cdc5c2cSKan Liang static int 17293cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list, 17303cdc5c2cSKan Liang struct parse_events_state *parse_state) 17313cdc5c2cSKan Liang { 173232dcd021SJiri Olsa struct evsel *evsel, *leader; 17333cdc5c2cSKan Liang uintptr_t *leaders; 17343cdc5c2cSKan Liang bool is_leader = true; 17353cdc5c2cSKan Liang int i, nr_pmu = 0, total_members, ret = 0; 17363cdc5c2cSKan Liang 1737b27c4eceSJiri Olsa leader = list_first_entry(list, struct evsel, core.node); 1738b27c4eceSJiri Olsa evsel = list_last_entry(list, struct evsel, core.node); 17393cdc5c2cSKan Liang total_members = evsel->idx - leader->idx + 1; 17403cdc5c2cSKan Liang 17413cdc5c2cSKan Liang leaders = calloc(total_members, sizeof(uintptr_t)); 17423cdc5c2cSKan Liang if (WARN_ON(!leaders)) 17433cdc5c2cSKan Liang return 0; 17443cdc5c2cSKan Liang 17453cdc5c2cSKan Liang /* 17463cdc5c2cSKan Liang * Going through the whole group and doing sanity check. 17473cdc5c2cSKan Liang * All members must use alias, and be from the same uncore block. 17483cdc5c2cSKan Liang * Also, storing the leader events in an array. 17493cdc5c2cSKan Liang */ 17503cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 17513cdc5c2cSKan Liang 17523cdc5c2cSKan Liang /* Only split the uncore group which members use alias */ 17533cdc5c2cSKan Liang if (!evsel->use_uncore_alias) 17543cdc5c2cSKan Liang goto out; 17553cdc5c2cSKan Liang 17563cdc5c2cSKan Liang /* The events must be from the same uncore block */ 17573cdc5c2cSKan Liang if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name)) 17583cdc5c2cSKan Liang goto out; 17593cdc5c2cSKan Liang 17603cdc5c2cSKan Liang if (!is_leader) 17613cdc5c2cSKan Liang continue; 17623cdc5c2cSKan Liang /* 17633cdc5c2cSKan Liang * If the event's PMU name starts to repeat, it must be a new 17643cdc5c2cSKan Liang * event. That can be used to distinguish the leader from 17653cdc5c2cSKan Liang * other members, even they have the same event name. 17663cdc5c2cSKan Liang */ 17678510895bSJin Yao if ((leader != evsel) && 17688510895bSJin Yao !strcmp(leader->pmu_name, evsel->pmu_name)) { 17693cdc5c2cSKan Liang is_leader = false; 17703cdc5c2cSKan Liang continue; 17713cdc5c2cSKan Liang } 17723cdc5c2cSKan Liang 17733cdc5c2cSKan Liang /* Store the leader event for each PMU */ 17743cdc5c2cSKan Liang leaders[nr_pmu++] = (uintptr_t) evsel; 17753cdc5c2cSKan Liang } 17763cdc5c2cSKan Liang 17773cdc5c2cSKan Liang /* only one event alias */ 17783cdc5c2cSKan Liang if (nr_pmu == total_members) { 17793cdc5c2cSKan Liang parse_state->nr_groups--; 17803cdc5c2cSKan Liang goto handled; 17813cdc5c2cSKan Liang } 17823cdc5c2cSKan Liang 17833cdc5c2cSKan Liang /* 17843cdc5c2cSKan Liang * An uncore event alias is a joint name which means the same event 17853cdc5c2cSKan Liang * runs on all PMUs of a block. 17863cdc5c2cSKan Liang * Perf doesn't support mixed events from different PMUs in the same 17873cdc5c2cSKan Liang * group. The big group has to be split into multiple small groups 17883cdc5c2cSKan Liang * which only include the events from the same PMU. 17893cdc5c2cSKan Liang * 17903cdc5c2cSKan Liang * Here the uncore event aliases must be from the same uncore block. 17913cdc5c2cSKan Liang * The number of PMUs must be same for each alias. The number of new 17923cdc5c2cSKan Liang * small groups equals to the number of PMUs. 17933cdc5c2cSKan Liang * Setting the leader event for corresponding members in each group. 17943cdc5c2cSKan Liang */ 17953cdc5c2cSKan Liang i = 0; 17963cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 17973cdc5c2cSKan Liang if (i >= nr_pmu) 17983cdc5c2cSKan Liang i = 0; 179932dcd021SJiri Olsa evsel->leader = (struct evsel *) leaders[i++]; 18003cdc5c2cSKan Liang } 18013cdc5c2cSKan Liang 18023cdc5c2cSKan Liang /* The number of members and group name are same for each group */ 18033cdc5c2cSKan Liang for (i = 0; i < nr_pmu; i++) { 180432dcd021SJiri Olsa evsel = (struct evsel *) leaders[i]; 18055643b1a5SJiri Olsa evsel->core.nr_members = total_members / nr_pmu; 18063cdc5c2cSKan Liang evsel->group_name = name ? strdup(name) : NULL; 18073cdc5c2cSKan Liang } 18083cdc5c2cSKan Liang 18093cdc5c2cSKan Liang /* Take the new small groups into account */ 18103cdc5c2cSKan Liang parse_state->nr_groups += nr_pmu - 1; 18113cdc5c2cSKan Liang 18123cdc5c2cSKan Liang handled: 18133cdc5c2cSKan Liang ret = 1; 18143cdc5c2cSKan Liang out: 18153cdc5c2cSKan Liang free(leaders); 18163cdc5c2cSKan Liang return ret; 18173cdc5c2cSKan Liang } 18183cdc5c2cSKan Liang 18193cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list, 18203cdc5c2cSKan Liang struct parse_events_state *parse_state) 18216a4bb04cSJiri Olsa { 182232dcd021SJiri Olsa struct evsel *leader; 18236a4bb04cSJiri Olsa 1824854f7363SWang Nan if (list_empty(list)) { 1825854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1826854f7363SWang Nan return; 1827854f7363SWang Nan } 1828854f7363SWang Nan 18293cdc5c2cSKan Liang if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state)) 18303cdc5c2cSKan Liang return; 18313cdc5c2cSKan Liang 1832a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(list); 1833b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 18346a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 183589efb029SJiri Olsa } 183689efb029SJiri Olsa 1837c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 18385d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 18395d7be90eSJiri Olsa struct list_head *list_all) 18405d7be90eSJiri Olsa { 18415d7be90eSJiri Olsa /* 18425d7be90eSJiri Olsa * Called for single event definition. Update the 184389efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 18445d7be90eSJiri Olsa * list, for next event definition. 18455d7be90eSJiri Olsa */ 18465d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1847b847cbdcSJiri Olsa free(list_event); 18485d7be90eSJiri Olsa } 18495d7be90eSJiri Olsa 1850f5b1135bSJiri Olsa struct event_modifier { 1851f5b1135bSJiri Olsa int eu; 1852f5b1135bSJiri Olsa int ek; 1853f5b1135bSJiri Olsa int eh; 1854f5b1135bSJiri Olsa int eH; 1855f5b1135bSJiri Olsa int eG; 1856a1e12da4SJiri Olsa int eI; 1857f5b1135bSJiri Olsa int precise; 18587f94af7aSJiri Olsa int precise_max; 1859f5b1135bSJiri Olsa int exclude_GH; 18603c176311SJiri Olsa int sample_read; 1861e9a7c414SMichael Ellerman int pinned; 18625a5dfe4bSAndi Kleen int weak; 18630997a266SAndi Kleen int exclusive; 186401bd8efcSSong Liu int bpf_counter; 1865f5b1135bSJiri Olsa }; 186661c45981SPaul Mackerras 1867f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 186832dcd021SJiri Olsa struct evsel *evsel) 1869f5b1135bSJiri Olsa { 18701fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 18711fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 18721fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 18731fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 18741fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 18751fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 18761fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 18777f94af7aSJiri Olsa int precise_max = 0; 18783c176311SJiri Olsa int sample_read = 0; 18791fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 18800997a266SAndi Kleen int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1881f5b1135bSJiri Olsa 1882f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1883f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 18845a5dfe4bSAndi Kleen int weak = 0; 188501bd8efcSSong Liu int bpf_counter = 0; 1886f5b1135bSJiri Olsa 1887f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1888ceb53fbfSIngo Molnar 188961c45981SPaul Mackerras while (*str) { 1890ab608344SPeter Zijlstra if (*str == 'u') { 1891ab608344SPeter Zijlstra if (!exclude) 1892ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 1893943b69acSJin Yao if (!exclude_GH && !perf_guest) 1894943b69acSJin Yao eG = 1; 189561c45981SPaul Mackerras eu = 0; 1896ab608344SPeter Zijlstra } else if (*str == 'k') { 1897ab608344SPeter Zijlstra if (!exclude) 1898ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 189961c45981SPaul Mackerras ek = 0; 1900ab608344SPeter Zijlstra } else if (*str == 'h') { 1901ab608344SPeter Zijlstra if (!exclude) 1902ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 190361c45981SPaul Mackerras eh = 0; 190499320cc8SJoerg Roedel } else if (*str == 'G') { 190599320cc8SJoerg Roedel if (!exclude_GH) 190699320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 190799320cc8SJoerg Roedel eG = 0; 190899320cc8SJoerg Roedel } else if (*str == 'H') { 190999320cc8SJoerg Roedel if (!exclude_GH) 191099320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 191199320cc8SJoerg Roedel eH = 0; 1912a1e12da4SJiri Olsa } else if (*str == 'I') { 1913a1e12da4SJiri Olsa eI = 1; 1914ab608344SPeter Zijlstra } else if (*str == 'p') { 1915ab608344SPeter Zijlstra precise++; 19161342798cSDavid Ahern /* use of precise requires exclude_guest */ 19171342798cSDavid Ahern if (!exclude_GH) 19181342798cSDavid Ahern eG = 1; 19197f94af7aSJiri Olsa } else if (*str == 'P') { 19207f94af7aSJiri Olsa precise_max = 1; 19213c176311SJiri Olsa } else if (*str == 'S') { 19223c176311SJiri Olsa sample_read = 1; 1923e9a7c414SMichael Ellerman } else if (*str == 'D') { 1924e9a7c414SMichael Ellerman pinned = 1; 19250997a266SAndi Kleen } else if (*str == 'e') { 19260997a266SAndi Kleen exclusive = 1; 19275a5dfe4bSAndi Kleen } else if (*str == 'W') { 19285a5dfe4bSAndi Kleen weak = 1; 192901bd8efcSSong Liu } else if (*str == 'b') { 193001bd8efcSSong Liu bpf_counter = 1; 1931ab608344SPeter Zijlstra } else 193261c45981SPaul Mackerras break; 1933ab608344SPeter Zijlstra 193461c45981SPaul Mackerras ++str; 193561c45981SPaul Mackerras } 193674d5b588SJaswinder Singh Rajput 193786470930SIngo Molnar /* 193889812fc8SJiri Olsa * precise ip: 193989812fc8SJiri Olsa * 194089812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 194189812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 194289812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 194389812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 194489812fc8SJiri Olsa * 194589812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 194686470930SIngo Molnar */ 194789812fc8SJiri Olsa if (precise > 3) 194889812fc8SJiri Olsa return -EINVAL; 194986470930SIngo Molnar 1950f5b1135bSJiri Olsa mod->eu = eu; 1951f5b1135bSJiri Olsa mod->ek = ek; 1952f5b1135bSJiri Olsa mod->eh = eh; 1953f5b1135bSJiri Olsa mod->eH = eH; 1954f5b1135bSJiri Olsa mod->eG = eG; 1955a1e12da4SJiri Olsa mod->eI = eI; 1956f5b1135bSJiri Olsa mod->precise = precise; 19577f94af7aSJiri Olsa mod->precise_max = precise_max; 1958f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 19593c176311SJiri Olsa mod->sample_read = sample_read; 1960e9a7c414SMichael Ellerman mod->pinned = pinned; 19615a5dfe4bSAndi Kleen mod->weak = weak; 196201bd8efcSSong Liu mod->bpf_counter = bpf_counter; 19630997a266SAndi Kleen mod->exclusive = exclusive; 1964e9a7c414SMichael Ellerman 1965f5b1135bSJiri Olsa return 0; 1966f5b1135bSJiri Olsa } 1967f5b1135bSJiri Olsa 1968534123f4SJiri Olsa /* 1969534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1970534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1971534123f4SJiri Olsa */ 1972534123f4SJiri Olsa static int check_modifier(char *str) 1973534123f4SJiri Olsa { 1974534123f4SJiri Olsa char *p = str; 1975534123f4SJiri Olsa 1976534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 197701bd8efcSSong Liu if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1978534123f4SJiri Olsa return -1; 1979534123f4SJiri Olsa 1980534123f4SJiri Olsa while (*p) { 1981534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1982534123f4SJiri Olsa return -1; 1983534123f4SJiri Olsa p++; 1984534123f4SJiri Olsa } 1985534123f4SJiri Olsa 1986534123f4SJiri Olsa return 0; 1987534123f4SJiri Olsa } 1988534123f4SJiri Olsa 1989f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1990f5b1135bSJiri Olsa { 199132dcd021SJiri Olsa struct evsel *evsel; 1992f5b1135bSJiri Olsa struct event_modifier mod; 1993f5b1135bSJiri Olsa 1994f5b1135bSJiri Olsa if (str == NULL) 1995f5b1135bSJiri Olsa return 0; 1996f5b1135bSJiri Olsa 1997534123f4SJiri Olsa if (check_modifier(str)) 1998534123f4SJiri Olsa return -EINVAL; 1999534123f4SJiri Olsa 2000f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 2001f5b1135bSJiri Olsa return -EINVAL; 2002f5b1135bSJiri Olsa 2003e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 2004f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 2005f5b1135bSJiri Olsa return -EINVAL; 2006f5b1135bSJiri Olsa 20071fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 20081fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 20091fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 20101fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 20111fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 20121fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 20131fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 2014f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 20153c176311SJiri Olsa evsel->sample_read = mod.sample_read; 20167f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 20175a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 201801bd8efcSSong Liu evsel->bpf_counter = mod.bpf_counter; 2019e9a7c414SMichael Ellerman 20200997a266SAndi Kleen if (evsel__is_group_leader(evsel)) { 20211fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 20220997a266SAndi Kleen evsel->core.attr.exclusive = mod.exclusive; 20230997a266SAndi Kleen } 2024ceb53fbfSIngo Molnar } 202586470930SIngo Molnar 202689812fc8SJiri Olsa return 0; 202786470930SIngo Molnar } 202886470930SIngo Molnar 2029ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 2030ac2ba9f3SRobert Richter { 203132dcd021SJiri Olsa struct evsel *evsel; 2032ac2ba9f3SRobert Richter 2033e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 2034ac2ba9f3SRobert Richter if (!evsel->name) 2035ac2ba9f3SRobert Richter evsel->name = strdup(name); 2036ac2ba9f3SRobert Richter } 2037ac2ba9f3SRobert Richter 2038ac2ba9f3SRobert Richter return 0; 2039ac2ba9f3SRobert Richter } 2040ac2ba9f3SRobert Richter 2041dcb4e102SKan Liang static int 2042dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 2043dcb4e102SKan Liang { 2044dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 2045dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 2046dcb4e102SKan Liang 2047e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 2048dcb4e102SKan Liang } 2049dcb4e102SKan Liang 2050dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 2051dcb4e102SKan Liang { 2052dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 2053dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 2054dcb4e102SKan Liang int i; 2055dcb4e102SKan Liang 2056dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 2057dcb4e102SKan Liang p = perf_pmu_events_list + i; 2058360e071bSTaeung Song zfree(&p->symbol); 2059dcb4e102SKan Liang } 2060506fde11STaeung Song zfree(&perf_pmu_events_list); 2061dcb4e102SKan Liang perf_pmu_events_list_num = 0; 2062dcb4e102SKan Liang } 2063dcb4e102SKan Liang } 2064dcb4e102SKan Liang 2065dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 2066dcb4e102SKan Liang do { \ 2067dcb4e102SKan Liang p->symbol = str; \ 2068dcb4e102SKan Liang if (!p->symbol) \ 2069dcb4e102SKan Liang goto err; \ 2070dcb4e102SKan Liang p->type = stype; \ 2071dcb4e102SKan Liang } while (0) 2072dcb4e102SKan Liang 2073dcb4e102SKan Liang /* 2074dcb4e102SKan Liang * Read the pmu events list from sysfs 2075dcb4e102SKan Liang * Save it into perf_pmu_events_list 2076dcb4e102SKan Liang */ 2077dcb4e102SKan Liang static void perf_pmu__parse_init(void) 2078dcb4e102SKan Liang { 2079dcb4e102SKan Liang 2080dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 2081dcb4e102SKan Liang struct perf_pmu_alias *alias; 2082dcb4e102SKan Liang int len = 0; 2083dcb4e102SKan Liang 2084231bb2aaSAndi Kleen pmu = NULL; 2085231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2086dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 2087dcb4e102SKan Liang if (strchr(alias->name, '-')) 2088dcb4e102SKan Liang len++; 2089dcb4e102SKan Liang len++; 2090dcb4e102SKan Liang } 2091231bb2aaSAndi Kleen } 2092231bb2aaSAndi Kleen 2093231bb2aaSAndi Kleen if (len == 0) { 2094231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 2095231bb2aaSAndi Kleen return; 2096231bb2aaSAndi Kleen } 2097dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 2098dcb4e102SKan Liang if (!perf_pmu_events_list) 2099dcb4e102SKan Liang return; 2100dcb4e102SKan Liang perf_pmu_events_list_num = len; 2101dcb4e102SKan Liang 2102dcb4e102SKan Liang len = 0; 2103231bb2aaSAndi Kleen pmu = NULL; 2104231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2105dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 2106dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 2107dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 2108dcb4e102SKan Liang 2109dcb4e102SKan Liang if (tmp != NULL) { 2110dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 2111dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 2112dcb4e102SKan Liang p++; 2113dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 2114dcb4e102SKan Liang len += 2; 2115dcb4e102SKan Liang } else { 2116dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 2117dcb4e102SKan Liang len++; 2118dcb4e102SKan Liang } 2119dcb4e102SKan Liang } 2120231bb2aaSAndi Kleen } 2121dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 2122dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2123dcb4e102SKan Liang 2124dcb4e102SKan Liang return; 2125dcb4e102SKan Liang err: 2126dcb4e102SKan Liang perf_pmu__parse_cleanup(); 2127dcb4e102SKan Liang } 2128dcb4e102SKan Liang 21294929e95aSJiri Olsa /* 21304929e95aSJiri Olsa * This function injects special term in 21314929e95aSJiri Olsa * perf_pmu_events_list so the test code 21324929e95aSJiri Olsa * can check on this functionality. 21334929e95aSJiri Olsa */ 21344929e95aSJiri Olsa int perf_pmu__test_parse_init(void) 21354929e95aSJiri Olsa { 21364929e95aSJiri Olsa struct perf_pmu_event_symbol *list; 21374929e95aSJiri Olsa 21384929e95aSJiri Olsa list = malloc(sizeof(*list) * 1); 21394929e95aSJiri Olsa if (!list) 21404929e95aSJiri Olsa return -ENOMEM; 21414929e95aSJiri Olsa 21424929e95aSJiri Olsa list->type = PMU_EVENT_SYMBOL; 21434929e95aSJiri Olsa list->symbol = strdup("read"); 21444929e95aSJiri Olsa 21454929e95aSJiri Olsa if (!list->symbol) { 21464929e95aSJiri Olsa free(list); 21474929e95aSJiri Olsa return -ENOMEM; 21484929e95aSJiri Olsa } 21494929e95aSJiri Olsa 21504929e95aSJiri Olsa perf_pmu_events_list = list; 21514929e95aSJiri Olsa perf_pmu_events_list_num = 1; 21524929e95aSJiri Olsa return 0; 21534929e95aSJiri Olsa } 21544929e95aSJiri Olsa 2155dcb4e102SKan Liang enum perf_pmu_event_symbol_type 2156dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 2157dcb4e102SKan Liang { 2158dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 2159dcb4e102SKan Liang 2160dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 2161dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 2162dcb4e102SKan Liang perf_pmu__parse_init(); 2163dcb4e102SKan Liang /* 2164dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 2165dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 2166dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 2167dcb4e102SKan Liang */ 2168dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 2169dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 2170dcb4e102SKan Liang 2171dcb4e102SKan Liang p.symbol = strdup(name); 2172dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 2173dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 2174dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2175360e071bSTaeung Song zfree(&p.symbol); 2176dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 2177dcb4e102SKan Liang } 2178dcb4e102SKan Liang 21791244a327SJiri Olsa static int parse_events__scanner(const char *str, 21801244a327SJiri Olsa struct parse_events_state *parse_state) 2181ac20de6fSZheng Yan { 2182ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 2183ac20de6fSZheng Yan void *scanner; 2184ac20de6fSZheng Yan int ret; 2185ac20de6fSZheng Yan 21861244a327SJiri Olsa ret = parse_events_lex_init_extra(parse_state, &scanner); 2187ac20de6fSZheng Yan if (ret) 2188ac20de6fSZheng Yan return ret; 2189ac20de6fSZheng Yan 2190ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 2191ac20de6fSZheng Yan 2192ac20de6fSZheng Yan #ifdef PARSER_DEBUG 2193ac20de6fSZheng Yan parse_events_debug = 1; 2194ae762641SJiri Olsa parse_events_set_debug(1, scanner); 2195ac20de6fSZheng Yan #endif 21965d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 2197ac20de6fSZheng Yan 2198ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 2199ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 2200ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 2201ac20de6fSZheng Yan return ret; 2202ac20de6fSZheng Yan } 2203ac20de6fSZheng Yan 220490e2b22dSJiri Olsa /* 220590e2b22dSJiri Olsa * parse event config string, return a list of event terms. 220690e2b22dSJiri Olsa */ 220790e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 220890e2b22dSJiri Olsa { 22095d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 221090e2b22dSJiri Olsa .terms = NULL, 22111244a327SJiri Olsa .stoken = PE_START_TERMS, 221290e2b22dSJiri Olsa }; 221390e2b22dSJiri Olsa int ret; 221490e2b22dSJiri Olsa 22151244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 22164929e95aSJiri Olsa perf_pmu__parse_cleanup(); 22174929e95aSJiri Olsa 221890e2b22dSJiri Olsa if (!ret) { 22195d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 22205d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 222190e2b22dSJiri Olsa return 0; 222290e2b22dSJiri Olsa } 222390e2b22dSJiri Olsa 22245d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 222590e2b22dSJiri Olsa return ret; 222690e2b22dSJiri Olsa } 222790e2b22dSJiri Olsa 2228*5e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 2229*5e4edd1fSJin Yao const char *str, char *pmu_name, 2230*5e4edd1fSJin Yao struct list_head *list) 2231*5e4edd1fSJin Yao { 2232*5e4edd1fSJin Yao struct parse_events_state ps = { 2233*5e4edd1fSJin Yao .list = LIST_HEAD_INIT(ps.list), 2234*5e4edd1fSJin Yao .stoken = PE_START_EVENTS, 2235*5e4edd1fSJin Yao .hybrid_pmu_name = pmu_name, 2236*5e4edd1fSJin Yao .idx = parse_state->idx, 2237*5e4edd1fSJin Yao }; 2238*5e4edd1fSJin Yao int ret; 2239*5e4edd1fSJin Yao 2240*5e4edd1fSJin Yao ret = parse_events__scanner(str, &ps); 2241*5e4edd1fSJin Yao perf_pmu__parse_cleanup(); 2242*5e4edd1fSJin Yao 2243*5e4edd1fSJin Yao if (!ret) { 2244*5e4edd1fSJin Yao if (!list_empty(&ps.list)) { 2245*5e4edd1fSJin Yao list_splice(&ps.list, list); 2246*5e4edd1fSJin Yao parse_state->idx = ps.idx; 2247*5e4edd1fSJin Yao return 0; 2248*5e4edd1fSJin Yao } else 2249*5e4edd1fSJin Yao return -1; 2250*5e4edd1fSJin Yao } 2251*5e4edd1fSJin Yao 2252*5e4edd1fSJin Yao return ret; 2253*5e4edd1fSJin Yao } 2254*5e4edd1fSJin Yao 22553bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str, 22563bf91aa5SArnaldo Carvalho de Melo struct parse_events_error *err, struct perf_pmu *fake_pmu) 225786470930SIngo Molnar { 22585d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 22595d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 22606484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 2261b39b8393SJiri Olsa .error = err, 22627630b3e2SWang Nan .evlist = evlist, 22631244a327SJiri Olsa .stoken = PE_START_EVENTS, 22643bf91aa5SArnaldo Carvalho de Melo .fake_pmu = fake_pmu, 226546010ab2SJiri Olsa }; 226646010ab2SJiri Olsa int ret; 226786470930SIngo Molnar 22681244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 2269dcb4e102SKan Liang perf_pmu__parse_cleanup(); 227015bfd2ccSWang Nan 22718e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2272c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2273854f7363SWang Nan return -1; 2274854f7363SWang Nan } 2275854f7363SWang Nan 22768e8714c3SIan Rogers /* 22778e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 22788e8714c3SIan Rogers */ 2279e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &parse_state.list); 22808e8714c3SIan Rogers 22818e8714c3SIan Rogers if (!ret) { 22828e8714c3SIan Rogers struct evsel *last; 22838e8714c3SIan Rogers 22845d9cdc11SArnaldo Carvalho de Melo evlist->nr_groups += parse_state.nr_groups; 2285515dbe48SJiri Olsa last = evlist__last(evlist); 228615bfd2ccSWang Nan last->cmdline_group_boundary = true; 228715bfd2ccSWang Nan 228886470930SIngo Molnar return 0; 228986470930SIngo Molnar } 229086470930SIngo Molnar 22915d7be90eSJiri Olsa /* 22925d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2293c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 22945d7be90eSJiri Olsa * need to bother. 22955d7be90eSJiri Olsa */ 229689812fc8SJiri Olsa return ret; 229789812fc8SJiri Olsa } 229889812fc8SJiri Olsa 2299b39b8393SJiri Olsa #define MAX_WIDTH 1000 2300b39b8393SJiri Olsa static int get_term_width(void) 2301b39b8393SJiri Olsa { 2302b39b8393SJiri Olsa struct winsize ws; 2303b39b8393SJiri Olsa 2304b39b8393SJiri Olsa get_term_dimensions(&ws); 2305b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2306b39b8393SJiri Olsa } 2307b39b8393SJiri Olsa 2308a910e466SIan Rogers static void __parse_events_print_error(int err_idx, const char *err_str, 2309a910e466SIan Rogers const char *err_help, const char *event) 2310b39b8393SJiri Olsa { 2311b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2312b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2313b39b8393SJiri Olsa char *buf = (char *) event; 2314b39b8393SJiri Olsa int idx = 0; 2315a910e466SIan Rogers if (err_str) { 2316b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2317b39b8393SJiri Olsa int width = get_term_width() - 2; 2318b39b8393SJiri Olsa int len_event = strlen(event); 2319b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2320b39b8393SJiri Olsa 2321b39b8393SJiri Olsa /* 2322b39b8393SJiri Olsa * Maximum error index indent, we will cut 2323b39b8393SJiri Olsa * the event string if it's bigger. 2324b39b8393SJiri Olsa */ 2325141b2d31SAdrian Hunter int max_err_idx = 13; 2326b39b8393SJiri Olsa 2327b39b8393SJiri Olsa /* 2328b39b8393SJiri Olsa * Let's be specific with the message when 2329b39b8393SJiri Olsa * we have the precise error. 2330b39b8393SJiri Olsa */ 2331b39b8393SJiri Olsa str = "event syntax error: "; 2332b39b8393SJiri Olsa len_str = strlen(str); 2333b39b8393SJiri Olsa max_len = width - len_str; 2334b39b8393SJiri Olsa 2335b39b8393SJiri Olsa buf = _buf; 2336b39b8393SJiri Olsa 2337bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2338a910e466SIan Rogers if (err_idx > max_err_idx) 2339a910e466SIan Rogers cut = err_idx - max_err_idx; 2340b39b8393SJiri Olsa 2341b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2342b39b8393SJiri Olsa 2343b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2344b39b8393SJiri Olsa if (cut) 2345b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2346b39b8393SJiri Olsa 2347b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2348b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2349b39b8393SJiri Olsa buf[max_len] = 0; 2350b39b8393SJiri Olsa } 2351b39b8393SJiri Olsa 2352a910e466SIan Rogers idx = len_str + err_idx - cut; 2353b39b8393SJiri Olsa } 2354b39b8393SJiri Olsa 2355b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2356b39b8393SJiri Olsa if (idx) { 2357a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2358a910e466SIan Rogers if (err_help) 2359a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2360a910e466SIan Rogers } 2361a910e466SIan Rogers } 2362a910e466SIan Rogers 2363a910e466SIan Rogers void parse_events_print_error(struct parse_events_error *err, 2364a910e466SIan Rogers const char *event) 2365a910e466SIan Rogers { 2366a910e466SIan Rogers if (!err->num_errors) 2367a910e466SIan Rogers return; 2368a910e466SIan Rogers 2369a910e466SIan Rogers __parse_events_print_error(err->idx, err->str, err->help, event); 2370360e071bSTaeung Song zfree(&err->str); 2371360e071bSTaeung Song zfree(&err->help); 2372a910e466SIan Rogers 2373a910e466SIan Rogers if (err->num_errors > 1) { 2374a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 2375a910e466SIan Rogers __parse_events_print_error(err->first_idx, err->first_str, 2376a910e466SIan Rogers err->first_help, event); 2377a910e466SIan Rogers zfree(&err->first_str); 2378a910e466SIan Rogers zfree(&err->first_help); 2379b39b8393SJiri Olsa } 2380b39b8393SJiri Olsa } 2381b39b8393SJiri Olsa 2382b39b8393SJiri Olsa #undef MAX_WIDTH 2383b39b8393SJiri Olsa 2384f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 23851d037ca1SIrina Tirdea int unset __maybe_unused) 2386f120f9d5SJiri Olsa { 238763503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2388a910e466SIan Rogers struct parse_events_error err; 2389a910e466SIan Rogers int ret; 2390a910e466SIan Rogers 2391a910e466SIan Rogers bzero(&err, sizeof(err)); 2392a910e466SIan Rogers ret = parse_events(evlist, str, &err); 23939175ce1fSAndi Kleen 2394333b5665SAndi Kleen if (ret) { 2395b39b8393SJiri Olsa parse_events_print_error(&err, str); 2396333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2397333b5665SAndi Kleen } 2398b39b8393SJiri Olsa 23999175ce1fSAndi Kleen return ret; 2400f120f9d5SJiri Olsa } 2401f120f9d5SJiri Olsa 2402d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2403d0abbc3cSArnaldo Carvalho de Melo { 2404d0abbc3cSArnaldo Carvalho de Melo struct evlist **evlistp = opt->value; 2405d0abbc3cSArnaldo Carvalho de Melo int ret; 2406d0abbc3cSArnaldo Carvalho de Melo 2407d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2408d0abbc3cSArnaldo Carvalho de Melo *evlistp = evlist__new(); 2409d0abbc3cSArnaldo Carvalho de Melo 2410d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2411d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2412d0abbc3cSArnaldo Carvalho de Melo return -1; 2413d0abbc3cSArnaldo Carvalho de Melo } 2414d0abbc3cSArnaldo Carvalho de Melo } 2415d0abbc3cSArnaldo Carvalho de Melo 2416d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2417d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2418d0abbc3cSArnaldo Carvalho de Melo evlist__delete(*evlistp); 2419d0abbc3cSArnaldo Carvalho de Melo *evlistp = NULL; 2420d0abbc3cSArnaldo Carvalho de Melo } 2421d0abbc3cSArnaldo Carvalho de Melo 2422d0abbc3cSArnaldo Carvalho de Melo return ret; 2423d0abbc3cSArnaldo Carvalho de Melo } 2424d0abbc3cSArnaldo Carvalho de Melo 24254ba1faa1SWang Nan static int 242663503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 242732dcd021SJiri Olsa int (*func)(struct evsel *evsel, 24284ba1faa1SWang Nan const void *arg), 24294ba1faa1SWang Nan const void *arg) 2430c171b552SLi Zefan { 243132dcd021SJiri Olsa struct evsel *last = NULL; 24324ba1faa1SWang Nan int err; 2433c171b552SLi Zefan 2434854f7363SWang Nan /* 2435854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2436854f7363SWang Nan * error when it found last == NULL. 2437854f7363SWang Nan * 2438854f7363SWang Nan * So no need to WARN here, let *func do this. 2439854f7363SWang Nan */ 24406484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2441515dbe48SJiri Olsa last = evlist__last(evlist); 244269aad6f1SArnaldo Carvalho de Melo 244315bfd2ccSWang Nan do { 24444ba1faa1SWang Nan err = (*func)(last, arg); 24454ba1faa1SWang Nan if (err) 2446c171b552SLi Zefan return -1; 24474ba1faa1SWang Nan if (!last) 24484ba1faa1SWang Nan return 0; 2449c171b552SLi Zefan 2450ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 245115bfd2ccSWang Nan return 0; 2452b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 245315bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 245415bfd2ccSWang Nan 2455c171b552SLi Zefan return 0; 2456c171b552SLi Zefan } 2457c171b552SLi Zefan 245832dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 24594ba1faa1SWang Nan { 24604ba1faa1SWang Nan const char *str = arg; 24611e857484SMathieu Poirier bool found = false; 24621e857484SMathieu Poirier int nr_addr_filters = 0; 24631e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 24644ba1faa1SWang Nan 246549836f78SJack Henschel if (evsel == NULL) { 246649836f78SJack Henschel fprintf(stderr, 246749836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 246849836f78SJack Henschel return -1; 246949836f78SJack Henschel } 24704ba1faa1SWang Nan 24711fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2472ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, str) < 0) { 24734ba1faa1SWang Nan fprintf(stderr, 24744ba1faa1SWang Nan "not enough memory to hold filter string\n"); 24754ba1faa1SWang Nan return -1; 24764ba1faa1SWang Nan } 24774ba1faa1SWang Nan 24784ba1faa1SWang Nan return 0; 24794ba1faa1SWang Nan } 24804ba1faa1SWang Nan 24811e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 24821fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 24831e857484SMathieu Poirier found = true; 24841e857484SMathieu Poirier break; 24851e857484SMathieu Poirier } 24861e857484SMathieu Poirier 24871e857484SMathieu Poirier if (found) 24881e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 24891e857484SMathieu Poirier "%d", &nr_addr_filters); 24901e857484SMathieu Poirier 249149836f78SJack Henschel if (!nr_addr_filters) { 249249836f78SJack Henschel fprintf(stderr, 249349836f78SJack Henschel "This CPU does not support address filtering\n"); 249449836f78SJack Henschel return -1; 249549836f78SJack Henschel } 24961e857484SMathieu Poirier 2497ad681adfSArnaldo Carvalho de Melo if (evsel__append_addr_filter(evsel, str) < 0) { 24981e857484SMathieu Poirier fprintf(stderr, 24991e857484SMathieu Poirier "not enough memory to hold filter string\n"); 25001e857484SMathieu Poirier return -1; 25011e857484SMathieu Poirier } 25021e857484SMathieu Poirier 25031e857484SMathieu Poirier return 0; 25041e857484SMathieu Poirier } 25051e857484SMathieu Poirier 25064ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 25074ba1faa1SWang Nan int unset __maybe_unused) 25084ba1faa1SWang Nan { 250963503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25104ba1faa1SWang Nan 25114ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 25124ba1faa1SWang Nan (const void *)str); 25134ba1faa1SWang Nan } 25144ba1faa1SWang Nan 251532dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 25164ba1faa1SWang Nan const void *arg __maybe_unused) 25174ba1faa1SWang Nan { 25184ba1faa1SWang Nan char new_filter[64]; 25194ba1faa1SWang Nan 25201fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 25214ba1faa1SWang Nan fprintf(stderr, 25224ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 25234ba1faa1SWang Nan return -1; 25244ba1faa1SWang Nan } 25254ba1faa1SWang Nan 25264ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 25274ba1faa1SWang Nan 2528ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, new_filter) < 0) { 25294ba1faa1SWang Nan fprintf(stderr, 25304ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25314ba1faa1SWang Nan return -1; 25324ba1faa1SWang Nan } 25334ba1faa1SWang Nan 25344ba1faa1SWang Nan return 0; 25354ba1faa1SWang Nan } 25364ba1faa1SWang Nan 25374ba1faa1SWang Nan int exclude_perf(const struct option *opt, 25384ba1faa1SWang Nan const char *arg __maybe_unused, 25394ba1faa1SWang Nan int unset __maybe_unused) 25404ba1faa1SWang Nan { 254163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25424ba1faa1SWang Nan 25434ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 25444ba1faa1SWang Nan NULL); 25454ba1faa1SWang Nan } 25464ba1faa1SWang Nan 254786470930SIngo Molnar static const char * const event_type_descriptors[] = { 254886470930SIngo Molnar "Hardware event", 254986470930SIngo Molnar "Software event", 255086470930SIngo Molnar "Tracepoint event", 255186470930SIngo Molnar "Hardware cache event", 255241bdcb23SLiming Wang "Raw hardware event descriptor", 255341bdcb23SLiming Wang "Hardware breakpoint", 255486470930SIngo Molnar }; 255586470930SIngo Molnar 2556ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 2557ab0e4800SYunlong Song { 2558ab0e4800SYunlong Song const char * const *as = a; 2559ab0e4800SYunlong Song const char * const *bs = b; 2560ab0e4800SYunlong Song 2561ab0e4800SYunlong Song return strcmp(*as, *bs); 2562ab0e4800SYunlong Song } 2563ab0e4800SYunlong Song 256486470930SIngo Molnar /* 2565f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 2566f6bdafefSJason Baron */ 2567f6bdafefSJason Baron 2568a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 2569a3277d2dSFrederic Weisbecker bool name_only) 2570f6bdafefSJason Baron { 2571f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 257222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2573f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 257425a7d914SArnaldo Carvalho de Melo char *dir_path; 2575ab0e4800SYunlong Song char **evt_list = NULL; 2576ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 2577ab0e4800SYunlong Song bool evt_num_known = false; 2578f6bdafefSJason Baron 2579ab0e4800SYunlong Song restart: 25807014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 2581f6bdafefSJason Baron if (!sys_dir) 2582725b1368SEric Dumazet return; 2583f6bdafefSJason Baron 2584ab0e4800SYunlong Song if (evt_num_known) { 2585ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2586ab0e4800SYunlong Song if (!evt_list) 2587ab0e4800SYunlong Song goto out_close_sys_dir; 2588ab0e4800SYunlong Song } 2589ab0e4800SYunlong Song 259022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 2591668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 259222a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 2593668b8788SArnaldo Carvalho de Melo continue; 2594725b1368SEric Dumazet 259525a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 259625a7d914SArnaldo Carvalho de Melo if (!dir_path) 259725a7d914SArnaldo Carvalho de Melo continue; 2598725b1368SEric Dumazet evt_dir = opendir(dir_path); 2599725b1368SEric Dumazet if (!evt_dir) 260025a7d914SArnaldo Carvalho de Melo goto next; 2601725b1368SEric Dumazet 2602c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 2603668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 260422a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 2605668b8788SArnaldo Carvalho de Melo continue; 2606668b8788SArnaldo Carvalho de Melo 2607ab0e4800SYunlong Song if (!evt_num_known) { 2608ab0e4800SYunlong Song evt_num++; 2609a3277d2dSFrederic Weisbecker continue; 2610a3277d2dSFrederic Weisbecker } 2611a3277d2dSFrederic Weisbecker 2612f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 261322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 2614ab0e4800SYunlong Song 2615ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 261625a7d914SArnaldo Carvalho de Melo if (evt_list[evt_i] == NULL) { 261725a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2618ab0e4800SYunlong Song goto out_close_evt_dir; 261925a7d914SArnaldo Carvalho de Melo } 2620ab0e4800SYunlong Song evt_i++; 2621f6bdafefSJason Baron } 2622f6bdafefSJason Baron closedir(evt_dir); 262325a7d914SArnaldo Carvalho de Melo next: 262425a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2625f6bdafefSJason Baron } 2626f6bdafefSJason Baron closedir(sys_dir); 2627ab0e4800SYunlong Song 2628ab0e4800SYunlong Song if (!evt_num_known) { 2629ab0e4800SYunlong Song evt_num_known = true; 2630ab0e4800SYunlong Song goto restart; 2631ab0e4800SYunlong Song } 2632ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2633ab0e4800SYunlong Song evt_i = 0; 2634ab0e4800SYunlong Song while (evt_i < evt_num) { 2635ab0e4800SYunlong Song if (name_only) { 2636ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2637ab0e4800SYunlong Song continue; 2638ab0e4800SYunlong Song } 2639ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2640ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2641ab0e4800SYunlong Song } 2642dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2643ab0e4800SYunlong Song printf("\n"); 2644ab0e4800SYunlong Song 2645ab0e4800SYunlong Song out_free: 2646ab0e4800SYunlong Song evt_num = evt_i; 2647ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2648ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2649ab0e4800SYunlong Song zfree(&evt_list); 2650ab0e4800SYunlong Song return; 2651ab0e4800SYunlong Song 2652ab0e4800SYunlong Song out_close_evt_dir: 2653ab0e4800SYunlong Song closedir(evt_dir); 2654ab0e4800SYunlong Song out_close_sys_dir: 2655ab0e4800SYunlong Song closedir(sys_dir); 2656ab0e4800SYunlong Song 2657ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2658ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2659ab0e4800SYunlong Song if (evt_list) 2660ab0e4800SYunlong Song goto out_free; 2661f6bdafefSJason Baron } 2662f6bdafefSJason Baron 2663f6bdafefSJason Baron /* 266420c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 266520c457b8SThomas Renninger */ 266620c457b8SThomas Renninger 266720c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 266820c457b8SThomas Renninger { 266920c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 267022a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 267120c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 267225a7d914SArnaldo Carvalho de Melo char *dir_path; 267320c457b8SThomas Renninger 26747014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 267520c457b8SThomas Renninger if (!sys_dir) 267620c457b8SThomas Renninger return 0; 267720c457b8SThomas Renninger 267822a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 267925a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 268025a7d914SArnaldo Carvalho de Melo if (!dir_path) 268125a7d914SArnaldo Carvalho de Melo continue; 268220c457b8SThomas Renninger evt_dir = opendir(dir_path); 268320c457b8SThomas Renninger if (!evt_dir) 268425a7d914SArnaldo Carvalho de Melo goto next; 268520c457b8SThomas Renninger 2686c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 268720c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 268822a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 268920c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 269020c457b8SThomas Renninger closedir(evt_dir); 269120c457b8SThomas Renninger closedir(sys_dir); 269220c457b8SThomas Renninger return 1; 269320c457b8SThomas Renninger } 269420c457b8SThomas Renninger } 269520c457b8SThomas Renninger closedir(evt_dir); 269625a7d914SArnaldo Carvalho de Melo next: 269725a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 269820c457b8SThomas Renninger } 269920c457b8SThomas Renninger closedir(sys_dir); 270020c457b8SThomas Renninger return 0; 270120c457b8SThomas Renninger } 270220c457b8SThomas Renninger 2703b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2704b41f1cecSNamhyung Kim { 2705b41f1cecSNamhyung Kim bool ret = true; 270688fee52eSVince Weaver int open_return; 270732dcd021SJiri Olsa struct evsel *evsel; 2708b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2709b41f1cecSNamhyung Kim .type = type, 2710b41f1cecSNamhyung Kim .config = config, 2711b41f1cecSNamhyung Kim .disabled = 1, 2712b41f1cecSNamhyung Kim }; 27139749b90eSJiri Olsa struct perf_thread_map *tmap = thread_map__new_by_tid(0); 271489896051SArnaldo Carvalho de Melo 271589896051SArnaldo Carvalho de Melo if (tmap == NULL) 271689896051SArnaldo Carvalho de Melo return false; 2717b41f1cecSNamhyung Kim 2718365c3ae7SJiri Olsa evsel = evsel__new(&attr); 2719b41f1cecSNamhyung Kim if (evsel) { 27205972d1e0SJiri Olsa open_return = evsel__open(evsel, NULL, tmap); 272188fee52eSVince Weaver ret = open_return >= 0; 272288fee52eSVince Weaver 272388fee52eSVince Weaver if (open_return == -EACCES) { 272488fee52eSVince Weaver /* 272588fee52eSVince Weaver * This happens if the paranoid value 272688fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 272788fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 272888fee52eSVince Weaver * by default as some ARM machines do not support it. 272988fee52eSVince Weaver * 273088fee52eSVince Weaver */ 27311fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = 1; 27325972d1e0SJiri Olsa ret = evsel__open(evsel, NULL, tmap) >= 0; 273388fee52eSVince Weaver } 27345eb2dd2aSJiri Olsa evsel__delete(evsel); 2735b41f1cecSNamhyung Kim } 2736b41f1cecSNamhyung Kim 27377836e52eSJiri Olsa perf_thread_map__put(tmap); 2738b41f1cecSNamhyung Kim return ret; 2739b41f1cecSNamhyung Kim } 2740b41f1cecSNamhyung Kim 274140218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 274240218daeSMasami Hiramatsu bool name_only) 274340218daeSMasami Hiramatsu { 274440218daeSMasami Hiramatsu struct probe_cache *pcache; 274540218daeSMasami Hiramatsu struct probe_cache_entry *ent; 274640218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 274740218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 274840218daeSMasami Hiramatsu struct str_node *nd, *nd2; 274940218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 275040218daeSMasami Hiramatsu bool show_detail = false; 275140218daeSMasami Hiramatsu int ret; 275240218daeSMasami Hiramatsu 275340218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 275440218daeSMasami Hiramatsu if (!sdtlist) { 275540218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 275640218daeSMasami Hiramatsu return; 275740218daeSMasami Hiramatsu } 275840218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 275940218daeSMasami Hiramatsu if (!bidlist) { 276040218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 276140218daeSMasami Hiramatsu return; 276240218daeSMasami Hiramatsu } 276340218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2764f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 276540218daeSMasami Hiramatsu if (!pcache) 276640218daeSMasami Hiramatsu continue; 276740218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 276840218daeSMasami Hiramatsu if (!ent->sdt) 276940218daeSMasami Hiramatsu continue; 277040218daeSMasami Hiramatsu if (subsys_glob && 277140218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 277240218daeSMasami Hiramatsu continue; 277340218daeSMasami Hiramatsu if (event_glob && 277440218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 277540218daeSMasami Hiramatsu continue; 277640218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 277740218daeSMasami Hiramatsu ent->pev.event, nd->s); 277840218daeSMasami Hiramatsu if (ret > 0) 277940218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 278040218daeSMasami Hiramatsu } 278140218daeSMasami Hiramatsu probe_cache__delete(pcache); 278240218daeSMasami Hiramatsu } 278340218daeSMasami Hiramatsu strlist__delete(bidlist); 278440218daeSMasami Hiramatsu 278540218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 278640218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 278740218daeSMasami Hiramatsu if (buf) 278840218daeSMasami Hiramatsu *(buf++) = '\0'; 278940218daeSMasami Hiramatsu if (name_only) { 279040218daeSMasami Hiramatsu printf("%s ", nd->s); 279140218daeSMasami Hiramatsu continue; 279240218daeSMasami Hiramatsu } 279340218daeSMasami Hiramatsu nd2 = strlist__next(nd); 279440218daeSMasami Hiramatsu if (nd2) { 279540218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 279640218daeSMasami Hiramatsu if (ptr) 279740218daeSMasami Hiramatsu *ptr = '\0'; 279840218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 279940218daeSMasami Hiramatsu show_detail = true; 280040218daeSMasami Hiramatsu } 280140218daeSMasami Hiramatsu if (show_detail) { 280240218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 280340218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 280440218daeSMasami Hiramatsu if (ret > 0) { 280540218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 280640218daeSMasami Hiramatsu free(buf); 280740218daeSMasami Hiramatsu } 28088bde8516SChangbin Du free(path); 280940218daeSMasami Hiramatsu } else 281040218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 281140218daeSMasami Hiramatsu if (nd2) { 281240218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 281340218daeSMasami Hiramatsu show_detail = false; 281440218daeSMasami Hiramatsu if (ptr) 281540218daeSMasami Hiramatsu *ptr = '@'; 281640218daeSMasami Hiramatsu } 281740218daeSMasami Hiramatsu } 281840218daeSMasami Hiramatsu strlist__delete(sdtlist); 281940218daeSMasami Hiramatsu } 282040218daeSMasami Hiramatsu 2821a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2822668b8788SArnaldo Carvalho de Melo { 2823ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 28240b668bc9SArnaldo Carvalho de Melo char name[64]; 2825ab0e4800SYunlong Song char **evt_list = NULL; 2826ab0e4800SYunlong Song bool evt_num_known = false; 2827ab0e4800SYunlong Song 2828ab0e4800SYunlong Song restart: 2829ab0e4800SYunlong Song if (evt_num_known) { 2830ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2831ab0e4800SYunlong Song if (!evt_list) 2832ab0e4800SYunlong Song goto out_enomem; 2833ab0e4800SYunlong Song } 2834668b8788SArnaldo Carvalho de Melo 2835668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2836668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2837668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 2838c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(type, op)) 2839668b8788SArnaldo Carvalho de Melo continue; 2840668b8788SArnaldo Carvalho de Melo 2841668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 28428ab2e96dSArnaldo Carvalho de Melo __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); 2843947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2844668b8788SArnaldo Carvalho de Melo continue; 2845668b8788SArnaldo Carvalho de Melo 2846b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2847b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2848b41f1cecSNamhyung Kim continue; 2849b41f1cecSNamhyung Kim 2850ab0e4800SYunlong Song if (!evt_num_known) { 2851ab0e4800SYunlong Song evt_num++; 2852ab0e4800SYunlong Song continue; 2853ab0e4800SYunlong Song } 2854ab0e4800SYunlong Song 2855ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2856ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2857ab0e4800SYunlong Song goto out_enomem; 2858ab0e4800SYunlong Song evt_i++; 2859668b8788SArnaldo Carvalho de Melo } 2860668b8788SArnaldo Carvalho de Melo } 2861668b8788SArnaldo Carvalho de Melo } 2862668b8788SArnaldo Carvalho de Melo 2863ab0e4800SYunlong Song if (!evt_num_known) { 2864ab0e4800SYunlong Song evt_num_known = true; 2865ab0e4800SYunlong Song goto restart; 2866ab0e4800SYunlong Song } 2867ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2868ab0e4800SYunlong Song evt_i = 0; 2869ab0e4800SYunlong Song while (evt_i < evt_num) { 2870ab0e4800SYunlong Song if (name_only) { 2871ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2872ab0e4800SYunlong Song continue; 2873ab0e4800SYunlong Song } 2874ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2875ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2876ab0e4800SYunlong Song } 2877dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2878dc098b35SAndi Kleen printf("\n"); 2879ab0e4800SYunlong Song 2880ab0e4800SYunlong Song out_free: 2881ab0e4800SYunlong Song evt_num = evt_i; 2882ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2883ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2884ab0e4800SYunlong Song zfree(&evt_list); 2885ab0e4800SYunlong Song return evt_num; 2886ab0e4800SYunlong Song 2887ab0e4800SYunlong Song out_enomem: 2888ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2889ab0e4800SYunlong Song if (evt_list) 2890ab0e4800SYunlong Song goto out_free; 2891ab0e4800SYunlong Song return evt_num; 2892668b8788SArnaldo Carvalho de Melo } 2893668b8788SArnaldo Carvalho de Melo 28945e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob, 28955e0861baSAndi Kleen bool name_only) 28965e0861baSAndi Kleen { 28975e0861baSAndi Kleen if (event_glob && !strglobmatch(name, event_glob)) 28985e0861baSAndi Kleen return; 28995e0861baSAndi Kleen if (name_only) 29005e0861baSAndi Kleen printf("%s ", name); 29015e0861baSAndi Kleen else 29025e0861baSAndi Kleen printf(" %-50s [%s]\n", name, "Tool event"); 29035e0861baSAndi Kleen 29045e0861baSAndi Kleen } 29055e0861baSAndi Kleen 29065e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only) 29075e0861baSAndi Kleen { 29085e0861baSAndi Kleen print_tool_event("duration_time", event_glob, name_only); 29095e0861baSAndi Kleen if (pager_in_use()) 29105e0861baSAndi Kleen printf("\n"); 29115e0861baSAndi Kleen } 29125e0861baSAndi Kleen 2913705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2914a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2915a3277d2dSFrederic Weisbecker bool name_only) 291686470930SIngo Molnar { 2917ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2918947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2919ab0e4800SYunlong Song char **evt_list = NULL; 2920ab0e4800SYunlong Song bool evt_num_known = false; 2921ab0e4800SYunlong Song 2922ab0e4800SYunlong Song restart: 2923ab0e4800SYunlong Song if (evt_num_known) { 2924ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2925ab0e4800SYunlong Song if (!evt_list) 2926ab0e4800SYunlong Song goto out_enomem; 2927ab0e4800SYunlong Song syms -= max; 2928ab0e4800SYunlong Song } 292986470930SIngo Molnar 29301dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2931668b8788SArnaldo Carvalho de Melo 2932e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2933668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2934668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2935668b8788SArnaldo Carvalho de Melo continue; 293686470930SIngo Molnar 2937b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2938b41f1cecSNamhyung Kim continue; 2939b41f1cecSNamhyung Kim 2940ab0e4800SYunlong Song if (!evt_num_known) { 2941ab0e4800SYunlong Song evt_num++; 2942a3277d2dSFrederic Weisbecker continue; 2943a3277d2dSFrederic Weisbecker } 2944a3277d2dSFrederic Weisbecker 2945ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2946947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 294774d5b588SJaswinder Singh Rajput else 2948bd8d57fbSArnaldo Carvalho de Melo strlcpy(name, syms->symbol, MAX_NAME_LEN); 294986470930SIngo Molnar 2950ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2951ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2952ab0e4800SYunlong Song goto out_enomem; 2953ab0e4800SYunlong Song evt_i++; 295486470930SIngo Molnar } 295586470930SIngo Molnar 2956ab0e4800SYunlong Song if (!evt_num_known) { 2957ab0e4800SYunlong Song evt_num_known = true; 2958ab0e4800SYunlong Song goto restart; 2959ab0e4800SYunlong Song } 2960ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2961ab0e4800SYunlong Song evt_i = 0; 2962ab0e4800SYunlong Song while (evt_i < evt_num) { 2963ab0e4800SYunlong Song if (name_only) { 2964ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2965ab0e4800SYunlong Song continue; 2966ab0e4800SYunlong Song } 2967ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2968ab0e4800SYunlong Song } 2969dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2970689d3018SMarti Raudsepp printf("\n"); 2971ab0e4800SYunlong Song 2972ab0e4800SYunlong Song out_free: 2973ab0e4800SYunlong Song evt_num = evt_i; 2974ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2975ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2976ab0e4800SYunlong Song zfree(&evt_list); 2977ab0e4800SYunlong Song return; 2978ab0e4800SYunlong Song 2979ab0e4800SYunlong Song out_enomem: 2980ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2981ab0e4800SYunlong Song if (evt_list) 2982ab0e4800SYunlong Song goto out_free; 2983668b8788SArnaldo Carvalho de Melo } 29841dc12760SJiri Olsa 29851dc12760SJiri Olsa /* 29861dc12760SJiri Olsa * Print the help text for the event symbols: 29871dc12760SJiri Olsa */ 2988c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2989a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 29901dc12760SJiri Olsa { 29911dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2992a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 29931dc12760SJiri Olsa 29941dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2995a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 29965e0861baSAndi Kleen print_tool_events(event_glob, name_only); 29971dc12760SJiri Olsa 2998a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 299973c24cb8SJaswinder Singh Rajput 3000bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 3001a7f6c8c8SJin Yao details_flag, deprecated); 3002dc098b35SAndi Kleen 3003668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 3004668b8788SArnaldo Carvalho de Melo return; 300573c24cb8SJaswinder Singh Rajput 3006a3277d2dSFrederic Weisbecker if (!name_only) { 3007947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 30085f537a26SJiri Olsa "rNNN", 30091cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 30105f537a26SJiri Olsa printf(" %-50s [%s]\n", 30115f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 30125f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 3013dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 3014dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 301586470930SIngo Molnar 3016947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 30173741eb9fSJacob Shin "mem:<addr>[/len][:access]", 301841bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 3019dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 30201b290d67SFrederic Weisbecker printf("\n"); 3021a3277d2dSFrederic Weisbecker } 30221b290d67SFrederic Weisbecker 3023a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 302440218daeSMasami Hiramatsu 302540218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 302671b0acceSAndi Kleen 302733bbc571SJiri Olsa metricgroup__print(true, true, NULL, name_only, details_flag); 302870943490SStephane Eranian 302970943490SStephane Eranian print_libpfm_events(name_only, long_desc); 303086470930SIngo Molnar } 30318f707d84SJiri Olsa 30326cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 30338f707d84SJiri Olsa { 303416fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 30358f707d84SJiri Olsa } 30368f707d84SJiri Olsa 303767b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 303867b49b38SJiri Olsa struct parse_events_term *temp, 303967b49b38SJiri Olsa char *str, u64 num) 30408f707d84SJiri Olsa { 30416cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 30428f707d84SJiri Olsa 304367b49b38SJiri Olsa term = malloc(sizeof(*term)); 30448f707d84SJiri Olsa if (!term) 30458f707d84SJiri Olsa return -ENOMEM; 30468f707d84SJiri Olsa 304767b49b38SJiri Olsa *term = *temp; 30488f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 3049c2f1ceadSAndi Kleen term->weak = false; 30508f707d84SJiri Olsa 305167b49b38SJiri Olsa switch (term->type_val) { 30528f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 30538f707d84SJiri Olsa term->val.num = num; 30548f707d84SJiri Olsa break; 30558f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 30568f707d84SJiri Olsa term->val.str = str; 30578f707d84SJiri Olsa break; 30588f707d84SJiri Olsa default: 30594be8be6bSAdrian Hunter free(term); 30608f707d84SJiri Olsa return -EINVAL; 30618f707d84SJiri Olsa } 30628f707d84SJiri Olsa 30638f707d84SJiri Olsa *_term = term; 30648f707d84SJiri Olsa return 0; 30658f707d84SJiri Olsa } 30668f707d84SJiri Olsa 30676cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 3068cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 306999e7138eSJiri Olsa bool no_value, 3070bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 307116fa7e82SJiri Olsa { 3072bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 3073bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 3074bb78ce7dSAdrian Hunter 307567b49b38SJiri Olsa struct parse_events_term temp = { 307667b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 307767b49b38SJiri Olsa .type_term = type_term, 307867b49b38SJiri Olsa .config = config, 307999e7138eSJiri Olsa .no_value = no_value, 308067b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 308167b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 308267b49b38SJiri Olsa }; 308367b49b38SJiri Olsa 308467b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 308516fa7e82SJiri Olsa } 308616fa7e82SJiri Olsa 30876cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 3088cecf3a2eSJiri Olsa int type_term, char *config, char *str, 3089bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 309016fa7e82SJiri Olsa { 3091bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 3092bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 3093bb78ce7dSAdrian Hunter 309467b49b38SJiri Olsa struct parse_events_term temp = { 309567b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 309667b49b38SJiri Olsa .type_term = type_term, 309767b49b38SJiri Olsa .config = config, 309867b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 309967b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 310067b49b38SJiri Olsa }; 310167b49b38SJiri Olsa 310267b49b38SJiri Olsa return new_term(term, &temp, str, 0); 310316fa7e82SJiri Olsa } 310416fa7e82SJiri Olsa 31056cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 31061d33d6dcSJiri Olsa char *config, unsigned idx) 31071d33d6dcSJiri Olsa { 31081d33d6dcSJiri Olsa struct event_symbol *sym; 3109b6645a72SIan Rogers char *str; 311067b49b38SJiri Olsa struct parse_events_term temp = { 311167b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 311267b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 3113b6645a72SIan Rogers .config = config, 311467b49b38SJiri Olsa }; 31151d33d6dcSJiri Olsa 3116b6645a72SIan Rogers if (!temp.config) { 3117b6645a72SIan Rogers temp.config = strdup("event"); 3118b6645a72SIan Rogers if (!temp.config) 3119b6645a72SIan Rogers return -ENOMEM; 3120b6645a72SIan Rogers } 31211d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 31221d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 31231d33d6dcSJiri Olsa 3124b6645a72SIan Rogers str = strdup(sym->symbol); 3125b6645a72SIan Rogers if (!str) 3126b6645a72SIan Rogers return -ENOMEM; 3127b6645a72SIan Rogers return new_term(term, &temp, str, 0); 31281d33d6dcSJiri Olsa } 31291d33d6dcSJiri Olsa 31306cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 31316cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 3132a6146d50SZheng Yan { 3133b6645a72SIan Rogers char *str; 313467b49b38SJiri Olsa struct parse_events_term temp = { 313567b49b38SJiri Olsa .type_val = term->type_val, 313667b49b38SJiri Olsa .type_term = term->type_term, 3137b6645a72SIan Rogers .config = NULL, 313867b49b38SJiri Olsa .err_term = term->err_term, 313967b49b38SJiri Olsa .err_val = term->err_val, 314067b49b38SJiri Olsa }; 314167b49b38SJiri Olsa 3142b6645a72SIan Rogers if (term->config) { 3143b6645a72SIan Rogers temp.config = strdup(term->config); 3144b6645a72SIan Rogers if (!temp.config) 3145b6645a72SIan Rogers return -ENOMEM; 3146b6645a72SIan Rogers } 3147b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3148b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 3149b6645a72SIan Rogers 3150b6645a72SIan Rogers str = strdup(term->val.str); 3151b6645a72SIan Rogers if (!str) 3152b6645a72SIan Rogers return -ENOMEM; 3153b6645a72SIan Rogers return new_term(new, &temp, str, 0); 3154a6146d50SZheng Yan } 3155a6146d50SZheng Yan 31561dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 31571dc92556SIan Rogers { 31581dc92556SIan Rogers if (term->array.nr_ranges) 31591dc92556SIan Rogers zfree(&term->array.ranges); 31601dc92556SIan Rogers 31611dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 31621dc92556SIan Rogers zfree(&term->val.str); 31631dc92556SIan Rogers 31641dc92556SIan Rogers zfree(&term->config); 31651dc92556SIan Rogers free(term); 31661dc92556SIan Rogers } 31671dc92556SIan Rogers 31688255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 31698255718fSAndi Kleen struct list_head **new) 31708255718fSAndi Kleen { 31718255718fSAndi Kleen struct parse_events_term *term, *n; 31728255718fSAndi Kleen int ret; 31738255718fSAndi Kleen 31748255718fSAndi Kleen if (!old) { 31758255718fSAndi Kleen *new = NULL; 31768255718fSAndi Kleen return 0; 31778255718fSAndi Kleen } 31788255718fSAndi Kleen 31798255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 31808255718fSAndi Kleen if (!*new) 31818255718fSAndi Kleen return -ENOMEM; 31828255718fSAndi Kleen INIT_LIST_HEAD(*new); 31838255718fSAndi Kleen 31848255718fSAndi Kleen list_for_each_entry (term, old, list) { 31858255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 31868255718fSAndi Kleen if (ret) 31878255718fSAndi Kleen return ret; 31888255718fSAndi Kleen list_add_tail(&n->list, *new); 31898255718fSAndi Kleen } 31908255718fSAndi Kleen return 0; 31918255718fSAndi Kleen } 31928255718fSAndi Kleen 3193fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 31948f707d84SJiri Olsa { 31956cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 31968f707d84SJiri Olsa 3197a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 3198a8adfcebSWang Nan list_del_init(&term->list); 31991dc92556SIan Rogers parse_events_term__delete(term); 32008f707d84SJiri Olsa } 3201a8adfcebSWang Nan } 3202b39b8393SJiri Olsa 32032146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 3204fc0a2c1dSArnaldo Carvalho de Melo { 32052146afc6SArnaldo Carvalho de Melo if (!terms) 32062146afc6SArnaldo Carvalho de Melo return; 3207fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 3208d20a5f2bSWang Nan free(terms); 3209fc0a2c1dSArnaldo Carvalho de Melo } 3210fc0a2c1dSArnaldo Carvalho de Melo 32112d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 32122d055bf2SWang Nan { 3213360e071bSTaeung Song zfree(&a->ranges); 32142d055bf2SWang Nan } 32152d055bf2SWang Nan 32165d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 3217b39b8393SJiri Olsa int idx, const char *str) 3218b39b8393SJiri Olsa { 3219448d732cSIan Rogers if (!parse_state->error) 3220a6ced2beSAdrian Hunter return; 3221448d732cSIan Rogers 3222448d732cSIan Rogers parse_events__handle_error(parse_state->error, idx, strdup(str), NULL); 3223b39b8393SJiri Olsa } 3224ffeb883eSHe Kuang 322517cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 322617cb5f84SWang Nan { 322717cb5f84SWang Nan int i; 322817cb5f84SWang Nan bool first = true; 322917cb5f84SWang Nan 323017cb5f84SWang Nan buf[0] = '\0'; 323117cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 323217cb5f84SWang Nan const char *name = config_term_names[i]; 323317cb5f84SWang Nan 32341669e509SWang Nan if (!config_term_avail(i, NULL)) 32351669e509SWang Nan continue; 323617cb5f84SWang Nan if (!name) 323717cb5f84SWang Nan continue; 323817cb5f84SWang Nan if (name[0] == '<') 323917cb5f84SWang Nan continue; 324017cb5f84SWang Nan 324117cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 324217cb5f84SWang Nan return; 324317cb5f84SWang Nan 324417cb5f84SWang Nan if (!first) 324517cb5f84SWang Nan strcat(buf, ","); 324617cb5f84SWang Nan else 324717cb5f84SWang Nan first = false; 324817cb5f84SWang Nan strcat(buf, name); 324917cb5f84SWang Nan } 325017cb5f84SWang Nan } 325117cb5f84SWang Nan 3252ffeb883eSHe Kuang /* 3253ffeb883eSHe Kuang * Return string contains valid config terms of an event. 3254ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 3255ffeb883eSHe Kuang */ 3256ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 3257ffeb883eSHe Kuang { 3258ffeb883eSHe Kuang char *str; 3259626a6b78SWang Nan /* "no-overwrite" is the longest name */ 326017cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 3261626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 3262ffeb883eSHe Kuang 326317cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 3264ffeb883eSHe Kuang /* valid terms */ 3265ffeb883eSHe Kuang if (additional_terms) { 326626dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 326726dee028SWang Nan additional_terms, static_terms) < 0) 3268ffeb883eSHe Kuang goto fail; 3269ffeb883eSHe Kuang } else { 327026dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 3271ffeb883eSHe Kuang goto fail; 3272ffeb883eSHe Kuang } 3273ffeb883eSHe Kuang return str; 3274ffeb883eSHe Kuang 3275ffeb883eSHe Kuang fail: 3276ffeb883eSHe Kuang return NULL; 3277ffeb883eSHe Kuang } 32789cbfa2f6SJin Yao 32799cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx, 32809cbfa2f6SJin Yao struct perf_event_attr *attr, 32819cbfa2f6SJin Yao char *name, struct perf_pmu *pmu, 32829cbfa2f6SJin Yao struct list_head *config_terms) 32839cbfa2f6SJin Yao { 32849cbfa2f6SJin Yao return __add_event(list, idx, attr, true, name, pmu, 32859cbfa2f6SJin Yao config_terms, false, NULL); 32869cbfa2f6SJin Yao } 3287