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" 415e4edd1fSJin 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); 525e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 535e4edd1fSJin Yao const char *str, char *pmu_name, 545e4edd1fSJin 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 }, 153fb6c79d7SNamhyung Kim [PERF_COUNT_SW_CGROUP_SWITCHES] = { 154fb6c79d7SNamhyung Kim .symbol = "cgroup-switches", 155fb6c79d7SNamhyung Kim .alias = "", 156fb6c79d7SNamhyung Kim }, 15786470930SIngo Molnar }; 15886470930SIngo Molnar 159cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 160cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 16186470930SIngo Molnar 162cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 163cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 164cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 165cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 16686470930SIngo Molnar 16722a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 16822a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 16922a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 17022a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 17122a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 172f6bdafefSJason Baron 173c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) 174ae07b63fSPeter Zijlstra { 175ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 176ae07b63fSPeter Zijlstra int fd; 177ae07b63fSPeter Zijlstra 178c02cab22SArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name); 179ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 180ae07b63fSPeter Zijlstra if (fd < 0) 181ae07b63fSPeter Zijlstra return -EINVAL; 182ae07b63fSPeter Zijlstra close(fd); 183ae07b63fSPeter Zijlstra 184ae07b63fSPeter Zijlstra return 0; 185ae07b63fSPeter Zijlstra } 186ae07b63fSPeter Zijlstra 187c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent) \ 18822a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 18922a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 19022a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 19122a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 192c02cab22SArnaldo Carvalho de Melo (!tp_event_has_id(dir_path, evt_dirent))) 193f6bdafefSJason Baron 194270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 195f6bdafefSJason Baron 196448d732cSIan Rogers void parse_events__handle_error(struct parse_events_error *err, int idx, 197448d732cSIan Rogers char *str, char *help) 198448d732cSIan Rogers { 199448d732cSIan Rogers if (WARN(!str, "WARNING: failed to provide error string\n")) { 200448d732cSIan Rogers free(help); 201448d732cSIan Rogers return; 202448d732cSIan Rogers } 203a910e466SIan Rogers switch (err->num_errors) { 204a910e466SIan Rogers case 0: 205448d732cSIan Rogers err->idx = idx; 206a910e466SIan Rogers err->str = str; 207a910e466SIan Rogers err->help = help; 208a910e466SIan Rogers break; 209a910e466SIan Rogers case 1: 210a910e466SIan Rogers err->first_idx = err->idx; 211a910e466SIan Rogers err->idx = idx; 212a910e466SIan Rogers err->first_str = err->str; 213a910e466SIan Rogers err->str = str; 214a910e466SIan Rogers err->first_help = err->help; 215a910e466SIan Rogers err->help = help; 216a910e466SIan Rogers break; 217a910e466SIan Rogers default: 2184ac22b48SIan Rogers pr_debug("Multiple errors dropping message: %s (%s)\n", 2194ac22b48SIan Rogers err->str, err->help); 220448d732cSIan Rogers free(err->str); 221448d732cSIan Rogers err->str = str; 222448d732cSIan Rogers free(err->help); 223448d732cSIan Rogers err->help = help; 224a910e466SIan Rogers break; 225a910e466SIan Rogers } 226a910e466SIan Rogers err->num_errors++; 227448d732cSIan Rogers } 228f6bdafefSJason Baron 2291ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 230f6bdafefSJason Baron { 2311ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 232f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 23322a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2348aa8a7c8SStephane Eranian char id_buf[24]; 235725b1368SEric Dumazet int fd; 236f6bdafefSJason Baron u64 id; 237f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 23825a7d914SArnaldo Carvalho de Melo char *dir_path; 239f6bdafefSJason Baron 2407014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 241f6bdafefSJason Baron if (!sys_dir) 242725b1368SEric Dumazet return NULL; 243f6bdafefSJason Baron 24422a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 24525a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 24625a7d914SArnaldo Carvalho de Melo if (!dir_path) 24725a7d914SArnaldo Carvalho de Melo continue; 248725b1368SEric Dumazet evt_dir = opendir(dir_path); 249725b1368SEric Dumazet if (!evt_dir) 25025a7d914SArnaldo Carvalho de Melo goto next; 251725b1368SEric Dumazet 252c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 253725b1368SEric Dumazet 25477f18153SJiri Olsa scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 25522a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 256725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 257f6bdafefSJason Baron if (fd < 0) 258f6bdafefSJason Baron continue; 259f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 260f6bdafefSJason Baron close(fd); 261f6bdafefSJason Baron continue; 262f6bdafefSJason Baron } 263f6bdafefSJason Baron close(fd); 264f6bdafefSJason Baron id = atoll(id_buf); 265f6bdafefSJason Baron if (id == config) { 26625a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 267f6bdafefSJason Baron closedir(evt_dir); 268f6bdafefSJason Baron closedir(sys_dir); 26959b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2705aa365f2STaeung Song if (!path) 2715aa365f2STaeung Song return NULL; 2727125f204SArnaldo Carvalho de Melo if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) { 2731ef2ed10SFrederic Weisbecker free(path); 2741ef2ed10SFrederic Weisbecker return NULL; 2751ef2ed10SFrederic Weisbecker } 2767125f204SArnaldo Carvalho de Melo if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) { 27774cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2781ef2ed10SFrederic Weisbecker free(path); 2791ef2ed10SFrederic Weisbecker return NULL; 2801ef2ed10SFrederic Weisbecker } 2811ef2ed10SFrederic Weisbecker return path; 282f6bdafefSJason Baron } 283f6bdafefSJason Baron } 284f6bdafefSJason Baron closedir(evt_dir); 28525a7d914SArnaldo Carvalho de Melo next: 28625a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 287f6bdafefSJason Baron } 288f6bdafefSJason Baron 289f6bdafefSJason Baron closedir(sys_dir); 2901ef2ed10SFrederic Weisbecker return NULL; 2911ef2ed10SFrederic Weisbecker } 2921ef2ed10SFrederic Weisbecker 293e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 294e7c93f09SNamhyung Kim { 295e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 296e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 297e7c93f09SNamhyung Kim 298e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 299e7c93f09SNamhyung Kim free(path); 300e7c93f09SNamhyung Kim return NULL; 301e7c93f09SNamhyung Kim } 302e7c93f09SNamhyung Kim 303e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 304e7c93f09SNamhyung Kim path->name = strdup(str+1); 305e7c93f09SNamhyung Kim 306e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 30774cf249dSArnaldo Carvalho de Melo zfree(&path->system); 30874cf249dSArnaldo Carvalho de Melo zfree(&path->name); 309506fde11STaeung Song zfree(&path); 310e7c93f09SNamhyung Kim } 311e7c93f09SNamhyung Kim 312e7c93f09SNamhyung Kim return path; 313e7c93f09SNamhyung Kim } 314e7c93f09SNamhyung Kim 3151424dc96SDavid Ahern const char *event_type(int type) 3161424dc96SDavid Ahern { 3171424dc96SDavid Ahern switch (type) { 3181424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 3191424dc96SDavid Ahern return "hardware"; 3201424dc96SDavid Ahern 3211424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 3221424dc96SDavid Ahern return "software"; 3231424dc96SDavid Ahern 3241424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 3251424dc96SDavid Ahern return "tracepoint"; 3261424dc96SDavid Ahern 3271424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 3281424dc96SDavid Ahern return "hardware-cache"; 3291424dc96SDavid Ahern 3301424dc96SDavid Ahern default: 3311424dc96SDavid Ahern break; 3321424dc96SDavid Ahern } 3331424dc96SDavid Ahern 3341424dc96SDavid Ahern return "unknown"; 3351424dc96SDavid Ahern } 3361424dc96SDavid Ahern 337e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 338e814fdddSWang Nan { 339e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 340e814fdddSWang Nan } 3417ae92e74SYan, Zheng 342e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 343e814fdddSWang Nan { 344e814fdddSWang Nan struct parse_events_term *term; 345e814fdddSWang Nan 346e814fdddSWang Nan if (!head_terms) 347e814fdddSWang Nan return NULL; 348e814fdddSWang Nan 349e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 350e814fdddSWang Nan if (parse_events__is_name_term(term)) 351e814fdddSWang Nan return term->val.str; 352e814fdddSWang Nan 353e814fdddSWang Nan return NULL; 354e814fdddSWang Nan } 3557ae92e74SYan, Zheng 35632dcd021SJiri Olsa static struct evsel * 357410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3587ae92e74SYan, Zheng struct perf_event_attr *attr, 35970943490SStephane Eranian bool init_attr, 36066ec1191SMark Rutland char *name, struct perf_pmu *pmu, 361f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 362f0fbb114SAndi Kleen const char *cpu_list) 36389812fc8SJiri Olsa { 36432dcd021SJiri Olsa struct evsel *evsel; 3655d680be3SNamhyung Kim struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 3669c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 36789812fc8SJiri Olsa 368e4064776SJin Yao if (pmu && attr->type == PERF_TYPE_RAW) 369e4064776SJin Yao perf_pmu__warn_invalid_config(pmu, attr->config, name); 370e4064776SJin Yao 37170943490SStephane Eranian if (init_attr) 37289812fc8SJiri Olsa event_attr_init(attr); 37389812fc8SJiri Olsa 3748f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attr, *idx); 3750f1b550eSNamhyung Kim if (!evsel) { 3760f1b550eSNamhyung Kim perf_cpu_map__put(cpus); 377410136f5SStephane Eranian return NULL; 3780f1b550eSNamhyung Kim } 37989812fc8SJiri Olsa 38075fc5ae5STaeung Song (*idx)++; 3815d680be3SNamhyung Kim evsel->core.cpus = cpus; 382fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 383648b5af3SJiri Olsa evsel->core.system_wide = pmu ? pmu->is_uncore : false; 38463ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 385f30a79b0SJiri Olsa 3869db1763cSArnaldo Carvalho de Melo if (name) 38789812fc8SJiri Olsa evsel->name = strdup(name); 388930a2e29SJiri Olsa 389930a2e29SJiri Olsa if (config_terms) 390930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 391930a2e29SJiri Olsa 39270943490SStephane Eranian if (list) 393b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 39470943490SStephane Eranian 395410136f5SStephane Eranian return evsel; 39689812fc8SJiri Olsa } 39789812fc8SJiri Olsa 39870943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 39970943490SStephane Eranian char *name, struct perf_pmu *pmu) 40070943490SStephane Eranian { 40170943490SStephane Eranian return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false, 40270943490SStephane Eranian NULL); 40370943490SStephane Eranian } 40470943490SStephane Eranian 405c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 406930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 407930a2e29SJiri Olsa struct list_head *config_terms) 4087ae92e74SYan, Zheng { 40970943490SStephane Eranian return __add_event(list, idx, attr, true, name, NULL, config_terms, 41070943490SStephane Eranian false, NULL) ? 0 : -ENOMEM; 411f0fbb114SAndi Kleen } 412f0fbb114SAndi Kleen 413f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 414f0fbb114SAndi Kleen enum perf_tool_event tool_event) 415f0fbb114SAndi Kleen { 41632dcd021SJiri Olsa struct evsel *evsel; 417f0fbb114SAndi Kleen struct perf_event_attr attr = { 418f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 419f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 420f0fbb114SAndi Kleen }; 421f0fbb114SAndi Kleen 42270943490SStephane Eranian evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false, 42370943490SStephane Eranian "0"); 424f0fbb114SAndi Kleen if (!evsel) 425f0fbb114SAndi Kleen return -ENOMEM; 426f0fbb114SAndi Kleen evsel->tool_event = tool_event; 427f0fbb114SAndi Kleen if (tool_event == PERF_TOOL_DURATION_TIME) 428b12eea5aSNamhyung Kim evsel->unit = "ns"; 429f0fbb114SAndi Kleen return 0; 4307ae92e74SYan, Zheng } 4317ae92e74SYan, Zheng 432c64e85e1SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size) 43386470930SIngo Molnar { 43486470930SIngo Molnar int i, j; 43561c45981SPaul Mackerras int n, longest = -1; 43686470930SIngo Molnar 43786470930SIngo Molnar for (i = 0; i < size; i++) { 438c64e85e1SArnaldo Carvalho de Melo for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 43961c45981SPaul Mackerras n = strlen(names[i][j]); 44089812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 44161c45981SPaul Mackerras longest = n; 44261c45981SPaul Mackerras } 44389812fc8SJiri Olsa if (longest > 0) 44486470930SIngo Molnar return i; 44586470930SIngo Molnar } 44686470930SIngo Molnar 4478953645fSIngo Molnar return -1; 44886470930SIngo Molnar } 44986470930SIngo Molnar 45043d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 45143d0b978SWang Nan struct parse_events_term *term, 45243d0b978SWang Nan struct parse_events_error *err); 45343d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 45443d0b978SWang Nan struct parse_events_term *term, 45543d0b978SWang Nan struct parse_events_error *err); 45643d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 45743d0b978SWang Nan struct list_head *head, 45843d0b978SWang Nan struct parse_events_error *err, 45943d0b978SWang Nan config_term_func_t config_term); 46043d0b978SWang Nan 461c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 46243d0b978SWang Nan char *type, char *op_result1, char *op_result2, 4632c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 464c93afadcSJin Yao struct list_head *head_config, 465c93afadcSJin Yao struct parse_events_state *parse_state) 46686470930SIngo Molnar { 46789812fc8SJiri Olsa struct perf_event_attr attr; 46843d0b978SWang Nan LIST_HEAD(config_terms); 46943d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 47061c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 47189812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 47230def61fSJin Yao int i, n, ret; 47330def61fSJin Yao bool hybrid; 47486470930SIngo Molnar 47586470930SIngo Molnar /* 47686470930SIngo Molnar * No fallback - if we cannot get a clear cache type 47786470930SIngo Molnar * then bail out: 47886470930SIngo Molnar */ 479c64e85e1SArnaldo Carvalho de Melo cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX); 48086470930SIngo Molnar if (cache_type == -1) 48189812fc8SJiri Olsa return -EINVAL; 48286470930SIngo Molnar 48343d0b978SWang Nan config_name = get_config_name(head_config); 48489812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 48589812fc8SJiri Olsa 48689812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 48789812fc8SJiri Olsa char *str = op_result[i]; 48889812fc8SJiri Olsa 489275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 49061c45981SPaul Mackerras 49161c45981SPaul Mackerras if (cache_op == -1) { 492c64e85e1SArnaldo Carvalho de Melo cache_op = parse_aliases(str, evsel__hw_cache_op, 49361c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 49461c45981SPaul Mackerras if (cache_op >= 0) { 495c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(cache_type, cache_op)) 49689812fc8SJiri Olsa return -EINVAL; 49761c45981SPaul Mackerras continue; 49861c45981SPaul Mackerras } 49961c45981SPaul Mackerras } 50061c45981SPaul Mackerras 50161c45981SPaul Mackerras if (cache_result == -1) { 502c64e85e1SArnaldo Carvalho de Melo cache_result = parse_aliases(str, evsel__hw_cache_result, 50361c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 50461c45981SPaul Mackerras if (cache_result >= 0) 50561c45981SPaul Mackerras continue; 50661c45981SPaul Mackerras } 50761c45981SPaul Mackerras } 50861c45981SPaul Mackerras 50986470930SIngo Molnar /* 51086470930SIngo Molnar * Fall back to reads: 51186470930SIngo Molnar */ 5128953645fSIngo Molnar if (cache_op == -1) 5138953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 51486470930SIngo Molnar 51586470930SIngo Molnar /* 51686470930SIngo Molnar * Fall back to accesses: 51786470930SIngo Molnar */ 51886470930SIngo Molnar if (cache_result == -1) 51986470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 52086470930SIngo Molnar 52189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 52289812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 52389812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 52443d0b978SWang Nan 52543d0b978SWang Nan if (head_config) { 5262c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 52743d0b978SWang Nan config_term_common)) 52843d0b978SWang Nan return -EINVAL; 52943d0b978SWang Nan 53043d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 53143d0b978SWang Nan return -ENOMEM; 53243d0b978SWang Nan } 53330def61fSJin Yao 53430def61fSJin Yao ret = parse_events__add_cache_hybrid(list, idx, &attr, 53530def61fSJin Yao config_name ? : name, &config_terms, 536c93afadcSJin Yao &hybrid, parse_state); 53730def61fSJin Yao if (hybrid) 53830def61fSJin Yao return ret; 53930def61fSJin Yao 54043d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 54186470930SIngo Molnar } 54286470930SIngo Molnar 543272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 5448c619d6aSWang Nan const char *sys, const char *name) 54519658171SJiri Olsa { 5464584f084SIan Rogers const char *str; 54719658171SJiri Olsa char help[BUFSIZ]; 54819658171SJiri Olsa 549ec183d22SAdrian Hunter if (!e) 550ec183d22SAdrian Hunter return; 551ec183d22SAdrian Hunter 55219658171SJiri Olsa /* 55319658171SJiri Olsa * We get error directly from syscall errno ( > 0), 55419658171SJiri Olsa * or from encoded pointer's error ( < 0). 55519658171SJiri Olsa */ 55619658171SJiri Olsa err = abs(err); 55719658171SJiri Olsa 55819658171SJiri Olsa switch (err) { 55919658171SJiri Olsa case EACCES: 5604584f084SIan Rogers str = "can't access trace events"; 56119658171SJiri Olsa break; 56219658171SJiri Olsa case ENOENT: 5634584f084SIan Rogers str = "unknown tracepoint"; 56419658171SJiri Olsa break; 56519658171SJiri Olsa default: 5664584f084SIan Rogers str = "failed to add tracepoint"; 56719658171SJiri Olsa break; 56819658171SJiri Olsa } 56919658171SJiri Olsa 57019658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 5714584f084SIan Rogers parse_events__handle_error(e, 0, strdup(str), strdup(help)); 57219658171SJiri Olsa } 57319658171SJiri Olsa 574c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 5758c619d6aSWang Nan const char *sys_name, const char *evt_name, 576272ed29aSJiri Olsa struct parse_events_error *err, 577e637d177SHe Kuang struct list_head *head_config) 578bcd3279fSFrederic Weisbecker { 5798f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 580bcd3279fSFrederic Weisbecker 58119658171SJiri Olsa if (IS_ERR(evsel)) { 582272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 5838dd2a131SJiri Olsa return PTR_ERR(evsel); 58419658171SJiri Olsa } 585bcd3279fSFrederic Weisbecker 586e637d177SHe Kuang if (head_config) { 587e637d177SHe Kuang LIST_HEAD(config_terms); 588e637d177SHe Kuang 589e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 590e637d177SHe Kuang return -ENOMEM; 591e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 592e637d177SHe Kuang } 593e637d177SHe Kuang 594b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 59582fe1c29SArnaldo Carvalho de Melo return 0; 596bcd3279fSFrederic Weisbecker } 597bcd3279fSFrederic Weisbecker 598c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5998c619d6aSWang Nan const char *sys_name, const char *evt_name, 600272ed29aSJiri Olsa struct parse_events_error *err, 601e637d177SHe Kuang struct list_head *head_config) 602bcd3279fSFrederic Weisbecker { 60325a7d914SArnaldo Carvalho de Melo char *evt_path; 604bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 605bcd3279fSFrederic Weisbecker DIR *evt_dir; 60627bf90bfSJiri Olsa int ret = 0, found = 0; 607bcd3279fSFrederic Weisbecker 60825a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 60925a7d914SArnaldo Carvalho de Melo if (!evt_path) { 61025a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 61125a7d914SArnaldo Carvalho de Melo return -1; 61225a7d914SArnaldo Carvalho de Melo } 613bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 614bcd3279fSFrederic Weisbecker if (!evt_dir) { 61525a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 616272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 61789812fc8SJiri Olsa return -1; 618bcd3279fSFrederic Weisbecker } 619bcd3279fSFrederic Weisbecker 62089812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 621bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 622bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 623bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 624bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 625bcd3279fSFrederic Weisbecker continue; 626bcd3279fSFrederic Weisbecker 62789812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 628fb1d2edfSMasami Hiramatsu continue; 629fb1d2edfSMasami Hiramatsu 63027bf90bfSJiri Olsa found++; 63127bf90bfSJiri Olsa 632e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 633272ed29aSJiri Olsa err, head_config); 634bcd3279fSFrederic Weisbecker } 635bcd3279fSFrederic Weisbecker 63627bf90bfSJiri Olsa if (!found) { 63727bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 63827bf90bfSJiri Olsa ret = -1; 63927bf90bfSJiri Olsa } 64027bf90bfSJiri Olsa 64125a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 6420bd3f084SJiri Olsa closedir(evt_dir); 64389812fc8SJiri Olsa return ret; 644bcd3279fSFrederic Weisbecker } 645bcd3279fSFrederic Weisbecker 646c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 6478c619d6aSWang Nan const char *sys_name, const char *evt_name, 648272ed29aSJiri Olsa struct parse_events_error *err, 649e637d177SHe Kuang struct list_head *head_config) 650f35488f9SJiri Olsa { 651f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 652e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 653272ed29aSJiri Olsa err, head_config) : 654e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 655272ed29aSJiri Olsa err, head_config); 656f35488f9SJiri Olsa } 657f35488f9SJiri Olsa 658c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 6598c619d6aSWang Nan const char *sys_name, const char *evt_name, 660272ed29aSJiri Olsa struct parse_events_error *err, 661e637d177SHe Kuang struct list_head *head_config) 662f35488f9SJiri Olsa { 663f35488f9SJiri Olsa struct dirent *events_ent; 664f35488f9SJiri Olsa DIR *events_dir; 665f35488f9SJiri Olsa int ret = 0; 666f35488f9SJiri Olsa 6677014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 668f35488f9SJiri Olsa if (!events_dir) { 669272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 670f35488f9SJiri Olsa return -1; 671f35488f9SJiri Olsa } 672f35488f9SJiri Olsa 673f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 674f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 675f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 676f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 677f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 678f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 679f35488f9SJiri Olsa continue; 680f35488f9SJiri Olsa 681f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 682f35488f9SJiri Olsa continue; 683f35488f9SJiri Olsa 684f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 685272ed29aSJiri Olsa evt_name, err, head_config); 686f35488f9SJiri Olsa } 687f35488f9SJiri Olsa 688f35488f9SJiri Olsa closedir(events_dir); 689f35488f9SJiri Olsa return ret; 690f35488f9SJiri Olsa } 691f35488f9SJiri Olsa 692c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT 6934edf30e3SWang Nan struct __add_bpf_event_param { 6945d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6954edf30e3SWang Nan struct list_head *list; 69695088a59SWang Nan struct list_head *head_config; 6974edf30e3SWang Nan }; 6984edf30e3SWang Nan 699af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 7004edf30e3SWang Nan void *_param) 7014edf30e3SWang Nan { 7024edf30e3SWang Nan LIST_HEAD(new_evsels); 7034edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 7045d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 7054edf30e3SWang Nan struct list_head *list = param->list; 70632dcd021SJiri Olsa struct evsel *pos; 7074edf30e3SWang Nan int err; 7082620b7e3SArnaldo Carvalho de Melo /* 7092620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 7102620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 7112620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 7122620b7e3SArnaldo Carvalho de Melo * 7132620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 7142620b7e3SArnaldo Carvalho de Melo */ 7152620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 7162620b7e3SArnaldo Carvalho de Melo return 0; 7174edf30e3SWang Nan 7184edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 719cd102d70SWang Nan group, event, fd); 7204edf30e3SWang Nan 7215d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 7225d369a75SArnaldo Carvalho de Melo event, parse_state->error, 72395088a59SWang Nan param->head_config); 7244edf30e3SWang Nan if (err) { 72532dcd021SJiri Olsa struct evsel *evsel, *tmp; 7264edf30e3SWang Nan 7274edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 728cd102d70SWang Nan group, event); 729b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 730b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 7315eb2dd2aSJiri Olsa evsel__delete(evsel); 7324edf30e3SWang Nan } 7334edf30e3SWang Nan return err; 7344edf30e3SWang Nan } 735cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 7364edf30e3SWang Nan 737b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 7381f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 739cd102d70SWang Nan group, event, pos); 7401f45b1d4SWang Nan pos->bpf_fd = fd; 741af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 7421f45b1d4SWang Nan } 7434edf30e3SWang Nan list_splice(&new_evsels, list); 7444edf30e3SWang Nan return 0; 7454edf30e3SWang Nan } 7464edf30e3SWang Nan 7475d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 74884c86ca1SWang Nan struct list_head *list, 74995088a59SWang Nan struct bpf_object *obj, 75095088a59SWang Nan struct list_head *head_config) 75184c86ca1SWang Nan { 75284c86ca1SWang Nan int err; 75384c86ca1SWang Nan char errbuf[BUFSIZ]; 7545d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 755aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 75684c86ca1SWang Nan 75784c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 75884c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 75984c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 76084c86ca1SWang Nan err = -EINVAL; 76184c86ca1SWang Nan goto errout; 76284c86ca1SWang Nan } 76384c86ca1SWang Nan 76484c86ca1SWang Nan /* 765aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 766aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 767aa3abf30SWang Nan * created when failure. 768aa3abf30SWang Nan */ 769aa3abf30SWang Nan if (!registered_unprobe_atexit) { 770aa3abf30SWang Nan atexit(bpf__clear); 771aa3abf30SWang Nan registered_unprobe_atexit = true; 772aa3abf30SWang Nan } 773aa3abf30SWang Nan 774aa3abf30SWang Nan err = bpf__probe(obj); 775aa3abf30SWang Nan if (err) { 776aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 777aa3abf30SWang Nan goto errout; 778aa3abf30SWang Nan } 779aa3abf30SWang Nan 7801e5e3ee8SWang Nan err = bpf__load(obj); 7811e5e3ee8SWang Nan if (err) { 7821e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 7831e5e3ee8SWang Nan goto errout; 7841e5e3ee8SWang Nan } 7851e5e3ee8SWang Nan 786cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 7874edf30e3SWang Nan if (err) { 7884edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 7894edf30e3SWang Nan "Attach events in BPF object failed"); 7904edf30e3SWang Nan goto errout; 7914edf30e3SWang Nan } 7924edf30e3SWang Nan 7934edf30e3SWang Nan return 0; 79484c86ca1SWang Nan errout: 7955f634c8eSIan Rogers parse_events__handle_error(parse_state->error, 0, 7965f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 79784c86ca1SWang Nan return err; 79884c86ca1SWang Nan } 79984c86ca1SWang Nan 800a34f3be7SWang Nan static int 8015d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 802a34f3be7SWang Nan struct bpf_object *obj, 803a34f3be7SWang Nan struct list_head *head_config) 804a34f3be7SWang Nan { 805a34f3be7SWang Nan struct parse_events_term *term; 806a34f3be7SWang Nan int error_pos; 807a34f3be7SWang Nan 808a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 809a34f3be7SWang Nan return 0; 810a34f3be7SWang Nan 811a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 812a34f3be7SWang Nan int err; 813a34f3be7SWang Nan 814a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 8155f634c8eSIan Rogers parse_events__handle_error(parse_state->error, term->err_term, 8165f634c8eSIan Rogers strdup("Invalid config term for BPF object"), 8175f634c8eSIan Rogers NULL); 818a34f3be7SWang Nan return -EINVAL; 819a34f3be7SWang Nan } 820a34f3be7SWang Nan 8215d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 822a34f3be7SWang Nan if (err) { 8235f634c8eSIan Rogers char errbuf[BUFSIZ]; 8245f634c8eSIan Rogers int idx; 8255f634c8eSIan Rogers 8265d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 827a34f3be7SWang Nan &error_pos, err, errbuf, 828a34f3be7SWang Nan sizeof(errbuf)); 8295f634c8eSIan Rogers 8305f634c8eSIan Rogers if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 8315f634c8eSIan Rogers idx = term->err_val; 8325f634c8eSIan Rogers else 8335f634c8eSIan Rogers idx = term->err_term + error_pos; 8345f634c8eSIan Rogers 8355f634c8eSIan Rogers parse_events__handle_error(parse_state->error, idx, 8365f634c8eSIan Rogers strdup(errbuf), 8375f634c8eSIan Rogers strdup( 8387630b3e2SWang Nan "Hint:\tValid config terms:\n" 839e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 840e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 8417630b3e2SWang Nan "\n" 842e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 8435f634c8eSIan Rogers " \t(add -v to see detail)")); 844a34f3be7SWang Nan return err; 845a34f3be7SWang Nan } 846a34f3be7SWang Nan } 847a34f3be7SWang Nan return 0; 848a34f3be7SWang Nan } 849a34f3be7SWang Nan 85095088a59SWang Nan /* 85195088a59SWang Nan * Split config terms: 85295088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 85395088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 85495088a59SWang Nan * events in bpf.c. 85595088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 85695088a59SWang Nan * with parse_events_config_bpf. 85795088a59SWang Nan * 85895088a59SWang Nan * Move object config terms from the first list to obj_head_config. 85995088a59SWang Nan */ 86095088a59SWang Nan static void 86195088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 86295088a59SWang Nan struct list_head *obj_head_config) 86395088a59SWang Nan { 86495088a59SWang Nan struct parse_events_term *term, *temp; 86595088a59SWang Nan 86695088a59SWang Nan /* 8674d39c89fSIngo Molnar * Currently, all possible user config term 86895088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 8694d39c89fSIngo Molnar * happens to be a good flag. 87095088a59SWang Nan * 87195088a59SWang Nan * See parse_events_config_bpf() and 87295088a59SWang Nan * config_term_tracepoint(). 87395088a59SWang Nan */ 87495088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 87595088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 87695088a59SWang Nan list_move_tail(&term->list, obj_head_config); 87795088a59SWang Nan } 87895088a59SWang Nan 8795d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 88084c86ca1SWang Nan struct list_head *list, 881d509db04SWang Nan char *bpf_file_name, 882a34f3be7SWang Nan bool source, 883a34f3be7SWang Nan struct list_head *head_config) 88484c86ca1SWang Nan { 885a34f3be7SWang Nan int err; 88695088a59SWang Nan struct bpf_object *obj; 88795088a59SWang Nan LIST_HEAD(obj_head_config); 88895088a59SWang Nan 88995088a59SWang Nan if (head_config) 89095088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 89184c86ca1SWang Nan 892d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8936371ca3bSWang Nan if (IS_ERR(obj)) { 89484c86ca1SWang Nan char errbuf[BUFSIZ]; 89584c86ca1SWang Nan 8966371ca3bSWang Nan err = PTR_ERR(obj); 89784c86ca1SWang Nan 89884c86ca1SWang Nan if (err == -ENOTSUP) 89984c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 90084c86ca1SWang Nan "BPF support is not compiled"); 90184c86ca1SWang Nan else 902d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 903d3e0ce39SWang Nan source, 904d3e0ce39SWang Nan -err, errbuf, 905d3e0ce39SWang Nan sizeof(errbuf)); 90684c86ca1SWang Nan 9075f634c8eSIan Rogers parse_events__handle_error(parse_state->error, 0, 9085f634c8eSIan Rogers strdup(errbuf), strdup("(add -v to see detail)")); 90984c86ca1SWang Nan return err; 91084c86ca1SWang Nan } 91184c86ca1SWang Nan 9125d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 913a34f3be7SWang Nan if (err) 914a34f3be7SWang Nan return err; 9155d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 91695088a59SWang Nan 91795088a59SWang Nan /* 91895088a59SWang Nan * Caller doesn't know anything about obj_head_config, 9194d39c89fSIngo Molnar * so combine them together again before returning. 92095088a59SWang Nan */ 92195088a59SWang Nan if (head_config) 92295088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 92395088a59SWang Nan return err; 92484c86ca1SWang Nan } 925c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT 926c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 927c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 928c18cf78dSArnaldo Carvalho de Melo struct bpf_object *obj __maybe_unused, 929c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 930c18cf78dSArnaldo Carvalho de Melo { 931c18cf78dSArnaldo Carvalho de Melo parse_events__handle_error(parse_state->error, 0, 932c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 933c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 934c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 935c18cf78dSArnaldo Carvalho de Melo } 936c18cf78dSArnaldo Carvalho de Melo 937c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 938c18cf78dSArnaldo Carvalho de Melo struct list_head *list __maybe_unused, 939c18cf78dSArnaldo Carvalho de Melo char *bpf_file_name __maybe_unused, 940c18cf78dSArnaldo Carvalho de Melo bool source __maybe_unused, 941c18cf78dSArnaldo Carvalho de Melo struct list_head *head_config __maybe_unused) 942c18cf78dSArnaldo Carvalho de Melo { 943c18cf78dSArnaldo Carvalho de Melo parse_events__handle_error(parse_state->error, 0, 944c18cf78dSArnaldo Carvalho de Melo strdup("BPF support is not compiled"), 945c18cf78dSArnaldo Carvalho de Melo strdup("Make sure libbpf-devel is available at build time.")); 946c18cf78dSArnaldo Carvalho de Melo return -ENOTSUP; 947c18cf78dSArnaldo Carvalho de Melo } 948c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT 94984c86ca1SWang Nan 95089812fc8SJiri Olsa static int 95189812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 9521b290d67SFrederic Weisbecker { 9531b290d67SFrederic Weisbecker int i; 9541b290d67SFrederic Weisbecker 9551b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 95689812fc8SJiri Olsa if (!type || !type[i]) 9571b290d67SFrederic Weisbecker break; 9581b290d67SFrederic Weisbecker 9597582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 9607582732fSJiri Olsa do { \ 9617582732fSJiri Olsa if (attr->bp_type & bit) \ 9627582732fSJiri Olsa return -EINVAL; \ 9637582732fSJiri Olsa else \ 9647582732fSJiri Olsa attr->bp_type |= bit; \ 9657582732fSJiri Olsa } while (0) 9667582732fSJiri Olsa 9671b290d67SFrederic Weisbecker switch (type[i]) { 9681b290d67SFrederic Weisbecker case 'r': 9697582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 9701b290d67SFrederic Weisbecker break; 9711b290d67SFrederic Weisbecker case 'w': 9727582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 9731b290d67SFrederic Weisbecker break; 9741b290d67SFrederic Weisbecker case 'x': 9757582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 9761b290d67SFrederic Weisbecker break; 9771b290d67SFrederic Weisbecker default: 97889812fc8SJiri Olsa return -EINVAL; 9791b290d67SFrederic Weisbecker } 9801b290d67SFrederic Weisbecker } 98189812fc8SJiri Olsa 9827582732fSJiri Olsa #undef CHECK_SET_TYPE 9837582732fSJiri Olsa 9841b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 9851b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 9861b290d67SFrederic Weisbecker 98789812fc8SJiri Olsa return 0; 9881b290d67SFrederic Weisbecker } 9891b290d67SFrederic Weisbecker 990c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 991aa98d848SIan Rogers u64 addr, char *type, u64 len) 9921b290d67SFrederic Weisbecker { 99389812fc8SJiri Olsa struct perf_event_attr attr; 9941b290d67SFrederic Weisbecker 99589812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 996aa98d848SIan Rogers attr.bp_addr = addr; 9971b290d67SFrederic Weisbecker 99889812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 99989812fc8SJiri Olsa return -EINVAL; 10001b290d67SFrederic Weisbecker 10013741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 10023741eb9fSJacob Shin if (!len) { 100389812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 10043741eb9fSJacob Shin len = sizeof(long); 1005aa59a485SFrederic Weisbecker else 10063741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 10073741eb9fSJacob Shin } 10083741eb9fSJacob Shin 10093741eb9fSJacob Shin attr.bp_len = len; 1010aa59a485SFrederic Weisbecker 101189812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 10124a841d65SJovi Zhang attr.sample_period = 1; 10131b290d67SFrederic Weisbecker 1014930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 10151b290d67SFrederic Weisbecker } 10161b290d67SFrederic Weisbecker 10173b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 10183b0e371cSJiri Olsa struct parse_events_error *err, 10193b0e371cSJiri Olsa int type) 10203b0e371cSJiri Olsa { 10213b0e371cSJiri Olsa if (type == term->type_val) 10223b0e371cSJiri Olsa return 0; 10233b0e371cSJiri Olsa 10243b0e371cSJiri Olsa if (err) { 1025448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1026448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 1027448d732cSIan Rogers ? strdup("expected numeric value") 1028448d732cSIan Rogers : strdup("expected string value"), 1029448d732cSIan Rogers NULL); 10303b0e371cSJiri Olsa } 10313b0e371cSJiri Olsa return -EINVAL; 10323b0e371cSJiri Olsa } 10333b0e371cSJiri Olsa 103417cb5f84SWang Nan /* 103517cb5f84SWang Nan * Update according to parse-events.l 103617cb5f84SWang Nan */ 103717cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 103817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 103917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 104017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 104117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 104217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 104317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 104417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 104517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 104617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 104717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 104817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 104917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 105017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 1051792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 10522fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 1053626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 1054626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 1055dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 1056064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 10571b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 1058eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 105917cb5f84SWang Nan }; 106017cb5f84SWang Nan 10611669e509SWang Nan static bool config_term_shrinked; 10621669e509SWang Nan 10631669e509SWang Nan static bool 10641669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 10651669e509SWang Nan { 1066448d732cSIan Rogers char *err_str; 1067448d732cSIan Rogers 10681669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 1069448d732cSIan Rogers parse_events__handle_error(err, -1, 1070448d732cSIan Rogers strdup("Invalid term_type"), NULL); 10711669e509SWang Nan return false; 10721669e509SWang Nan } 10731669e509SWang Nan if (!config_term_shrinked) 10741669e509SWang Nan return true; 10751669e509SWang Nan 10761669e509SWang Nan switch (term_type) { 10771669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 10781669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 10791669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 10801669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 108106835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1082064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 10831669e509SWang Nan return true; 10841669e509SWang Nan default: 10851669e509SWang Nan if (!err) 10861669e509SWang Nan return false; 10871669e509SWang Nan 10881669e509SWang Nan /* term_type is validated so indexing is safe */ 1089448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 1090448d732cSIan Rogers config_term_names[term_type]) >= 0) 1091448d732cSIan Rogers parse_events__handle_error(err, -1, err_str, NULL); 10921669e509SWang Nan return false; 10931669e509SWang Nan } 10941669e509SWang Nan } 10951669e509SWang Nan 10961669e509SWang Nan void parse_events__shrink_config_terms(void) 10971669e509SWang Nan { 10981669e509SWang Nan config_term_shrinked = true; 10991669e509SWang Nan } 11001669e509SWang Nan 11010b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 11023b0e371cSJiri Olsa struct parse_events_term *term, 11033b0e371cSJiri Olsa struct parse_events_error *err) 11048f707d84SJiri Olsa { 110516fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 110616fa7e82SJiri Olsa do { \ 11073b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 110816fa7e82SJiri Olsa return -EINVAL; \ 110916fa7e82SJiri Olsa } while (0) 111016fa7e82SJiri Olsa 111116fa7e82SJiri Olsa switch (term->type_term) { 11128f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 111316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11148f707d84SJiri Olsa attr->config = term->val.num; 11158f707d84SJiri Olsa break; 11168f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 111716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11188f707d84SJiri Olsa attr->config1 = term->val.num; 11198f707d84SJiri Olsa break; 11208f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 112116fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11228f707d84SJiri Olsa attr->config2 = term->val.num; 11238f707d84SJiri Olsa break; 11248f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 112516fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 11268f707d84SJiri Olsa break; 112709af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 112809af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 112909af2a55SNamhyung Kim break; 11308f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1131ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1132ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1133448d732cSIan Rogers parse_branch_str(term->val.str, 1134448d732cSIan Rogers &attr->branch_sample_type)) { 1135448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1136448d732cSIan Rogers strdup("invalid branch sample type"), 1137448d732cSIan Rogers NULL); 1138ac12f676SAndi Kleen return -EINVAL; 1139ac12f676SAndi Kleen } 11408f707d84SJiri Olsa break; 114132067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 114232067712SKan Liang CHECK_TYPE_VAL(NUM); 114332067712SKan Liang if (term->val.num > 1) { 1144448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1145448d732cSIan Rogers strdup("expected 0 or 1"), 1146448d732cSIan Rogers NULL); 114732067712SKan Liang return -EINVAL; 114832067712SKan Liang } 114932067712SKan Liang break; 1150d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1151d457c963SKan Liang CHECK_TYPE_VAL(STR); 1152d457c963SKan Liang break; 1153d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1154d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1155d457c963SKan Liang break; 1156374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1157374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1158374ce938SWang Nan break; 1159374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1160374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1161374ce938SWang Nan break; 1162626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1163626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1164626a6b78SWang Nan break; 1165626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1166626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1167626a6b78SWang Nan break; 11686b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 11696b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 11706b5fc39bSJiri Olsa break; 1171792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1172792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1173792d48b4SArnaldo Carvalho de Melo break; 11742fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 11752fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 11762fda5adaSArnaldo Carvalho de Melo break; 1177064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1178064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1179064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 1180448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1181448d732cSIan Rogers strdup("expected 0 or 1"), 1182448d732cSIan Rogers NULL); 1183064b4e82SJin Yao return -EINVAL; 1184064b4e82SJin Yao } 1185064b4e82SJin Yao break; 11861b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 11871b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 11881b992154SAdrian Hunter break; 1189eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1190eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 1191eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 1192eb7a52d4SAdrian Hunter parse_events__handle_error(err, term->err_val, 1193eb7a52d4SAdrian Hunter strdup("too big"), 1194eb7a52d4SAdrian Hunter NULL); 1195eb7a52d4SAdrian Hunter return -EINVAL; 1196eb7a52d4SAdrian Hunter } 1197eb7a52d4SAdrian Hunter break; 11988f707d84SJiri Olsa default: 1199448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1200448d732cSIan Rogers strdup("unknown term"), 1201448d732cSIan Rogers parse_events_formats_error_string(NULL)); 12028f707d84SJiri Olsa return -EINVAL; 12038f707d84SJiri Olsa } 120416fa7e82SJiri Olsa 12051669e509SWang Nan /* 12064d39c89fSIngo Molnar * Check term availability after basic checking so 12071669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 12081669e509SWang Nan * 12094d39c89fSIngo Molnar * If check availability at the entry of this function, 12101669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 12111669e509SWang Nan * if an invalid config term is provided for legacy events 12121669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 12131669e509SWang Nan */ 12141669e509SWang Nan if (!config_term_avail(term->type_term, err)) 12151669e509SWang Nan return -EINVAL; 12168f707d84SJiri Olsa return 0; 121716fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 12188f707d84SJiri Olsa } 12198f707d84SJiri Olsa 12200b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 12210b8891a8SHe Kuang struct parse_events_term *term, 12220b8891a8SHe Kuang struct parse_events_error *err) 12230b8891a8SHe Kuang { 1224dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1225dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 12260b8891a8SHe Kuang /* 12270b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 12280b8891a8SHe Kuang * at this point what type they need to have. 12290b8891a8SHe Kuang */ 12300b8891a8SHe Kuang return 0; 12310b8891a8SHe Kuang else 12320b8891a8SHe Kuang return config_term_common(attr, term, err); 12330b8891a8SHe Kuang } 12340b8891a8SHe Kuang 1235e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1236e637d177SHe Kuang struct parse_events_term *term, 1237e637d177SHe Kuang struct parse_events_error *err) 1238e637d177SHe Kuang { 1239e637d177SHe Kuang switch (term->type_term) { 1240e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1241e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1242374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1243374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1244792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 12452fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1246626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1247626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 12481b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1249eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1250e637d177SHe Kuang return config_term_common(attr, term, err); 1251e637d177SHe Kuang default: 1252e637d177SHe Kuang if (err) { 1253448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1254448d732cSIan Rogers strdup("unknown term"), 1255448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1256e637d177SHe Kuang } 1257e637d177SHe Kuang return -EINVAL; 1258e637d177SHe Kuang } 1259e637d177SHe Kuang 1260e637d177SHe Kuang return 0; 1261e637d177SHe Kuang } 1262e637d177SHe Kuang 12638f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 12643b0e371cSJiri Olsa struct list_head *head, 12650b8891a8SHe Kuang struct parse_events_error *err, 12660b8891a8SHe Kuang config_term_func_t config_term) 12678f707d84SJiri Olsa { 12686cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 12698f707d84SJiri Olsa 12708f707d84SJiri Olsa list_for_each_entry(term, head, list) 12713b0e371cSJiri Olsa if (config_term(attr, term, err)) 12728f707d84SJiri Olsa return -EINVAL; 12738f707d84SJiri Olsa 12748f707d84SJiri Olsa return 0; 12758f707d84SJiri Olsa } 12768f707d84SJiri Olsa 1277930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1278930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1279930a2e29SJiri Olsa { 128005e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 128135ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *__t; \ 1282930a2e29SJiri Olsa \ 1283930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1284930a2e29SJiri Olsa if (!__t) \ 1285930a2e29SJiri Olsa return -ENOMEM; \ 1286930a2e29SJiri Olsa \ 1287930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 128835ac0cadSArnaldo Carvalho de Melo __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 128905e54e23SIan Rogers __t->weak = __weak; \ 1290e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1291e884602bSLeo Yan 129205e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1293e884602bSLeo Yan do { \ 129405e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1295e884602bSLeo Yan __t->val.__name = __val; \ 1296e884602bSLeo Yan } while (0) 1297e884602bSLeo Yan 129805e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1299e884602bSLeo Yan do { \ 130005e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 13013220fb8dSLeo Yan __t->val.str = strdup(__val); \ 13023220fb8dSLeo Yan if (!__t->val.str) { \ 13033220fb8dSLeo Yan zfree(&__t); \ 13043220fb8dSLeo Yan return -ENOMEM; \ 13053220fb8dSLeo Yan } \ 13063220fb8dSLeo Yan __t->free_str = true; \ 1307930a2e29SJiri Olsa } while (0) 1308930a2e29SJiri Olsa 1309930a2e29SJiri Olsa struct parse_events_term *term; 1310930a2e29SJiri Olsa 1311930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1312930a2e29SJiri Olsa switch (term->type_term) { 1313ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 131405e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 131532067712SKan Liang break; 131609af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 131705e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 131809af2a55SNamhyung Kim break; 131932067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 132005e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 132132067712SKan Liang break; 1322d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 132305e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1324d457c963SKan Liang break; 1325ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 132605e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1327ac12f676SAndi Kleen break; 1328d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1329e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 133005e54e23SIan Rogers term->val.num, term->weak); 1331d457c963SKan Liang break; 1332374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1333e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 133405e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1335374ce938SWang Nan break; 1336374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1337e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 133805e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1339374ce938SWang Nan break; 1340792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1341e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 134205e54e23SIan Rogers term->val.num, term->weak); 1343792d48b4SArnaldo Carvalho de Melo break; 13442fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1345e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 134605e54e23SIan Rogers term->val.num, term->weak); 13472fda5adaSArnaldo Carvalho de Melo break; 1348626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1349e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 135005e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1351626a6b78SWang Nan break; 1352626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1353e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 135405e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1355626a6b78SWang Nan break; 1356dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 135705e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1358dd60fba7SMathieu Poirier break; 1359064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1360e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 136105e54e23SIan Rogers term->val.num ? true : false, term->weak); 1362064b4e82SJin Yao break; 13631b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1364e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 136505e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 13661b992154SAdrian Hunter break; 1367eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1368e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 136905e54e23SIan Rogers term->val.num, term->weak); 1370eb7a52d4SAdrian Hunter break; 1371930a2e29SJiri Olsa default: 1372930a2e29SJiri Olsa break; 1373930a2e29SJiri Olsa } 1374930a2e29SJiri Olsa } 1375a1ac7de6SAdrian Hunter return 0; 1376a1ac7de6SAdrian Hunter } 1377a1ac7de6SAdrian Hunter 1378a1ac7de6SAdrian Hunter /* 137935ac0cadSArnaldo Carvalho de Melo * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1380a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1381a1ac7de6SAdrian Hunter */ 1382a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1383a1ac7de6SAdrian Hunter struct list_head *head_terms) 1384a1ac7de6SAdrian Hunter { 1385a1ac7de6SAdrian Hunter struct parse_events_term *term; 1386a1ac7de6SAdrian Hunter u64 bits = 0; 1387a1ac7de6SAdrian Hunter int type; 1388a1ac7de6SAdrian Hunter 1389a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1390a1ac7de6SAdrian Hunter switch (term->type_term) { 1391a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 1392a1ac7de6SAdrian Hunter type = perf_pmu__format_type(&pmu->format, term->config); 1393a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1394a1ac7de6SAdrian Hunter continue; 1395a1ac7de6SAdrian Hunter bits |= perf_pmu__format_bits(&pmu->format, term->config); 1396a1ac7de6SAdrian Hunter break; 1397a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1398a1ac7de6SAdrian Hunter bits = ~(u64)0; 1399a1ac7de6SAdrian Hunter break; 1400a1ac7de6SAdrian Hunter default: 1401a1ac7de6SAdrian Hunter break; 1402a1ac7de6SAdrian Hunter } 1403a1ac7de6SAdrian Hunter } 1404a1ac7de6SAdrian Hunter 1405a1ac7de6SAdrian Hunter if (bits) 140605e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1407a1ac7de6SAdrian Hunter 1408a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1409930a2e29SJiri Olsa return 0; 1410930a2e29SJiri Olsa } 1411930a2e29SJiri Olsa 1412e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 14138c619d6aSWang Nan const char *sys, const char *event, 1414272ed29aSJiri Olsa struct parse_events_error *err, 1415e637d177SHe Kuang struct list_head *head_config) 1416e637d177SHe Kuang { 1417e637d177SHe Kuang if (head_config) { 1418e637d177SHe Kuang struct perf_event_attr attr; 1419e637d177SHe Kuang 1420272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1421e637d177SHe Kuang config_term_tracepoint)) 1422e637d177SHe Kuang return -EINVAL; 1423e637d177SHe Kuang } 1424e637d177SHe Kuang 1425e637d177SHe Kuang if (strpbrk(sys, "*?")) 1426e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1427272ed29aSJiri Olsa err, head_config); 1428e637d177SHe Kuang else 1429e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1430272ed29aSJiri Olsa err, head_config); 1431e637d177SHe Kuang } 1432e637d177SHe Kuang 14335d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 143487d650beSJiri Olsa struct list_head *list, 1435b527bab5SRobert Richter u32 type, u64 config, 14368f707d84SJiri Olsa struct list_head *head_config) 143774d5b588SJaswinder Singh Rajput { 143889812fc8SJiri Olsa struct perf_event_attr attr; 1439930a2e29SJiri Olsa LIST_HEAD(config_terms); 14409cbfa2f6SJin Yao bool hybrid; 14419cbfa2f6SJin Yao int ret; 144274d5b588SJaswinder Singh Rajput 144389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 144489812fc8SJiri Olsa attr.type = type; 144589812fc8SJiri Olsa attr.config = config; 14468f707d84SJiri Olsa 1447930a2e29SJiri Olsa if (head_config) { 14485d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 14490b8891a8SHe Kuang config_term_common)) 14508f707d84SJiri Olsa return -EINVAL; 14518f707d84SJiri Olsa 1452930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1453930a2e29SJiri Olsa return -ENOMEM; 1454930a2e29SJiri Olsa } 1455930a2e29SJiri Olsa 14569cbfa2f6SJin Yao ret = parse_events__add_numeric_hybrid(parse_state, list, &attr, 14579cbfa2f6SJin Yao get_config_name(head_config), 14589cbfa2f6SJin Yao &config_terms, &hybrid); 14599cbfa2f6SJin Yao if (hybrid) 14609cbfa2f6SJin Yao return ret; 14619cbfa2f6SJin Yao 14625d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 146310bf358aSWang Nan get_config_name(head_config), &config_terms); 1464b908debdSIngo Molnar } 1465b908debdSIngo Molnar 1466f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1467f0fbb114SAndi Kleen struct list_head *list, 1468f0fbb114SAndi Kleen enum perf_tool_event tool_event) 1469f0fbb114SAndi Kleen { 1470f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1471f0fbb114SAndi Kleen } 1472f0fbb114SAndi Kleen 1473064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1474064b4e82SJin Yao { 147535ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *term; 1476064b4e82SJin Yao 1477064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 147835ac0cadSArnaldo Carvalho de Melo if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1479064b4e82SJin Yao return term->val.percore; 1480064b4e82SJin Yao } 1481064b4e82SJin Yao 1482064b4e82SJin Yao return false; 1483064b4e82SJin Yao } 1484064b4e82SJin Yao 14855e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state, 14865e4edd1fSJin Yao struct list_head *list, char *name, 14875e4edd1fSJin Yao struct list_head *head_config) 14885e4edd1fSJin Yao { 14895e4edd1fSJin Yao struct parse_events_term *term; 14905e4edd1fSJin Yao int ret = -1; 14915e4edd1fSJin Yao 14925e4edd1fSJin Yao if (parse_state->fake_pmu || !head_config || list_empty(head_config) || 14935e4edd1fSJin Yao !perf_pmu__is_hybrid(name)) { 14945e4edd1fSJin Yao return -1; 14955e4edd1fSJin Yao } 14965e4edd1fSJin Yao 14975e4edd1fSJin Yao /* 14985e4edd1fSJin Yao * More than one term in list. 14995e4edd1fSJin Yao */ 15005e4edd1fSJin Yao if (head_config->next && head_config->next->next != head_config) 15015e4edd1fSJin Yao return -1; 15025e4edd1fSJin Yao 15035e4edd1fSJin Yao term = list_first_entry(head_config, struct parse_events_term, list); 15045e4edd1fSJin Yao if (term && term->config && strcmp(term->config, "event")) { 15055e4edd1fSJin Yao ret = parse_events__with_hybrid_pmu(parse_state, term->config, 15065e4edd1fSJin Yao name, list); 15075e4edd1fSJin Yao } 15085e4edd1fSJin Yao 15095e4edd1fSJin Yao return ret; 15105e4edd1fSJin Yao } 15115e4edd1fSJin Yao 1512c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 151336adec85SJiri Olsa struct list_head *list, char *name, 15143cdc5c2cSKan Liang struct list_head *head_config, 15153cdc5c2cSKan Liang bool auto_merge_stats, 15163cdc5c2cSKan Liang bool use_alias) 15175f537a26SJiri Olsa { 15185f537a26SJiri Olsa struct perf_event_attr attr; 151946441bdcSMatt Fleming struct perf_pmu_info info; 15205f537a26SJiri Olsa struct perf_pmu *pmu; 152132dcd021SJiri Olsa struct evsel *evsel; 1522333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 15233cdc5c2cSKan Liang bool use_uncore_alias; 1524930a2e29SJiri Olsa LIST_HEAD(config_terms); 15255f537a26SJiri Olsa 15264ac22b48SIan Rogers if (verbose > 1) { 15274ac22b48SIan Rogers fprintf(stderr, "Attempting to add event pmu '%s' with '", 15284ac22b48SIan Rogers name); 15294ac22b48SIan Rogers if (head_config) { 15304ac22b48SIan Rogers struct parse_events_term *term; 15314ac22b48SIan Rogers 15324ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15334ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15344ac22b48SIan Rogers } 15354ac22b48SIan Rogers } 15364ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15374ac22b48SIan Rogers } 15384ac22b48SIan Rogers 1539387ad33fSJiri Olsa pmu = parse_state->fake_pmu ?: perf_pmu__find(name); 1540333b5665SAndi Kleen if (!pmu) { 1541448d732cSIan Rogers char *err_str; 1542448d732cSIan Rogers 1543448d732cSIan Rogers if (asprintf(&err_str, 1544333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1545448d732cSIan Rogers name) >= 0) 1546a910e466SIan Rogers parse_events__handle_error(err, 0, err_str, NULL); 15475f537a26SJiri Olsa return -EINVAL; 1548333b5665SAndi Kleen } 15495f537a26SJiri Olsa 1550dc0a6202SAdrian Hunter if (pmu->default_config) { 1551dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1552dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1553dc0a6202SAdrian Hunter } else { 15545f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1555dc0a6202SAdrian Hunter } 15565f537a26SJiri Olsa 15573cdc5c2cSKan Liang use_uncore_alias = (pmu->is_uncore && use_alias); 15583cdc5c2cSKan Liang 1559ad962273SAdrian Hunter if (!head_config) { 1560ad962273SAdrian Hunter attr.type = pmu->type; 156170943490SStephane Eranian evsel = __add_event(list, &parse_state->idx, &attr, true, NULL, 156270943490SStephane Eranian pmu, NULL, auto_merge_stats, NULL); 15638c5421c0SAgustin Vega-Frias if (evsel) { 1564d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 15653cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 15668c5421c0SAgustin Vega-Frias return 0; 15678c5421c0SAgustin Vega-Frias } else { 15688c5421c0SAgustin Vega-Frias return -ENOMEM; 15698c5421c0SAgustin Vega-Frias } 1570ad962273SAdrian Hunter } 1571ad962273SAdrian Hunter 1572387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) 1573a6146d50SZheng Yan return -EINVAL; 1574a6146d50SZheng Yan 15754ac22b48SIan Rogers if (verbose > 1) { 15764ac22b48SIan Rogers fprintf(stderr, "After aliases, add event pmu '%s' with '", 15774ac22b48SIan Rogers name); 15784ac22b48SIan Rogers if (head_config) { 15794ac22b48SIan Rogers struct parse_events_term *term; 15804ac22b48SIan Rogers 15814ac22b48SIan Rogers list_for_each_entry(term, head_config, list) { 15824ac22b48SIan Rogers fprintf(stderr, "%s,", term->config); 15834ac22b48SIan Rogers } 15844ac22b48SIan Rogers } 15854ac22b48SIan Rogers fprintf(stderr, "' that may result in non-fatal errors\n"); 15864ac22b48SIan Rogers } 15874ac22b48SIan Rogers 15885f537a26SJiri Olsa /* 15895f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 15905f537a26SJiri Olsa * return value when called with fail == 0 ;) 15915f537a26SJiri Olsa */ 15925d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1593c056ba6aSJiri Olsa return -EINVAL; 15945f537a26SJiri Olsa 1595930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1596930a2e29SJiri Olsa return -ENOMEM; 1597930a2e29SJiri Olsa 1598a1ac7de6SAdrian Hunter /* 1599a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1600a1ac7de6SAdrian Hunter * changed by the user. 1601a1ac7de6SAdrian Hunter */ 1602a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1603a1ac7de6SAdrian Hunter return -ENOMEM; 1604a1ac7de6SAdrian Hunter 16055e4edd1fSJin Yao if (!parse_events__inside_hybrid_pmu(parse_state, list, name, 16065e4edd1fSJin Yao head_config)) { 16075e4edd1fSJin Yao return 0; 16085e4edd1fSJin Yao } 16095e4edd1fSJin Yao 1610387ad33fSJiri Olsa if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 161135ac0cadSArnaldo Carvalho de Melo struct evsel_config_term *pos, *tmp; 161238f2c422SIan Rogers 161338f2c422SIan Rogers list_for_each_entry_safe(pos, tmp, &config_terms, list) { 161438f2c422SIan Rogers list_del_init(&pos->list); 16157fcdccd4SArnaldo Carvalho de Melo if (pos->free_str) 1616e8dfb818SIan Rogers zfree(&pos->val.str); 161738f2c422SIan Rogers free(pos); 161838f2c422SIan Rogers } 16195f537a26SJiri Olsa return -EINVAL; 162038f2c422SIan Rogers } 16215f537a26SJiri Olsa 162270943490SStephane Eranian evsel = __add_event(list, &parse_state->idx, &attr, true, 162366ec1191SMark Rutland get_config_name(head_config), pmu, 1624f0fbb114SAndi Kleen &config_terms, auto_merge_stats, NULL); 162533321a06SIan Rogers if (!evsel) 162633321a06SIan Rogers return -ENOMEM; 162733321a06SIan Rogers 162812279429SJin Yao if (evsel->name) 162912279429SJin Yao evsel->use_config_name = true; 163012279429SJin Yao 163133321a06SIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 163233321a06SIan Rogers evsel->use_uncore_alias = use_uncore_alias; 163333321a06SIan Rogers evsel->percore = config_term_percore(&evsel->config_terms); 163433321a06SIan Rogers 163533321a06SIan Rogers if (parse_state->fake_pmu) 163633321a06SIan Rogers return 0; 163733321a06SIan Rogers 163846441bdcSMatt Fleming evsel->unit = info.unit; 163946441bdcSMatt Fleming evsel->scale = info.scale; 1640044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 16411d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 164237932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 164396284814SAndi Kleen evsel->metric_name = info.metric_name; 164433321a06SIan Rogers return 0; 16455f537a26SJiri Olsa } 16465f537a26SJiri Olsa 16475d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 16482073ad33SAndi Kleen char *str, struct list_head **listp) 16492073ad33SAndi Kleen { 16502073ad33SAndi Kleen struct parse_events_term *term; 16512073ad33SAndi Kleen struct list_head *list; 16522073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 16532073ad33SAndi Kleen int ok = 0; 16542073ad33SAndi Kleen 16552073ad33SAndi Kleen *listp = NULL; 16562073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 16572073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 16582073ad33SAndi Kleen if (!list) 16592073ad33SAndi Kleen return -1; 16602073ad33SAndi Kleen INIT_LIST_HEAD(list); 16612073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 16622073ad33SAndi Kleen struct perf_pmu_alias *alias; 16632073ad33SAndi Kleen 16642073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 16652073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 1666b6645a72SIan Rogers struct list_head *head; 1667b6645a72SIan Rogers char *config; 1668b6645a72SIan Rogers 16692073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 16702073ad33SAndi Kleen if (!head) 16712073ad33SAndi Kleen return -1; 16722073ad33SAndi Kleen INIT_LIST_HEAD(head); 1673b6645a72SIan Rogers config = strdup(str); 1674b6645a72SIan Rogers if (!config) 16752073ad33SAndi Kleen return -1; 1676b6645a72SIan Rogers if (parse_events_term__num(&term, 1677b6645a72SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 1678b6645a72SIan Rogers config, 1, false, &config, 1679b6645a72SIan Rogers NULL) < 0) { 1680b6645a72SIan Rogers free(list); 1681b6645a72SIan Rogers free(config); 1682b6645a72SIan Rogers return -1; 1683b6645a72SIan Rogers } 16842073ad33SAndi Kleen list_add_tail(&term->list, head); 16852073ad33SAndi Kleen 1686c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 16873cdc5c2cSKan Liang pmu->name, head, 16883cdc5c2cSKan Liang true, true)) { 1689d4953f7eSIan Rogers pr_debug("%s -> %s/%s/\n", str, 16902073ad33SAndi Kleen pmu->name, alias->str); 16912073ad33SAndi Kleen ok++; 16922073ad33SAndi Kleen } 16932073ad33SAndi Kleen 16942073ad33SAndi Kleen parse_events_terms__delete(head); 16952073ad33SAndi Kleen } 16962073ad33SAndi Kleen } 16972073ad33SAndi Kleen } 1698b6645a72SIan Rogers if (!ok) { 1699b6645a72SIan Rogers free(list); 17002073ad33SAndi Kleen return -1; 1701b6645a72SIan Rogers } 17022073ad33SAndi Kleen *listp = list; 17032073ad33SAndi Kleen return 0; 17042073ad33SAndi Kleen } 17052073ad33SAndi Kleen 17066a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 17076a4bb04cSJiri Olsa char *event_mod) 170889efb029SJiri Olsa { 17096a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 17106a4bb04cSJiri Olsa } 17116a4bb04cSJiri Olsa 17123cdc5c2cSKan Liang /* 17133cdc5c2cSKan Liang * Check if the two uncore PMUs are from the same uncore block 17143cdc5c2cSKan Liang * The format of the uncore PMU name is uncore_#blockname_#pmuidx 17153cdc5c2cSKan Liang */ 17163cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b) 17173cdc5c2cSKan Liang { 17183cdc5c2cSKan Liang char *end_a, *end_b; 17193cdc5c2cSKan Liang 17203cdc5c2cSKan Liang end_a = strrchr(pmu_name_a, '_'); 17213cdc5c2cSKan Liang end_b = strrchr(pmu_name_b, '_'); 17223cdc5c2cSKan Liang 17233cdc5c2cSKan Liang if (!end_a || !end_b) 17243cdc5c2cSKan Liang return false; 17253cdc5c2cSKan Liang 17263cdc5c2cSKan Liang if ((end_a - pmu_name_a) != (end_b - pmu_name_b)) 17273cdc5c2cSKan Liang return false; 17283cdc5c2cSKan Liang 17293cdc5c2cSKan Liang return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0); 17303cdc5c2cSKan Liang } 17313cdc5c2cSKan Liang 17323cdc5c2cSKan Liang static int 17333cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list, 17343cdc5c2cSKan Liang struct parse_events_state *parse_state) 17353cdc5c2cSKan Liang { 173632dcd021SJiri Olsa struct evsel *evsel, *leader; 17373cdc5c2cSKan Liang uintptr_t *leaders; 17383cdc5c2cSKan Liang bool is_leader = true; 17393cdc5c2cSKan Liang int i, nr_pmu = 0, total_members, ret = 0; 17403cdc5c2cSKan Liang 1741b27c4eceSJiri Olsa leader = list_first_entry(list, struct evsel, core.node); 1742b27c4eceSJiri Olsa evsel = list_last_entry(list, struct evsel, core.node); 174338fe0e01SJiri Olsa total_members = evsel->core.idx - leader->core.idx + 1; 17443cdc5c2cSKan Liang 17453cdc5c2cSKan Liang leaders = calloc(total_members, sizeof(uintptr_t)); 17463cdc5c2cSKan Liang if (WARN_ON(!leaders)) 17473cdc5c2cSKan Liang return 0; 17483cdc5c2cSKan Liang 17493cdc5c2cSKan Liang /* 17503cdc5c2cSKan Liang * Going through the whole group and doing sanity check. 17513cdc5c2cSKan Liang * All members must use alias, and be from the same uncore block. 17523cdc5c2cSKan Liang * Also, storing the leader events in an array. 17533cdc5c2cSKan Liang */ 17543cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 17553cdc5c2cSKan Liang 17563cdc5c2cSKan Liang /* Only split the uncore group which members use alias */ 17573cdc5c2cSKan Liang if (!evsel->use_uncore_alias) 17583cdc5c2cSKan Liang goto out; 17593cdc5c2cSKan Liang 17603cdc5c2cSKan Liang /* The events must be from the same uncore block */ 17613cdc5c2cSKan Liang if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name)) 17623cdc5c2cSKan Liang goto out; 17633cdc5c2cSKan Liang 17643cdc5c2cSKan Liang if (!is_leader) 17653cdc5c2cSKan Liang continue; 17663cdc5c2cSKan Liang /* 17673cdc5c2cSKan Liang * If the event's PMU name starts to repeat, it must be a new 17683cdc5c2cSKan Liang * event. That can be used to distinguish the leader from 17693cdc5c2cSKan Liang * other members, even they have the same event name. 17703cdc5c2cSKan Liang */ 17718510895bSJin Yao if ((leader != evsel) && 17728510895bSJin Yao !strcmp(leader->pmu_name, evsel->pmu_name)) { 17733cdc5c2cSKan Liang is_leader = false; 17743cdc5c2cSKan Liang continue; 17753cdc5c2cSKan Liang } 17763cdc5c2cSKan Liang 17773cdc5c2cSKan Liang /* Store the leader event for each PMU */ 17783cdc5c2cSKan Liang leaders[nr_pmu++] = (uintptr_t) evsel; 17793cdc5c2cSKan Liang } 17803cdc5c2cSKan Liang 17813cdc5c2cSKan Liang /* only one event alias */ 17823cdc5c2cSKan Liang if (nr_pmu == total_members) { 17833cdc5c2cSKan Liang parse_state->nr_groups--; 17843cdc5c2cSKan Liang goto handled; 17853cdc5c2cSKan Liang } 17863cdc5c2cSKan Liang 17873cdc5c2cSKan Liang /* 17883cdc5c2cSKan Liang * An uncore event alias is a joint name which means the same event 17893cdc5c2cSKan Liang * runs on all PMUs of a block. 17903cdc5c2cSKan Liang * Perf doesn't support mixed events from different PMUs in the same 17913cdc5c2cSKan Liang * group. The big group has to be split into multiple small groups 17923cdc5c2cSKan Liang * which only include the events from the same PMU. 17933cdc5c2cSKan Liang * 17943cdc5c2cSKan Liang * Here the uncore event aliases must be from the same uncore block. 17953cdc5c2cSKan Liang * The number of PMUs must be same for each alias. The number of new 17963cdc5c2cSKan Liang * small groups equals to the number of PMUs. 17973cdc5c2cSKan Liang * Setting the leader event for corresponding members in each group. 17983cdc5c2cSKan Liang */ 17993cdc5c2cSKan Liang i = 0; 18003cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 18013cdc5c2cSKan Liang if (i >= nr_pmu) 18023cdc5c2cSKan Liang i = 0; 1803fba7c866SJiri Olsa evsel__set_leader(evsel, (struct evsel *) leaders[i++]); 18043cdc5c2cSKan Liang } 18053cdc5c2cSKan Liang 18063cdc5c2cSKan Liang /* The number of members and group name are same for each group */ 18073cdc5c2cSKan Liang for (i = 0; i < nr_pmu; i++) { 180832dcd021SJiri Olsa evsel = (struct evsel *) leaders[i]; 18095643b1a5SJiri Olsa evsel->core.nr_members = total_members / nr_pmu; 18103cdc5c2cSKan Liang evsel->group_name = name ? strdup(name) : NULL; 18113cdc5c2cSKan Liang } 18123cdc5c2cSKan Liang 18133cdc5c2cSKan Liang /* Take the new small groups into account */ 18143cdc5c2cSKan Liang parse_state->nr_groups += nr_pmu - 1; 18153cdc5c2cSKan Liang 18163cdc5c2cSKan Liang handled: 18173cdc5c2cSKan Liang ret = 1; 18183cdc5c2cSKan Liang out: 18193cdc5c2cSKan Liang free(leaders); 18203cdc5c2cSKan Liang return ret; 18213cdc5c2cSKan Liang } 18223cdc5c2cSKan Liang 18233cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list, 18243cdc5c2cSKan Liang struct parse_events_state *parse_state) 18256a4bb04cSJiri Olsa { 182632dcd021SJiri Olsa struct evsel *leader; 18276a4bb04cSJiri Olsa 1828854f7363SWang Nan if (list_empty(list)) { 1829854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1830854f7363SWang Nan return; 1831854f7363SWang Nan } 1832854f7363SWang Nan 18333cdc5c2cSKan Liang if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state)) 18343cdc5c2cSKan Liang return; 18353cdc5c2cSKan Liang 1836a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(list); 1837b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 18386a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 183989efb029SJiri Olsa } 184089efb029SJiri Olsa 1841c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 18425d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 18435d7be90eSJiri Olsa struct list_head *list_all) 18445d7be90eSJiri Olsa { 18455d7be90eSJiri Olsa /* 18465d7be90eSJiri Olsa * Called for single event definition. Update the 184789efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 18485d7be90eSJiri Olsa * list, for next event definition. 18495d7be90eSJiri Olsa */ 18505d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1851b847cbdcSJiri Olsa free(list_event); 18525d7be90eSJiri Olsa } 18535d7be90eSJiri Olsa 1854f5b1135bSJiri Olsa struct event_modifier { 1855f5b1135bSJiri Olsa int eu; 1856f5b1135bSJiri Olsa int ek; 1857f5b1135bSJiri Olsa int eh; 1858f5b1135bSJiri Olsa int eH; 1859f5b1135bSJiri Olsa int eG; 1860a1e12da4SJiri Olsa int eI; 1861f5b1135bSJiri Olsa int precise; 18627f94af7aSJiri Olsa int precise_max; 1863f5b1135bSJiri Olsa int exclude_GH; 18643c176311SJiri Olsa int sample_read; 1865e9a7c414SMichael Ellerman int pinned; 18665a5dfe4bSAndi Kleen int weak; 18670997a266SAndi Kleen int exclusive; 186801bd8efcSSong Liu int bpf_counter; 1869f5b1135bSJiri Olsa }; 187061c45981SPaul Mackerras 1871f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 187232dcd021SJiri Olsa struct evsel *evsel) 1873f5b1135bSJiri Olsa { 18741fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 18751fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 18761fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 18771fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 18781fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 18791fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 18801fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 18817f94af7aSJiri Olsa int precise_max = 0; 18823c176311SJiri Olsa int sample_read = 0; 18831fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 18840997a266SAndi Kleen int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1885f5b1135bSJiri Olsa 1886f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1887f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 18885a5dfe4bSAndi Kleen int weak = 0; 188901bd8efcSSong Liu int bpf_counter = 0; 1890f5b1135bSJiri Olsa 1891f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1892ceb53fbfSIngo Molnar 189361c45981SPaul Mackerras while (*str) { 1894ab608344SPeter Zijlstra if (*str == 'u') { 1895ab608344SPeter Zijlstra if (!exclude) 1896ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 1897943b69acSJin Yao if (!exclude_GH && !perf_guest) 1898943b69acSJin Yao eG = 1; 189961c45981SPaul Mackerras eu = 0; 1900ab608344SPeter Zijlstra } else if (*str == 'k') { 1901ab608344SPeter Zijlstra if (!exclude) 1902ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 190361c45981SPaul Mackerras ek = 0; 1904ab608344SPeter Zijlstra } else if (*str == 'h') { 1905ab608344SPeter Zijlstra if (!exclude) 1906ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 190761c45981SPaul Mackerras eh = 0; 190899320cc8SJoerg Roedel } else if (*str == 'G') { 190999320cc8SJoerg Roedel if (!exclude_GH) 191099320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 191199320cc8SJoerg Roedel eG = 0; 191299320cc8SJoerg Roedel } else if (*str == 'H') { 191399320cc8SJoerg Roedel if (!exclude_GH) 191499320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 191599320cc8SJoerg Roedel eH = 0; 1916a1e12da4SJiri Olsa } else if (*str == 'I') { 1917a1e12da4SJiri Olsa eI = 1; 1918ab608344SPeter Zijlstra } else if (*str == 'p') { 1919ab608344SPeter Zijlstra precise++; 19201342798cSDavid Ahern /* use of precise requires exclude_guest */ 19211342798cSDavid Ahern if (!exclude_GH) 19221342798cSDavid Ahern eG = 1; 19237f94af7aSJiri Olsa } else if (*str == 'P') { 19247f94af7aSJiri Olsa precise_max = 1; 19253c176311SJiri Olsa } else if (*str == 'S') { 19263c176311SJiri Olsa sample_read = 1; 1927e9a7c414SMichael Ellerman } else if (*str == 'D') { 1928e9a7c414SMichael Ellerman pinned = 1; 19290997a266SAndi Kleen } else if (*str == 'e') { 19300997a266SAndi Kleen exclusive = 1; 19315a5dfe4bSAndi Kleen } else if (*str == 'W') { 19325a5dfe4bSAndi Kleen weak = 1; 193301bd8efcSSong Liu } else if (*str == 'b') { 193401bd8efcSSong Liu bpf_counter = 1; 1935ab608344SPeter Zijlstra } else 193661c45981SPaul Mackerras break; 1937ab608344SPeter Zijlstra 193861c45981SPaul Mackerras ++str; 193961c45981SPaul Mackerras } 194074d5b588SJaswinder Singh Rajput 194186470930SIngo Molnar /* 194289812fc8SJiri Olsa * precise ip: 194389812fc8SJiri Olsa * 194489812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 194589812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 194689812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 194789812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 194889812fc8SJiri Olsa * 194989812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 195086470930SIngo Molnar */ 195189812fc8SJiri Olsa if (precise > 3) 195289812fc8SJiri Olsa return -EINVAL; 195386470930SIngo Molnar 1954f5b1135bSJiri Olsa mod->eu = eu; 1955f5b1135bSJiri Olsa mod->ek = ek; 1956f5b1135bSJiri Olsa mod->eh = eh; 1957f5b1135bSJiri Olsa mod->eH = eH; 1958f5b1135bSJiri Olsa mod->eG = eG; 1959a1e12da4SJiri Olsa mod->eI = eI; 1960f5b1135bSJiri Olsa mod->precise = precise; 19617f94af7aSJiri Olsa mod->precise_max = precise_max; 1962f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 19633c176311SJiri Olsa mod->sample_read = sample_read; 1964e9a7c414SMichael Ellerman mod->pinned = pinned; 19655a5dfe4bSAndi Kleen mod->weak = weak; 196601bd8efcSSong Liu mod->bpf_counter = bpf_counter; 19670997a266SAndi Kleen mod->exclusive = exclusive; 1968e9a7c414SMichael Ellerman 1969f5b1135bSJiri Olsa return 0; 1970f5b1135bSJiri Olsa } 1971f5b1135bSJiri Olsa 1972534123f4SJiri Olsa /* 1973534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1974534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1975534123f4SJiri Olsa */ 1976534123f4SJiri Olsa static int check_modifier(char *str) 1977534123f4SJiri Olsa { 1978534123f4SJiri Olsa char *p = str; 1979534123f4SJiri Olsa 1980534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 198101bd8efcSSong Liu if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1982534123f4SJiri Olsa return -1; 1983534123f4SJiri Olsa 1984534123f4SJiri Olsa while (*p) { 1985534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1986534123f4SJiri Olsa return -1; 1987534123f4SJiri Olsa p++; 1988534123f4SJiri Olsa } 1989534123f4SJiri Olsa 1990534123f4SJiri Olsa return 0; 1991534123f4SJiri Olsa } 1992534123f4SJiri Olsa 1993f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1994f5b1135bSJiri Olsa { 199532dcd021SJiri Olsa struct evsel *evsel; 1996f5b1135bSJiri Olsa struct event_modifier mod; 1997f5b1135bSJiri Olsa 1998f5b1135bSJiri Olsa if (str == NULL) 1999f5b1135bSJiri Olsa return 0; 2000f5b1135bSJiri Olsa 2001534123f4SJiri Olsa if (check_modifier(str)) 2002534123f4SJiri Olsa return -EINVAL; 2003534123f4SJiri Olsa 2004f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 2005f5b1135bSJiri Olsa return -EINVAL; 2006f5b1135bSJiri Olsa 2007e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 2008f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 2009f5b1135bSJiri Olsa return -EINVAL; 2010f5b1135bSJiri Olsa 20111fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 20121fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 20131fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 20141fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 20151fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 20161fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 20171fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 2018f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 20193c176311SJiri Olsa evsel->sample_read = mod.sample_read; 20207f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 20215a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 202201bd8efcSSong Liu evsel->bpf_counter = mod.bpf_counter; 2023e9a7c414SMichael Ellerman 20240997a266SAndi Kleen if (evsel__is_group_leader(evsel)) { 20251fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 20260997a266SAndi Kleen evsel->core.attr.exclusive = mod.exclusive; 20270997a266SAndi Kleen } 2028ceb53fbfSIngo Molnar } 202986470930SIngo Molnar 203089812fc8SJiri Olsa return 0; 203186470930SIngo Molnar } 203286470930SIngo Molnar 2033ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 2034ac2ba9f3SRobert Richter { 203532dcd021SJiri Olsa struct evsel *evsel; 2036ac2ba9f3SRobert Richter 2037e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 2038ac2ba9f3SRobert Richter if (!evsel->name) 2039ac2ba9f3SRobert Richter evsel->name = strdup(name); 2040ac2ba9f3SRobert Richter } 2041ac2ba9f3SRobert Richter 2042ac2ba9f3SRobert Richter return 0; 2043ac2ba9f3SRobert Richter } 2044ac2ba9f3SRobert Richter 2045dcb4e102SKan Liang static int 2046dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 2047dcb4e102SKan Liang { 2048dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 2049dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 2050dcb4e102SKan Liang 2051e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 2052dcb4e102SKan Liang } 2053dcb4e102SKan Liang 2054dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 2055dcb4e102SKan Liang { 2056dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 2057dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 2058dcb4e102SKan Liang int i; 2059dcb4e102SKan Liang 2060dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 2061dcb4e102SKan Liang p = perf_pmu_events_list + i; 2062360e071bSTaeung Song zfree(&p->symbol); 2063dcb4e102SKan Liang } 2064506fde11STaeung Song zfree(&perf_pmu_events_list); 2065dcb4e102SKan Liang perf_pmu_events_list_num = 0; 2066dcb4e102SKan Liang } 2067dcb4e102SKan Liang } 2068dcb4e102SKan Liang 2069dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 2070dcb4e102SKan Liang do { \ 2071dcb4e102SKan Liang p->symbol = str; \ 2072dcb4e102SKan Liang if (!p->symbol) \ 2073dcb4e102SKan Liang goto err; \ 2074dcb4e102SKan Liang p->type = stype; \ 2075dcb4e102SKan Liang } while (0) 2076dcb4e102SKan Liang 2077dcb4e102SKan Liang /* 2078dcb4e102SKan Liang * Read the pmu events list from sysfs 2079dcb4e102SKan Liang * Save it into perf_pmu_events_list 2080dcb4e102SKan Liang */ 2081dcb4e102SKan Liang static void perf_pmu__parse_init(void) 2082dcb4e102SKan Liang { 2083dcb4e102SKan Liang 2084dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 2085dcb4e102SKan Liang struct perf_pmu_alias *alias; 2086dcb4e102SKan Liang int len = 0; 2087dcb4e102SKan Liang 2088231bb2aaSAndi Kleen pmu = NULL; 2089231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2090dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 2091dcb4e102SKan Liang if (strchr(alias->name, '-')) 2092dcb4e102SKan Liang len++; 2093dcb4e102SKan Liang len++; 2094dcb4e102SKan Liang } 2095231bb2aaSAndi Kleen } 2096231bb2aaSAndi Kleen 2097231bb2aaSAndi Kleen if (len == 0) { 2098231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 2099231bb2aaSAndi Kleen return; 2100231bb2aaSAndi Kleen } 2101dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 2102dcb4e102SKan Liang if (!perf_pmu_events_list) 2103dcb4e102SKan Liang return; 2104dcb4e102SKan Liang perf_pmu_events_list_num = len; 2105dcb4e102SKan Liang 2106dcb4e102SKan Liang len = 0; 2107231bb2aaSAndi Kleen pmu = NULL; 2108231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2109dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 2110dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 2111dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 2112dcb4e102SKan Liang 2113dcb4e102SKan Liang if (tmp != NULL) { 2114dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 2115dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 2116dcb4e102SKan Liang p++; 2117dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 2118dcb4e102SKan Liang len += 2; 2119dcb4e102SKan Liang } else { 2120dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 2121dcb4e102SKan Liang len++; 2122dcb4e102SKan Liang } 2123dcb4e102SKan Liang } 2124231bb2aaSAndi Kleen } 2125dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 2126dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2127dcb4e102SKan Liang 2128dcb4e102SKan Liang return; 2129dcb4e102SKan Liang err: 2130dcb4e102SKan Liang perf_pmu__parse_cleanup(); 2131dcb4e102SKan Liang } 2132dcb4e102SKan Liang 21334929e95aSJiri Olsa /* 21344929e95aSJiri Olsa * This function injects special term in 21354929e95aSJiri Olsa * perf_pmu_events_list so the test code 21364929e95aSJiri Olsa * can check on this functionality. 21374929e95aSJiri Olsa */ 21384929e95aSJiri Olsa int perf_pmu__test_parse_init(void) 21394929e95aSJiri Olsa { 21404929e95aSJiri Olsa struct perf_pmu_event_symbol *list; 21414929e95aSJiri Olsa 21424929e95aSJiri Olsa list = malloc(sizeof(*list) * 1); 21434929e95aSJiri Olsa if (!list) 21444929e95aSJiri Olsa return -ENOMEM; 21454929e95aSJiri Olsa 21464929e95aSJiri Olsa list->type = PMU_EVENT_SYMBOL; 21474929e95aSJiri Olsa list->symbol = strdup("read"); 21484929e95aSJiri Olsa 21494929e95aSJiri Olsa if (!list->symbol) { 21504929e95aSJiri Olsa free(list); 21514929e95aSJiri Olsa return -ENOMEM; 21524929e95aSJiri Olsa } 21534929e95aSJiri Olsa 21544929e95aSJiri Olsa perf_pmu_events_list = list; 21554929e95aSJiri Olsa perf_pmu_events_list_num = 1; 21564929e95aSJiri Olsa return 0; 21574929e95aSJiri Olsa } 21584929e95aSJiri Olsa 2159dcb4e102SKan Liang enum perf_pmu_event_symbol_type 2160dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 2161dcb4e102SKan Liang { 2162dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 2163dcb4e102SKan Liang 2164dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 2165dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 2166dcb4e102SKan Liang perf_pmu__parse_init(); 2167dcb4e102SKan Liang /* 2168dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 2169dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 2170dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 2171dcb4e102SKan Liang */ 2172dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 2173dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 2174dcb4e102SKan Liang 2175dcb4e102SKan Liang p.symbol = strdup(name); 2176dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 2177dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 2178dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2179360e071bSTaeung Song zfree(&p.symbol); 2180dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 2181dcb4e102SKan Liang } 2182dcb4e102SKan Liang 21831244a327SJiri Olsa static int parse_events__scanner(const char *str, 21841244a327SJiri Olsa struct parse_events_state *parse_state) 2185ac20de6fSZheng Yan { 2186ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 2187ac20de6fSZheng Yan void *scanner; 2188ac20de6fSZheng Yan int ret; 2189ac20de6fSZheng Yan 21901244a327SJiri Olsa ret = parse_events_lex_init_extra(parse_state, &scanner); 2191ac20de6fSZheng Yan if (ret) 2192ac20de6fSZheng Yan return ret; 2193ac20de6fSZheng Yan 2194ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 2195ac20de6fSZheng Yan 2196ac20de6fSZheng Yan #ifdef PARSER_DEBUG 2197ac20de6fSZheng Yan parse_events_debug = 1; 2198ae762641SJiri Olsa parse_events_set_debug(1, scanner); 2199ac20de6fSZheng Yan #endif 22005d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 2201ac20de6fSZheng Yan 2202ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 2203ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 2204ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 2205ac20de6fSZheng Yan return ret; 2206ac20de6fSZheng Yan } 2207ac20de6fSZheng Yan 220890e2b22dSJiri Olsa /* 220990e2b22dSJiri Olsa * parse event config string, return a list of event terms. 221090e2b22dSJiri Olsa */ 221190e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 221290e2b22dSJiri Olsa { 22135d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 221490e2b22dSJiri Olsa .terms = NULL, 22151244a327SJiri Olsa .stoken = PE_START_TERMS, 221690e2b22dSJiri Olsa }; 221790e2b22dSJiri Olsa int ret; 221890e2b22dSJiri Olsa 22191244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 22204929e95aSJiri Olsa perf_pmu__parse_cleanup(); 22214929e95aSJiri Olsa 222290e2b22dSJiri Olsa if (!ret) { 22235d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 22245d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 222590e2b22dSJiri Olsa return 0; 222690e2b22dSJiri Olsa } 222790e2b22dSJiri Olsa 22285d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 222990e2b22dSJiri Olsa return ret; 223090e2b22dSJiri Olsa } 223190e2b22dSJiri Olsa 22325e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, 22335e4edd1fSJin Yao const char *str, char *pmu_name, 22345e4edd1fSJin Yao struct list_head *list) 22355e4edd1fSJin Yao { 22365e4edd1fSJin Yao struct parse_events_state ps = { 22375e4edd1fSJin Yao .list = LIST_HEAD_INIT(ps.list), 22385e4edd1fSJin Yao .stoken = PE_START_EVENTS, 22395e4edd1fSJin Yao .hybrid_pmu_name = pmu_name, 22405e4edd1fSJin Yao .idx = parse_state->idx, 22415e4edd1fSJin Yao }; 22425e4edd1fSJin Yao int ret; 22435e4edd1fSJin Yao 22445e4edd1fSJin Yao ret = parse_events__scanner(str, &ps); 22455e4edd1fSJin Yao perf_pmu__parse_cleanup(); 22465e4edd1fSJin Yao 22475e4edd1fSJin Yao if (!ret) { 22485e4edd1fSJin Yao if (!list_empty(&ps.list)) { 22495e4edd1fSJin Yao list_splice(&ps.list, list); 22505e4edd1fSJin Yao parse_state->idx = ps.idx; 22515e4edd1fSJin Yao return 0; 22525e4edd1fSJin Yao } else 22535e4edd1fSJin Yao return -1; 22545e4edd1fSJin Yao } 22555e4edd1fSJin Yao 22565e4edd1fSJin Yao return ret; 22575e4edd1fSJin Yao } 22585e4edd1fSJin Yao 22593bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str, 22603bf91aa5SArnaldo Carvalho de Melo struct parse_events_error *err, struct perf_pmu *fake_pmu) 226186470930SIngo Molnar { 22625d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 22635d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 22646484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 2265b39b8393SJiri Olsa .error = err, 22667630b3e2SWang Nan .evlist = evlist, 22671244a327SJiri Olsa .stoken = PE_START_EVENTS, 22683bf91aa5SArnaldo Carvalho de Melo .fake_pmu = fake_pmu, 226946010ab2SJiri Olsa }; 227046010ab2SJiri Olsa int ret; 227186470930SIngo Molnar 22721244a327SJiri Olsa ret = parse_events__scanner(str, &parse_state); 2273dcb4e102SKan Liang perf_pmu__parse_cleanup(); 227415bfd2ccSWang Nan 22758e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2276c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2277854f7363SWang Nan return -1; 2278854f7363SWang Nan } 2279854f7363SWang Nan 22808e8714c3SIan Rogers /* 22818e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 22828e8714c3SIan Rogers */ 2283e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &parse_state.list); 22848e8714c3SIan Rogers 22858e8714c3SIan Rogers if (!ret) { 22868e8714c3SIan Rogers struct evsel *last; 22878e8714c3SIan Rogers 2288*3a683120SJiri Olsa evlist->core.nr_groups += parse_state.nr_groups; 2289515dbe48SJiri Olsa last = evlist__last(evlist); 229015bfd2ccSWang Nan last->cmdline_group_boundary = true; 229115bfd2ccSWang Nan 229286470930SIngo Molnar return 0; 229386470930SIngo Molnar } 229486470930SIngo Molnar 22955d7be90eSJiri Olsa /* 22965d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2297c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 22985d7be90eSJiri Olsa * need to bother. 22995d7be90eSJiri Olsa */ 230089812fc8SJiri Olsa return ret; 230189812fc8SJiri Olsa } 230289812fc8SJiri Olsa 2303b39b8393SJiri Olsa #define MAX_WIDTH 1000 2304b39b8393SJiri Olsa static int get_term_width(void) 2305b39b8393SJiri Olsa { 2306b39b8393SJiri Olsa struct winsize ws; 2307b39b8393SJiri Olsa 2308b39b8393SJiri Olsa get_term_dimensions(&ws); 2309b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2310b39b8393SJiri Olsa } 2311b39b8393SJiri Olsa 2312a910e466SIan Rogers static void __parse_events_print_error(int err_idx, const char *err_str, 2313a910e466SIan Rogers const char *err_help, const char *event) 2314b39b8393SJiri Olsa { 2315b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2316b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2317b39b8393SJiri Olsa char *buf = (char *) event; 2318b39b8393SJiri Olsa int idx = 0; 2319a910e466SIan Rogers if (err_str) { 2320b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2321b39b8393SJiri Olsa int width = get_term_width() - 2; 2322b39b8393SJiri Olsa int len_event = strlen(event); 2323b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2324b39b8393SJiri Olsa 2325b39b8393SJiri Olsa /* 2326b39b8393SJiri Olsa * Maximum error index indent, we will cut 2327b39b8393SJiri Olsa * the event string if it's bigger. 2328b39b8393SJiri Olsa */ 2329141b2d31SAdrian Hunter int max_err_idx = 13; 2330b39b8393SJiri Olsa 2331b39b8393SJiri Olsa /* 2332b39b8393SJiri Olsa * Let's be specific with the message when 2333b39b8393SJiri Olsa * we have the precise error. 2334b39b8393SJiri Olsa */ 2335b39b8393SJiri Olsa str = "event syntax error: "; 2336b39b8393SJiri Olsa len_str = strlen(str); 2337b39b8393SJiri Olsa max_len = width - len_str; 2338b39b8393SJiri Olsa 2339b39b8393SJiri Olsa buf = _buf; 2340b39b8393SJiri Olsa 2341bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2342a910e466SIan Rogers if (err_idx > max_err_idx) 2343a910e466SIan Rogers cut = err_idx - max_err_idx; 2344b39b8393SJiri Olsa 2345b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2346b39b8393SJiri Olsa 2347b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2348b39b8393SJiri Olsa if (cut) 2349b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2350b39b8393SJiri Olsa 2351b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2352b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2353b39b8393SJiri Olsa buf[max_len] = 0; 2354b39b8393SJiri Olsa } 2355b39b8393SJiri Olsa 2356a910e466SIan Rogers idx = len_str + err_idx - cut; 2357b39b8393SJiri Olsa } 2358b39b8393SJiri Olsa 2359b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2360b39b8393SJiri Olsa if (idx) { 2361a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2362a910e466SIan Rogers if (err_help) 2363a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2364a910e466SIan Rogers } 2365a910e466SIan Rogers } 2366a910e466SIan Rogers 2367a910e466SIan Rogers void parse_events_print_error(struct parse_events_error *err, 2368a910e466SIan Rogers const char *event) 2369a910e466SIan Rogers { 2370a910e466SIan Rogers if (!err->num_errors) 2371a910e466SIan Rogers return; 2372a910e466SIan Rogers 2373a910e466SIan Rogers __parse_events_print_error(err->idx, err->str, err->help, event); 2374360e071bSTaeung Song zfree(&err->str); 2375360e071bSTaeung Song zfree(&err->help); 2376a910e466SIan Rogers 2377a910e466SIan Rogers if (err->num_errors > 1) { 2378a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 2379a910e466SIan Rogers __parse_events_print_error(err->first_idx, err->first_str, 2380a910e466SIan Rogers err->first_help, event); 2381a910e466SIan Rogers zfree(&err->first_str); 2382a910e466SIan Rogers zfree(&err->first_help); 2383b39b8393SJiri Olsa } 2384b39b8393SJiri Olsa } 2385b39b8393SJiri Olsa 2386b39b8393SJiri Olsa #undef MAX_WIDTH 2387b39b8393SJiri Olsa 2388f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 23891d037ca1SIrina Tirdea int unset __maybe_unused) 2390f120f9d5SJiri Olsa { 239163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2392a910e466SIan Rogers struct parse_events_error err; 2393a910e466SIan Rogers int ret; 2394a910e466SIan Rogers 2395a910e466SIan Rogers bzero(&err, sizeof(err)); 2396a910e466SIan Rogers ret = parse_events(evlist, str, &err); 23979175ce1fSAndi Kleen 2398333b5665SAndi Kleen if (ret) { 2399b39b8393SJiri Olsa parse_events_print_error(&err, str); 2400333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2401333b5665SAndi Kleen } 2402b39b8393SJiri Olsa 24039175ce1fSAndi Kleen return ret; 2404f120f9d5SJiri Olsa } 2405f120f9d5SJiri Olsa 2406d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2407d0abbc3cSArnaldo Carvalho de Melo { 2408d0abbc3cSArnaldo Carvalho de Melo struct evlist **evlistp = opt->value; 2409d0abbc3cSArnaldo Carvalho de Melo int ret; 2410d0abbc3cSArnaldo Carvalho de Melo 2411d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2412d0abbc3cSArnaldo Carvalho de Melo *evlistp = evlist__new(); 2413d0abbc3cSArnaldo Carvalho de Melo 2414d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2415d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2416d0abbc3cSArnaldo Carvalho de Melo return -1; 2417d0abbc3cSArnaldo Carvalho de Melo } 2418d0abbc3cSArnaldo Carvalho de Melo } 2419d0abbc3cSArnaldo Carvalho de Melo 2420d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2421d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2422d0abbc3cSArnaldo Carvalho de Melo evlist__delete(*evlistp); 2423d0abbc3cSArnaldo Carvalho de Melo *evlistp = NULL; 2424d0abbc3cSArnaldo Carvalho de Melo } 2425d0abbc3cSArnaldo Carvalho de Melo 2426d0abbc3cSArnaldo Carvalho de Melo return ret; 2427d0abbc3cSArnaldo Carvalho de Melo } 2428d0abbc3cSArnaldo Carvalho de Melo 24294ba1faa1SWang Nan static int 243063503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 243132dcd021SJiri Olsa int (*func)(struct evsel *evsel, 24324ba1faa1SWang Nan const void *arg), 24334ba1faa1SWang Nan const void *arg) 2434c171b552SLi Zefan { 243532dcd021SJiri Olsa struct evsel *last = NULL; 24364ba1faa1SWang Nan int err; 2437c171b552SLi Zefan 2438854f7363SWang Nan /* 2439854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2440854f7363SWang Nan * error when it found last == NULL. 2441854f7363SWang Nan * 2442854f7363SWang Nan * So no need to WARN here, let *func do this. 2443854f7363SWang Nan */ 24446484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2445515dbe48SJiri Olsa last = evlist__last(evlist); 244669aad6f1SArnaldo Carvalho de Melo 244715bfd2ccSWang Nan do { 24484ba1faa1SWang Nan err = (*func)(last, arg); 24494ba1faa1SWang Nan if (err) 2450c171b552SLi Zefan return -1; 24514ba1faa1SWang Nan if (!last) 24524ba1faa1SWang Nan return 0; 2453c171b552SLi Zefan 2454ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 245515bfd2ccSWang Nan return 0; 2456b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 245715bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 245815bfd2ccSWang Nan 2459c171b552SLi Zefan return 0; 2460c171b552SLi Zefan } 2461c171b552SLi Zefan 246232dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 24634ba1faa1SWang Nan { 24644ba1faa1SWang Nan const char *str = arg; 24651e857484SMathieu Poirier bool found = false; 24661e857484SMathieu Poirier int nr_addr_filters = 0; 24671e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 24684ba1faa1SWang Nan 246949836f78SJack Henschel if (evsel == NULL) { 247049836f78SJack Henschel fprintf(stderr, 247149836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 247249836f78SJack Henschel return -1; 247349836f78SJack Henschel } 24744ba1faa1SWang Nan 24751fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2476ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, str) < 0) { 24774ba1faa1SWang Nan fprintf(stderr, 24784ba1faa1SWang Nan "not enough memory to hold filter string\n"); 24794ba1faa1SWang Nan return -1; 24804ba1faa1SWang Nan } 24814ba1faa1SWang Nan 24824ba1faa1SWang Nan return 0; 24834ba1faa1SWang Nan } 24844ba1faa1SWang Nan 24851e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 24861fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 24871e857484SMathieu Poirier found = true; 24881e857484SMathieu Poirier break; 24891e857484SMathieu Poirier } 24901e857484SMathieu Poirier 24911e857484SMathieu Poirier if (found) 24921e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 24931e857484SMathieu Poirier "%d", &nr_addr_filters); 24941e857484SMathieu Poirier 249549836f78SJack Henschel if (!nr_addr_filters) { 249649836f78SJack Henschel fprintf(stderr, 249749836f78SJack Henschel "This CPU does not support address filtering\n"); 249849836f78SJack Henschel return -1; 249949836f78SJack Henschel } 25001e857484SMathieu Poirier 2501ad681adfSArnaldo Carvalho de Melo if (evsel__append_addr_filter(evsel, str) < 0) { 25021e857484SMathieu Poirier fprintf(stderr, 25031e857484SMathieu Poirier "not enough memory to hold filter string\n"); 25041e857484SMathieu Poirier return -1; 25051e857484SMathieu Poirier } 25061e857484SMathieu Poirier 25071e857484SMathieu Poirier return 0; 25081e857484SMathieu Poirier } 25091e857484SMathieu Poirier 25104ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 25114ba1faa1SWang Nan int unset __maybe_unused) 25124ba1faa1SWang Nan { 251363503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25144ba1faa1SWang Nan 25154ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 25164ba1faa1SWang Nan (const void *)str); 25174ba1faa1SWang Nan } 25184ba1faa1SWang Nan 251932dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 25204ba1faa1SWang Nan const void *arg __maybe_unused) 25214ba1faa1SWang Nan { 25224ba1faa1SWang Nan char new_filter[64]; 25234ba1faa1SWang Nan 25241fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 25254ba1faa1SWang Nan fprintf(stderr, 25264ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 25274ba1faa1SWang Nan return -1; 25284ba1faa1SWang Nan } 25294ba1faa1SWang Nan 25304ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 25314ba1faa1SWang Nan 2532ad681adfSArnaldo Carvalho de Melo if (evsel__append_tp_filter(evsel, new_filter) < 0) { 25334ba1faa1SWang Nan fprintf(stderr, 25344ba1faa1SWang Nan "not enough memory to hold filter string\n"); 25354ba1faa1SWang Nan return -1; 25364ba1faa1SWang Nan } 25374ba1faa1SWang Nan 25384ba1faa1SWang Nan return 0; 25394ba1faa1SWang Nan } 25404ba1faa1SWang Nan 25414ba1faa1SWang Nan int exclude_perf(const struct option *opt, 25424ba1faa1SWang Nan const char *arg __maybe_unused, 25434ba1faa1SWang Nan int unset __maybe_unused) 25444ba1faa1SWang Nan { 254563503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 25464ba1faa1SWang Nan 25474ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 25484ba1faa1SWang Nan NULL); 25494ba1faa1SWang Nan } 25504ba1faa1SWang Nan 255186470930SIngo Molnar static const char * const event_type_descriptors[] = { 255286470930SIngo Molnar "Hardware event", 255386470930SIngo Molnar "Software event", 255486470930SIngo Molnar "Tracepoint event", 255586470930SIngo Molnar "Hardware cache event", 255641bdcb23SLiming Wang "Raw hardware event descriptor", 255741bdcb23SLiming Wang "Hardware breakpoint", 255886470930SIngo Molnar }; 255986470930SIngo Molnar 2560ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 2561ab0e4800SYunlong Song { 2562ab0e4800SYunlong Song const char * const *as = a; 2563ab0e4800SYunlong Song const char * const *bs = b; 2564ab0e4800SYunlong Song 2565ab0e4800SYunlong Song return strcmp(*as, *bs); 2566ab0e4800SYunlong Song } 2567ab0e4800SYunlong Song 256886470930SIngo Molnar /* 2569f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 2570f6bdafefSJason Baron */ 2571f6bdafefSJason Baron 2572a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 2573a3277d2dSFrederic Weisbecker bool name_only) 2574f6bdafefSJason Baron { 2575f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 257622a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2577f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 257825a7d914SArnaldo Carvalho de Melo char *dir_path; 2579ab0e4800SYunlong Song char **evt_list = NULL; 2580ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 2581ab0e4800SYunlong Song bool evt_num_known = false; 2582f6bdafefSJason Baron 2583ab0e4800SYunlong Song restart: 25847014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 2585f6bdafefSJason Baron if (!sys_dir) 2586725b1368SEric Dumazet return; 2587f6bdafefSJason Baron 2588ab0e4800SYunlong Song if (evt_num_known) { 2589ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2590ab0e4800SYunlong Song if (!evt_list) 2591ab0e4800SYunlong Song goto out_close_sys_dir; 2592ab0e4800SYunlong Song } 2593ab0e4800SYunlong Song 259422a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 2595668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 259622a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 2597668b8788SArnaldo Carvalho de Melo continue; 2598725b1368SEric Dumazet 259925a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 260025a7d914SArnaldo Carvalho de Melo if (!dir_path) 260125a7d914SArnaldo Carvalho de Melo continue; 2602725b1368SEric Dumazet evt_dir = opendir(dir_path); 2603725b1368SEric Dumazet if (!evt_dir) 260425a7d914SArnaldo Carvalho de Melo goto next; 2605725b1368SEric Dumazet 2606c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 2607668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 260822a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 2609668b8788SArnaldo Carvalho de Melo continue; 2610668b8788SArnaldo Carvalho de Melo 2611ab0e4800SYunlong Song if (!evt_num_known) { 2612ab0e4800SYunlong Song evt_num++; 2613a3277d2dSFrederic Weisbecker continue; 2614a3277d2dSFrederic Weisbecker } 2615a3277d2dSFrederic Weisbecker 2616f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 261722a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 2618ab0e4800SYunlong Song 2619ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 262025a7d914SArnaldo Carvalho de Melo if (evt_list[evt_i] == NULL) { 262125a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2622ab0e4800SYunlong Song goto out_close_evt_dir; 262325a7d914SArnaldo Carvalho de Melo } 2624ab0e4800SYunlong Song evt_i++; 2625f6bdafefSJason Baron } 2626f6bdafefSJason Baron closedir(evt_dir); 262725a7d914SArnaldo Carvalho de Melo next: 262825a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2629f6bdafefSJason Baron } 2630f6bdafefSJason Baron closedir(sys_dir); 2631ab0e4800SYunlong Song 2632ab0e4800SYunlong Song if (!evt_num_known) { 2633ab0e4800SYunlong Song evt_num_known = true; 2634ab0e4800SYunlong Song goto restart; 2635ab0e4800SYunlong Song } 2636ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2637ab0e4800SYunlong Song evt_i = 0; 2638ab0e4800SYunlong Song while (evt_i < evt_num) { 2639ab0e4800SYunlong Song if (name_only) { 2640ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2641ab0e4800SYunlong Song continue; 2642ab0e4800SYunlong Song } 2643ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2644ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2645ab0e4800SYunlong Song } 2646dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2647ab0e4800SYunlong Song printf("\n"); 2648ab0e4800SYunlong Song 2649ab0e4800SYunlong Song out_free: 2650ab0e4800SYunlong Song evt_num = evt_i; 2651ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2652ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2653ab0e4800SYunlong Song zfree(&evt_list); 2654ab0e4800SYunlong Song return; 2655ab0e4800SYunlong Song 2656ab0e4800SYunlong Song out_close_evt_dir: 2657ab0e4800SYunlong Song closedir(evt_dir); 2658ab0e4800SYunlong Song out_close_sys_dir: 2659ab0e4800SYunlong Song closedir(sys_dir); 2660ab0e4800SYunlong Song 2661ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2662ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2663ab0e4800SYunlong Song if (evt_list) 2664ab0e4800SYunlong Song goto out_free; 2665f6bdafefSJason Baron } 2666f6bdafefSJason Baron 2667f6bdafefSJason Baron /* 266820c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 266920c457b8SThomas Renninger */ 267020c457b8SThomas Renninger 267120c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 267220c457b8SThomas Renninger { 267320c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 267422a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 267520c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 267625a7d914SArnaldo Carvalho de Melo char *dir_path; 267720c457b8SThomas Renninger 26787014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 267920c457b8SThomas Renninger if (!sys_dir) 268020c457b8SThomas Renninger return 0; 268120c457b8SThomas Renninger 268222a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 268325a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 268425a7d914SArnaldo Carvalho de Melo if (!dir_path) 268525a7d914SArnaldo Carvalho de Melo continue; 268620c457b8SThomas Renninger evt_dir = opendir(dir_path); 268720c457b8SThomas Renninger if (!evt_dir) 268825a7d914SArnaldo Carvalho de Melo goto next; 268920c457b8SThomas Renninger 2690c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 269120c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 269222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 269320c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 269420c457b8SThomas Renninger closedir(evt_dir); 269520c457b8SThomas Renninger closedir(sys_dir); 269620c457b8SThomas Renninger return 1; 269720c457b8SThomas Renninger } 269820c457b8SThomas Renninger } 269920c457b8SThomas Renninger closedir(evt_dir); 270025a7d914SArnaldo Carvalho de Melo next: 270125a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 270220c457b8SThomas Renninger } 270320c457b8SThomas Renninger closedir(sys_dir); 270420c457b8SThomas Renninger return 0; 270520c457b8SThomas Renninger } 270620c457b8SThomas Renninger 2707b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2708b41f1cecSNamhyung Kim { 2709b41f1cecSNamhyung Kim bool ret = true; 271088fee52eSVince Weaver int open_return; 271132dcd021SJiri Olsa struct evsel *evsel; 2712b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2713b41f1cecSNamhyung Kim .type = type, 2714b41f1cecSNamhyung Kim .config = config, 2715b41f1cecSNamhyung Kim .disabled = 1, 2716b41f1cecSNamhyung Kim }; 27179749b90eSJiri Olsa struct perf_thread_map *tmap = thread_map__new_by_tid(0); 271889896051SArnaldo Carvalho de Melo 271989896051SArnaldo Carvalho de Melo if (tmap == NULL) 272089896051SArnaldo Carvalho de Melo return false; 2721b41f1cecSNamhyung Kim 2722365c3ae7SJiri Olsa evsel = evsel__new(&attr); 2723b41f1cecSNamhyung Kim if (evsel) { 27245972d1e0SJiri Olsa open_return = evsel__open(evsel, NULL, tmap); 272588fee52eSVince Weaver ret = open_return >= 0; 272688fee52eSVince Weaver 272788fee52eSVince Weaver if (open_return == -EACCES) { 272888fee52eSVince Weaver /* 272988fee52eSVince Weaver * This happens if the paranoid value 273088fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 273188fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 273288fee52eSVince Weaver * by default as some ARM machines do not support it. 273388fee52eSVince Weaver * 273488fee52eSVince Weaver */ 27351fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = 1; 27365972d1e0SJiri Olsa ret = evsel__open(evsel, NULL, tmap) >= 0; 273788fee52eSVince Weaver } 27385eb2dd2aSJiri Olsa evsel__delete(evsel); 2739b41f1cecSNamhyung Kim } 2740b41f1cecSNamhyung Kim 27417836e52eSJiri Olsa perf_thread_map__put(tmap); 2742b41f1cecSNamhyung Kim return ret; 2743b41f1cecSNamhyung Kim } 2744b41f1cecSNamhyung Kim 274540218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 274640218daeSMasami Hiramatsu bool name_only) 274740218daeSMasami Hiramatsu { 274840218daeSMasami Hiramatsu struct probe_cache *pcache; 274940218daeSMasami Hiramatsu struct probe_cache_entry *ent; 275040218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 275140218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 275240218daeSMasami Hiramatsu struct str_node *nd, *nd2; 275340218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 275440218daeSMasami Hiramatsu bool show_detail = false; 275540218daeSMasami Hiramatsu int ret; 275640218daeSMasami Hiramatsu 275740218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 275840218daeSMasami Hiramatsu if (!sdtlist) { 275940218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 276040218daeSMasami Hiramatsu return; 276140218daeSMasami Hiramatsu } 276240218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 276340218daeSMasami Hiramatsu if (!bidlist) { 276440218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 276540218daeSMasami Hiramatsu return; 276640218daeSMasami Hiramatsu } 276740218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2768f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 276940218daeSMasami Hiramatsu if (!pcache) 277040218daeSMasami Hiramatsu continue; 277140218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 277240218daeSMasami Hiramatsu if (!ent->sdt) 277340218daeSMasami Hiramatsu continue; 277440218daeSMasami Hiramatsu if (subsys_glob && 277540218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 277640218daeSMasami Hiramatsu continue; 277740218daeSMasami Hiramatsu if (event_glob && 277840218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 277940218daeSMasami Hiramatsu continue; 278040218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 278140218daeSMasami Hiramatsu ent->pev.event, nd->s); 278240218daeSMasami Hiramatsu if (ret > 0) 278340218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 278440218daeSMasami Hiramatsu } 278540218daeSMasami Hiramatsu probe_cache__delete(pcache); 278640218daeSMasami Hiramatsu } 278740218daeSMasami Hiramatsu strlist__delete(bidlist); 278840218daeSMasami Hiramatsu 278940218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 279040218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 279140218daeSMasami Hiramatsu if (buf) 279240218daeSMasami Hiramatsu *(buf++) = '\0'; 279340218daeSMasami Hiramatsu if (name_only) { 279440218daeSMasami Hiramatsu printf("%s ", nd->s); 279540218daeSMasami Hiramatsu continue; 279640218daeSMasami Hiramatsu } 279740218daeSMasami Hiramatsu nd2 = strlist__next(nd); 279840218daeSMasami Hiramatsu if (nd2) { 279940218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 280040218daeSMasami Hiramatsu if (ptr) 280140218daeSMasami Hiramatsu *ptr = '\0'; 280240218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 280340218daeSMasami Hiramatsu show_detail = true; 280440218daeSMasami Hiramatsu } 280540218daeSMasami Hiramatsu if (show_detail) { 280640218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 280740218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 280840218daeSMasami Hiramatsu if (ret > 0) { 280940218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 281040218daeSMasami Hiramatsu free(buf); 281140218daeSMasami Hiramatsu } 28128bde8516SChangbin Du free(path); 281340218daeSMasami Hiramatsu } else 281440218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 281540218daeSMasami Hiramatsu if (nd2) { 281640218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 281740218daeSMasami Hiramatsu show_detail = false; 281840218daeSMasami Hiramatsu if (ptr) 281940218daeSMasami Hiramatsu *ptr = '@'; 282040218daeSMasami Hiramatsu } 282140218daeSMasami Hiramatsu } 282240218daeSMasami Hiramatsu strlist__delete(sdtlist); 282340218daeSMasami Hiramatsu } 282440218daeSMasami Hiramatsu 2825a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2826668b8788SArnaldo Carvalho de Melo { 2827ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 28280b668bc9SArnaldo Carvalho de Melo char name[64]; 2829ab0e4800SYunlong Song char **evt_list = NULL; 2830ab0e4800SYunlong Song bool evt_num_known = false; 2831ab0e4800SYunlong Song 2832ab0e4800SYunlong Song restart: 2833ab0e4800SYunlong Song if (evt_num_known) { 2834ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2835ab0e4800SYunlong Song if (!evt_list) 2836ab0e4800SYunlong Song goto out_enomem; 2837ab0e4800SYunlong Song } 2838668b8788SArnaldo Carvalho de Melo 2839668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2840668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2841668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 2842c754c382SArnaldo Carvalho de Melo if (!evsel__is_cache_op_valid(type, op)) 2843668b8788SArnaldo Carvalho de Melo continue; 2844668b8788SArnaldo Carvalho de Melo 2845668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 28468ab2e96dSArnaldo Carvalho de Melo __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); 2847947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2848668b8788SArnaldo Carvalho de Melo continue; 2849668b8788SArnaldo Carvalho de Melo 2850b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2851b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2852b41f1cecSNamhyung Kim continue; 2853b41f1cecSNamhyung Kim 2854ab0e4800SYunlong Song if (!evt_num_known) { 2855ab0e4800SYunlong Song evt_num++; 2856ab0e4800SYunlong Song continue; 2857ab0e4800SYunlong Song } 2858ab0e4800SYunlong Song 2859ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2860ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2861ab0e4800SYunlong Song goto out_enomem; 2862ab0e4800SYunlong Song evt_i++; 2863668b8788SArnaldo Carvalho de Melo } 2864668b8788SArnaldo Carvalho de Melo } 2865668b8788SArnaldo Carvalho de Melo } 2866668b8788SArnaldo Carvalho de Melo 2867ab0e4800SYunlong Song if (!evt_num_known) { 2868ab0e4800SYunlong Song evt_num_known = true; 2869ab0e4800SYunlong Song goto restart; 2870ab0e4800SYunlong Song } 2871ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2872ab0e4800SYunlong Song evt_i = 0; 2873ab0e4800SYunlong Song while (evt_i < evt_num) { 2874ab0e4800SYunlong Song if (name_only) { 2875ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2876ab0e4800SYunlong Song continue; 2877ab0e4800SYunlong Song } 2878ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2879ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2880ab0e4800SYunlong Song } 2881dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2882dc098b35SAndi Kleen printf("\n"); 2883ab0e4800SYunlong Song 2884ab0e4800SYunlong Song out_free: 2885ab0e4800SYunlong Song evt_num = evt_i; 2886ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2887ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2888ab0e4800SYunlong Song zfree(&evt_list); 2889ab0e4800SYunlong Song return evt_num; 2890ab0e4800SYunlong Song 2891ab0e4800SYunlong Song out_enomem: 2892ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2893ab0e4800SYunlong Song if (evt_list) 2894ab0e4800SYunlong Song goto out_free; 2895ab0e4800SYunlong Song return evt_num; 2896668b8788SArnaldo Carvalho de Melo } 2897668b8788SArnaldo Carvalho de Melo 28985e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob, 28995e0861baSAndi Kleen bool name_only) 29005e0861baSAndi Kleen { 29015e0861baSAndi Kleen if (event_glob && !strglobmatch(name, event_glob)) 29025e0861baSAndi Kleen return; 29035e0861baSAndi Kleen if (name_only) 29045e0861baSAndi Kleen printf("%s ", name); 29055e0861baSAndi Kleen else 29065e0861baSAndi Kleen printf(" %-50s [%s]\n", name, "Tool event"); 29075e0861baSAndi Kleen 29085e0861baSAndi Kleen } 29095e0861baSAndi Kleen 29105e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only) 29115e0861baSAndi Kleen { 29125e0861baSAndi Kleen print_tool_event("duration_time", event_glob, name_only); 29135e0861baSAndi Kleen if (pager_in_use()) 29145e0861baSAndi Kleen printf("\n"); 29155e0861baSAndi Kleen } 29165e0861baSAndi Kleen 2917705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2918a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2919a3277d2dSFrederic Weisbecker bool name_only) 292086470930SIngo Molnar { 2921ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2922947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2923ab0e4800SYunlong Song char **evt_list = NULL; 2924ab0e4800SYunlong Song bool evt_num_known = false; 2925ab0e4800SYunlong Song 2926ab0e4800SYunlong Song restart: 2927ab0e4800SYunlong Song if (evt_num_known) { 2928ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2929ab0e4800SYunlong Song if (!evt_list) 2930ab0e4800SYunlong Song goto out_enomem; 2931ab0e4800SYunlong Song syms -= max; 2932ab0e4800SYunlong Song } 293386470930SIngo Molnar 29341dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 29353b2f17adSArnaldo Carvalho de Melo /* 29363b2f17adSArnaldo Carvalho de Melo * New attr.config still not supported here, the latest 29373b2f17adSArnaldo Carvalho de Melo * example was PERF_COUNT_SW_CGROUP_SWITCHES 29383b2f17adSArnaldo Carvalho de Melo */ 29393b2f17adSArnaldo Carvalho de Melo if (syms->symbol == NULL) 29403b2f17adSArnaldo Carvalho de Melo continue; 2941668b8788SArnaldo Carvalho de Melo 29423b2f17adSArnaldo Carvalho de Melo if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) || 2943668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2944668b8788SArnaldo Carvalho de Melo continue; 294586470930SIngo Molnar 2946b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2947b41f1cecSNamhyung Kim continue; 2948b41f1cecSNamhyung Kim 2949ab0e4800SYunlong Song if (!evt_num_known) { 2950ab0e4800SYunlong Song evt_num++; 2951a3277d2dSFrederic Weisbecker continue; 2952a3277d2dSFrederic Weisbecker } 2953a3277d2dSFrederic Weisbecker 2954ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2955947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 295674d5b588SJaswinder Singh Rajput else 2957bd8d57fbSArnaldo Carvalho de Melo strlcpy(name, syms->symbol, MAX_NAME_LEN); 295886470930SIngo Molnar 2959ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2960ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2961ab0e4800SYunlong Song goto out_enomem; 2962ab0e4800SYunlong Song evt_i++; 296386470930SIngo Molnar } 296486470930SIngo Molnar 2965ab0e4800SYunlong Song if (!evt_num_known) { 2966ab0e4800SYunlong Song evt_num_known = true; 2967ab0e4800SYunlong Song goto restart; 2968ab0e4800SYunlong Song } 2969ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2970ab0e4800SYunlong Song evt_i = 0; 2971ab0e4800SYunlong Song while (evt_i < evt_num) { 2972ab0e4800SYunlong Song if (name_only) { 2973ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2974ab0e4800SYunlong Song continue; 2975ab0e4800SYunlong Song } 2976ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2977ab0e4800SYunlong Song } 2978dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2979689d3018SMarti Raudsepp printf("\n"); 2980ab0e4800SYunlong Song 2981ab0e4800SYunlong Song out_free: 2982ab0e4800SYunlong Song evt_num = evt_i; 2983ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2984ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2985ab0e4800SYunlong Song zfree(&evt_list); 2986ab0e4800SYunlong Song return; 2987ab0e4800SYunlong Song 2988ab0e4800SYunlong Song out_enomem: 2989ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2990ab0e4800SYunlong Song if (evt_list) 2991ab0e4800SYunlong Song goto out_free; 2992668b8788SArnaldo Carvalho de Melo } 29931dc12760SJiri Olsa 29941dc12760SJiri Olsa /* 29951dc12760SJiri Olsa * Print the help text for the event symbols: 29961dc12760SJiri Olsa */ 2997c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2998a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 29991dc12760SJiri Olsa { 30001dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 3001a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 30021dc12760SJiri Olsa 30031dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 3004a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 30055e0861baSAndi Kleen print_tool_events(event_glob, name_only); 30061dc12760SJiri Olsa 3007a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 300873c24cb8SJaswinder Singh Rajput 3009bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 3010a7f6c8c8SJin Yao details_flag, deprecated); 3011dc098b35SAndi Kleen 3012668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 3013668b8788SArnaldo Carvalho de Melo return; 301473c24cb8SJaswinder Singh Rajput 3015a3277d2dSFrederic Weisbecker if (!name_only) { 3016947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 30175f537a26SJiri Olsa "rNNN", 30181cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 30195f537a26SJiri Olsa printf(" %-50s [%s]\n", 30205f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 30215f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 3022dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 3023dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 302486470930SIngo Molnar 3025947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 30263741eb9fSJacob Shin "mem:<addr>[/len][:access]", 302741bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 3028dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 30291b290d67SFrederic Weisbecker printf("\n"); 3030a3277d2dSFrederic Weisbecker } 30311b290d67SFrederic Weisbecker 3032a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 303340218daeSMasami Hiramatsu 303440218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 303571b0acceSAndi Kleen 303633bbc571SJiri Olsa metricgroup__print(true, true, NULL, name_only, details_flag); 303770943490SStephane Eranian 303870943490SStephane Eranian print_libpfm_events(name_only, long_desc); 303986470930SIngo Molnar } 30408f707d84SJiri Olsa 30416cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 30428f707d84SJiri Olsa { 304316fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 30448f707d84SJiri Olsa } 30458f707d84SJiri Olsa 304667b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 304767b49b38SJiri Olsa struct parse_events_term *temp, 304867b49b38SJiri Olsa char *str, u64 num) 30498f707d84SJiri Olsa { 30506cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 30518f707d84SJiri Olsa 305267b49b38SJiri Olsa term = malloc(sizeof(*term)); 30538f707d84SJiri Olsa if (!term) 30548f707d84SJiri Olsa return -ENOMEM; 30558f707d84SJiri Olsa 305667b49b38SJiri Olsa *term = *temp; 30578f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 3058c2f1ceadSAndi Kleen term->weak = false; 30598f707d84SJiri Olsa 306067b49b38SJiri Olsa switch (term->type_val) { 30618f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 30628f707d84SJiri Olsa term->val.num = num; 30638f707d84SJiri Olsa break; 30648f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 30658f707d84SJiri Olsa term->val.str = str; 30668f707d84SJiri Olsa break; 30678f707d84SJiri Olsa default: 30684be8be6bSAdrian Hunter free(term); 30698f707d84SJiri Olsa return -EINVAL; 30708f707d84SJiri Olsa } 30718f707d84SJiri Olsa 30728f707d84SJiri Olsa *_term = term; 30738f707d84SJiri Olsa return 0; 30748f707d84SJiri Olsa } 30758f707d84SJiri Olsa 30766cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 3077cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 307899e7138eSJiri Olsa bool no_value, 3079bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 308016fa7e82SJiri Olsa { 3081bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 3082bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 3083bb78ce7dSAdrian Hunter 308467b49b38SJiri Olsa struct parse_events_term temp = { 308567b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 308667b49b38SJiri Olsa .type_term = type_term, 308767b49b38SJiri Olsa .config = config, 308899e7138eSJiri Olsa .no_value = no_value, 308967b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 309067b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 309167b49b38SJiri Olsa }; 309267b49b38SJiri Olsa 309367b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 309416fa7e82SJiri Olsa } 309516fa7e82SJiri Olsa 30966cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 3097cecf3a2eSJiri Olsa int type_term, char *config, char *str, 3098bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 309916fa7e82SJiri Olsa { 3100bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 3101bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 3102bb78ce7dSAdrian Hunter 310367b49b38SJiri Olsa struct parse_events_term temp = { 310467b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 310567b49b38SJiri Olsa .type_term = type_term, 310667b49b38SJiri Olsa .config = config, 310767b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 310867b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 310967b49b38SJiri Olsa }; 311067b49b38SJiri Olsa 311167b49b38SJiri Olsa return new_term(term, &temp, str, 0); 311216fa7e82SJiri Olsa } 311316fa7e82SJiri Olsa 31146cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 31151d33d6dcSJiri Olsa char *config, unsigned idx) 31161d33d6dcSJiri Olsa { 31171d33d6dcSJiri Olsa struct event_symbol *sym; 3118b6645a72SIan Rogers char *str; 311967b49b38SJiri Olsa struct parse_events_term temp = { 312067b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 312167b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 3122b6645a72SIan Rogers .config = config, 312367b49b38SJiri Olsa }; 31241d33d6dcSJiri Olsa 3125b6645a72SIan Rogers if (!temp.config) { 3126b6645a72SIan Rogers temp.config = strdup("event"); 3127b6645a72SIan Rogers if (!temp.config) 3128b6645a72SIan Rogers return -ENOMEM; 3129b6645a72SIan Rogers } 31301d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 31311d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 31321d33d6dcSJiri Olsa 3133b6645a72SIan Rogers str = strdup(sym->symbol); 3134b6645a72SIan Rogers if (!str) 3135b6645a72SIan Rogers return -ENOMEM; 3136b6645a72SIan Rogers return new_term(term, &temp, str, 0); 31371d33d6dcSJiri Olsa } 31381d33d6dcSJiri Olsa 31396cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 31406cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 3141a6146d50SZheng Yan { 3142b6645a72SIan Rogers char *str; 314367b49b38SJiri Olsa struct parse_events_term temp = { 314467b49b38SJiri Olsa .type_val = term->type_val, 314567b49b38SJiri Olsa .type_term = term->type_term, 3146b6645a72SIan Rogers .config = NULL, 314767b49b38SJiri Olsa .err_term = term->err_term, 314867b49b38SJiri Olsa .err_val = term->err_val, 314967b49b38SJiri Olsa }; 315067b49b38SJiri Olsa 3151b6645a72SIan Rogers if (term->config) { 3152b6645a72SIan Rogers temp.config = strdup(term->config); 3153b6645a72SIan Rogers if (!temp.config) 3154b6645a72SIan Rogers return -ENOMEM; 3155b6645a72SIan Rogers } 3156b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3157b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 3158b6645a72SIan Rogers 3159b6645a72SIan Rogers str = strdup(term->val.str); 3160b6645a72SIan Rogers if (!str) 3161b6645a72SIan Rogers return -ENOMEM; 3162b6645a72SIan Rogers return new_term(new, &temp, str, 0); 3163a6146d50SZheng Yan } 3164a6146d50SZheng Yan 31651dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 31661dc92556SIan Rogers { 31671dc92556SIan Rogers if (term->array.nr_ranges) 31681dc92556SIan Rogers zfree(&term->array.ranges); 31691dc92556SIan Rogers 31701dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 31711dc92556SIan Rogers zfree(&term->val.str); 31721dc92556SIan Rogers 31731dc92556SIan Rogers zfree(&term->config); 31741dc92556SIan Rogers free(term); 31751dc92556SIan Rogers } 31761dc92556SIan Rogers 31778255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 31788255718fSAndi Kleen struct list_head **new) 31798255718fSAndi Kleen { 31808255718fSAndi Kleen struct parse_events_term *term, *n; 31818255718fSAndi Kleen int ret; 31828255718fSAndi Kleen 31838255718fSAndi Kleen if (!old) { 31848255718fSAndi Kleen *new = NULL; 31858255718fSAndi Kleen return 0; 31868255718fSAndi Kleen } 31878255718fSAndi Kleen 31888255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 31898255718fSAndi Kleen if (!*new) 31908255718fSAndi Kleen return -ENOMEM; 31918255718fSAndi Kleen INIT_LIST_HEAD(*new); 31928255718fSAndi Kleen 31938255718fSAndi Kleen list_for_each_entry (term, old, list) { 31948255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 31958255718fSAndi Kleen if (ret) 31968255718fSAndi Kleen return ret; 31978255718fSAndi Kleen list_add_tail(&n->list, *new); 31988255718fSAndi Kleen } 31998255718fSAndi Kleen return 0; 32008255718fSAndi Kleen } 32018255718fSAndi Kleen 3202fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 32038f707d84SJiri Olsa { 32046cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 32058f707d84SJiri Olsa 3206a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 3207a8adfcebSWang Nan list_del_init(&term->list); 32081dc92556SIan Rogers parse_events_term__delete(term); 32098f707d84SJiri Olsa } 3210a8adfcebSWang Nan } 3211b39b8393SJiri Olsa 32122146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 3213fc0a2c1dSArnaldo Carvalho de Melo { 32142146afc6SArnaldo Carvalho de Melo if (!terms) 32152146afc6SArnaldo Carvalho de Melo return; 3216fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 3217d20a5f2bSWang Nan free(terms); 3218fc0a2c1dSArnaldo Carvalho de Melo } 3219fc0a2c1dSArnaldo Carvalho de Melo 32202d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 32212d055bf2SWang Nan { 3222360e071bSTaeung Song zfree(&a->ranges); 32232d055bf2SWang Nan } 32242d055bf2SWang Nan 32255d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 3226b39b8393SJiri Olsa int idx, const char *str) 3227b39b8393SJiri Olsa { 3228448d732cSIan Rogers if (!parse_state->error) 3229a6ced2beSAdrian Hunter return; 3230448d732cSIan Rogers 3231448d732cSIan Rogers parse_events__handle_error(parse_state->error, idx, strdup(str), NULL); 3232b39b8393SJiri Olsa } 3233ffeb883eSHe Kuang 323417cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 323517cb5f84SWang Nan { 323617cb5f84SWang Nan int i; 323717cb5f84SWang Nan bool first = true; 323817cb5f84SWang Nan 323917cb5f84SWang Nan buf[0] = '\0'; 324017cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 324117cb5f84SWang Nan const char *name = config_term_names[i]; 324217cb5f84SWang Nan 32431669e509SWang Nan if (!config_term_avail(i, NULL)) 32441669e509SWang Nan continue; 324517cb5f84SWang Nan if (!name) 324617cb5f84SWang Nan continue; 324717cb5f84SWang Nan if (name[0] == '<') 324817cb5f84SWang Nan continue; 324917cb5f84SWang Nan 325017cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 325117cb5f84SWang Nan return; 325217cb5f84SWang Nan 325317cb5f84SWang Nan if (!first) 325417cb5f84SWang Nan strcat(buf, ","); 325517cb5f84SWang Nan else 325617cb5f84SWang Nan first = false; 325717cb5f84SWang Nan strcat(buf, name); 325817cb5f84SWang Nan } 325917cb5f84SWang Nan } 326017cb5f84SWang Nan 3261ffeb883eSHe Kuang /* 3262ffeb883eSHe Kuang * Return string contains valid config terms of an event. 3263ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 3264ffeb883eSHe Kuang */ 3265ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 3266ffeb883eSHe Kuang { 3267ffeb883eSHe Kuang char *str; 3268626a6b78SWang Nan /* "no-overwrite" is the longest name */ 326917cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 3270626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 3271ffeb883eSHe Kuang 327217cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 3273ffeb883eSHe Kuang /* valid terms */ 3274ffeb883eSHe Kuang if (additional_terms) { 327526dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 327626dee028SWang Nan additional_terms, static_terms) < 0) 3277ffeb883eSHe Kuang goto fail; 3278ffeb883eSHe Kuang } else { 327926dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 3280ffeb883eSHe Kuang goto fail; 3281ffeb883eSHe Kuang } 3282ffeb883eSHe Kuang return str; 3283ffeb883eSHe Kuang 3284ffeb883eSHe Kuang fail: 3285ffeb883eSHe Kuang return NULL; 3286ffeb883eSHe Kuang } 32879cbfa2f6SJin Yao 32889cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx, 32899cbfa2f6SJin Yao struct perf_event_attr *attr, 32909cbfa2f6SJin Yao char *name, struct perf_pmu *pmu, 32919cbfa2f6SJin Yao struct list_head *config_terms) 32929cbfa2f6SJin Yao { 32939cbfa2f6SJin Yao return __add_event(list, idx, attr, true, name, pmu, 32949cbfa2f6SJin Yao config_terms, false, NULL); 32959cbfa2f6SJin Yao } 3296