1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 38dd2a131SJiri Olsa #include <linux/err.h> 476b31a29SArnaldo Carvalho de Melo #include <dirent.h> 5a43783aeSArnaldo Carvalho de Melo #include <errno.h> 686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 7bafae98eSArnaldo Carvalho de Melo #include <sys/types.h> 8bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h> 9bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 10391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 11b0742e90SArnaldo Carvalho de Melo #include "term.h" 126b58e7f1SUlrich Drepper #include "../perf.h" 13361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 1469aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 154b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1686470930SIngo Molnar #include "parse-events.h" 174b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 18a067558eSArnaldo Carvalho de Melo #include "string2.h" 198ec20b17SArnaldo Carvalho de Melo #include "strlist.h" 205aab621bSArnaldo Carvalho de Melo #include "symbol.h" 215beeded1SJason Baron #include "cache.h" 228755a8f2SArjan van de Ven #include "header.h" 2384c86ca1SWang Nan #include "bpf-loader.h" 246e81c74cSMasami Hiramatsu #include "debug.h" 25592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 26ac20de6fSZheng Yan #include "parse-events-bison.h" 2790e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 2889812fc8SJiri Olsa #include "parse-events-flex.h" 295f537a26SJiri Olsa #include "pmu.h" 30b41f1cecSNamhyung Kim #include "thread_map.h" 31f30a79b0SJiri Olsa #include "cpumap.h" 3240218daeSMasami Hiramatsu #include "probe-file.h" 33b39b8393SJiri Olsa #include "asm/bug.h" 34ac12f676SAndi Kleen #include "util/parse-branch-options.h" 3571b0acceSAndi Kleen #include "metricgroup.h" 3689812fc8SJiri Olsa 3789812fc8SJiri Olsa #define MAX_NAME_LEN 100 3886470930SIngo Molnar 3982ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4082ba1f2fSJiri Olsa extern int parse_events_debug; 4182ba1f2fSJiri Olsa #endif 425d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 43e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 44e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 45bcd3279fSFrederic Weisbecker 46dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 47dcb4e102SKan Liang /* 48dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 49dcb4e102SKan Liang * 0 means not initialized and ready to init 50dcb4e102SKan Liang * -1 means failed to init, don't try anymore 51dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 52dcb4e102SKan Liang */ 53dcb4e102SKan Liang static int perf_pmu_events_list_num; 54dcb4e102SKan Liang 55705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 561dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 571dc12760SJiri Olsa .symbol = "cpu-cycles", 581dc12760SJiri Olsa .alias = "cycles", 591dc12760SJiri Olsa }, 601dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 611dc12760SJiri Olsa .symbol = "instructions", 621dc12760SJiri Olsa .alias = "", 631dc12760SJiri Olsa }, 641dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 651dc12760SJiri Olsa .symbol = "cache-references", 661dc12760SJiri Olsa .alias = "", 671dc12760SJiri Olsa }, 681dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 691dc12760SJiri Olsa .symbol = "cache-misses", 701dc12760SJiri Olsa .alias = "", 711dc12760SJiri Olsa }, 721dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 731dc12760SJiri Olsa .symbol = "branch-instructions", 741dc12760SJiri Olsa .alias = "branches", 751dc12760SJiri Olsa }, 761dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 771dc12760SJiri Olsa .symbol = "branch-misses", 781dc12760SJiri Olsa .alias = "", 791dc12760SJiri Olsa }, 801dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 811dc12760SJiri Olsa .symbol = "bus-cycles", 821dc12760SJiri Olsa .alias = "", 831dc12760SJiri Olsa }, 841dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 851dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 861dc12760SJiri Olsa .alias = "idle-cycles-frontend", 871dc12760SJiri Olsa }, 881dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 891dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 901dc12760SJiri Olsa .alias = "idle-cycles-backend", 911dc12760SJiri Olsa }, 921dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 931dc12760SJiri Olsa .symbol = "ref-cycles", 941dc12760SJiri Olsa .alias = "", 951dc12760SJiri Olsa }, 961dc12760SJiri Olsa }; 9786470930SIngo Molnar 98705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 991dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1001dc12760SJiri Olsa .symbol = "cpu-clock", 1011dc12760SJiri Olsa .alias = "", 1021dc12760SJiri Olsa }, 1031dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1041dc12760SJiri Olsa .symbol = "task-clock", 1051dc12760SJiri Olsa .alias = "", 1061dc12760SJiri Olsa }, 1071dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1081dc12760SJiri Olsa .symbol = "page-faults", 1091dc12760SJiri Olsa .alias = "faults", 1101dc12760SJiri Olsa }, 1111dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1121dc12760SJiri Olsa .symbol = "context-switches", 1131dc12760SJiri Olsa .alias = "cs", 1141dc12760SJiri Olsa }, 1151dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1161dc12760SJiri Olsa .symbol = "cpu-migrations", 1171dc12760SJiri Olsa .alias = "migrations", 1181dc12760SJiri Olsa }, 1191dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1201dc12760SJiri Olsa .symbol = "minor-faults", 1211dc12760SJiri Olsa .alias = "", 1221dc12760SJiri Olsa }, 1231dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1241dc12760SJiri Olsa .symbol = "major-faults", 1251dc12760SJiri Olsa .alias = "", 1261dc12760SJiri Olsa }, 1271dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1281dc12760SJiri Olsa .symbol = "alignment-faults", 1291dc12760SJiri Olsa .alias = "", 1301dc12760SJiri Olsa }, 1311dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1321dc12760SJiri Olsa .symbol = "emulation-faults", 1331dc12760SJiri Olsa .alias = "", 1341dc12760SJiri Olsa }, 135d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 136d22d1a2aSAdrian Hunter .symbol = "dummy", 137d22d1a2aSAdrian Hunter .alias = "", 138d22d1a2aSAdrian Hunter }, 139bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 140bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 141bae9cc41SArnaldo Carvalho de Melo .alias = "", 142bae9cc41SArnaldo Carvalho de Melo }, 14386470930SIngo Molnar }; 14486470930SIngo Molnar 145cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 146cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 14786470930SIngo Molnar 148cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 149cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 150cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 151cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 15286470930SIngo Molnar 15322a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 15422a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 15522a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 15622a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 15722a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 158f6bdafefSJason Baron 159ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 160ae07b63fSPeter Zijlstra { 161ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 162ae07b63fSPeter Zijlstra int fd; 163ae07b63fSPeter Zijlstra 164ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 165ae07b63fSPeter Zijlstra sys_dir->d_name, evt_dir->d_name); 166ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 167ae07b63fSPeter Zijlstra if (fd < 0) 168ae07b63fSPeter Zijlstra return -EINVAL; 169ae07b63fSPeter Zijlstra close(fd); 170ae07b63fSPeter Zijlstra 171ae07b63fSPeter Zijlstra return 0; 172ae07b63fSPeter Zijlstra } 173ae07b63fSPeter Zijlstra 17422a9f41bSArnaldo Carvalho de Melo #define for_each_event(sys_dirent, evt_dir, evt_dirent) \ 17522a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 17622a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 17722a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 17822a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 17922a9f41bSArnaldo Carvalho de Melo (!tp_event_has_id(sys_dirent, evt_dirent))) 180f6bdafefSJason Baron 181270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 182f6bdafefSJason Baron 183f6bdafefSJason Baron 1841ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 185f6bdafefSJason Baron { 1861ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 187f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 18822a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1898aa8a7c8SStephane Eranian char id_buf[24]; 190725b1368SEric Dumazet int fd; 191f6bdafefSJason Baron u64 id; 192f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 193725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 194f6bdafefSJason Baron 195ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 196f6bdafefSJason Baron if (!sys_dir) 197725b1368SEric Dumazet return NULL; 198f6bdafefSJason Baron 19922a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 200725b1368SEric Dumazet 201ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 20222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 203725b1368SEric Dumazet evt_dir = opendir(dir_path); 204725b1368SEric Dumazet if (!evt_dir) 2056b58e7f1SUlrich Drepper continue; 206725b1368SEric Dumazet 20722a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 208725b1368SEric Dumazet 209*77f18153SJiri Olsa scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 21022a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 211725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 212f6bdafefSJason Baron if (fd < 0) 213f6bdafefSJason Baron continue; 214f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 215f6bdafefSJason Baron close(fd); 216f6bdafefSJason Baron continue; 217f6bdafefSJason Baron } 218f6bdafefSJason Baron close(fd); 219f6bdafefSJason Baron id = atoll(id_buf); 220f6bdafefSJason Baron if (id == config) { 221f6bdafefSJason Baron closedir(evt_dir); 222f6bdafefSJason Baron closedir(sys_dir); 22359b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2245aa365f2STaeung Song if (!path) 2255aa365f2STaeung Song return NULL; 2261ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2271ef2ed10SFrederic Weisbecker if (!path->system) { 2281ef2ed10SFrederic Weisbecker free(path); 2291ef2ed10SFrederic Weisbecker return NULL; 2301ef2ed10SFrederic Weisbecker } 2311ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2321ef2ed10SFrederic Weisbecker if (!path->name) { 23374cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2341ef2ed10SFrederic Weisbecker free(path); 2351ef2ed10SFrederic Weisbecker return NULL; 2361ef2ed10SFrederic Weisbecker } 23722a9f41bSArnaldo Carvalho de Melo strncpy(path->system, sys_dirent->d_name, 2381ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 23922a9f41bSArnaldo Carvalho de Melo strncpy(path->name, evt_dirent->d_name, 2401ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2411ef2ed10SFrederic Weisbecker return path; 242f6bdafefSJason Baron } 243f6bdafefSJason Baron } 244f6bdafefSJason Baron closedir(evt_dir); 245f6bdafefSJason Baron } 246f6bdafefSJason Baron 247f6bdafefSJason Baron closedir(sys_dir); 2481ef2ed10SFrederic Weisbecker return NULL; 2491ef2ed10SFrederic Weisbecker } 2501ef2ed10SFrederic Weisbecker 251e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 252e7c93f09SNamhyung Kim { 253e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 254e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 255e7c93f09SNamhyung Kim 256e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 257e7c93f09SNamhyung Kim free(path); 258e7c93f09SNamhyung Kim return NULL; 259e7c93f09SNamhyung Kim } 260e7c93f09SNamhyung Kim 261e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 262e7c93f09SNamhyung Kim path->name = strdup(str+1); 263e7c93f09SNamhyung Kim 264e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 26574cf249dSArnaldo Carvalho de Melo zfree(&path->system); 26674cf249dSArnaldo Carvalho de Melo zfree(&path->name); 267506fde11STaeung Song zfree(&path); 268e7c93f09SNamhyung Kim } 269e7c93f09SNamhyung Kim 270e7c93f09SNamhyung Kim return path; 271e7c93f09SNamhyung Kim } 272e7c93f09SNamhyung Kim 2731424dc96SDavid Ahern const char *event_type(int type) 2741424dc96SDavid Ahern { 2751424dc96SDavid Ahern switch (type) { 2761424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2771424dc96SDavid Ahern return "hardware"; 2781424dc96SDavid Ahern 2791424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2801424dc96SDavid Ahern return "software"; 2811424dc96SDavid Ahern 2821424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2831424dc96SDavid Ahern return "tracepoint"; 2841424dc96SDavid Ahern 2851424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2861424dc96SDavid Ahern return "hardware-cache"; 2871424dc96SDavid Ahern 2881424dc96SDavid Ahern default: 2891424dc96SDavid Ahern break; 2901424dc96SDavid Ahern } 2911424dc96SDavid Ahern 2921424dc96SDavid Ahern return "unknown"; 2931424dc96SDavid Ahern } 2941424dc96SDavid Ahern 295e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 296e814fdddSWang Nan { 297e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 298e814fdddSWang Nan } 2997ae92e74SYan, Zheng 300e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 301e814fdddSWang Nan { 302e814fdddSWang Nan struct parse_events_term *term; 303e814fdddSWang Nan 304e814fdddSWang Nan if (!head_terms) 305e814fdddSWang Nan return NULL; 306e814fdddSWang Nan 307e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 308e814fdddSWang Nan if (parse_events__is_name_term(term)) 309e814fdddSWang Nan return term->val.str; 310e814fdddSWang Nan 311e814fdddSWang Nan return NULL; 312e814fdddSWang Nan } 3137ae92e74SYan, Zheng 314410136f5SStephane Eranian static struct perf_evsel * 315410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3167ae92e74SYan, Zheng struct perf_event_attr *attr, 31766ec1191SMark Rutland char *name, struct perf_pmu *pmu, 31863ce8449SArnaldo Carvalho de Melo struct list_head *config_terms, bool auto_merge_stats) 31989812fc8SJiri Olsa { 32089812fc8SJiri Olsa struct perf_evsel *evsel; 32166ec1191SMark Rutland struct cpu_map *cpus = pmu ? pmu->cpus : NULL; 32289812fc8SJiri Olsa 32389812fc8SJiri Olsa event_attr_init(attr); 32489812fc8SJiri Olsa 32575fc5ae5STaeung Song evsel = perf_evsel__new_idx(attr, *idx); 326c5cd8ac0SDavid Ahern if (!evsel) 327410136f5SStephane Eranian return NULL; 32889812fc8SJiri Olsa 32975fc5ae5STaeung Song (*idx)++; 330f30a79b0SJiri Olsa evsel->cpus = cpu_map__get(cpus); 331fce4d296SAdrian Hunter evsel->own_cpus = cpu_map__get(cpus); 33266ec1191SMark Rutland evsel->system_wide = pmu ? pmu->is_uncore : false; 33363ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 334f30a79b0SJiri Olsa 3359db1763cSArnaldo Carvalho de Melo if (name) 33689812fc8SJiri Olsa evsel->name = strdup(name); 337930a2e29SJiri Olsa 338930a2e29SJiri Olsa if (config_terms) 339930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 340930a2e29SJiri Olsa 341b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 342410136f5SStephane Eranian return evsel; 34389812fc8SJiri Olsa } 34489812fc8SJiri Olsa 345c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 346930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 347930a2e29SJiri Olsa struct list_head *config_terms) 3487ae92e74SYan, Zheng { 34963ce8449SArnaldo Carvalho de Melo return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM; 3507ae92e74SYan, Zheng } 3517ae92e74SYan, Zheng 3520b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 35386470930SIngo Molnar { 35486470930SIngo Molnar int i, j; 35561c45981SPaul Mackerras int n, longest = -1; 35686470930SIngo Molnar 35786470930SIngo Molnar for (i = 0; i < size; i++) { 3580b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 35961c45981SPaul Mackerras n = strlen(names[i][j]); 36089812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 36161c45981SPaul Mackerras longest = n; 36261c45981SPaul Mackerras } 36389812fc8SJiri Olsa if (longest > 0) 36486470930SIngo Molnar return i; 36586470930SIngo Molnar } 36686470930SIngo Molnar 3678953645fSIngo Molnar return -1; 36886470930SIngo Molnar } 36986470930SIngo Molnar 37043d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 37143d0b978SWang Nan struct parse_events_term *term, 37243d0b978SWang Nan struct parse_events_error *err); 37343d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 37443d0b978SWang Nan struct parse_events_term *term, 37543d0b978SWang Nan struct parse_events_error *err); 37643d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 37743d0b978SWang Nan struct list_head *head, 37843d0b978SWang Nan struct parse_events_error *err, 37943d0b978SWang Nan config_term_func_t config_term); 38043d0b978SWang Nan 381c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 38243d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3832c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 38443d0b978SWang Nan struct list_head *head_config) 38586470930SIngo Molnar { 38689812fc8SJiri Olsa struct perf_event_attr attr; 38743d0b978SWang Nan LIST_HEAD(config_terms); 38843d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 38961c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 39089812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 39189812fc8SJiri Olsa int i, n; 39286470930SIngo Molnar 39386470930SIngo Molnar /* 39486470930SIngo Molnar * No fallback - if we cannot get a clear cache type 39586470930SIngo Molnar * then bail out: 39686470930SIngo Molnar */ 3970b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 39889812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 39986470930SIngo Molnar if (cache_type == -1) 40089812fc8SJiri Olsa return -EINVAL; 40186470930SIngo Molnar 40243d0b978SWang Nan config_name = get_config_name(head_config); 40389812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 40489812fc8SJiri Olsa 40589812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 40689812fc8SJiri Olsa char *str = op_result[i]; 40789812fc8SJiri Olsa 408275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 40961c45981SPaul Mackerras 41061c45981SPaul Mackerras if (cache_op == -1) { 4110b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 41261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 41361c45981SPaul Mackerras if (cache_op >= 0) { 4140b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 41589812fc8SJiri Olsa return -EINVAL; 41661c45981SPaul Mackerras continue; 41761c45981SPaul Mackerras } 41861c45981SPaul Mackerras } 41961c45981SPaul Mackerras 42061c45981SPaul Mackerras if (cache_result == -1) { 4210b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 42261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 42361c45981SPaul Mackerras if (cache_result >= 0) 42461c45981SPaul Mackerras continue; 42561c45981SPaul Mackerras } 42661c45981SPaul Mackerras } 42761c45981SPaul Mackerras 42886470930SIngo Molnar /* 42986470930SIngo Molnar * Fall back to reads: 43086470930SIngo Molnar */ 4318953645fSIngo Molnar if (cache_op == -1) 4328953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 43386470930SIngo Molnar 43486470930SIngo Molnar /* 43586470930SIngo Molnar * Fall back to accesses: 43686470930SIngo Molnar */ 43786470930SIngo Molnar if (cache_result == -1) 43886470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 43986470930SIngo Molnar 44089812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 44189812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 44289812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 44343d0b978SWang Nan 44443d0b978SWang Nan if (head_config) { 4452c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 44643d0b978SWang Nan config_term_common)) 44743d0b978SWang Nan return -EINVAL; 44843d0b978SWang Nan 44943d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 45043d0b978SWang Nan return -ENOMEM; 45143d0b978SWang Nan } 45243d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 45386470930SIngo Molnar } 45486470930SIngo Molnar 455272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4568c619d6aSWang Nan const char *sys, const char *name) 45719658171SJiri Olsa { 45819658171SJiri Olsa char help[BUFSIZ]; 45919658171SJiri Olsa 460ec183d22SAdrian Hunter if (!e) 461ec183d22SAdrian Hunter return; 462ec183d22SAdrian Hunter 46319658171SJiri Olsa /* 46419658171SJiri Olsa * We get error directly from syscall errno ( > 0), 46519658171SJiri Olsa * or from encoded pointer's error ( < 0). 46619658171SJiri Olsa */ 46719658171SJiri Olsa err = abs(err); 46819658171SJiri Olsa 46919658171SJiri Olsa switch (err) { 47019658171SJiri Olsa case EACCES: 471272ed29aSJiri Olsa e->str = strdup("can't access trace events"); 47219658171SJiri Olsa break; 47319658171SJiri Olsa case ENOENT: 474272ed29aSJiri Olsa e->str = strdup("unknown tracepoint"); 47519658171SJiri Olsa break; 47619658171SJiri Olsa default: 477272ed29aSJiri Olsa e->str = strdup("failed to add tracepoint"); 47819658171SJiri Olsa break; 47919658171SJiri Olsa } 48019658171SJiri Olsa 48119658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 482272ed29aSJiri Olsa e->help = strdup(help); 48319658171SJiri Olsa } 48419658171SJiri Olsa 485c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4868c619d6aSWang Nan const char *sys_name, const char *evt_name, 487272ed29aSJiri Olsa struct parse_events_error *err, 488e637d177SHe Kuang struct list_head *head_config) 489bcd3279fSFrederic Weisbecker { 49082fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 491bcd3279fSFrederic Weisbecker 492ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 49319658171SJiri Olsa if (IS_ERR(evsel)) { 494272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4958dd2a131SJiri Olsa return PTR_ERR(evsel); 49619658171SJiri Olsa } 497bcd3279fSFrederic Weisbecker 498e637d177SHe Kuang if (head_config) { 499e637d177SHe Kuang LIST_HEAD(config_terms); 500e637d177SHe Kuang 501e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 502e637d177SHe Kuang return -ENOMEM; 503e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 504e637d177SHe Kuang } 505e637d177SHe Kuang 50682fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 50782fe1c29SArnaldo Carvalho de Melo return 0; 508bcd3279fSFrederic Weisbecker } 509bcd3279fSFrederic Weisbecker 510c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5118c619d6aSWang Nan const char *sys_name, const char *evt_name, 512272ed29aSJiri Olsa struct parse_events_error *err, 513e637d177SHe Kuang struct list_head *head_config) 514bcd3279fSFrederic Weisbecker { 515bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 516bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 517bcd3279fSFrederic Weisbecker DIR *evt_dir; 51827bf90bfSJiri Olsa int ret = 0, found = 0; 519bcd3279fSFrederic Weisbecker 520ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 521bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 522bcd3279fSFrederic Weisbecker if (!evt_dir) { 523272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 52489812fc8SJiri Olsa return -1; 525bcd3279fSFrederic Weisbecker } 526bcd3279fSFrederic Weisbecker 52789812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 528bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 529bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 530bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 531bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 532bcd3279fSFrederic Weisbecker continue; 533bcd3279fSFrederic Weisbecker 53489812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 535fb1d2edfSMasami Hiramatsu continue; 536fb1d2edfSMasami Hiramatsu 53727bf90bfSJiri Olsa found++; 53827bf90bfSJiri Olsa 539e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 540272ed29aSJiri Olsa err, head_config); 541bcd3279fSFrederic Weisbecker } 542bcd3279fSFrederic Weisbecker 54327bf90bfSJiri Olsa if (!found) { 54427bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 54527bf90bfSJiri Olsa ret = -1; 54627bf90bfSJiri Olsa } 54727bf90bfSJiri Olsa 5480bd3f084SJiri Olsa closedir(evt_dir); 54989812fc8SJiri Olsa return ret; 550bcd3279fSFrederic Weisbecker } 551bcd3279fSFrederic Weisbecker 552c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5538c619d6aSWang Nan const char *sys_name, const char *evt_name, 554272ed29aSJiri Olsa struct parse_events_error *err, 555e637d177SHe Kuang struct list_head *head_config) 556f35488f9SJiri Olsa { 557f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 558e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 559272ed29aSJiri Olsa err, head_config) : 560e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 561272ed29aSJiri Olsa err, head_config); 562f35488f9SJiri Olsa } 563f35488f9SJiri Olsa 564c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5658c619d6aSWang Nan const char *sys_name, const char *evt_name, 566272ed29aSJiri Olsa struct parse_events_error *err, 567e637d177SHe Kuang struct list_head *head_config) 568f35488f9SJiri Olsa { 569f35488f9SJiri Olsa struct dirent *events_ent; 570f35488f9SJiri Olsa DIR *events_dir; 571f35488f9SJiri Olsa int ret = 0; 572f35488f9SJiri Olsa 573f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 574f35488f9SJiri Olsa if (!events_dir) { 575272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 576f35488f9SJiri Olsa return -1; 577f35488f9SJiri Olsa } 578f35488f9SJiri Olsa 579f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 580f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 581f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 582f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 583f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 584f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 585f35488f9SJiri Olsa continue; 586f35488f9SJiri Olsa 587f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 588f35488f9SJiri Olsa continue; 589f35488f9SJiri Olsa 590f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 591272ed29aSJiri Olsa evt_name, err, head_config); 592f35488f9SJiri Olsa } 593f35488f9SJiri Olsa 594f35488f9SJiri Olsa closedir(events_dir); 595f35488f9SJiri Olsa return ret; 596f35488f9SJiri Olsa } 597f35488f9SJiri Olsa 5984edf30e3SWang Nan struct __add_bpf_event_param { 5995d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6004edf30e3SWang Nan struct list_head *list; 60195088a59SWang Nan struct list_head *head_config; 6024edf30e3SWang Nan }; 6034edf30e3SWang Nan 604cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd, 6054edf30e3SWang Nan void *_param) 6064edf30e3SWang Nan { 6074edf30e3SWang Nan LIST_HEAD(new_evsels); 6084edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6095d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6104edf30e3SWang Nan struct list_head *list = param->list; 6111f45b1d4SWang Nan struct perf_evsel *pos; 6124edf30e3SWang Nan int err; 6134edf30e3SWang Nan 6144edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 615cd102d70SWang Nan group, event, fd); 6164edf30e3SWang Nan 6175d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6185d369a75SArnaldo Carvalho de Melo event, parse_state->error, 61995088a59SWang Nan param->head_config); 6204edf30e3SWang Nan if (err) { 6214edf30e3SWang Nan struct perf_evsel *evsel, *tmp; 6224edf30e3SWang Nan 6234edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 624cd102d70SWang Nan group, event); 6254edf30e3SWang Nan list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { 6264edf30e3SWang Nan list_del(&evsel->node); 6274edf30e3SWang Nan perf_evsel__delete(evsel); 6284edf30e3SWang Nan } 6294edf30e3SWang Nan return err; 6304edf30e3SWang Nan } 631cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6324edf30e3SWang Nan 6331f45b1d4SWang Nan list_for_each_entry(pos, &new_evsels, node) { 6341f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 635cd102d70SWang Nan group, event, pos); 6361f45b1d4SWang Nan pos->bpf_fd = fd; 6371f45b1d4SWang Nan } 6384edf30e3SWang Nan list_splice(&new_evsels, list); 6394edf30e3SWang Nan return 0; 6404edf30e3SWang Nan } 6414edf30e3SWang Nan 6425d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 64384c86ca1SWang Nan struct list_head *list, 64495088a59SWang Nan struct bpf_object *obj, 64595088a59SWang Nan struct list_head *head_config) 64684c86ca1SWang Nan { 64784c86ca1SWang Nan int err; 64884c86ca1SWang Nan char errbuf[BUFSIZ]; 6495d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 650aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 65184c86ca1SWang Nan 65284c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 65384c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 65484c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 65584c86ca1SWang Nan err = -EINVAL; 65684c86ca1SWang Nan goto errout; 65784c86ca1SWang Nan } 65884c86ca1SWang Nan 65984c86ca1SWang Nan /* 660aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 661aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 662aa3abf30SWang Nan * created when failure. 663aa3abf30SWang Nan */ 664aa3abf30SWang Nan if (!registered_unprobe_atexit) { 665aa3abf30SWang Nan atexit(bpf__clear); 666aa3abf30SWang Nan registered_unprobe_atexit = true; 667aa3abf30SWang Nan } 668aa3abf30SWang Nan 669aa3abf30SWang Nan err = bpf__probe(obj); 670aa3abf30SWang Nan if (err) { 671aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 672aa3abf30SWang Nan goto errout; 673aa3abf30SWang Nan } 674aa3abf30SWang Nan 6751e5e3ee8SWang Nan err = bpf__load(obj); 6761e5e3ee8SWang Nan if (err) { 6771e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6781e5e3ee8SWang Nan goto errout; 6791e5e3ee8SWang Nan } 6801e5e3ee8SWang Nan 681cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6824edf30e3SWang Nan if (err) { 6834edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6844edf30e3SWang Nan "Attach events in BPF object failed"); 6854edf30e3SWang Nan goto errout; 6864edf30e3SWang Nan } 6874edf30e3SWang Nan 6884edf30e3SWang Nan return 0; 68984c86ca1SWang Nan errout: 6905d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 6915d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 69284c86ca1SWang Nan return err; 69384c86ca1SWang Nan } 69484c86ca1SWang Nan 695a34f3be7SWang Nan static int 6965d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 697a34f3be7SWang Nan struct bpf_object *obj, 698a34f3be7SWang Nan struct list_head *head_config) 699a34f3be7SWang Nan { 700a34f3be7SWang Nan struct parse_events_term *term; 701a34f3be7SWang Nan int error_pos; 702a34f3be7SWang Nan 703a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 704a34f3be7SWang Nan return 0; 705a34f3be7SWang Nan 706a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 707a34f3be7SWang Nan char errbuf[BUFSIZ]; 708a34f3be7SWang Nan int err; 709a34f3be7SWang Nan 710a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 711a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 712a34f3be7SWang Nan "Invalid config term for BPF object"); 713a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 714a34f3be7SWang Nan 7155d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term; 7165d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 717a34f3be7SWang Nan return -EINVAL; 718a34f3be7SWang Nan } 719a34f3be7SWang Nan 7205d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 721a34f3be7SWang Nan if (err) { 7225d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 723a34f3be7SWang Nan &error_pos, err, errbuf, 724a34f3be7SWang Nan sizeof(errbuf)); 7255d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup( 7267630b3e2SWang Nan "Hint:\tValid config terms:\n" 727e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 728e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7297630b3e2SWang Nan "\n" 730e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 731a34f3be7SWang Nan " \t(add -v to see detail)"); 7325d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 733a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 7345d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_val; 735a34f3be7SWang Nan else 7365d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term + error_pos; 737a34f3be7SWang Nan return err; 738a34f3be7SWang Nan } 739a34f3be7SWang Nan } 740a34f3be7SWang Nan return 0; 741a34f3be7SWang Nan } 742a34f3be7SWang Nan 74395088a59SWang Nan /* 74495088a59SWang Nan * Split config terms: 74595088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 74695088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 74795088a59SWang Nan * events in bpf.c. 74895088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 74995088a59SWang Nan * with parse_events_config_bpf. 75095088a59SWang Nan * 75195088a59SWang Nan * Move object config terms from the first list to obj_head_config. 75295088a59SWang Nan */ 75395088a59SWang Nan static void 75495088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 75595088a59SWang Nan struct list_head *obj_head_config) 75695088a59SWang Nan { 75795088a59SWang Nan struct parse_events_term *term, *temp; 75895088a59SWang Nan 75995088a59SWang Nan /* 76095088a59SWang Nan * Currectly, all possible user config term 76195088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 76295088a59SWang Nan * happends to be a good flag. 76395088a59SWang Nan * 76495088a59SWang Nan * See parse_events_config_bpf() and 76595088a59SWang Nan * config_term_tracepoint(). 76695088a59SWang Nan */ 76795088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 76895088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 76995088a59SWang Nan list_move_tail(&term->list, obj_head_config); 77095088a59SWang Nan } 77195088a59SWang Nan 7725d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 77384c86ca1SWang Nan struct list_head *list, 774d509db04SWang Nan char *bpf_file_name, 775a34f3be7SWang Nan bool source, 776a34f3be7SWang Nan struct list_head *head_config) 77784c86ca1SWang Nan { 778a34f3be7SWang Nan int err; 77995088a59SWang Nan struct bpf_object *obj; 78095088a59SWang Nan LIST_HEAD(obj_head_config); 78195088a59SWang Nan 78295088a59SWang Nan if (head_config) 78395088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 78484c86ca1SWang Nan 785d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 7866371ca3bSWang Nan if (IS_ERR(obj)) { 78784c86ca1SWang Nan char errbuf[BUFSIZ]; 78884c86ca1SWang Nan 7896371ca3bSWang Nan err = PTR_ERR(obj); 79084c86ca1SWang Nan 79184c86ca1SWang Nan if (err == -ENOTSUP) 79284c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 79384c86ca1SWang Nan "BPF support is not compiled"); 79484c86ca1SWang Nan else 795d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 796d3e0ce39SWang Nan source, 797d3e0ce39SWang Nan -err, errbuf, 798d3e0ce39SWang Nan sizeof(errbuf)); 79984c86ca1SWang Nan 8005d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 8015d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 80284c86ca1SWang Nan return err; 80384c86ca1SWang Nan } 80484c86ca1SWang Nan 8055d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 806a34f3be7SWang Nan if (err) 807a34f3be7SWang Nan return err; 8085d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 80995088a59SWang Nan 81095088a59SWang Nan /* 81195088a59SWang Nan * Caller doesn't know anything about obj_head_config, 81295088a59SWang Nan * so combine them together again before returnning. 81395088a59SWang Nan */ 81495088a59SWang Nan if (head_config) 81595088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 81695088a59SWang Nan return err; 81784c86ca1SWang Nan } 81884c86ca1SWang Nan 81989812fc8SJiri Olsa static int 82089812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8211b290d67SFrederic Weisbecker { 8221b290d67SFrederic Weisbecker int i; 8231b290d67SFrederic Weisbecker 8241b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 82589812fc8SJiri Olsa if (!type || !type[i]) 8261b290d67SFrederic Weisbecker break; 8271b290d67SFrederic Weisbecker 8287582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8297582732fSJiri Olsa do { \ 8307582732fSJiri Olsa if (attr->bp_type & bit) \ 8317582732fSJiri Olsa return -EINVAL; \ 8327582732fSJiri Olsa else \ 8337582732fSJiri Olsa attr->bp_type |= bit; \ 8347582732fSJiri Olsa } while (0) 8357582732fSJiri Olsa 8361b290d67SFrederic Weisbecker switch (type[i]) { 8371b290d67SFrederic Weisbecker case 'r': 8387582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8391b290d67SFrederic Weisbecker break; 8401b290d67SFrederic Weisbecker case 'w': 8417582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8421b290d67SFrederic Weisbecker break; 8431b290d67SFrederic Weisbecker case 'x': 8447582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8451b290d67SFrederic Weisbecker break; 8461b290d67SFrederic Weisbecker default: 84789812fc8SJiri Olsa return -EINVAL; 8481b290d67SFrederic Weisbecker } 8491b290d67SFrederic Weisbecker } 85089812fc8SJiri Olsa 8517582732fSJiri Olsa #undef CHECK_SET_TYPE 8527582732fSJiri Olsa 8531b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8541b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8551b290d67SFrederic Weisbecker 85689812fc8SJiri Olsa return 0; 8571b290d67SFrederic Weisbecker } 8581b290d67SFrederic Weisbecker 859c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 8603741eb9fSJacob Shin void *ptr, char *type, u64 len) 8611b290d67SFrederic Weisbecker { 86289812fc8SJiri Olsa struct perf_event_attr attr; 8631b290d67SFrederic Weisbecker 86489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 8659fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 8661b290d67SFrederic Weisbecker 86789812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 86889812fc8SJiri Olsa return -EINVAL; 8691b290d67SFrederic Weisbecker 8703741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 8713741eb9fSJacob Shin if (!len) { 87289812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 8733741eb9fSJacob Shin len = sizeof(long); 874aa59a485SFrederic Weisbecker else 8753741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 8763741eb9fSJacob Shin } 8773741eb9fSJacob Shin 8783741eb9fSJacob Shin attr.bp_len = len; 879aa59a485SFrederic Weisbecker 88089812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 8814a841d65SJovi Zhang attr.sample_period = 1; 8821b290d67SFrederic Weisbecker 883930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 8841b290d67SFrederic Weisbecker } 8851b290d67SFrederic Weisbecker 8863b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 8873b0e371cSJiri Olsa struct parse_events_error *err, 8883b0e371cSJiri Olsa int type) 8893b0e371cSJiri Olsa { 8903b0e371cSJiri Olsa if (type == term->type_val) 8913b0e371cSJiri Olsa return 0; 8923b0e371cSJiri Olsa 8933b0e371cSJiri Olsa if (err) { 8943b0e371cSJiri Olsa err->idx = term->err_val; 8953b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 8963b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 8973b0e371cSJiri Olsa else 8983b0e371cSJiri Olsa err->str = strdup("expected string value"); 8993b0e371cSJiri Olsa } 9003b0e371cSJiri Olsa return -EINVAL; 9013b0e371cSJiri Olsa } 9023b0e371cSJiri Olsa 90317cb5f84SWang Nan /* 90417cb5f84SWang Nan * Update according to parse-events.l 90517cb5f84SWang Nan */ 90617cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 90717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 90817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 90917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 91017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 91117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 91217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 91317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 91417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 91517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 91617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 91717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 91817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 91917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 920792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 921626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 922626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 923dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 92417cb5f84SWang Nan }; 92517cb5f84SWang Nan 9261669e509SWang Nan static bool config_term_shrinked; 9271669e509SWang Nan 9281669e509SWang Nan static bool 9291669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9301669e509SWang Nan { 9311669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9321669e509SWang Nan err->str = strdup("Invalid term_type"); 9331669e509SWang Nan return false; 9341669e509SWang Nan } 9351669e509SWang Nan if (!config_term_shrinked) 9361669e509SWang Nan return true; 9371669e509SWang Nan 9381669e509SWang Nan switch (term_type) { 9391669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9401669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9411669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9421669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 94306835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 9441669e509SWang Nan return true; 9451669e509SWang Nan default: 9461669e509SWang Nan if (!err) 9471669e509SWang Nan return false; 9481669e509SWang Nan 9491669e509SWang Nan /* term_type is validated so indexing is safe */ 9501669e509SWang Nan if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", 9511669e509SWang Nan config_term_names[term_type]) < 0) 9521669e509SWang Nan err->str = NULL; 9531669e509SWang Nan return false; 9541669e509SWang Nan } 9551669e509SWang Nan } 9561669e509SWang Nan 9571669e509SWang Nan void parse_events__shrink_config_terms(void) 9581669e509SWang Nan { 9591669e509SWang Nan config_term_shrinked = true; 9601669e509SWang Nan } 9611669e509SWang Nan 9620b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 9633b0e371cSJiri Olsa struct parse_events_term *term, 9643b0e371cSJiri Olsa struct parse_events_error *err) 9658f707d84SJiri Olsa { 96616fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 96716fa7e82SJiri Olsa do { \ 9683b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 96916fa7e82SJiri Olsa return -EINVAL; \ 97016fa7e82SJiri Olsa } while (0) 97116fa7e82SJiri Olsa 97216fa7e82SJiri Olsa switch (term->type_term) { 9738f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 97416fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9758f707d84SJiri Olsa attr->config = term->val.num; 9768f707d84SJiri Olsa break; 9778f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 97816fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9798f707d84SJiri Olsa attr->config1 = term->val.num; 9808f707d84SJiri Olsa break; 9818f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 98216fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9838f707d84SJiri Olsa attr->config2 = term->val.num; 9848f707d84SJiri Olsa break; 9858f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 98616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9878f707d84SJiri Olsa break; 98809af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 98909af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 99009af2a55SNamhyung Kim break; 9918f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 992ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 993ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 994ac12f676SAndi Kleen parse_branch_str(term->val.str, &attr->branch_sample_type)) { 995ac12f676SAndi Kleen err->str = strdup("invalid branch sample type"); 996ac12f676SAndi Kleen err->idx = term->err_val; 997ac12f676SAndi Kleen return -EINVAL; 998ac12f676SAndi Kleen } 9998f707d84SJiri Olsa break; 100032067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 100132067712SKan Liang CHECK_TYPE_VAL(NUM); 100232067712SKan Liang if (term->val.num > 1) { 100332067712SKan Liang err->str = strdup("expected 0 or 1"); 100432067712SKan Liang err->idx = term->err_val; 100532067712SKan Liang return -EINVAL; 100632067712SKan Liang } 100732067712SKan Liang break; 1008d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1009d457c963SKan Liang CHECK_TYPE_VAL(STR); 1010d457c963SKan Liang break; 1011d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1012d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1013d457c963SKan Liang break; 1014374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1015374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1016374ce938SWang Nan break; 1017374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1018374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1019374ce938SWang Nan break; 1020626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1021626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1022626a6b78SWang Nan break; 1023626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1024626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1025626a6b78SWang Nan break; 10266b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10276b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10286b5fc39bSJiri Olsa break; 1029792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1030792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1031792d48b4SArnaldo Carvalho de Melo break; 10328f707d84SJiri Olsa default: 1033ffeb883eSHe Kuang err->str = strdup("unknown term"); 1034ffeb883eSHe Kuang err->idx = term->err_term; 1035ffeb883eSHe Kuang err->help = parse_events_formats_error_string(NULL); 10368f707d84SJiri Olsa return -EINVAL; 10378f707d84SJiri Olsa } 103816fa7e82SJiri Olsa 10391669e509SWang Nan /* 10401669e509SWang Nan * Check term availbility after basic checking so 10411669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 10421669e509SWang Nan * 10431669e509SWang Nan * If check availbility at the entry of this function, 10441669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 10451669e509SWang Nan * if an invalid config term is provided for legacy events 10461669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 10471669e509SWang Nan */ 10481669e509SWang Nan if (!config_term_avail(term->type_term, err)) 10491669e509SWang Nan return -EINVAL; 10508f707d84SJiri Olsa return 0; 105116fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 10528f707d84SJiri Olsa } 10538f707d84SJiri Olsa 10540b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 10550b8891a8SHe Kuang struct parse_events_term *term, 10560b8891a8SHe Kuang struct parse_events_error *err) 10570b8891a8SHe Kuang { 1058dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1059dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 10600b8891a8SHe Kuang /* 10610b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 10620b8891a8SHe Kuang * at this point what type they need to have. 10630b8891a8SHe Kuang */ 10640b8891a8SHe Kuang return 0; 10650b8891a8SHe Kuang else 10660b8891a8SHe Kuang return config_term_common(attr, term, err); 10670b8891a8SHe Kuang } 10680b8891a8SHe Kuang 1069e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1070e637d177SHe Kuang struct parse_events_term *term, 1071e637d177SHe Kuang struct parse_events_error *err) 1072e637d177SHe Kuang { 1073e637d177SHe Kuang switch (term->type_term) { 1074e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1075e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1076374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1077374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1078792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1079626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1080626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1081e637d177SHe Kuang return config_term_common(attr, term, err); 1082e637d177SHe Kuang default: 1083e637d177SHe Kuang if (err) { 1084e637d177SHe Kuang err->idx = term->err_term; 1085e637d177SHe Kuang err->str = strdup("unknown term"); 1086e637d177SHe Kuang err->help = strdup("valid terms: call-graph,stack-size\n"); 1087e637d177SHe Kuang } 1088e637d177SHe Kuang return -EINVAL; 1089e637d177SHe Kuang } 1090e637d177SHe Kuang 1091e637d177SHe Kuang return 0; 1092e637d177SHe Kuang } 1093e637d177SHe Kuang 10948f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 10953b0e371cSJiri Olsa struct list_head *head, 10960b8891a8SHe Kuang struct parse_events_error *err, 10970b8891a8SHe Kuang config_term_func_t config_term) 10988f707d84SJiri Olsa { 10996cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 11008f707d84SJiri Olsa 11018f707d84SJiri Olsa list_for_each_entry(term, head, list) 11023b0e371cSJiri Olsa if (config_term(attr, term, err)) 11038f707d84SJiri Olsa return -EINVAL; 11048f707d84SJiri Olsa 11058f707d84SJiri Olsa return 0; 11068f707d84SJiri Olsa } 11078f707d84SJiri Olsa 1108930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1109930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1110930a2e29SJiri Olsa { 1111930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 1112930a2e29SJiri Olsa do { \ 1113930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1114930a2e29SJiri Olsa \ 1115930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1116930a2e29SJiri Olsa if (!__t) \ 1117930a2e29SJiri Olsa return -ENOMEM; \ 1118930a2e29SJiri Olsa \ 1119930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1120930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 1121930a2e29SJiri Olsa __t->val.__name = __val; \ 1122c2f1ceadSAndi Kleen __t->weak = term->weak; \ 1123930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 1124930a2e29SJiri Olsa } while (0) 1125930a2e29SJiri Olsa 1126930a2e29SJiri Olsa struct parse_events_term *term; 1127930a2e29SJiri Olsa 1128930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1129930a2e29SJiri Olsa switch (term->type_term) { 1130ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1131ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 113232067712SKan Liang break; 113309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 113409af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 113509af2a55SNamhyung Kim break; 113632067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 113732067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 113832067712SKan Liang break; 1139d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1140d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 1141d457c963SKan Liang break; 1142ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1143ac12f676SAndi Kleen ADD_CONFIG_TERM(BRANCH, branch, term->val.str); 1144ac12f676SAndi Kleen break; 1145d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1146d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 1147d457c963SKan Liang break; 1148374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1149374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); 1150374ce938SWang Nan break; 1151374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1152374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); 1153374ce938SWang Nan break; 1154792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1155792d48b4SArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num); 1156792d48b4SArnaldo Carvalho de Melo break; 1157626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1158626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0); 1159626a6b78SWang Nan break; 1160626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1161626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1); 1162626a6b78SWang Nan break; 1163dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1164dd60fba7SMathieu Poirier ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str); 1165dd60fba7SMathieu Poirier break; 1166930a2e29SJiri Olsa default: 1167930a2e29SJiri Olsa break; 1168930a2e29SJiri Olsa } 1169930a2e29SJiri Olsa } 1170930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 1171930a2e29SJiri Olsa return 0; 1172930a2e29SJiri Olsa } 1173930a2e29SJiri Olsa 1174e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 11758c619d6aSWang Nan const char *sys, const char *event, 1176272ed29aSJiri Olsa struct parse_events_error *err, 1177e637d177SHe Kuang struct list_head *head_config) 1178e637d177SHe Kuang { 1179e637d177SHe Kuang if (head_config) { 1180e637d177SHe Kuang struct perf_event_attr attr; 1181e637d177SHe Kuang 1182272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1183e637d177SHe Kuang config_term_tracepoint)) 1184e637d177SHe Kuang return -EINVAL; 1185e637d177SHe Kuang } 1186e637d177SHe Kuang 1187e637d177SHe Kuang if (strpbrk(sys, "*?")) 1188e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1189272ed29aSJiri Olsa err, head_config); 1190e637d177SHe Kuang else 1191e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1192272ed29aSJiri Olsa err, head_config); 1193e637d177SHe Kuang } 1194e637d177SHe Kuang 11955d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 119687d650beSJiri Olsa struct list_head *list, 1197b527bab5SRobert Richter u32 type, u64 config, 11988f707d84SJiri Olsa struct list_head *head_config) 119974d5b588SJaswinder Singh Rajput { 120089812fc8SJiri Olsa struct perf_event_attr attr; 1201930a2e29SJiri Olsa LIST_HEAD(config_terms); 120274d5b588SJaswinder Singh Rajput 120389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 120489812fc8SJiri Olsa attr.type = type; 120589812fc8SJiri Olsa attr.config = config; 12068f707d84SJiri Olsa 1207930a2e29SJiri Olsa if (head_config) { 12085d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 12090b8891a8SHe Kuang config_term_common)) 12108f707d84SJiri Olsa return -EINVAL; 12118f707d84SJiri Olsa 1212930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1213930a2e29SJiri Olsa return -ENOMEM; 1214930a2e29SJiri Olsa } 1215930a2e29SJiri Olsa 12165d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 121710bf358aSWang Nan get_config_name(head_config), &config_terms); 1218b908debdSIngo Molnar } 1219b908debdSIngo Molnar 1220c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 122136adec85SJiri Olsa struct list_head *list, char *name, 122263ce8449SArnaldo Carvalho de Melo struct list_head *head_config, bool auto_merge_stats) 12235f537a26SJiri Olsa { 12245f537a26SJiri Olsa struct perf_event_attr attr; 122546441bdcSMatt Fleming struct perf_pmu_info info; 12265f537a26SJiri Olsa struct perf_pmu *pmu; 1227410136f5SStephane Eranian struct perf_evsel *evsel; 1228333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 1229930a2e29SJiri Olsa LIST_HEAD(config_terms); 12305f537a26SJiri Olsa 12315f537a26SJiri Olsa pmu = perf_pmu__find(name); 1232333b5665SAndi Kleen if (!pmu) { 1233333b5665SAndi Kleen if (asprintf(&err->str, 1234333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1235333b5665SAndi Kleen name) < 0) 1236333b5665SAndi Kleen err->str = NULL; 12375f537a26SJiri Olsa return -EINVAL; 1238333b5665SAndi Kleen } 12395f537a26SJiri Olsa 1240dc0a6202SAdrian Hunter if (pmu->default_config) { 1241dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1242dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1243dc0a6202SAdrian Hunter } else { 12445f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1245dc0a6202SAdrian Hunter } 12465f537a26SJiri Olsa 1247ad962273SAdrian Hunter if (!head_config) { 1248ad962273SAdrian Hunter attr.type = pmu->type; 124966ec1191SMark Rutland evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats); 12508c5421c0SAgustin Vega-Frias if (evsel) { 12518c5421c0SAgustin Vega-Frias evsel->pmu_name = name; 12528c5421c0SAgustin Vega-Frias return 0; 12538c5421c0SAgustin Vega-Frias } else { 12548c5421c0SAgustin Vega-Frias return -ENOMEM; 12558c5421c0SAgustin Vega-Frias } 1256ad962273SAdrian Hunter } 1257ad962273SAdrian Hunter 125846441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1259a6146d50SZheng Yan return -EINVAL; 1260a6146d50SZheng Yan 12615f537a26SJiri Olsa /* 12625f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 12635f537a26SJiri Olsa * return value when called with fail == 0 ;) 12645f537a26SJiri Olsa */ 12655d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1266c056ba6aSJiri Olsa return -EINVAL; 12675f537a26SJiri Olsa 1268930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1269930a2e29SJiri Olsa return -ENOMEM; 1270930a2e29SJiri Olsa 12715d9cdc11SArnaldo Carvalho de Melo if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) 12725f537a26SJiri Olsa return -EINVAL; 12735f537a26SJiri Olsa 12745d9cdc11SArnaldo Carvalho de Melo evsel = __add_event(list, &parse_state->idx, &attr, 127566ec1191SMark Rutland get_config_name(head_config), pmu, 127663ce8449SArnaldo Carvalho de Melo &config_terms, auto_merge_stats); 1277410136f5SStephane Eranian if (evsel) { 127846441bdcSMatt Fleming evsel->unit = info.unit; 127946441bdcSMatt Fleming evsel->scale = info.scale; 1280044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 12811d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 128237932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 128396284814SAndi Kleen evsel->metric_name = info.metric_name; 12848c5421c0SAgustin Vega-Frias evsel->pmu_name = name; 1285410136f5SStephane Eranian } 1286410136f5SStephane Eranian 1287410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 12885f537a26SJiri Olsa } 12895f537a26SJiri Olsa 12905d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 12912073ad33SAndi Kleen char *str, struct list_head **listp) 12922073ad33SAndi Kleen { 12932073ad33SAndi Kleen struct list_head *head; 12942073ad33SAndi Kleen struct parse_events_term *term; 12952073ad33SAndi Kleen struct list_head *list; 12962073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 12972073ad33SAndi Kleen int ok = 0; 12982073ad33SAndi Kleen 12992073ad33SAndi Kleen *listp = NULL; 13002073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 13012073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 13022073ad33SAndi Kleen if (!list) 13032073ad33SAndi Kleen return -1; 13042073ad33SAndi Kleen INIT_LIST_HEAD(list); 13052073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 13062073ad33SAndi Kleen struct perf_pmu_alias *alias; 13072073ad33SAndi Kleen 13082073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 13092073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 13102073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 13112073ad33SAndi Kleen if (!head) 13122073ad33SAndi Kleen return -1; 13132073ad33SAndi Kleen INIT_LIST_HEAD(head); 13142073ad33SAndi Kleen if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 13152073ad33SAndi Kleen str, 1, false, &str, NULL) < 0) 13162073ad33SAndi Kleen return -1; 13172073ad33SAndi Kleen list_add_tail(&term->list, head); 13182073ad33SAndi Kleen 1319c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 132063ce8449SArnaldo Carvalho de Melo pmu->name, head, true)) { 13212073ad33SAndi Kleen pr_debug("%s -> %s/%s/\n", str, 13222073ad33SAndi Kleen pmu->name, alias->str); 13232073ad33SAndi Kleen ok++; 13242073ad33SAndi Kleen } 13252073ad33SAndi Kleen 13262073ad33SAndi Kleen parse_events_terms__delete(head); 13272073ad33SAndi Kleen } 13282073ad33SAndi Kleen } 13292073ad33SAndi Kleen } 13302073ad33SAndi Kleen if (!ok) 13312073ad33SAndi Kleen return -1; 13322073ad33SAndi Kleen *listp = list; 13332073ad33SAndi Kleen return 0; 13342073ad33SAndi Kleen } 13352073ad33SAndi Kleen 13366a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 13376a4bb04cSJiri Olsa char *event_mod) 133889efb029SJiri Olsa { 13396a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 13406a4bb04cSJiri Olsa } 13416a4bb04cSJiri Olsa 134263dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 13436a4bb04cSJiri Olsa { 13446a4bb04cSJiri Olsa struct perf_evsel *leader; 13456a4bb04cSJiri Olsa 1346854f7363SWang Nan if (list_empty(list)) { 1347854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1348854f7363SWang Nan return; 1349854f7363SWang Nan } 1350854f7363SWang Nan 135163dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 135263dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 13536a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 135489efb029SJiri Olsa } 135589efb029SJiri Olsa 1356c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 13575d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 13585d7be90eSJiri Olsa struct list_head *list_all) 13595d7be90eSJiri Olsa { 13605d7be90eSJiri Olsa /* 13615d7be90eSJiri Olsa * Called for single event definition. Update the 136289efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 13635d7be90eSJiri Olsa * list, for next event definition. 13645d7be90eSJiri Olsa */ 13655d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1366b847cbdcSJiri Olsa free(list_event); 13675d7be90eSJiri Olsa } 13685d7be90eSJiri Olsa 1369f5b1135bSJiri Olsa struct event_modifier { 1370f5b1135bSJiri Olsa int eu; 1371f5b1135bSJiri Olsa int ek; 1372f5b1135bSJiri Olsa int eh; 1373f5b1135bSJiri Olsa int eH; 1374f5b1135bSJiri Olsa int eG; 1375a1e12da4SJiri Olsa int eI; 1376f5b1135bSJiri Olsa int precise; 13777f94af7aSJiri Olsa int precise_max; 1378f5b1135bSJiri Olsa int exclude_GH; 13793c176311SJiri Olsa int sample_read; 1380e9a7c414SMichael Ellerman int pinned; 13815a5dfe4bSAndi Kleen int weak; 1382f5b1135bSJiri Olsa }; 138361c45981SPaul Mackerras 1384f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 1385f5b1135bSJiri Olsa struct perf_evsel *evsel) 1386f5b1135bSJiri Olsa { 1387f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 1388f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 1389f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 1390f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 1391f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 1392a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 1393f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 13947f94af7aSJiri Olsa int precise_max = 0; 13953c176311SJiri Olsa int sample_read = 0; 1396e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 1397f5b1135bSJiri Olsa 1398f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1399f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 14005a5dfe4bSAndi Kleen int weak = 0; 1401f5b1135bSJiri Olsa 1402f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1403ceb53fbfSIngo Molnar 140461c45981SPaul Mackerras while (*str) { 1405ab608344SPeter Zijlstra if (*str == 'u') { 1406ab608344SPeter Zijlstra if (!exclude) 1407ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 140861c45981SPaul Mackerras eu = 0; 1409ab608344SPeter Zijlstra } else if (*str == 'k') { 1410ab608344SPeter Zijlstra if (!exclude) 1411ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 141261c45981SPaul Mackerras ek = 0; 1413ab608344SPeter Zijlstra } else if (*str == 'h') { 1414ab608344SPeter Zijlstra if (!exclude) 1415ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 141661c45981SPaul Mackerras eh = 0; 141799320cc8SJoerg Roedel } else if (*str == 'G') { 141899320cc8SJoerg Roedel if (!exclude_GH) 141999320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 142099320cc8SJoerg Roedel eG = 0; 142199320cc8SJoerg Roedel } else if (*str == 'H') { 142299320cc8SJoerg Roedel if (!exclude_GH) 142399320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 142499320cc8SJoerg Roedel eH = 0; 1425a1e12da4SJiri Olsa } else if (*str == 'I') { 1426a1e12da4SJiri Olsa eI = 1; 1427ab608344SPeter Zijlstra } else if (*str == 'p') { 1428ab608344SPeter Zijlstra precise++; 14291342798cSDavid Ahern /* use of precise requires exclude_guest */ 14301342798cSDavid Ahern if (!exclude_GH) 14311342798cSDavid Ahern eG = 1; 14327f94af7aSJiri Olsa } else if (*str == 'P') { 14337f94af7aSJiri Olsa precise_max = 1; 14343c176311SJiri Olsa } else if (*str == 'S') { 14353c176311SJiri Olsa sample_read = 1; 1436e9a7c414SMichael Ellerman } else if (*str == 'D') { 1437e9a7c414SMichael Ellerman pinned = 1; 14385a5dfe4bSAndi Kleen } else if (*str == 'W') { 14395a5dfe4bSAndi Kleen weak = 1; 1440ab608344SPeter Zijlstra } else 144161c45981SPaul Mackerras break; 1442ab608344SPeter Zijlstra 144361c45981SPaul Mackerras ++str; 144461c45981SPaul Mackerras } 144574d5b588SJaswinder Singh Rajput 144686470930SIngo Molnar /* 144789812fc8SJiri Olsa * precise ip: 144889812fc8SJiri Olsa * 144989812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 145089812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 145189812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 145289812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 145389812fc8SJiri Olsa * 145489812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 145586470930SIngo Molnar */ 145689812fc8SJiri Olsa if (precise > 3) 145789812fc8SJiri Olsa return -EINVAL; 145886470930SIngo Molnar 1459f5b1135bSJiri Olsa mod->eu = eu; 1460f5b1135bSJiri Olsa mod->ek = ek; 1461f5b1135bSJiri Olsa mod->eh = eh; 1462f5b1135bSJiri Olsa mod->eH = eH; 1463f5b1135bSJiri Olsa mod->eG = eG; 1464a1e12da4SJiri Olsa mod->eI = eI; 1465f5b1135bSJiri Olsa mod->precise = precise; 14667f94af7aSJiri Olsa mod->precise_max = precise_max; 1467f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 14683c176311SJiri Olsa mod->sample_read = sample_read; 1469e9a7c414SMichael Ellerman mod->pinned = pinned; 14705a5dfe4bSAndi Kleen mod->weak = weak; 1471e9a7c414SMichael Ellerman 1472f5b1135bSJiri Olsa return 0; 1473f5b1135bSJiri Olsa } 1474f5b1135bSJiri Olsa 1475534123f4SJiri Olsa /* 1476534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1477534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1478534123f4SJiri Olsa */ 1479534123f4SJiri Olsa static int check_modifier(char *str) 1480534123f4SJiri Olsa { 1481534123f4SJiri Olsa char *p = str; 1482534123f4SJiri Olsa 1483534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 14845a5dfe4bSAndi Kleen if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1)) 1485534123f4SJiri Olsa return -1; 1486534123f4SJiri Olsa 1487534123f4SJiri Olsa while (*p) { 1488534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1489534123f4SJiri Olsa return -1; 1490534123f4SJiri Olsa p++; 1491534123f4SJiri Olsa } 1492534123f4SJiri Olsa 1493534123f4SJiri Olsa return 0; 1494534123f4SJiri Olsa } 1495534123f4SJiri Olsa 1496f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1497f5b1135bSJiri Olsa { 1498f5b1135bSJiri Olsa struct perf_evsel *evsel; 1499f5b1135bSJiri Olsa struct event_modifier mod; 1500f5b1135bSJiri Olsa 1501f5b1135bSJiri Olsa if (str == NULL) 1502f5b1135bSJiri Olsa return 0; 1503f5b1135bSJiri Olsa 1504534123f4SJiri Olsa if (check_modifier(str)) 1505534123f4SJiri Olsa return -EINVAL; 1506534123f4SJiri Olsa 1507f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1508f5b1135bSJiri Olsa return -EINVAL; 1509f5b1135bSJiri Olsa 1510e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1511f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1512f5b1135bSJiri Olsa return -EINVAL; 1513f5b1135bSJiri Olsa 1514f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 1515f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 1516f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 1517f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 1518f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 1519f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 1520a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 1521f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 15223c176311SJiri Olsa evsel->sample_read = mod.sample_read; 15237f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 15245a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 1525e9a7c414SMichael Ellerman 1526e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 1527e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 1528ceb53fbfSIngo Molnar } 152986470930SIngo Molnar 153089812fc8SJiri Olsa return 0; 153186470930SIngo Molnar } 153286470930SIngo Molnar 1533ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1534ac2ba9f3SRobert Richter { 1535ac2ba9f3SRobert Richter struct perf_evsel *evsel; 1536ac2ba9f3SRobert Richter 1537e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1538ac2ba9f3SRobert Richter if (!evsel->name) 1539ac2ba9f3SRobert Richter evsel->name = strdup(name); 1540ac2ba9f3SRobert Richter } 1541ac2ba9f3SRobert Richter 1542ac2ba9f3SRobert Richter return 0; 1543ac2ba9f3SRobert Richter } 1544ac2ba9f3SRobert Richter 1545dcb4e102SKan Liang static int 1546dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1547dcb4e102SKan Liang { 1548dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1549dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1550dcb4e102SKan Liang 1551e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1552dcb4e102SKan Liang } 1553dcb4e102SKan Liang 1554dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1555dcb4e102SKan Liang { 1556dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1557dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1558dcb4e102SKan Liang int i; 1559dcb4e102SKan Liang 1560dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1561dcb4e102SKan Liang p = perf_pmu_events_list + i; 1562360e071bSTaeung Song zfree(&p->symbol); 1563dcb4e102SKan Liang } 1564506fde11STaeung Song zfree(&perf_pmu_events_list); 1565dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1566dcb4e102SKan Liang } 1567dcb4e102SKan Liang } 1568dcb4e102SKan Liang 1569dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1570dcb4e102SKan Liang do { \ 1571dcb4e102SKan Liang p->symbol = str; \ 1572dcb4e102SKan Liang if (!p->symbol) \ 1573dcb4e102SKan Liang goto err; \ 1574dcb4e102SKan Liang p->type = stype; \ 1575dcb4e102SKan Liang } while (0) 1576dcb4e102SKan Liang 1577dcb4e102SKan Liang /* 1578dcb4e102SKan Liang * Read the pmu events list from sysfs 1579dcb4e102SKan Liang * Save it into perf_pmu_events_list 1580dcb4e102SKan Liang */ 1581dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1582dcb4e102SKan Liang { 1583dcb4e102SKan Liang 1584dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1585dcb4e102SKan Liang struct perf_pmu_alias *alias; 1586dcb4e102SKan Liang int len = 0; 1587dcb4e102SKan Liang 1588231bb2aaSAndi Kleen pmu = NULL; 1589231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1590dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1591dcb4e102SKan Liang if (strchr(alias->name, '-')) 1592dcb4e102SKan Liang len++; 1593dcb4e102SKan Liang len++; 1594dcb4e102SKan Liang } 1595231bb2aaSAndi Kleen } 1596231bb2aaSAndi Kleen 1597231bb2aaSAndi Kleen if (len == 0) { 1598231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1599231bb2aaSAndi Kleen return; 1600231bb2aaSAndi Kleen } 1601dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1602dcb4e102SKan Liang if (!perf_pmu_events_list) 1603dcb4e102SKan Liang return; 1604dcb4e102SKan Liang perf_pmu_events_list_num = len; 1605dcb4e102SKan Liang 1606dcb4e102SKan Liang len = 0; 1607231bb2aaSAndi Kleen pmu = NULL; 1608231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1609dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1610dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1611dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1612dcb4e102SKan Liang 1613dcb4e102SKan Liang if (tmp != NULL) { 1614dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1615dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1616dcb4e102SKan Liang p++; 1617dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1618dcb4e102SKan Liang len += 2; 1619dcb4e102SKan Liang } else { 1620dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1621dcb4e102SKan Liang len++; 1622dcb4e102SKan Liang } 1623dcb4e102SKan Liang } 1624231bb2aaSAndi Kleen } 1625dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1626dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1627dcb4e102SKan Liang 1628dcb4e102SKan Liang return; 1629dcb4e102SKan Liang err: 1630dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1631dcb4e102SKan Liang } 1632dcb4e102SKan Liang 1633dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1634dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1635dcb4e102SKan Liang { 1636dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1637dcb4e102SKan Liang 1638dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1639dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1640dcb4e102SKan Liang perf_pmu__parse_init(); 1641dcb4e102SKan Liang /* 1642dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1643dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1644dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1645dcb4e102SKan Liang */ 1646dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1647dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1648dcb4e102SKan Liang 1649dcb4e102SKan Liang p.symbol = strdup(name); 1650dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1651dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1652dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1653360e071bSTaeung Song zfree(&p.symbol); 1654dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1655dcb4e102SKan Liang } 1656dcb4e102SKan Liang 16575d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token) 1658ac20de6fSZheng Yan { 1659ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1660ac20de6fSZheng Yan void *scanner; 1661ac20de6fSZheng Yan int ret; 1662ac20de6fSZheng Yan 166390e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1664ac20de6fSZheng Yan if (ret) 1665ac20de6fSZheng Yan return ret; 1666ac20de6fSZheng Yan 1667ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1668ac20de6fSZheng Yan 1669ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1670ac20de6fSZheng Yan parse_events_debug = 1; 1671ac20de6fSZheng Yan #endif 16725d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 1673ac20de6fSZheng Yan 1674ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1675ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1676ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1677ac20de6fSZheng Yan return ret; 1678ac20de6fSZheng Yan } 1679ac20de6fSZheng Yan 168090e2b22dSJiri Olsa /* 168190e2b22dSJiri Olsa * parse event config string, return a list of event terms. 168290e2b22dSJiri Olsa */ 168390e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 168490e2b22dSJiri Olsa { 16855d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 168690e2b22dSJiri Olsa .terms = NULL, 168790e2b22dSJiri Olsa }; 168890e2b22dSJiri Olsa int ret; 168990e2b22dSJiri Olsa 16905d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_TERMS); 169190e2b22dSJiri Olsa if (!ret) { 16925d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 16935d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 169490e2b22dSJiri Olsa return 0; 169590e2b22dSJiri Olsa } 169690e2b22dSJiri Olsa 16975d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 169890e2b22dSJiri Olsa return ret; 169990e2b22dSJiri Olsa } 170090e2b22dSJiri Olsa 1701b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1702b39b8393SJiri Olsa struct parse_events_error *err) 170386470930SIngo Molnar { 17045d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 17055d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 170646010ab2SJiri Olsa .idx = evlist->nr_entries, 1707b39b8393SJiri Olsa .error = err, 17087630b3e2SWang Nan .evlist = evlist, 170946010ab2SJiri Olsa }; 171046010ab2SJiri Olsa int ret; 171186470930SIngo Molnar 17125d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS); 1713dcb4e102SKan Liang perf_pmu__parse_cleanup(); 171489812fc8SJiri Olsa if (!ret) { 171515bfd2ccSWang Nan struct perf_evsel *last; 171615bfd2ccSWang Nan 17175d9cdc11SArnaldo Carvalho de Melo if (list_empty(&parse_state.list)) { 1718854f7363SWang Nan WARN_ONCE(true, "WARNING: event parser found nothing"); 1719854f7363SWang Nan return -1; 1720854f7363SWang Nan } 1721854f7363SWang Nan 17225d9cdc11SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &parse_state.list); 17235d9cdc11SArnaldo Carvalho de Melo evlist->nr_groups += parse_state.nr_groups; 172415bfd2ccSWang Nan last = perf_evlist__last(evlist); 172515bfd2ccSWang Nan last->cmdline_group_boundary = true; 172615bfd2ccSWang Nan 172786470930SIngo Molnar return 0; 172886470930SIngo Molnar } 172986470930SIngo Molnar 17305d7be90eSJiri Olsa /* 17315d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 17325d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 17335d7be90eSJiri Olsa * need to bother. 17345d7be90eSJiri Olsa */ 173589812fc8SJiri Olsa return ret; 173689812fc8SJiri Olsa } 173789812fc8SJiri Olsa 1738b39b8393SJiri Olsa #define MAX_WIDTH 1000 1739b39b8393SJiri Olsa static int get_term_width(void) 1740b39b8393SJiri Olsa { 1741b39b8393SJiri Olsa struct winsize ws; 1742b39b8393SJiri Olsa 1743b39b8393SJiri Olsa get_term_dimensions(&ws); 1744b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1745b39b8393SJiri Olsa } 1746b39b8393SJiri Olsa 1747333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err, 1748b39b8393SJiri Olsa const char *event) 1749b39b8393SJiri Olsa { 1750b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1751b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1752b39b8393SJiri Olsa char *buf = (char *) event; 1753b39b8393SJiri Olsa int idx = 0; 1754b39b8393SJiri Olsa 1755b39b8393SJiri Olsa if (err->str) { 1756b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1757b39b8393SJiri Olsa int width = get_term_width() - 2; 1758b39b8393SJiri Olsa int len_event = strlen(event); 1759b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1760b39b8393SJiri Olsa 1761b39b8393SJiri Olsa /* 1762b39b8393SJiri Olsa * Maximum error index indent, we will cut 1763b39b8393SJiri Olsa * the event string if it's bigger. 1764b39b8393SJiri Olsa */ 1765141b2d31SAdrian Hunter int max_err_idx = 13; 1766b39b8393SJiri Olsa 1767b39b8393SJiri Olsa /* 1768b39b8393SJiri Olsa * Let's be specific with the message when 1769b39b8393SJiri Olsa * we have the precise error. 1770b39b8393SJiri Olsa */ 1771b39b8393SJiri Olsa str = "event syntax error: "; 1772b39b8393SJiri Olsa len_str = strlen(str); 1773b39b8393SJiri Olsa max_len = width - len_str; 1774b39b8393SJiri Olsa 1775b39b8393SJiri Olsa buf = _buf; 1776b39b8393SJiri Olsa 1777bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 1778b39b8393SJiri Olsa if (err->idx > max_err_idx) 1779b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1780b39b8393SJiri Olsa 1781b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1782b39b8393SJiri Olsa 1783b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1784b39b8393SJiri Olsa if (cut) 1785b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1786b39b8393SJiri Olsa 1787b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1788b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1789b39b8393SJiri Olsa buf[max_len] = 0; 1790b39b8393SJiri Olsa } 1791b39b8393SJiri Olsa 1792b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1793b39b8393SJiri Olsa } 1794b39b8393SJiri Olsa 1795b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1796b39b8393SJiri Olsa if (idx) { 1797b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1798b39b8393SJiri Olsa if (err->help) 1799b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1800360e071bSTaeung Song zfree(&err->str); 1801360e071bSTaeung Song zfree(&err->help); 1802b39b8393SJiri Olsa } 1803b39b8393SJiri Olsa } 1804b39b8393SJiri Olsa 1805b39b8393SJiri Olsa #undef MAX_WIDTH 1806b39b8393SJiri Olsa 1807f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 18081d037ca1SIrina Tirdea int unset __maybe_unused) 1809f120f9d5SJiri Olsa { 1810f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1811b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1812b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 18139175ce1fSAndi Kleen 1814333b5665SAndi Kleen if (ret) { 1815b39b8393SJiri Olsa parse_events_print_error(&err, str); 1816333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1817333b5665SAndi Kleen } 1818b39b8393SJiri Olsa 18199175ce1fSAndi Kleen return ret; 1820f120f9d5SJiri Olsa } 1821f120f9d5SJiri Olsa 18224ba1faa1SWang Nan static int 18234ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 18244ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 18254ba1faa1SWang Nan const void *arg), 18264ba1faa1SWang Nan const void *arg) 1827c171b552SLi Zefan { 182869aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 18294ba1faa1SWang Nan int err; 1830c171b552SLi Zefan 1831854f7363SWang Nan /* 1832854f7363SWang Nan * Don't return when list_empty, give func a chance to report 1833854f7363SWang Nan * error when it found last == NULL. 1834854f7363SWang Nan * 1835854f7363SWang Nan * So no need to WARN here, let *func do this. 1836854f7363SWang Nan */ 1837361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 18380c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 183969aad6f1SArnaldo Carvalho de Melo 184015bfd2ccSWang Nan do { 18414ba1faa1SWang Nan err = (*func)(last, arg); 18424ba1faa1SWang Nan if (err) 1843c171b552SLi Zefan return -1; 18444ba1faa1SWang Nan if (!last) 18454ba1faa1SWang Nan return 0; 1846c171b552SLi Zefan 184715bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 184815bfd2ccSWang Nan return 0; 184915bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 185015bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 185115bfd2ccSWang Nan 1852c171b552SLi Zefan return 0; 1853c171b552SLi Zefan } 1854c171b552SLi Zefan 18554ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 18564ba1faa1SWang Nan { 18574ba1faa1SWang Nan const char *str = arg; 18581e857484SMathieu Poirier bool found = false; 18591e857484SMathieu Poirier int nr_addr_filters = 0; 18601e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 18614ba1faa1SWang Nan 18621e857484SMathieu Poirier if (evsel == NULL) 18631e857484SMathieu Poirier goto err; 18644ba1faa1SWang Nan 18651e857484SMathieu Poirier if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 18663541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 18674ba1faa1SWang Nan fprintf(stderr, 18684ba1faa1SWang Nan "not enough memory to hold filter string\n"); 18694ba1faa1SWang Nan return -1; 18704ba1faa1SWang Nan } 18714ba1faa1SWang Nan 18724ba1faa1SWang Nan return 0; 18734ba1faa1SWang Nan } 18744ba1faa1SWang Nan 18751e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 18761e857484SMathieu Poirier if (pmu->type == evsel->attr.type) { 18771e857484SMathieu Poirier found = true; 18781e857484SMathieu Poirier break; 18791e857484SMathieu Poirier } 18801e857484SMathieu Poirier 18811e857484SMathieu Poirier if (found) 18821e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 18831e857484SMathieu Poirier "%d", &nr_addr_filters); 18841e857484SMathieu Poirier 18851e857484SMathieu Poirier if (!nr_addr_filters) 18861e857484SMathieu Poirier goto err; 18871e857484SMathieu Poirier 18881e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 18891e857484SMathieu Poirier fprintf(stderr, 18901e857484SMathieu Poirier "not enough memory to hold filter string\n"); 18911e857484SMathieu Poirier return -1; 18921e857484SMathieu Poirier } 18931e857484SMathieu Poirier 18941e857484SMathieu Poirier return 0; 18951e857484SMathieu Poirier 18961e857484SMathieu Poirier err: 18971e857484SMathieu Poirier fprintf(stderr, 18981e857484SMathieu Poirier "--filter option should follow a -e tracepoint or HW tracer option\n"); 18991e857484SMathieu Poirier 19001e857484SMathieu Poirier return -1; 19011e857484SMathieu Poirier } 19021e857484SMathieu Poirier 19034ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 19044ba1faa1SWang Nan int unset __maybe_unused) 19054ba1faa1SWang Nan { 19064ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 19074ba1faa1SWang Nan 19084ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 19094ba1faa1SWang Nan (const void *)str); 19104ba1faa1SWang Nan } 19114ba1faa1SWang Nan 19124ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 19134ba1faa1SWang Nan const void *arg __maybe_unused) 19144ba1faa1SWang Nan { 19154ba1faa1SWang Nan char new_filter[64]; 19164ba1faa1SWang Nan 19174ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 19184ba1faa1SWang Nan fprintf(stderr, 19194ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 19204ba1faa1SWang Nan return -1; 19214ba1faa1SWang Nan } 19224ba1faa1SWang Nan 19234ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 19244ba1faa1SWang Nan 19253541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 19264ba1faa1SWang Nan fprintf(stderr, 19274ba1faa1SWang Nan "not enough memory to hold filter string\n"); 19284ba1faa1SWang Nan return -1; 19294ba1faa1SWang Nan } 19304ba1faa1SWang Nan 19314ba1faa1SWang Nan return 0; 19324ba1faa1SWang Nan } 19334ba1faa1SWang Nan 19344ba1faa1SWang Nan int exclude_perf(const struct option *opt, 19354ba1faa1SWang Nan const char *arg __maybe_unused, 19364ba1faa1SWang Nan int unset __maybe_unused) 19374ba1faa1SWang Nan { 19384ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 19394ba1faa1SWang Nan 19404ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 19414ba1faa1SWang Nan NULL); 19424ba1faa1SWang Nan } 19434ba1faa1SWang Nan 194486470930SIngo Molnar static const char * const event_type_descriptors[] = { 194586470930SIngo Molnar "Hardware event", 194686470930SIngo Molnar "Software event", 194786470930SIngo Molnar "Tracepoint event", 194886470930SIngo Molnar "Hardware cache event", 194941bdcb23SLiming Wang "Raw hardware event descriptor", 195041bdcb23SLiming Wang "Hardware breakpoint", 195186470930SIngo Molnar }; 195286470930SIngo Molnar 1953ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1954ab0e4800SYunlong Song { 1955ab0e4800SYunlong Song const char * const *as = a; 1956ab0e4800SYunlong Song const char * const *bs = b; 1957ab0e4800SYunlong Song 1958ab0e4800SYunlong Song return strcmp(*as, *bs); 1959ab0e4800SYunlong Song } 1960ab0e4800SYunlong Song 196186470930SIngo Molnar /* 1962f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1963f6bdafefSJason Baron */ 1964f6bdafefSJason Baron 1965a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1966a3277d2dSFrederic Weisbecker bool name_only) 1967f6bdafefSJason Baron { 1968f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 196922a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1970f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1971725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1972ab0e4800SYunlong Song char **evt_list = NULL; 1973ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1974ab0e4800SYunlong Song bool evt_num_known = false; 1975f6bdafefSJason Baron 1976ab0e4800SYunlong Song restart: 1977ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1978f6bdafefSJason Baron if (!sys_dir) 1979725b1368SEric Dumazet return; 1980f6bdafefSJason Baron 1981ab0e4800SYunlong Song if (evt_num_known) { 1982ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1983ab0e4800SYunlong Song if (!evt_list) 1984ab0e4800SYunlong Song goto out_close_sys_dir; 1985ab0e4800SYunlong Song } 1986ab0e4800SYunlong Song 198722a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 1988668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 198922a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 1990668b8788SArnaldo Carvalho de Melo continue; 1991725b1368SEric Dumazet 1992ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 199322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 1994725b1368SEric Dumazet evt_dir = opendir(dir_path); 1995725b1368SEric Dumazet if (!evt_dir) 19966b58e7f1SUlrich Drepper continue; 1997725b1368SEric Dumazet 199822a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 1999668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 200022a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 2001668b8788SArnaldo Carvalho de Melo continue; 2002668b8788SArnaldo Carvalho de Melo 2003ab0e4800SYunlong Song if (!evt_num_known) { 2004ab0e4800SYunlong Song evt_num++; 2005a3277d2dSFrederic Weisbecker continue; 2006a3277d2dSFrederic Weisbecker } 2007a3277d2dSFrederic Weisbecker 2008f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 200922a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 2010ab0e4800SYunlong Song 2011ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 2012ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2013ab0e4800SYunlong Song goto out_close_evt_dir; 2014ab0e4800SYunlong Song evt_i++; 2015f6bdafefSJason Baron } 2016f6bdafefSJason Baron closedir(evt_dir); 2017f6bdafefSJason Baron } 2018f6bdafefSJason Baron closedir(sys_dir); 2019ab0e4800SYunlong Song 2020ab0e4800SYunlong Song if (!evt_num_known) { 2021ab0e4800SYunlong Song evt_num_known = true; 2022ab0e4800SYunlong Song goto restart; 2023ab0e4800SYunlong Song } 2024ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2025ab0e4800SYunlong Song evt_i = 0; 2026ab0e4800SYunlong Song while (evt_i < evt_num) { 2027ab0e4800SYunlong Song if (name_only) { 2028ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2029ab0e4800SYunlong Song continue; 2030ab0e4800SYunlong Song } 2031ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2032ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2033ab0e4800SYunlong Song } 2034dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2035ab0e4800SYunlong Song printf("\n"); 2036ab0e4800SYunlong Song 2037ab0e4800SYunlong Song out_free: 2038ab0e4800SYunlong Song evt_num = evt_i; 2039ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2040ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2041ab0e4800SYunlong Song zfree(&evt_list); 2042ab0e4800SYunlong Song return; 2043ab0e4800SYunlong Song 2044ab0e4800SYunlong Song out_close_evt_dir: 2045ab0e4800SYunlong Song closedir(evt_dir); 2046ab0e4800SYunlong Song out_close_sys_dir: 2047ab0e4800SYunlong Song closedir(sys_dir); 2048ab0e4800SYunlong Song 2049ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2050ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2051ab0e4800SYunlong Song if (evt_list) 2052ab0e4800SYunlong Song goto out_free; 2053f6bdafefSJason Baron } 2054f6bdafefSJason Baron 2055f6bdafefSJason Baron /* 205620c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 205720c457b8SThomas Renninger */ 205820c457b8SThomas Renninger 205920c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 206020c457b8SThomas Renninger { 206120c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 206222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 206320c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 206420c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 206520c457b8SThomas Renninger 2066ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 206720c457b8SThomas Renninger if (!sys_dir) 206820c457b8SThomas Renninger return 0; 206920c457b8SThomas Renninger 207022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 207120c457b8SThomas Renninger 2072ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 207322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 207420c457b8SThomas Renninger evt_dir = opendir(dir_path); 207520c457b8SThomas Renninger if (!evt_dir) 207620c457b8SThomas Renninger continue; 207720c457b8SThomas Renninger 207822a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 207920c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 208022a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 208120c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 208220c457b8SThomas Renninger closedir(evt_dir); 208320c457b8SThomas Renninger closedir(sys_dir); 208420c457b8SThomas Renninger return 1; 208520c457b8SThomas Renninger } 208620c457b8SThomas Renninger } 208720c457b8SThomas Renninger closedir(evt_dir); 208820c457b8SThomas Renninger } 208920c457b8SThomas Renninger closedir(sys_dir); 209020c457b8SThomas Renninger return 0; 209120c457b8SThomas Renninger } 209220c457b8SThomas Renninger 2093b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2094b41f1cecSNamhyung Kim { 2095b41f1cecSNamhyung Kim bool ret = true; 209688fee52eSVince Weaver int open_return; 2097b41f1cecSNamhyung Kim struct perf_evsel *evsel; 2098b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2099b41f1cecSNamhyung Kim .type = type, 2100b41f1cecSNamhyung Kim .config = config, 2101b41f1cecSNamhyung Kim .disabled = 1, 2102b41f1cecSNamhyung Kim }; 210389896051SArnaldo Carvalho de Melo struct thread_map *tmap = thread_map__new_by_tid(0); 210489896051SArnaldo Carvalho de Melo 210589896051SArnaldo Carvalho de Melo if (tmap == NULL) 210689896051SArnaldo Carvalho de Melo return false; 2107b41f1cecSNamhyung Kim 2108ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 2109b41f1cecSNamhyung Kim if (evsel) { 211089896051SArnaldo Carvalho de Melo open_return = perf_evsel__open(evsel, NULL, tmap); 211188fee52eSVince Weaver ret = open_return >= 0; 211288fee52eSVince Weaver 211388fee52eSVince Weaver if (open_return == -EACCES) { 211488fee52eSVince Weaver /* 211588fee52eSVince Weaver * This happens if the paranoid value 211688fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 211788fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 211888fee52eSVince Weaver * by default as some ARM machines do not support it. 211988fee52eSVince Weaver * 212088fee52eSVince Weaver */ 212188fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 212289896051SArnaldo Carvalho de Melo ret = perf_evsel__open(evsel, NULL, tmap) >= 0; 212388fee52eSVince Weaver } 2124b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 2125b41f1cecSNamhyung Kim } 2126b41f1cecSNamhyung Kim 2127b41f1cecSNamhyung Kim return ret; 2128b41f1cecSNamhyung Kim } 2129b41f1cecSNamhyung Kim 213040218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 213140218daeSMasami Hiramatsu bool name_only) 213240218daeSMasami Hiramatsu { 213340218daeSMasami Hiramatsu struct probe_cache *pcache; 213440218daeSMasami Hiramatsu struct probe_cache_entry *ent; 213540218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 213640218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 213740218daeSMasami Hiramatsu struct str_node *nd, *nd2; 213840218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 213940218daeSMasami Hiramatsu bool show_detail = false; 214040218daeSMasami Hiramatsu int ret; 214140218daeSMasami Hiramatsu 214240218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 214340218daeSMasami Hiramatsu if (!sdtlist) { 214440218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 214540218daeSMasami Hiramatsu return; 214640218daeSMasami Hiramatsu } 214740218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 214840218daeSMasami Hiramatsu if (!bidlist) { 214940218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 215040218daeSMasami Hiramatsu return; 215140218daeSMasami Hiramatsu } 215240218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2153f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 215440218daeSMasami Hiramatsu if (!pcache) 215540218daeSMasami Hiramatsu continue; 215640218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 215740218daeSMasami Hiramatsu if (!ent->sdt) 215840218daeSMasami Hiramatsu continue; 215940218daeSMasami Hiramatsu if (subsys_glob && 216040218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 216140218daeSMasami Hiramatsu continue; 216240218daeSMasami Hiramatsu if (event_glob && 216340218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 216440218daeSMasami Hiramatsu continue; 216540218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 216640218daeSMasami Hiramatsu ent->pev.event, nd->s); 216740218daeSMasami Hiramatsu if (ret > 0) 216840218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 216940218daeSMasami Hiramatsu } 217040218daeSMasami Hiramatsu probe_cache__delete(pcache); 217140218daeSMasami Hiramatsu } 217240218daeSMasami Hiramatsu strlist__delete(bidlist); 217340218daeSMasami Hiramatsu 217440218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 217540218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 217640218daeSMasami Hiramatsu if (buf) 217740218daeSMasami Hiramatsu *(buf++) = '\0'; 217840218daeSMasami Hiramatsu if (name_only) { 217940218daeSMasami Hiramatsu printf("%s ", nd->s); 218040218daeSMasami Hiramatsu continue; 218140218daeSMasami Hiramatsu } 218240218daeSMasami Hiramatsu nd2 = strlist__next(nd); 218340218daeSMasami Hiramatsu if (nd2) { 218440218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 218540218daeSMasami Hiramatsu if (ptr) 218640218daeSMasami Hiramatsu *ptr = '\0'; 218740218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 218840218daeSMasami Hiramatsu show_detail = true; 218940218daeSMasami Hiramatsu } 219040218daeSMasami Hiramatsu if (show_detail) { 219140218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 219240218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 219340218daeSMasami Hiramatsu if (ret > 0) { 219440218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 219540218daeSMasami Hiramatsu free(buf); 219640218daeSMasami Hiramatsu } 219740218daeSMasami Hiramatsu } else 219840218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 219940218daeSMasami Hiramatsu if (nd2) { 220040218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 220140218daeSMasami Hiramatsu show_detail = false; 220240218daeSMasami Hiramatsu if (ptr) 220340218daeSMasami Hiramatsu *ptr = '@'; 220440218daeSMasami Hiramatsu } 220540218daeSMasami Hiramatsu } 220640218daeSMasami Hiramatsu strlist__delete(sdtlist); 220740218daeSMasami Hiramatsu } 220840218daeSMasami Hiramatsu 2209a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2210668b8788SArnaldo Carvalho de Melo { 2211ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 22120b668bc9SArnaldo Carvalho de Melo char name[64]; 2213ab0e4800SYunlong Song char **evt_list = NULL; 2214ab0e4800SYunlong Song bool evt_num_known = false; 2215ab0e4800SYunlong Song 2216ab0e4800SYunlong Song restart: 2217ab0e4800SYunlong Song if (evt_num_known) { 2218ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2219ab0e4800SYunlong Song if (!evt_list) 2220ab0e4800SYunlong Song goto out_enomem; 2221ab0e4800SYunlong Song } 2222668b8788SArnaldo Carvalho de Melo 2223668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2224668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2225668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 22260b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2227668b8788SArnaldo Carvalho de Melo continue; 2228668b8788SArnaldo Carvalho de Melo 2229668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 22300b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 22310b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 2232947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2233668b8788SArnaldo Carvalho de Melo continue; 2234668b8788SArnaldo Carvalho de Melo 2235b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2236b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2237b41f1cecSNamhyung Kim continue; 2238b41f1cecSNamhyung Kim 2239ab0e4800SYunlong Song if (!evt_num_known) { 2240ab0e4800SYunlong Song evt_num++; 2241ab0e4800SYunlong Song continue; 2242ab0e4800SYunlong Song } 2243ab0e4800SYunlong Song 2244ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2245ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2246ab0e4800SYunlong Song goto out_enomem; 2247ab0e4800SYunlong Song evt_i++; 2248668b8788SArnaldo Carvalho de Melo } 2249668b8788SArnaldo Carvalho de Melo } 2250668b8788SArnaldo Carvalho de Melo } 2251668b8788SArnaldo Carvalho de Melo 2252ab0e4800SYunlong Song if (!evt_num_known) { 2253ab0e4800SYunlong Song evt_num_known = true; 2254ab0e4800SYunlong Song goto restart; 2255ab0e4800SYunlong Song } 2256ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2257ab0e4800SYunlong Song evt_i = 0; 2258ab0e4800SYunlong Song while (evt_i < evt_num) { 2259ab0e4800SYunlong Song if (name_only) { 2260ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2261ab0e4800SYunlong Song continue; 2262ab0e4800SYunlong Song } 2263ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2264ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2265ab0e4800SYunlong Song } 2266dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2267dc098b35SAndi Kleen printf("\n"); 2268ab0e4800SYunlong Song 2269ab0e4800SYunlong Song out_free: 2270ab0e4800SYunlong Song evt_num = evt_i; 2271ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2272ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2273ab0e4800SYunlong Song zfree(&evt_list); 2274ab0e4800SYunlong Song return evt_num; 2275ab0e4800SYunlong Song 2276ab0e4800SYunlong Song out_enomem: 2277ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2278ab0e4800SYunlong Song if (evt_list) 2279ab0e4800SYunlong Song goto out_free; 2280ab0e4800SYunlong Song return evt_num; 2281668b8788SArnaldo Carvalho de Melo } 2282668b8788SArnaldo Carvalho de Melo 2283705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2284a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2285a3277d2dSFrederic Weisbecker bool name_only) 228686470930SIngo Molnar { 2287ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2288947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2289ab0e4800SYunlong Song char **evt_list = NULL; 2290ab0e4800SYunlong Song bool evt_num_known = false; 2291ab0e4800SYunlong Song 2292ab0e4800SYunlong Song restart: 2293ab0e4800SYunlong Song if (evt_num_known) { 2294ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2295ab0e4800SYunlong Song if (!evt_list) 2296ab0e4800SYunlong Song goto out_enomem; 2297ab0e4800SYunlong Song syms -= max; 2298ab0e4800SYunlong Song } 229986470930SIngo Molnar 23001dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2301668b8788SArnaldo Carvalho de Melo 2302e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2303668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2304668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2305668b8788SArnaldo Carvalho de Melo continue; 230686470930SIngo Molnar 2307b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2308b41f1cecSNamhyung Kim continue; 2309b41f1cecSNamhyung Kim 2310ab0e4800SYunlong Song if (!evt_num_known) { 2311ab0e4800SYunlong Song evt_num++; 2312a3277d2dSFrederic Weisbecker continue; 2313a3277d2dSFrederic Weisbecker } 2314a3277d2dSFrederic Weisbecker 2315ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2316947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 231774d5b588SJaswinder Singh Rajput else 2318947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 231986470930SIngo Molnar 2320ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2321ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2322ab0e4800SYunlong Song goto out_enomem; 2323ab0e4800SYunlong Song evt_i++; 232486470930SIngo Molnar } 232586470930SIngo Molnar 2326ab0e4800SYunlong Song if (!evt_num_known) { 2327ab0e4800SYunlong Song evt_num_known = true; 2328ab0e4800SYunlong Song goto restart; 2329ab0e4800SYunlong Song } 2330ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2331ab0e4800SYunlong Song evt_i = 0; 2332ab0e4800SYunlong Song while (evt_i < evt_num) { 2333ab0e4800SYunlong Song if (name_only) { 2334ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2335ab0e4800SYunlong Song continue; 2336ab0e4800SYunlong Song } 2337ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2338ab0e4800SYunlong Song } 2339dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2340689d3018SMarti Raudsepp printf("\n"); 2341ab0e4800SYunlong Song 2342ab0e4800SYunlong Song out_free: 2343ab0e4800SYunlong Song evt_num = evt_i; 2344ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2345ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2346ab0e4800SYunlong Song zfree(&evt_list); 2347ab0e4800SYunlong Song return; 2348ab0e4800SYunlong Song 2349ab0e4800SYunlong Song out_enomem: 2350ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2351ab0e4800SYunlong Song if (evt_list) 2352ab0e4800SYunlong Song goto out_free; 2353668b8788SArnaldo Carvalho de Melo } 23541dc12760SJiri Olsa 23551dc12760SJiri Olsa /* 23561dc12760SJiri Olsa * Print the help text for the event symbols: 23571dc12760SJiri Olsa */ 2358c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2359bf874fcfSAndi Kleen bool long_desc, bool details_flag) 23601dc12760SJiri Olsa { 23611dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2362a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 23631dc12760SJiri Olsa 23641dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2365a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 23661dc12760SJiri Olsa 2367a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 236873c24cb8SJaswinder Singh Rajput 2369bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 2370bf874fcfSAndi Kleen details_flag); 2371dc098b35SAndi Kleen 2372668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2373668b8788SArnaldo Carvalho de Melo return; 237473c24cb8SJaswinder Singh Rajput 2375a3277d2dSFrederic Weisbecker if (!name_only) { 2376947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23775f537a26SJiri Olsa "rNNN", 23781cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 23795f537a26SJiri Olsa printf(" %-50s [%s]\n", 23805f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 23815f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2382dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2383dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 238486470930SIngo Molnar 2385947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23863741eb9fSJacob Shin "mem:<addr>[/len][:access]", 238741bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2388dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 23891b290d67SFrederic Weisbecker printf("\n"); 2390a3277d2dSFrederic Weisbecker } 23911b290d67SFrederic Weisbecker 2392a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 239340218daeSMasami Hiramatsu 239440218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 239571b0acceSAndi Kleen 239671b0acceSAndi Kleen metricgroup__print(true, true, NULL, name_only); 239786470930SIngo Molnar } 23988f707d84SJiri Olsa 23996cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 24008f707d84SJiri Olsa { 240116fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 24028f707d84SJiri Olsa } 24038f707d84SJiri Olsa 240467b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 240567b49b38SJiri Olsa struct parse_events_term *temp, 240667b49b38SJiri Olsa char *str, u64 num) 24078f707d84SJiri Olsa { 24086cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 24098f707d84SJiri Olsa 241067b49b38SJiri Olsa term = malloc(sizeof(*term)); 24118f707d84SJiri Olsa if (!term) 24128f707d84SJiri Olsa return -ENOMEM; 24138f707d84SJiri Olsa 241467b49b38SJiri Olsa *term = *temp; 24158f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2416c2f1ceadSAndi Kleen term->weak = false; 24178f707d84SJiri Olsa 241867b49b38SJiri Olsa switch (term->type_val) { 24198f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 24208f707d84SJiri Olsa term->val.num = num; 24218f707d84SJiri Olsa break; 24228f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 24238f707d84SJiri Olsa term->val.str = str; 24248f707d84SJiri Olsa break; 24258f707d84SJiri Olsa default: 24264be8be6bSAdrian Hunter free(term); 24278f707d84SJiri Olsa return -EINVAL; 24288f707d84SJiri Olsa } 24298f707d84SJiri Olsa 24308f707d84SJiri Olsa *_term = term; 24318f707d84SJiri Olsa return 0; 24328f707d84SJiri Olsa } 24338f707d84SJiri Olsa 24346cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2435cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 243699e7138eSJiri Olsa bool no_value, 2437bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 243816fa7e82SJiri Olsa { 2439bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2440bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2441bb78ce7dSAdrian Hunter 244267b49b38SJiri Olsa struct parse_events_term temp = { 244367b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 244467b49b38SJiri Olsa .type_term = type_term, 244567b49b38SJiri Olsa .config = config, 244699e7138eSJiri Olsa .no_value = no_value, 244767b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 244867b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 244967b49b38SJiri Olsa }; 245067b49b38SJiri Olsa 245167b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 245216fa7e82SJiri Olsa } 245316fa7e82SJiri Olsa 24546cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2455cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2456bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 245716fa7e82SJiri Olsa { 2458bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2459bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2460bb78ce7dSAdrian Hunter 246167b49b38SJiri Olsa struct parse_events_term temp = { 246267b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 246367b49b38SJiri Olsa .type_term = type_term, 246467b49b38SJiri Olsa .config = config, 246567b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 246667b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 246767b49b38SJiri Olsa }; 246867b49b38SJiri Olsa 246967b49b38SJiri Olsa return new_term(term, &temp, str, 0); 247016fa7e82SJiri Olsa } 247116fa7e82SJiri Olsa 24726cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 24731d33d6dcSJiri Olsa char *config, unsigned idx) 24741d33d6dcSJiri Olsa { 24751d33d6dcSJiri Olsa struct event_symbol *sym; 247667b49b38SJiri Olsa struct parse_events_term temp = { 247767b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 247867b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 247967b49b38SJiri Olsa .config = config ?: (char *) "event", 248067b49b38SJiri Olsa }; 24811d33d6dcSJiri Olsa 24821d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 24831d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 24841d33d6dcSJiri Olsa 248567b49b38SJiri Olsa return new_term(term, &temp, (char *) sym->symbol, 0); 24861d33d6dcSJiri Olsa } 24871d33d6dcSJiri Olsa 24886cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 24896cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2490a6146d50SZheng Yan { 249167b49b38SJiri Olsa struct parse_events_term temp = { 249267b49b38SJiri Olsa .type_val = term->type_val, 249367b49b38SJiri Olsa .type_term = term->type_term, 249467b49b38SJiri Olsa .config = term->config, 249567b49b38SJiri Olsa .err_term = term->err_term, 249667b49b38SJiri Olsa .err_val = term->err_val, 249767b49b38SJiri Olsa }; 249867b49b38SJiri Olsa 249967b49b38SJiri Olsa return new_term(new, &temp, term->val.str, term->val.num); 2500a6146d50SZheng Yan } 2501a6146d50SZheng Yan 25028255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 25038255718fSAndi Kleen struct list_head **new) 25048255718fSAndi Kleen { 25058255718fSAndi Kleen struct parse_events_term *term, *n; 25068255718fSAndi Kleen int ret; 25078255718fSAndi Kleen 25088255718fSAndi Kleen if (!old) { 25098255718fSAndi Kleen *new = NULL; 25108255718fSAndi Kleen return 0; 25118255718fSAndi Kleen } 25128255718fSAndi Kleen 25138255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 25148255718fSAndi Kleen if (!*new) 25158255718fSAndi Kleen return -ENOMEM; 25168255718fSAndi Kleen INIT_LIST_HEAD(*new); 25178255718fSAndi Kleen 25188255718fSAndi Kleen list_for_each_entry (term, old, list) { 25198255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 25208255718fSAndi Kleen if (ret) 25218255718fSAndi Kleen return ret; 25228255718fSAndi Kleen list_add_tail(&n->list, *new); 25238255718fSAndi Kleen } 25248255718fSAndi Kleen return 0; 25258255718fSAndi Kleen } 25268255718fSAndi Kleen 2527fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 25288f707d84SJiri Olsa { 25296cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 25308f707d84SJiri Olsa 2531a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 25322d055bf2SWang Nan if (term->array.nr_ranges) 2533360e071bSTaeung Song zfree(&term->array.ranges); 2534a8adfcebSWang Nan list_del_init(&term->list); 25358f707d84SJiri Olsa free(term); 25368f707d84SJiri Olsa } 2537a8adfcebSWang Nan } 2538b39b8393SJiri Olsa 25392146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2540fc0a2c1dSArnaldo Carvalho de Melo { 25412146afc6SArnaldo Carvalho de Melo if (!terms) 25422146afc6SArnaldo Carvalho de Melo return; 2543fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2544d20a5f2bSWang Nan free(terms); 2545fc0a2c1dSArnaldo Carvalho de Melo } 2546fc0a2c1dSArnaldo Carvalho de Melo 25472d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 25482d055bf2SWang Nan { 2549360e071bSTaeung Song zfree(&a->ranges); 25502d055bf2SWang Nan } 25512d055bf2SWang Nan 25525d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2553b39b8393SJiri Olsa int idx, const char *str) 2554b39b8393SJiri Olsa { 25555d9cdc11SArnaldo Carvalho de Melo struct parse_events_error *err = parse_state->error; 2556b39b8393SJiri Olsa 2557a6ced2beSAdrian Hunter if (!err) 2558a6ced2beSAdrian Hunter return; 2559b39b8393SJiri Olsa err->idx = idx; 2560b39b8393SJiri Olsa err->str = strdup(str); 2561b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 2562b39b8393SJiri Olsa } 2563ffeb883eSHe Kuang 256417cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 256517cb5f84SWang Nan { 256617cb5f84SWang Nan int i; 256717cb5f84SWang Nan bool first = true; 256817cb5f84SWang Nan 256917cb5f84SWang Nan buf[0] = '\0'; 257017cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 257117cb5f84SWang Nan const char *name = config_term_names[i]; 257217cb5f84SWang Nan 25731669e509SWang Nan if (!config_term_avail(i, NULL)) 25741669e509SWang Nan continue; 257517cb5f84SWang Nan if (!name) 257617cb5f84SWang Nan continue; 257717cb5f84SWang Nan if (name[0] == '<') 257817cb5f84SWang Nan continue; 257917cb5f84SWang Nan 258017cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 258117cb5f84SWang Nan return; 258217cb5f84SWang Nan 258317cb5f84SWang Nan if (!first) 258417cb5f84SWang Nan strcat(buf, ","); 258517cb5f84SWang Nan else 258617cb5f84SWang Nan first = false; 258717cb5f84SWang Nan strcat(buf, name); 258817cb5f84SWang Nan } 258917cb5f84SWang Nan } 259017cb5f84SWang Nan 2591ffeb883eSHe Kuang /* 2592ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2593ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2594ffeb883eSHe Kuang */ 2595ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2596ffeb883eSHe Kuang { 2597ffeb883eSHe Kuang char *str; 2598626a6b78SWang Nan /* "no-overwrite" is the longest name */ 259917cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2600626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2601ffeb883eSHe Kuang 260217cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2603ffeb883eSHe Kuang /* valid terms */ 2604ffeb883eSHe Kuang if (additional_terms) { 260526dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 260626dee028SWang Nan additional_terms, static_terms) < 0) 2607ffeb883eSHe Kuang goto fail; 2608ffeb883eSHe Kuang } else { 260926dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2610ffeb883eSHe Kuang goto fail; 2611ffeb883eSHe Kuang } 2612ffeb883eSHe Kuang return str; 2613ffeb883eSHe Kuang 2614ffeb883eSHe Kuang fail: 2615ffeb883eSHe Kuang return NULL; 2616ffeb883eSHe Kuang } 2617