1d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 28dd2a131SJiri Olsa #include <linux/err.h> 386470930SIngo Molnar #include "util.h" 46b58e7f1SUlrich Drepper #include "../perf.h" 5361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 669aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 74b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 886470930SIngo Molnar #include "parse-events.h" 94b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 1042f60c2dSKan Liang #include "string.h" 115aab621bSArnaldo Carvalho de Melo #include "symbol.h" 125beeded1SJason Baron #include "cache.h" 138755a8f2SArjan van de Ven #include "header.h" 1484c86ca1SWang Nan #include "bpf-loader.h" 156e81c74cSMasami Hiramatsu #include "debug.h" 16592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 17ac20de6fSZheng Yan #include "parse-events-bison.h" 1890e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 1989812fc8SJiri Olsa #include "parse-events-flex.h" 205f537a26SJiri Olsa #include "pmu.h" 21b41f1cecSNamhyung Kim #include "thread_map.h" 22f30a79b0SJiri Olsa #include "cpumap.h" 2340218daeSMasami Hiramatsu #include "probe-file.h" 24b39b8393SJiri Olsa #include "asm/bug.h" 25ac12f676SAndi Kleen #include "util/parse-branch-options.h" 2689812fc8SJiri Olsa 2789812fc8SJiri Olsa #define MAX_NAME_LEN 100 2886470930SIngo Molnar 2982ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 3082ba1f2fSJiri Olsa extern int parse_events_debug; 3182ba1f2fSJiri Olsa #endif 32ac20de6fSZheng Yan int parse_events_parse(void *data, void *scanner); 33e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 34e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 35bcd3279fSFrederic Weisbecker 36dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 37dcb4e102SKan Liang /* 38dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 39dcb4e102SKan Liang * 0 means not initialized and ready to init 40dcb4e102SKan Liang * -1 means failed to init, don't try anymore 41dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 42dcb4e102SKan Liang */ 43dcb4e102SKan Liang static int perf_pmu_events_list_num; 44dcb4e102SKan Liang 45705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 461dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 471dc12760SJiri Olsa .symbol = "cpu-cycles", 481dc12760SJiri Olsa .alias = "cycles", 491dc12760SJiri Olsa }, 501dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 511dc12760SJiri Olsa .symbol = "instructions", 521dc12760SJiri Olsa .alias = "", 531dc12760SJiri Olsa }, 541dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 551dc12760SJiri Olsa .symbol = "cache-references", 561dc12760SJiri Olsa .alias = "", 571dc12760SJiri Olsa }, 581dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 591dc12760SJiri Olsa .symbol = "cache-misses", 601dc12760SJiri Olsa .alias = "", 611dc12760SJiri Olsa }, 621dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 631dc12760SJiri Olsa .symbol = "branch-instructions", 641dc12760SJiri Olsa .alias = "branches", 651dc12760SJiri Olsa }, 661dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 671dc12760SJiri Olsa .symbol = "branch-misses", 681dc12760SJiri Olsa .alias = "", 691dc12760SJiri Olsa }, 701dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 711dc12760SJiri Olsa .symbol = "bus-cycles", 721dc12760SJiri Olsa .alias = "", 731dc12760SJiri Olsa }, 741dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 751dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 761dc12760SJiri Olsa .alias = "idle-cycles-frontend", 771dc12760SJiri Olsa }, 781dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 791dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 801dc12760SJiri Olsa .alias = "idle-cycles-backend", 811dc12760SJiri Olsa }, 821dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 831dc12760SJiri Olsa .symbol = "ref-cycles", 841dc12760SJiri Olsa .alias = "", 851dc12760SJiri Olsa }, 861dc12760SJiri Olsa }; 8786470930SIngo Molnar 88705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 891dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 901dc12760SJiri Olsa .symbol = "cpu-clock", 911dc12760SJiri Olsa .alias = "", 921dc12760SJiri Olsa }, 931dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 941dc12760SJiri Olsa .symbol = "task-clock", 951dc12760SJiri Olsa .alias = "", 961dc12760SJiri Olsa }, 971dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 981dc12760SJiri Olsa .symbol = "page-faults", 991dc12760SJiri Olsa .alias = "faults", 1001dc12760SJiri Olsa }, 1011dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1021dc12760SJiri Olsa .symbol = "context-switches", 1031dc12760SJiri Olsa .alias = "cs", 1041dc12760SJiri Olsa }, 1051dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1061dc12760SJiri Olsa .symbol = "cpu-migrations", 1071dc12760SJiri Olsa .alias = "migrations", 1081dc12760SJiri Olsa }, 1091dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1101dc12760SJiri Olsa .symbol = "minor-faults", 1111dc12760SJiri Olsa .alias = "", 1121dc12760SJiri Olsa }, 1131dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1141dc12760SJiri Olsa .symbol = "major-faults", 1151dc12760SJiri Olsa .alias = "", 1161dc12760SJiri Olsa }, 1171dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1181dc12760SJiri Olsa .symbol = "alignment-faults", 1191dc12760SJiri Olsa .alias = "", 1201dc12760SJiri Olsa }, 1211dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1221dc12760SJiri Olsa .symbol = "emulation-faults", 1231dc12760SJiri Olsa .alias = "", 1241dc12760SJiri Olsa }, 125d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 126d22d1a2aSAdrian Hunter .symbol = "dummy", 127d22d1a2aSAdrian Hunter .alias = "", 128d22d1a2aSAdrian Hunter }, 129bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 130bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 131bae9cc41SArnaldo Carvalho de Melo .alias = "", 132bae9cc41SArnaldo Carvalho de Melo }, 13386470930SIngo Molnar }; 13486470930SIngo Molnar 135cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 136cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 13786470930SIngo Molnar 138cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 139cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 140cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 141cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 14286470930SIngo Molnar 14322a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 14422a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 14522a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 14622a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 14722a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 148f6bdafefSJason Baron 149ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 150ae07b63fSPeter Zijlstra { 151ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 152ae07b63fSPeter Zijlstra int fd; 153ae07b63fSPeter Zijlstra 154ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 155ae07b63fSPeter Zijlstra sys_dir->d_name, evt_dir->d_name); 156ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 157ae07b63fSPeter Zijlstra if (fd < 0) 158ae07b63fSPeter Zijlstra return -EINVAL; 159ae07b63fSPeter Zijlstra close(fd); 160ae07b63fSPeter Zijlstra 161ae07b63fSPeter Zijlstra return 0; 162ae07b63fSPeter Zijlstra } 163ae07b63fSPeter Zijlstra 16422a9f41bSArnaldo Carvalho de Melo #define for_each_event(sys_dirent, evt_dir, evt_dirent) \ 16522a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 16622a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 16722a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 16822a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 16922a9f41bSArnaldo Carvalho de Melo (!tp_event_has_id(sys_dirent, evt_dirent))) 170f6bdafefSJason Baron 171270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 172f6bdafefSJason Baron 173f6bdafefSJason Baron 1741ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 175f6bdafefSJason Baron { 1761ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 177f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 17822a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1798aa8a7c8SStephane Eranian char id_buf[24]; 180725b1368SEric Dumazet int fd; 181f6bdafefSJason Baron u64 id; 182f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 183725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 184f6bdafefSJason Baron 185ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 186f6bdafefSJason Baron if (!sys_dir) 187725b1368SEric Dumazet return NULL; 188f6bdafefSJason Baron 18922a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 190725b1368SEric Dumazet 191ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 19222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 193725b1368SEric Dumazet evt_dir = opendir(dir_path); 194725b1368SEric Dumazet if (!evt_dir) 1956b58e7f1SUlrich Drepper continue; 196725b1368SEric Dumazet 19722a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 198725b1368SEric Dumazet 199725b1368SEric Dumazet snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 20022a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 201725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 202f6bdafefSJason Baron if (fd < 0) 203f6bdafefSJason Baron continue; 204f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 205f6bdafefSJason Baron close(fd); 206f6bdafefSJason Baron continue; 207f6bdafefSJason Baron } 208f6bdafefSJason Baron close(fd); 209f6bdafefSJason Baron id = atoll(id_buf); 210f6bdafefSJason Baron if (id == config) { 211f6bdafefSJason Baron closedir(evt_dir); 212f6bdafefSJason Baron closedir(sys_dir); 21359b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2145aa365f2STaeung Song if (!path) 2155aa365f2STaeung Song return NULL; 2161ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2171ef2ed10SFrederic Weisbecker if (!path->system) { 2181ef2ed10SFrederic Weisbecker free(path); 2191ef2ed10SFrederic Weisbecker return NULL; 2201ef2ed10SFrederic Weisbecker } 2211ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2221ef2ed10SFrederic Weisbecker if (!path->name) { 22374cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2241ef2ed10SFrederic Weisbecker free(path); 2251ef2ed10SFrederic Weisbecker return NULL; 2261ef2ed10SFrederic Weisbecker } 22722a9f41bSArnaldo Carvalho de Melo strncpy(path->system, sys_dirent->d_name, 2281ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 22922a9f41bSArnaldo Carvalho de Melo strncpy(path->name, evt_dirent->d_name, 2301ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2311ef2ed10SFrederic Weisbecker return path; 232f6bdafefSJason Baron } 233f6bdafefSJason Baron } 234f6bdafefSJason Baron closedir(evt_dir); 235f6bdafefSJason Baron } 236f6bdafefSJason Baron 237f6bdafefSJason Baron closedir(sys_dir); 2381ef2ed10SFrederic Weisbecker return NULL; 2391ef2ed10SFrederic Weisbecker } 2401ef2ed10SFrederic Weisbecker 241e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 242e7c93f09SNamhyung Kim { 243e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 244e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 245e7c93f09SNamhyung Kim 246e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 247e7c93f09SNamhyung Kim free(path); 248e7c93f09SNamhyung Kim return NULL; 249e7c93f09SNamhyung Kim } 250e7c93f09SNamhyung Kim 251e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 252e7c93f09SNamhyung Kim path->name = strdup(str+1); 253e7c93f09SNamhyung Kim 254e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 25574cf249dSArnaldo Carvalho de Melo zfree(&path->system); 25674cf249dSArnaldo Carvalho de Melo zfree(&path->name); 257506fde11STaeung Song zfree(&path); 258e7c93f09SNamhyung Kim } 259e7c93f09SNamhyung Kim 260e7c93f09SNamhyung Kim return path; 261e7c93f09SNamhyung Kim } 262e7c93f09SNamhyung Kim 2631424dc96SDavid Ahern const char *event_type(int type) 2641424dc96SDavid Ahern { 2651424dc96SDavid Ahern switch (type) { 2661424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2671424dc96SDavid Ahern return "hardware"; 2681424dc96SDavid Ahern 2691424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2701424dc96SDavid Ahern return "software"; 2711424dc96SDavid Ahern 2721424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2731424dc96SDavid Ahern return "tracepoint"; 2741424dc96SDavid Ahern 2751424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2761424dc96SDavid Ahern return "hardware-cache"; 2771424dc96SDavid Ahern 2781424dc96SDavid Ahern default: 2791424dc96SDavid Ahern break; 2801424dc96SDavid Ahern } 2811424dc96SDavid Ahern 2821424dc96SDavid Ahern return "unknown"; 2831424dc96SDavid Ahern } 2841424dc96SDavid Ahern 285e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 286e814fdddSWang Nan { 287e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 288e814fdddSWang Nan } 2897ae92e74SYan, Zheng 290e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 291e814fdddSWang Nan { 292e814fdddSWang Nan struct parse_events_term *term; 293e814fdddSWang Nan 294e814fdddSWang Nan if (!head_terms) 295e814fdddSWang Nan return NULL; 296e814fdddSWang Nan 297e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 298e814fdddSWang Nan if (parse_events__is_name_term(term)) 299e814fdddSWang Nan return term->val.str; 300e814fdddSWang Nan 301e814fdddSWang Nan return NULL; 302e814fdddSWang Nan } 3037ae92e74SYan, Zheng 304410136f5SStephane Eranian static struct perf_evsel * 305410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3067ae92e74SYan, Zheng struct perf_event_attr *attr, 307930a2e29SJiri Olsa char *name, struct cpu_map *cpus, 308930a2e29SJiri Olsa struct list_head *config_terms) 30989812fc8SJiri Olsa { 31089812fc8SJiri Olsa struct perf_evsel *evsel; 31189812fc8SJiri Olsa 31289812fc8SJiri Olsa event_attr_init(attr); 31389812fc8SJiri Olsa 31475fc5ae5STaeung Song evsel = perf_evsel__new_idx(attr, *idx); 315c5cd8ac0SDavid Ahern if (!evsel) 316410136f5SStephane Eranian return NULL; 31789812fc8SJiri Olsa 31875fc5ae5STaeung Song (*idx)++; 319f30a79b0SJiri Olsa evsel->cpus = cpu_map__get(cpus); 320fce4d296SAdrian Hunter evsel->own_cpus = cpu_map__get(cpus); 321e3ba76deSJiri Olsa evsel->system_wide = !!cpus; 322f30a79b0SJiri Olsa 3239db1763cSArnaldo Carvalho de Melo if (name) 32489812fc8SJiri Olsa evsel->name = strdup(name); 325930a2e29SJiri Olsa 326930a2e29SJiri Olsa if (config_terms) 327930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 328930a2e29SJiri Olsa 329b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 330410136f5SStephane Eranian return evsel; 33189812fc8SJiri Olsa } 33289812fc8SJiri Olsa 333c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 334930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 335930a2e29SJiri Olsa struct list_head *config_terms) 3367ae92e74SYan, Zheng { 337930a2e29SJiri Olsa return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; 3387ae92e74SYan, Zheng } 3397ae92e74SYan, Zheng 3400b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 34186470930SIngo Molnar { 34286470930SIngo Molnar int i, j; 34361c45981SPaul Mackerras int n, longest = -1; 34486470930SIngo Molnar 34586470930SIngo Molnar for (i = 0; i < size; i++) { 3460b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 34761c45981SPaul Mackerras n = strlen(names[i][j]); 34889812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 34961c45981SPaul Mackerras longest = n; 35061c45981SPaul Mackerras } 35189812fc8SJiri Olsa if (longest > 0) 35286470930SIngo Molnar return i; 35386470930SIngo Molnar } 35486470930SIngo Molnar 3558953645fSIngo Molnar return -1; 35686470930SIngo Molnar } 35786470930SIngo Molnar 35843d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 35943d0b978SWang Nan struct parse_events_term *term, 36043d0b978SWang Nan struct parse_events_error *err); 36143d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 36243d0b978SWang Nan struct parse_events_term *term, 36343d0b978SWang Nan struct parse_events_error *err); 36443d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 36543d0b978SWang Nan struct list_head *head, 36643d0b978SWang Nan struct parse_events_error *err, 36743d0b978SWang Nan config_term_func_t config_term); 36843d0b978SWang Nan 369c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 37043d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3712c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 37243d0b978SWang Nan struct list_head *head_config) 37386470930SIngo Molnar { 37489812fc8SJiri Olsa struct perf_event_attr attr; 37543d0b978SWang Nan LIST_HEAD(config_terms); 37643d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 37761c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 37889812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 37989812fc8SJiri Olsa int i, n; 38086470930SIngo Molnar 38186470930SIngo Molnar /* 38286470930SIngo Molnar * No fallback - if we cannot get a clear cache type 38386470930SIngo Molnar * then bail out: 38486470930SIngo Molnar */ 3850b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 38689812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 38786470930SIngo Molnar if (cache_type == -1) 38889812fc8SJiri Olsa return -EINVAL; 38986470930SIngo Molnar 39043d0b978SWang Nan config_name = get_config_name(head_config); 39189812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 39289812fc8SJiri Olsa 39389812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 39489812fc8SJiri Olsa char *str = op_result[i]; 39589812fc8SJiri Olsa 396275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 39761c45981SPaul Mackerras 39861c45981SPaul Mackerras if (cache_op == -1) { 3990b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 40061c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 40161c45981SPaul Mackerras if (cache_op >= 0) { 4020b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 40389812fc8SJiri Olsa return -EINVAL; 40461c45981SPaul Mackerras continue; 40561c45981SPaul Mackerras } 40661c45981SPaul Mackerras } 40761c45981SPaul Mackerras 40861c45981SPaul Mackerras if (cache_result == -1) { 4090b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 41061c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 41161c45981SPaul Mackerras if (cache_result >= 0) 41261c45981SPaul Mackerras continue; 41361c45981SPaul Mackerras } 41461c45981SPaul Mackerras } 41561c45981SPaul Mackerras 41686470930SIngo Molnar /* 41786470930SIngo Molnar * Fall back to reads: 41886470930SIngo Molnar */ 4198953645fSIngo Molnar if (cache_op == -1) 4208953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 42186470930SIngo Molnar 42286470930SIngo Molnar /* 42386470930SIngo Molnar * Fall back to accesses: 42486470930SIngo Molnar */ 42586470930SIngo Molnar if (cache_result == -1) 42686470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 42786470930SIngo Molnar 42889812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 42989812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 43089812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 43143d0b978SWang Nan 43243d0b978SWang Nan if (head_config) { 4332c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 43443d0b978SWang Nan config_term_common)) 43543d0b978SWang Nan return -EINVAL; 43643d0b978SWang Nan 43743d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 43843d0b978SWang Nan return -ENOMEM; 43943d0b978SWang Nan } 44043d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 44186470930SIngo Molnar } 44286470930SIngo Molnar 443272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4448c619d6aSWang Nan const char *sys, const char *name) 44519658171SJiri Olsa { 44619658171SJiri Olsa char help[BUFSIZ]; 44719658171SJiri Olsa 448ec183d22SAdrian Hunter if (!e) 449ec183d22SAdrian Hunter return; 450ec183d22SAdrian Hunter 45119658171SJiri Olsa /* 45219658171SJiri Olsa * We get error directly from syscall errno ( > 0), 45319658171SJiri Olsa * or from encoded pointer's error ( < 0). 45419658171SJiri Olsa */ 45519658171SJiri Olsa err = abs(err); 45619658171SJiri Olsa 45719658171SJiri Olsa switch (err) { 45819658171SJiri Olsa case EACCES: 459272ed29aSJiri Olsa e->str = strdup("can't access trace events"); 46019658171SJiri Olsa break; 46119658171SJiri Olsa case ENOENT: 462272ed29aSJiri Olsa e->str = strdup("unknown tracepoint"); 46319658171SJiri Olsa break; 46419658171SJiri Olsa default: 465272ed29aSJiri Olsa e->str = strdup("failed to add tracepoint"); 46619658171SJiri Olsa break; 46719658171SJiri Olsa } 46819658171SJiri Olsa 46919658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 470272ed29aSJiri Olsa e->help = strdup(help); 47119658171SJiri Olsa } 47219658171SJiri Olsa 473c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4748c619d6aSWang Nan const char *sys_name, const char *evt_name, 475272ed29aSJiri Olsa struct parse_events_error *err, 476e637d177SHe Kuang struct list_head *head_config) 477bcd3279fSFrederic Weisbecker { 47882fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 479bcd3279fSFrederic Weisbecker 480ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 48119658171SJiri Olsa if (IS_ERR(evsel)) { 482272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4838dd2a131SJiri Olsa return PTR_ERR(evsel); 48419658171SJiri Olsa } 485bcd3279fSFrederic Weisbecker 486e637d177SHe Kuang if (head_config) { 487e637d177SHe Kuang LIST_HEAD(config_terms); 488e637d177SHe Kuang 489e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 490e637d177SHe Kuang return -ENOMEM; 491e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 492e637d177SHe Kuang } 493e637d177SHe Kuang 49482fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 49582fe1c29SArnaldo Carvalho de Melo return 0; 496bcd3279fSFrederic Weisbecker } 497bcd3279fSFrederic Weisbecker 498c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 4998c619d6aSWang Nan const char *sys_name, const char *evt_name, 500272ed29aSJiri Olsa struct parse_events_error *err, 501e637d177SHe Kuang struct list_head *head_config) 502bcd3279fSFrederic Weisbecker { 503bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 504bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 505bcd3279fSFrederic Weisbecker DIR *evt_dir; 50627bf90bfSJiri Olsa int ret = 0, found = 0; 507bcd3279fSFrederic Weisbecker 508ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 509bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 510bcd3279fSFrederic Weisbecker if (!evt_dir) { 511272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 51289812fc8SJiri Olsa return -1; 513bcd3279fSFrederic Weisbecker } 514bcd3279fSFrederic Weisbecker 51589812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 516bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 517bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 518bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 519bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 520bcd3279fSFrederic Weisbecker continue; 521bcd3279fSFrederic Weisbecker 52289812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 523fb1d2edfSMasami Hiramatsu continue; 524fb1d2edfSMasami Hiramatsu 52527bf90bfSJiri Olsa found++; 52627bf90bfSJiri Olsa 527e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 528272ed29aSJiri Olsa err, head_config); 529bcd3279fSFrederic Weisbecker } 530bcd3279fSFrederic Weisbecker 53127bf90bfSJiri Olsa if (!found) { 53227bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 53327bf90bfSJiri Olsa ret = -1; 53427bf90bfSJiri Olsa } 53527bf90bfSJiri Olsa 5360bd3f084SJiri Olsa closedir(evt_dir); 53789812fc8SJiri Olsa return ret; 538bcd3279fSFrederic Weisbecker } 539bcd3279fSFrederic Weisbecker 540c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5418c619d6aSWang Nan const char *sys_name, const char *evt_name, 542272ed29aSJiri Olsa struct parse_events_error *err, 543e637d177SHe Kuang struct list_head *head_config) 544f35488f9SJiri Olsa { 545f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 546e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 547272ed29aSJiri Olsa err, head_config) : 548e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 549272ed29aSJiri Olsa err, head_config); 550f35488f9SJiri Olsa } 551f35488f9SJiri Olsa 552c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(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 struct dirent *events_ent; 558f35488f9SJiri Olsa DIR *events_dir; 559f35488f9SJiri Olsa int ret = 0; 560f35488f9SJiri Olsa 561f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 562f35488f9SJiri Olsa if (!events_dir) { 563272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 564f35488f9SJiri Olsa return -1; 565f35488f9SJiri Olsa } 566f35488f9SJiri Olsa 567f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 568f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 569f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 570f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 571f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 572f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 573f35488f9SJiri Olsa continue; 574f35488f9SJiri Olsa 575f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 576f35488f9SJiri Olsa continue; 577f35488f9SJiri Olsa 578f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 579272ed29aSJiri Olsa evt_name, err, head_config); 580f35488f9SJiri Olsa } 581f35488f9SJiri Olsa 582f35488f9SJiri Olsa closedir(events_dir); 583f35488f9SJiri Olsa return ret; 584f35488f9SJiri Olsa } 585f35488f9SJiri Olsa 5864edf30e3SWang Nan struct __add_bpf_event_param { 5874edf30e3SWang Nan struct parse_events_evlist *data; 5884edf30e3SWang Nan struct list_head *list; 58995088a59SWang Nan struct list_head *head_config; 5904edf30e3SWang Nan }; 5914edf30e3SWang Nan 592cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd, 5934edf30e3SWang Nan void *_param) 5944edf30e3SWang Nan { 5954edf30e3SWang Nan LIST_HEAD(new_evsels); 5964edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 5974edf30e3SWang Nan struct parse_events_evlist *evlist = param->data; 5984edf30e3SWang Nan struct list_head *list = param->list; 5991f45b1d4SWang Nan struct perf_evsel *pos; 6004edf30e3SWang Nan int err; 6014edf30e3SWang Nan 6024edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 603cd102d70SWang Nan group, event, fd); 6044edf30e3SWang Nan 605cd102d70SWang Nan err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, group, 606cd102d70SWang Nan event, evlist->error, 60795088a59SWang Nan param->head_config); 6084edf30e3SWang Nan if (err) { 6094edf30e3SWang Nan struct perf_evsel *evsel, *tmp; 6104edf30e3SWang Nan 6114edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 612cd102d70SWang Nan group, event); 6134edf30e3SWang Nan list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { 6144edf30e3SWang Nan list_del(&evsel->node); 6154edf30e3SWang Nan perf_evsel__delete(evsel); 6164edf30e3SWang Nan } 6174edf30e3SWang Nan return err; 6184edf30e3SWang Nan } 619cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6204edf30e3SWang Nan 6211f45b1d4SWang Nan list_for_each_entry(pos, &new_evsels, node) { 6221f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 623cd102d70SWang Nan group, event, pos); 6241f45b1d4SWang Nan pos->bpf_fd = fd; 6251f45b1d4SWang Nan } 6264edf30e3SWang Nan list_splice(&new_evsels, list); 6274edf30e3SWang Nan return 0; 6284edf30e3SWang Nan } 6294edf30e3SWang Nan 63084c86ca1SWang Nan int parse_events_load_bpf_obj(struct parse_events_evlist *data, 63184c86ca1SWang Nan struct list_head *list, 63295088a59SWang Nan struct bpf_object *obj, 63395088a59SWang Nan struct list_head *head_config) 63484c86ca1SWang Nan { 63584c86ca1SWang Nan int err; 63684c86ca1SWang Nan char errbuf[BUFSIZ]; 63795088a59SWang Nan struct __add_bpf_event_param param = {data, list, head_config}; 638aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 63984c86ca1SWang Nan 64084c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 64184c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 64284c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 64384c86ca1SWang Nan err = -EINVAL; 64484c86ca1SWang Nan goto errout; 64584c86ca1SWang Nan } 64684c86ca1SWang Nan 64784c86ca1SWang Nan /* 648aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 649aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 650aa3abf30SWang Nan * created when failure. 651aa3abf30SWang Nan */ 652aa3abf30SWang Nan if (!registered_unprobe_atexit) { 653aa3abf30SWang Nan atexit(bpf__clear); 654aa3abf30SWang Nan registered_unprobe_atexit = true; 655aa3abf30SWang Nan } 656aa3abf30SWang Nan 657aa3abf30SWang Nan err = bpf__probe(obj); 658aa3abf30SWang Nan if (err) { 659aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 660aa3abf30SWang Nan goto errout; 661aa3abf30SWang Nan } 662aa3abf30SWang Nan 6631e5e3ee8SWang Nan err = bpf__load(obj); 6641e5e3ee8SWang Nan if (err) { 6651e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6661e5e3ee8SWang Nan goto errout; 6671e5e3ee8SWang Nan } 6681e5e3ee8SWang Nan 669cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6704edf30e3SWang Nan if (err) { 6714edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6724edf30e3SWang Nan "Attach events in BPF object failed"); 6734edf30e3SWang Nan goto errout; 6744edf30e3SWang Nan } 6754edf30e3SWang Nan 6764edf30e3SWang Nan return 0; 67784c86ca1SWang Nan errout: 67884c86ca1SWang Nan data->error->help = strdup("(add -v to see detail)"); 67984c86ca1SWang Nan data->error->str = strdup(errbuf); 68084c86ca1SWang Nan return err; 68184c86ca1SWang Nan } 68284c86ca1SWang Nan 683a34f3be7SWang Nan static int 684a34f3be7SWang Nan parse_events_config_bpf(struct parse_events_evlist *data, 685a34f3be7SWang Nan struct bpf_object *obj, 686a34f3be7SWang Nan struct list_head *head_config) 687a34f3be7SWang Nan { 688a34f3be7SWang Nan struct parse_events_term *term; 689a34f3be7SWang Nan int error_pos; 690a34f3be7SWang Nan 691a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 692a34f3be7SWang Nan return 0; 693a34f3be7SWang Nan 694a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 695a34f3be7SWang Nan char errbuf[BUFSIZ]; 696a34f3be7SWang Nan int err; 697a34f3be7SWang Nan 698a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 699a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 700a34f3be7SWang Nan "Invalid config term for BPF object"); 701a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 702a34f3be7SWang Nan 703a34f3be7SWang Nan data->error->idx = term->err_term; 704a34f3be7SWang Nan data->error->str = strdup(errbuf); 705a34f3be7SWang Nan return -EINVAL; 706a34f3be7SWang Nan } 707a34f3be7SWang Nan 7087630b3e2SWang Nan err = bpf__config_obj(obj, term, data->evlist, &error_pos); 709a34f3be7SWang Nan if (err) { 7107630b3e2SWang Nan bpf__strerror_config_obj(obj, term, data->evlist, 711a34f3be7SWang Nan &error_pos, err, errbuf, 712a34f3be7SWang Nan sizeof(errbuf)); 713a34f3be7SWang Nan data->error->help = strdup( 7147630b3e2SWang Nan "Hint:\tValid config terms:\n" 715e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 716e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7177630b3e2SWang Nan "\n" 718e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 719a34f3be7SWang Nan " \t(add -v to see detail)"); 720a34f3be7SWang Nan data->error->str = strdup(errbuf); 721a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 722a34f3be7SWang Nan data->error->idx = term->err_val; 723a34f3be7SWang Nan else 724a34f3be7SWang Nan data->error->idx = term->err_term + error_pos; 725a34f3be7SWang Nan return err; 726a34f3be7SWang Nan } 727a34f3be7SWang Nan } 728a34f3be7SWang Nan return 0; 729a34f3be7SWang Nan } 730a34f3be7SWang Nan 73195088a59SWang Nan /* 73295088a59SWang Nan * Split config terms: 73395088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 73495088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 73595088a59SWang Nan * events in bpf.c. 73695088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 73795088a59SWang Nan * with parse_events_config_bpf. 73895088a59SWang Nan * 73995088a59SWang Nan * Move object config terms from the first list to obj_head_config. 74095088a59SWang Nan */ 74195088a59SWang Nan static void 74295088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 74395088a59SWang Nan struct list_head *obj_head_config) 74495088a59SWang Nan { 74595088a59SWang Nan struct parse_events_term *term, *temp; 74695088a59SWang Nan 74795088a59SWang Nan /* 74895088a59SWang Nan * Currectly, all possible user config term 74995088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 75095088a59SWang Nan * happends to be a good flag. 75195088a59SWang Nan * 75295088a59SWang Nan * See parse_events_config_bpf() and 75395088a59SWang Nan * config_term_tracepoint(). 75495088a59SWang Nan */ 75595088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 75695088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 75795088a59SWang Nan list_move_tail(&term->list, obj_head_config); 75895088a59SWang Nan } 75995088a59SWang Nan 76084c86ca1SWang Nan int parse_events_load_bpf(struct parse_events_evlist *data, 76184c86ca1SWang Nan struct list_head *list, 762d509db04SWang Nan char *bpf_file_name, 763a34f3be7SWang Nan bool source, 764a34f3be7SWang Nan struct list_head *head_config) 76584c86ca1SWang Nan { 766a34f3be7SWang Nan int err; 76795088a59SWang Nan struct bpf_object *obj; 76895088a59SWang Nan LIST_HEAD(obj_head_config); 76995088a59SWang Nan 77095088a59SWang Nan if (head_config) 77195088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 77284c86ca1SWang Nan 773d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 7746371ca3bSWang Nan if (IS_ERR(obj)) { 77584c86ca1SWang Nan char errbuf[BUFSIZ]; 77684c86ca1SWang Nan 7776371ca3bSWang Nan err = PTR_ERR(obj); 77884c86ca1SWang Nan 77984c86ca1SWang Nan if (err == -ENOTSUP) 78084c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 78184c86ca1SWang Nan "BPF support is not compiled"); 78284c86ca1SWang Nan else 783d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 784d3e0ce39SWang Nan source, 785d3e0ce39SWang Nan -err, errbuf, 786d3e0ce39SWang Nan sizeof(errbuf)); 78784c86ca1SWang Nan 78884c86ca1SWang Nan data->error->help = strdup("(add -v to see detail)"); 78984c86ca1SWang Nan data->error->str = strdup(errbuf); 79084c86ca1SWang Nan return err; 79184c86ca1SWang Nan } 79284c86ca1SWang Nan 79395088a59SWang Nan err = parse_events_load_bpf_obj(data, list, obj, head_config); 794a34f3be7SWang Nan if (err) 795a34f3be7SWang Nan return err; 79695088a59SWang Nan err = parse_events_config_bpf(data, obj, &obj_head_config); 79795088a59SWang Nan 79895088a59SWang Nan /* 79995088a59SWang Nan * Caller doesn't know anything about obj_head_config, 80095088a59SWang Nan * so combine them together again before returnning. 80195088a59SWang Nan */ 80295088a59SWang Nan if (head_config) 80395088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 80495088a59SWang Nan return err; 80584c86ca1SWang Nan } 80684c86ca1SWang Nan 80789812fc8SJiri Olsa static int 80889812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8091b290d67SFrederic Weisbecker { 8101b290d67SFrederic Weisbecker int i; 8111b290d67SFrederic Weisbecker 8121b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 81389812fc8SJiri Olsa if (!type || !type[i]) 8141b290d67SFrederic Weisbecker break; 8151b290d67SFrederic Weisbecker 8167582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8177582732fSJiri Olsa do { \ 8187582732fSJiri Olsa if (attr->bp_type & bit) \ 8197582732fSJiri Olsa return -EINVAL; \ 8207582732fSJiri Olsa else \ 8217582732fSJiri Olsa attr->bp_type |= bit; \ 8227582732fSJiri Olsa } while (0) 8237582732fSJiri Olsa 8241b290d67SFrederic Weisbecker switch (type[i]) { 8251b290d67SFrederic Weisbecker case 'r': 8267582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8271b290d67SFrederic Weisbecker break; 8281b290d67SFrederic Weisbecker case 'w': 8297582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8301b290d67SFrederic Weisbecker break; 8311b290d67SFrederic Weisbecker case 'x': 8327582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8331b290d67SFrederic Weisbecker break; 8341b290d67SFrederic Weisbecker default: 83589812fc8SJiri Olsa return -EINVAL; 8361b290d67SFrederic Weisbecker } 8371b290d67SFrederic Weisbecker } 83889812fc8SJiri Olsa 8397582732fSJiri Olsa #undef CHECK_SET_TYPE 8407582732fSJiri Olsa 8411b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8421b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8431b290d67SFrederic Weisbecker 84489812fc8SJiri Olsa return 0; 8451b290d67SFrederic Weisbecker } 8461b290d67SFrederic Weisbecker 847c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 8483741eb9fSJacob Shin void *ptr, char *type, u64 len) 8491b290d67SFrederic Weisbecker { 85089812fc8SJiri Olsa struct perf_event_attr attr; 8511b290d67SFrederic Weisbecker 85289812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 8539fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 8541b290d67SFrederic Weisbecker 85589812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 85689812fc8SJiri Olsa return -EINVAL; 8571b290d67SFrederic Weisbecker 8583741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 8593741eb9fSJacob Shin if (!len) { 86089812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 8613741eb9fSJacob Shin len = sizeof(long); 862aa59a485SFrederic Weisbecker else 8633741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 8643741eb9fSJacob Shin } 8653741eb9fSJacob Shin 8663741eb9fSJacob Shin attr.bp_len = len; 867aa59a485SFrederic Weisbecker 86889812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 8694a841d65SJovi Zhang attr.sample_period = 1; 8701b290d67SFrederic Weisbecker 871930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 8721b290d67SFrederic Weisbecker } 8731b290d67SFrederic Weisbecker 8743b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 8753b0e371cSJiri Olsa struct parse_events_error *err, 8763b0e371cSJiri Olsa int type) 8773b0e371cSJiri Olsa { 8783b0e371cSJiri Olsa if (type == term->type_val) 8793b0e371cSJiri Olsa return 0; 8803b0e371cSJiri Olsa 8813b0e371cSJiri Olsa if (err) { 8823b0e371cSJiri Olsa err->idx = term->err_val; 8833b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 8843b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 8853b0e371cSJiri Olsa else 8863b0e371cSJiri Olsa err->str = strdup("expected string value"); 8873b0e371cSJiri Olsa } 8883b0e371cSJiri Olsa return -EINVAL; 8893b0e371cSJiri Olsa } 8903b0e371cSJiri Olsa 89117cb5f84SWang Nan /* 89217cb5f84SWang Nan * Update according to parse-events.l 89317cb5f84SWang Nan */ 89417cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 89517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 89617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 89717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 89817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 89917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 90017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 90117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 90217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 90317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 90417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 90517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 90617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 90717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 908792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 909626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 910626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 911dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 91217cb5f84SWang Nan }; 91317cb5f84SWang Nan 9141669e509SWang Nan static bool config_term_shrinked; 9151669e509SWang Nan 9161669e509SWang Nan static bool 9171669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9181669e509SWang Nan { 9191669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9201669e509SWang Nan err->str = strdup("Invalid term_type"); 9211669e509SWang Nan return false; 9221669e509SWang Nan } 9231669e509SWang Nan if (!config_term_shrinked) 9241669e509SWang Nan return true; 9251669e509SWang Nan 9261669e509SWang Nan switch (term_type) { 9271669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9281669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9291669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9301669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 93106835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 9321669e509SWang Nan return true; 9331669e509SWang Nan default: 9341669e509SWang Nan if (!err) 9351669e509SWang Nan return false; 9361669e509SWang Nan 9371669e509SWang Nan /* term_type is validated so indexing is safe */ 9381669e509SWang Nan if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", 9391669e509SWang Nan config_term_names[term_type]) < 0) 9401669e509SWang Nan err->str = NULL; 9411669e509SWang Nan return false; 9421669e509SWang Nan } 9431669e509SWang Nan } 9441669e509SWang Nan 9451669e509SWang Nan void parse_events__shrink_config_terms(void) 9461669e509SWang Nan { 9471669e509SWang Nan config_term_shrinked = true; 9481669e509SWang Nan } 9491669e509SWang Nan 9500b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 9513b0e371cSJiri Olsa struct parse_events_term *term, 9523b0e371cSJiri Olsa struct parse_events_error *err) 9538f707d84SJiri Olsa { 95416fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 95516fa7e82SJiri Olsa do { \ 9563b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 95716fa7e82SJiri Olsa return -EINVAL; \ 95816fa7e82SJiri Olsa } while (0) 95916fa7e82SJiri Olsa 96016fa7e82SJiri Olsa switch (term->type_term) { 9618f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 96216fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9638f707d84SJiri Olsa attr->config = term->val.num; 9648f707d84SJiri Olsa break; 9658f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 96616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9678f707d84SJiri Olsa attr->config1 = term->val.num; 9688f707d84SJiri Olsa break; 9698f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 97016fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9718f707d84SJiri Olsa attr->config2 = term->val.num; 9728f707d84SJiri Olsa break; 9738f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 97416fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9758f707d84SJiri Olsa break; 97609af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 97709af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 97809af2a55SNamhyung Kim break; 9798f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 980ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 981ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 982ac12f676SAndi Kleen parse_branch_str(term->val.str, &attr->branch_sample_type)) { 983ac12f676SAndi Kleen err->str = strdup("invalid branch sample type"); 984ac12f676SAndi Kleen err->idx = term->err_val; 985ac12f676SAndi Kleen return -EINVAL; 986ac12f676SAndi Kleen } 9878f707d84SJiri Olsa break; 98832067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 98932067712SKan Liang CHECK_TYPE_VAL(NUM); 99032067712SKan Liang if (term->val.num > 1) { 99132067712SKan Liang err->str = strdup("expected 0 or 1"); 99232067712SKan Liang err->idx = term->err_val; 99332067712SKan Liang return -EINVAL; 99432067712SKan Liang } 99532067712SKan Liang break; 996d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 997d457c963SKan Liang CHECK_TYPE_VAL(STR); 998d457c963SKan Liang break; 999d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1000d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1001d457c963SKan Liang break; 1002374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1003374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1004374ce938SWang Nan break; 1005374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1006374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1007374ce938SWang Nan break; 1008626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1009626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1010626a6b78SWang Nan break; 1011626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1012626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1013626a6b78SWang Nan break; 10146b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10156b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10166b5fc39bSJiri Olsa break; 1017792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1018792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1019792d48b4SArnaldo Carvalho de Melo break; 10208f707d84SJiri Olsa default: 1021ffeb883eSHe Kuang err->str = strdup("unknown term"); 1022ffeb883eSHe Kuang err->idx = term->err_term; 1023ffeb883eSHe Kuang err->help = parse_events_formats_error_string(NULL); 10248f707d84SJiri Olsa return -EINVAL; 10258f707d84SJiri Olsa } 102616fa7e82SJiri Olsa 10271669e509SWang Nan /* 10281669e509SWang Nan * Check term availbility after basic checking so 10291669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 10301669e509SWang Nan * 10311669e509SWang Nan * If check availbility at the entry of this function, 10321669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 10331669e509SWang Nan * if an invalid config term is provided for legacy events 10341669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 10351669e509SWang Nan */ 10361669e509SWang Nan if (!config_term_avail(term->type_term, err)) 10371669e509SWang Nan return -EINVAL; 10388f707d84SJiri Olsa return 0; 103916fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 10408f707d84SJiri Olsa } 10418f707d84SJiri Olsa 10420b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 10430b8891a8SHe Kuang struct parse_events_term *term, 10440b8891a8SHe Kuang struct parse_events_error *err) 10450b8891a8SHe Kuang { 1046dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1047dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 10480b8891a8SHe Kuang /* 10490b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 10500b8891a8SHe Kuang * at this point what type they need to have. 10510b8891a8SHe Kuang */ 10520b8891a8SHe Kuang return 0; 10530b8891a8SHe Kuang else 10540b8891a8SHe Kuang return config_term_common(attr, term, err); 10550b8891a8SHe Kuang } 10560b8891a8SHe Kuang 1057e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1058e637d177SHe Kuang struct parse_events_term *term, 1059e637d177SHe Kuang struct parse_events_error *err) 1060e637d177SHe Kuang { 1061e637d177SHe Kuang switch (term->type_term) { 1062e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1063e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1064374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1065374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1066792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1067626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1068626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1069e637d177SHe Kuang return config_term_common(attr, term, err); 1070e637d177SHe Kuang default: 1071e637d177SHe Kuang if (err) { 1072e637d177SHe Kuang err->idx = term->err_term; 1073e637d177SHe Kuang err->str = strdup("unknown term"); 1074e637d177SHe Kuang err->help = strdup("valid terms: call-graph,stack-size\n"); 1075e637d177SHe Kuang } 1076e637d177SHe Kuang return -EINVAL; 1077e637d177SHe Kuang } 1078e637d177SHe Kuang 1079e637d177SHe Kuang return 0; 1080e637d177SHe Kuang } 1081e637d177SHe Kuang 10828f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 10833b0e371cSJiri Olsa struct list_head *head, 10840b8891a8SHe Kuang struct parse_events_error *err, 10850b8891a8SHe Kuang config_term_func_t config_term) 10868f707d84SJiri Olsa { 10876cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 10888f707d84SJiri Olsa 10898f707d84SJiri Olsa list_for_each_entry(term, head, list) 10903b0e371cSJiri Olsa if (config_term(attr, term, err)) 10918f707d84SJiri Olsa return -EINVAL; 10928f707d84SJiri Olsa 10938f707d84SJiri Olsa return 0; 10948f707d84SJiri Olsa } 10958f707d84SJiri Olsa 1096930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1097930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1098930a2e29SJiri Olsa { 1099930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 1100930a2e29SJiri Olsa do { \ 1101930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1102930a2e29SJiri Olsa \ 1103930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1104930a2e29SJiri Olsa if (!__t) \ 1105930a2e29SJiri Olsa return -ENOMEM; \ 1106930a2e29SJiri Olsa \ 1107930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1108930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 1109930a2e29SJiri Olsa __t->val.__name = __val; \ 1110930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 1111930a2e29SJiri Olsa } while (0) 1112930a2e29SJiri Olsa 1113930a2e29SJiri Olsa struct parse_events_term *term; 1114930a2e29SJiri Olsa 1115930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1116930a2e29SJiri Olsa switch (term->type_term) { 1117ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1118ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 111932067712SKan Liang break; 112009af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 112109af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 112209af2a55SNamhyung Kim break; 112332067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 112432067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 112532067712SKan Liang break; 1126d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1127d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 1128d457c963SKan Liang break; 1129ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1130ac12f676SAndi Kleen ADD_CONFIG_TERM(BRANCH, branch, term->val.str); 1131ac12f676SAndi Kleen break; 1132d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1133d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 1134d457c963SKan Liang break; 1135374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1136374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); 1137374ce938SWang Nan break; 1138374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1139374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); 1140374ce938SWang Nan break; 1141792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1142792d48b4SArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num); 1143792d48b4SArnaldo Carvalho de Melo break; 1144626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1145626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0); 1146626a6b78SWang Nan break; 1147626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1148626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1); 1149626a6b78SWang Nan break; 1150dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1151dd60fba7SMathieu Poirier ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str); 1152dd60fba7SMathieu Poirier break; 1153930a2e29SJiri Olsa default: 1154930a2e29SJiri Olsa break; 1155930a2e29SJiri Olsa } 1156930a2e29SJiri Olsa } 1157930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 1158930a2e29SJiri Olsa return 0; 1159930a2e29SJiri Olsa } 1160930a2e29SJiri Olsa 1161e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 11628c619d6aSWang Nan const char *sys, const char *event, 1163272ed29aSJiri Olsa struct parse_events_error *err, 1164e637d177SHe Kuang struct list_head *head_config) 1165e637d177SHe Kuang { 1166e637d177SHe Kuang if (head_config) { 1167e637d177SHe Kuang struct perf_event_attr attr; 1168e637d177SHe Kuang 1169272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1170e637d177SHe Kuang config_term_tracepoint)) 1171e637d177SHe Kuang return -EINVAL; 1172e637d177SHe Kuang } 1173e637d177SHe Kuang 1174e637d177SHe Kuang if (strpbrk(sys, "*?")) 1175e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1176272ed29aSJiri Olsa err, head_config); 1177e637d177SHe Kuang else 1178e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1179272ed29aSJiri Olsa err, head_config); 1180e637d177SHe Kuang } 1181e637d177SHe Kuang 118287d650beSJiri Olsa int parse_events_add_numeric(struct parse_events_evlist *data, 118387d650beSJiri Olsa struct list_head *list, 1184b527bab5SRobert Richter u32 type, u64 config, 11858f707d84SJiri Olsa struct list_head *head_config) 118674d5b588SJaswinder Singh Rajput { 118789812fc8SJiri Olsa struct perf_event_attr attr; 1188930a2e29SJiri Olsa LIST_HEAD(config_terms); 118974d5b588SJaswinder Singh Rajput 119089812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 119189812fc8SJiri Olsa attr.type = type; 119289812fc8SJiri Olsa attr.config = config; 11938f707d84SJiri Olsa 1194930a2e29SJiri Olsa if (head_config) { 11950b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, 11960b8891a8SHe Kuang config_term_common)) 11978f707d84SJiri Olsa return -EINVAL; 11988f707d84SJiri Olsa 1199930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1200930a2e29SJiri Olsa return -ENOMEM; 1201930a2e29SJiri Olsa } 1202930a2e29SJiri Olsa 120310bf358aSWang Nan return add_event(list, &data->idx, &attr, 120410bf358aSWang Nan get_config_name(head_config), &config_terms); 1205b908debdSIngo Molnar } 1206b908debdSIngo Molnar 120736adec85SJiri Olsa int parse_events_add_pmu(struct parse_events_evlist *data, 120836adec85SJiri Olsa struct list_head *list, char *name, 120936adec85SJiri Olsa struct list_head *head_config) 12105f537a26SJiri Olsa { 12115f537a26SJiri Olsa struct perf_event_attr attr; 121246441bdcSMatt Fleming struct perf_pmu_info info; 12135f537a26SJiri Olsa struct perf_pmu *pmu; 1214410136f5SStephane Eranian struct perf_evsel *evsel; 1215930a2e29SJiri Olsa LIST_HEAD(config_terms); 12165f537a26SJiri Olsa 12175f537a26SJiri Olsa pmu = perf_pmu__find(name); 12185f537a26SJiri Olsa if (!pmu) 12195f537a26SJiri Olsa return -EINVAL; 12205f537a26SJiri Olsa 1221dc0a6202SAdrian Hunter if (pmu->default_config) { 1222dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1223dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1224dc0a6202SAdrian Hunter } else { 12255f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1226dc0a6202SAdrian Hunter } 12275f537a26SJiri Olsa 1228ad962273SAdrian Hunter if (!head_config) { 1229ad962273SAdrian Hunter attr.type = pmu->type; 1230930a2e29SJiri Olsa evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); 1231ad962273SAdrian Hunter return evsel ? 0 : -ENOMEM; 1232ad962273SAdrian Hunter } 1233ad962273SAdrian Hunter 123446441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1235a6146d50SZheng Yan return -EINVAL; 1236a6146d50SZheng Yan 12375f537a26SJiri Olsa /* 12385f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 12395f537a26SJiri Olsa * return value when called with fail == 0 ;) 12405f537a26SJiri Olsa */ 12410b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, config_term_pmu)) 1242c056ba6aSJiri Olsa return -EINVAL; 12435f537a26SJiri Olsa 1244930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1245930a2e29SJiri Olsa return -ENOMEM; 1246930a2e29SJiri Olsa 1247e64b020bSJiri Olsa if (perf_pmu__config(pmu, &attr, head_config, data->error)) 12485f537a26SJiri Olsa return -EINVAL; 12495f537a26SJiri Olsa 125036adec85SJiri Olsa evsel = __add_event(list, &data->idx, &attr, 1251e814fdddSWang Nan get_config_name(head_config), pmu->cpus, 1252930a2e29SJiri Olsa &config_terms); 1253410136f5SStephane Eranian if (evsel) { 125446441bdcSMatt Fleming evsel->unit = info.unit; 125546441bdcSMatt Fleming evsel->scale = info.scale; 1256044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 12571d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 125837932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 125996284814SAndi Kleen evsel->metric_name = info.metric_name; 1260410136f5SStephane Eranian } 1261410136f5SStephane Eranian 1262410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 12635f537a26SJiri Olsa } 12645f537a26SJiri Olsa 12652073ad33SAndi Kleen int parse_events_multi_pmu_add(struct parse_events_evlist *data, 12662073ad33SAndi Kleen char *str, struct list_head **listp) 12672073ad33SAndi Kleen { 12682073ad33SAndi Kleen struct list_head *head; 12692073ad33SAndi Kleen struct parse_events_term *term; 12702073ad33SAndi Kleen struct list_head *list; 12712073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 12722073ad33SAndi Kleen int ok = 0; 12732073ad33SAndi Kleen 12742073ad33SAndi Kleen *listp = NULL; 12752073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 12762073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 12772073ad33SAndi Kleen if (!list) 12782073ad33SAndi Kleen return -1; 12792073ad33SAndi Kleen INIT_LIST_HEAD(list); 12802073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 12812073ad33SAndi Kleen struct perf_pmu_alias *alias; 12822073ad33SAndi Kleen 12832073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 12842073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 12852073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 12862073ad33SAndi Kleen if (!head) 12872073ad33SAndi Kleen return -1; 12882073ad33SAndi Kleen INIT_LIST_HEAD(head); 12892073ad33SAndi Kleen if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 12902073ad33SAndi Kleen str, 1, false, &str, NULL) < 0) 12912073ad33SAndi Kleen return -1; 12922073ad33SAndi Kleen list_add_tail(&term->list, head); 12932073ad33SAndi Kleen 12942073ad33SAndi Kleen if (!parse_events_add_pmu(data, list, 12952073ad33SAndi Kleen pmu->name, head)) { 12962073ad33SAndi Kleen pr_debug("%s -> %s/%s/\n", str, 12972073ad33SAndi Kleen pmu->name, alias->str); 12982073ad33SAndi Kleen ok++; 12992073ad33SAndi Kleen } 13002073ad33SAndi Kleen 13012073ad33SAndi Kleen parse_events_terms__delete(head); 13022073ad33SAndi Kleen } 13032073ad33SAndi Kleen } 13042073ad33SAndi Kleen } 13052073ad33SAndi Kleen if (!ok) 13062073ad33SAndi Kleen return -1; 13072073ad33SAndi Kleen *listp = list; 13082073ad33SAndi Kleen return 0; 13092073ad33SAndi Kleen } 13102073ad33SAndi Kleen 13116a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 13126a4bb04cSJiri Olsa char *event_mod) 131389efb029SJiri Olsa { 13146a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 13156a4bb04cSJiri Olsa } 13166a4bb04cSJiri Olsa 131763dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 13186a4bb04cSJiri Olsa { 13196a4bb04cSJiri Olsa struct perf_evsel *leader; 13206a4bb04cSJiri Olsa 1321854f7363SWang Nan if (list_empty(list)) { 1322854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1323854f7363SWang Nan return; 1324854f7363SWang Nan } 1325854f7363SWang Nan 132663dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 132763dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 13286a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 132989efb029SJiri Olsa } 133089efb029SJiri Olsa 1331c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 13325d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 13335d7be90eSJiri Olsa struct list_head *list_all) 13345d7be90eSJiri Olsa { 13355d7be90eSJiri Olsa /* 13365d7be90eSJiri Olsa * Called for single event definition. Update the 133789efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 13385d7be90eSJiri Olsa * list, for next event definition. 13395d7be90eSJiri Olsa */ 13405d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1341b847cbdcSJiri Olsa free(list_event); 13425d7be90eSJiri Olsa } 13435d7be90eSJiri Olsa 1344f5b1135bSJiri Olsa struct event_modifier { 1345f5b1135bSJiri Olsa int eu; 1346f5b1135bSJiri Olsa int ek; 1347f5b1135bSJiri Olsa int eh; 1348f5b1135bSJiri Olsa int eH; 1349f5b1135bSJiri Olsa int eG; 1350a1e12da4SJiri Olsa int eI; 1351f5b1135bSJiri Olsa int precise; 13527f94af7aSJiri Olsa int precise_max; 1353f5b1135bSJiri Olsa int exclude_GH; 13543c176311SJiri Olsa int sample_read; 1355e9a7c414SMichael Ellerman int pinned; 1356f5b1135bSJiri Olsa }; 135761c45981SPaul Mackerras 1358f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 1359f5b1135bSJiri Olsa struct perf_evsel *evsel) 1360f5b1135bSJiri Olsa { 1361f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 1362f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 1363f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 1364f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 1365f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 1366a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 1367f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 13687f94af7aSJiri Olsa int precise_max = 0; 13693c176311SJiri Olsa int sample_read = 0; 1370e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 1371f5b1135bSJiri Olsa 1372f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1373f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 1374f5b1135bSJiri Olsa 1375f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1376ceb53fbfSIngo Molnar 137761c45981SPaul Mackerras while (*str) { 1378ab608344SPeter Zijlstra if (*str == 'u') { 1379ab608344SPeter Zijlstra if (!exclude) 1380ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 138161c45981SPaul Mackerras eu = 0; 1382ab608344SPeter Zijlstra } else if (*str == 'k') { 1383ab608344SPeter Zijlstra if (!exclude) 1384ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 138561c45981SPaul Mackerras ek = 0; 1386ab608344SPeter Zijlstra } else if (*str == 'h') { 1387ab608344SPeter Zijlstra if (!exclude) 1388ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 138961c45981SPaul Mackerras eh = 0; 139099320cc8SJoerg Roedel } else if (*str == 'G') { 139199320cc8SJoerg Roedel if (!exclude_GH) 139299320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 139399320cc8SJoerg Roedel eG = 0; 139499320cc8SJoerg Roedel } else if (*str == 'H') { 139599320cc8SJoerg Roedel if (!exclude_GH) 139699320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 139799320cc8SJoerg Roedel eH = 0; 1398a1e12da4SJiri Olsa } else if (*str == 'I') { 1399a1e12da4SJiri Olsa eI = 1; 1400ab608344SPeter Zijlstra } else if (*str == 'p') { 1401ab608344SPeter Zijlstra precise++; 14021342798cSDavid Ahern /* use of precise requires exclude_guest */ 14031342798cSDavid Ahern if (!exclude_GH) 14041342798cSDavid Ahern eG = 1; 14057f94af7aSJiri Olsa } else if (*str == 'P') { 14067f94af7aSJiri Olsa precise_max = 1; 14073c176311SJiri Olsa } else if (*str == 'S') { 14083c176311SJiri Olsa sample_read = 1; 1409e9a7c414SMichael Ellerman } else if (*str == 'D') { 1410e9a7c414SMichael Ellerman pinned = 1; 1411ab608344SPeter Zijlstra } else 141261c45981SPaul Mackerras break; 1413ab608344SPeter Zijlstra 141461c45981SPaul Mackerras ++str; 141561c45981SPaul Mackerras } 141674d5b588SJaswinder Singh Rajput 141786470930SIngo Molnar /* 141889812fc8SJiri Olsa * precise ip: 141989812fc8SJiri Olsa * 142089812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 142189812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 142289812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 142389812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 142489812fc8SJiri Olsa * 142589812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 142686470930SIngo Molnar */ 142789812fc8SJiri Olsa if (precise > 3) 142889812fc8SJiri Olsa return -EINVAL; 142986470930SIngo Molnar 1430f5b1135bSJiri Olsa mod->eu = eu; 1431f5b1135bSJiri Olsa mod->ek = ek; 1432f5b1135bSJiri Olsa mod->eh = eh; 1433f5b1135bSJiri Olsa mod->eH = eH; 1434f5b1135bSJiri Olsa mod->eG = eG; 1435a1e12da4SJiri Olsa mod->eI = eI; 1436f5b1135bSJiri Olsa mod->precise = precise; 14377f94af7aSJiri Olsa mod->precise_max = precise_max; 1438f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 14393c176311SJiri Olsa mod->sample_read = sample_read; 1440e9a7c414SMichael Ellerman mod->pinned = pinned; 1441e9a7c414SMichael Ellerman 1442f5b1135bSJiri Olsa return 0; 1443f5b1135bSJiri Olsa } 1444f5b1135bSJiri Olsa 1445534123f4SJiri Olsa /* 1446534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1447534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1448534123f4SJiri Olsa */ 1449534123f4SJiri Olsa static int check_modifier(char *str) 1450534123f4SJiri Olsa { 1451534123f4SJiri Olsa char *p = str; 1452534123f4SJiri Olsa 1453534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 14547f94af7aSJiri Olsa if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1)) 1455534123f4SJiri Olsa return -1; 1456534123f4SJiri Olsa 1457534123f4SJiri Olsa while (*p) { 1458534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1459534123f4SJiri Olsa return -1; 1460534123f4SJiri Olsa p++; 1461534123f4SJiri Olsa } 1462534123f4SJiri Olsa 1463534123f4SJiri Olsa return 0; 1464534123f4SJiri Olsa } 1465534123f4SJiri Olsa 1466f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1467f5b1135bSJiri Olsa { 1468f5b1135bSJiri Olsa struct perf_evsel *evsel; 1469f5b1135bSJiri Olsa struct event_modifier mod; 1470f5b1135bSJiri Olsa 1471f5b1135bSJiri Olsa if (str == NULL) 1472f5b1135bSJiri Olsa return 0; 1473f5b1135bSJiri Olsa 1474534123f4SJiri Olsa if (check_modifier(str)) 1475534123f4SJiri Olsa return -EINVAL; 1476534123f4SJiri Olsa 1477f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1478f5b1135bSJiri Olsa return -EINVAL; 1479f5b1135bSJiri Olsa 1480e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1481f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1482f5b1135bSJiri Olsa return -EINVAL; 1483f5b1135bSJiri Olsa 1484f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 1485f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 1486f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 1487f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 1488f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 1489f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 1490a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 1491f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 14923c176311SJiri Olsa evsel->sample_read = mod.sample_read; 14937f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 1494e9a7c414SMichael Ellerman 1495e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 1496e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 1497ceb53fbfSIngo Molnar } 149886470930SIngo Molnar 149989812fc8SJiri Olsa return 0; 150086470930SIngo Molnar } 150186470930SIngo Molnar 1502ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1503ac2ba9f3SRobert Richter { 1504ac2ba9f3SRobert Richter struct perf_evsel *evsel; 1505ac2ba9f3SRobert Richter 1506e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1507ac2ba9f3SRobert Richter if (!evsel->name) 1508ac2ba9f3SRobert Richter evsel->name = strdup(name); 1509ac2ba9f3SRobert Richter } 1510ac2ba9f3SRobert Richter 1511ac2ba9f3SRobert Richter return 0; 1512ac2ba9f3SRobert Richter } 1513ac2ba9f3SRobert Richter 1514dcb4e102SKan Liang static int 1515dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1516dcb4e102SKan Liang { 1517dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1518dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1519dcb4e102SKan Liang 1520e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1521dcb4e102SKan Liang } 1522dcb4e102SKan Liang 1523dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1524dcb4e102SKan Liang { 1525dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1526dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1527dcb4e102SKan Liang int i; 1528dcb4e102SKan Liang 1529dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1530dcb4e102SKan Liang p = perf_pmu_events_list + i; 1531360e071bSTaeung Song zfree(&p->symbol); 1532dcb4e102SKan Liang } 1533506fde11STaeung Song zfree(&perf_pmu_events_list); 1534dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1535dcb4e102SKan Liang } 1536dcb4e102SKan Liang } 1537dcb4e102SKan Liang 1538dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1539dcb4e102SKan Liang do { \ 1540dcb4e102SKan Liang p->symbol = str; \ 1541dcb4e102SKan Liang if (!p->symbol) \ 1542dcb4e102SKan Liang goto err; \ 1543dcb4e102SKan Liang p->type = stype; \ 1544dcb4e102SKan Liang } while (0) 1545dcb4e102SKan Liang 1546dcb4e102SKan Liang /* 1547dcb4e102SKan Liang * Read the pmu events list from sysfs 1548dcb4e102SKan Liang * Save it into perf_pmu_events_list 1549dcb4e102SKan Liang */ 1550dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1551dcb4e102SKan Liang { 1552dcb4e102SKan Liang 1553dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1554dcb4e102SKan Liang struct perf_pmu_alias *alias; 1555dcb4e102SKan Liang int len = 0; 1556dcb4e102SKan Liang 1557231bb2aaSAndi Kleen pmu = NULL; 1558231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1559dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1560dcb4e102SKan Liang if (strchr(alias->name, '-')) 1561dcb4e102SKan Liang len++; 1562dcb4e102SKan Liang len++; 1563dcb4e102SKan Liang } 1564231bb2aaSAndi Kleen } 1565231bb2aaSAndi Kleen 1566231bb2aaSAndi Kleen if (len == 0) { 1567231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1568231bb2aaSAndi Kleen return; 1569231bb2aaSAndi Kleen } 1570dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1571dcb4e102SKan Liang if (!perf_pmu_events_list) 1572dcb4e102SKan Liang return; 1573dcb4e102SKan Liang perf_pmu_events_list_num = len; 1574dcb4e102SKan Liang 1575dcb4e102SKan Liang len = 0; 1576231bb2aaSAndi Kleen pmu = NULL; 1577231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1578dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1579dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1580dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1581dcb4e102SKan Liang 1582dcb4e102SKan Liang if (tmp != NULL) { 1583dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1584dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1585dcb4e102SKan Liang p++; 1586dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1587dcb4e102SKan Liang len += 2; 1588dcb4e102SKan Liang } else { 1589dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1590dcb4e102SKan Liang len++; 1591dcb4e102SKan Liang } 1592dcb4e102SKan Liang } 1593231bb2aaSAndi Kleen } 1594dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1595dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1596dcb4e102SKan Liang 1597dcb4e102SKan Liang return; 1598dcb4e102SKan Liang err: 1599dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1600dcb4e102SKan Liang } 1601dcb4e102SKan Liang 1602dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1603dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1604dcb4e102SKan Liang { 1605dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1606dcb4e102SKan Liang 1607dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1608dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1609dcb4e102SKan Liang perf_pmu__parse_init(); 1610dcb4e102SKan Liang /* 1611dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1612dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1613dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1614dcb4e102SKan Liang */ 1615dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1616dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1617dcb4e102SKan Liang 1618dcb4e102SKan Liang p.symbol = strdup(name); 1619dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1620dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1621dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1622360e071bSTaeung Song zfree(&p.symbol); 1623dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1624dcb4e102SKan Liang } 1625dcb4e102SKan Liang 162690e2b22dSJiri Olsa static int parse_events__scanner(const char *str, void *data, int start_token) 1627ac20de6fSZheng Yan { 1628ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1629ac20de6fSZheng Yan void *scanner; 1630ac20de6fSZheng Yan int ret; 1631ac20de6fSZheng Yan 163290e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1633ac20de6fSZheng Yan if (ret) 1634ac20de6fSZheng Yan return ret; 1635ac20de6fSZheng Yan 1636ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1637ac20de6fSZheng Yan 1638ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1639ac20de6fSZheng Yan parse_events_debug = 1; 1640ac20de6fSZheng Yan #endif 1641ac20de6fSZheng Yan ret = parse_events_parse(data, scanner); 1642ac20de6fSZheng Yan 1643ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1644ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1645ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1646ac20de6fSZheng Yan return ret; 1647ac20de6fSZheng Yan } 1648ac20de6fSZheng Yan 164990e2b22dSJiri Olsa /* 165090e2b22dSJiri Olsa * parse event config string, return a list of event terms. 165190e2b22dSJiri Olsa */ 165290e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 165390e2b22dSJiri Olsa { 165423b6339bSArnaldo Carvalho de Melo struct parse_events_terms data = { 165590e2b22dSJiri Olsa .terms = NULL, 165690e2b22dSJiri Olsa }; 165790e2b22dSJiri Olsa int ret; 165890e2b22dSJiri Olsa 165990e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_TERMS); 166090e2b22dSJiri Olsa if (!ret) { 166190e2b22dSJiri Olsa list_splice(data.terms, terms); 166274cf249dSArnaldo Carvalho de Melo zfree(&data.terms); 166390e2b22dSJiri Olsa return 0; 166490e2b22dSJiri Olsa } 166590e2b22dSJiri Olsa 16662146afc6SArnaldo Carvalho de Melo parse_events_terms__delete(data.terms); 166790e2b22dSJiri Olsa return ret; 166890e2b22dSJiri Olsa } 166990e2b22dSJiri Olsa 1670b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1671b39b8393SJiri Olsa struct parse_events_error *err) 167286470930SIngo Molnar { 167323b6339bSArnaldo Carvalho de Melo struct parse_events_evlist data = { 167446010ab2SJiri Olsa .list = LIST_HEAD_INIT(data.list), 167546010ab2SJiri Olsa .idx = evlist->nr_entries, 1676b39b8393SJiri Olsa .error = err, 16777630b3e2SWang Nan .evlist = evlist, 167846010ab2SJiri Olsa }; 167946010ab2SJiri Olsa int ret; 168086470930SIngo Molnar 168190e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1682dcb4e102SKan Liang perf_pmu__parse_cleanup(); 168389812fc8SJiri Olsa if (!ret) { 168415bfd2ccSWang Nan struct perf_evsel *last; 168515bfd2ccSWang Nan 1686854f7363SWang Nan if (list_empty(&data.list)) { 1687854f7363SWang Nan WARN_ONCE(true, "WARNING: event parser found nothing"); 1688854f7363SWang Nan return -1; 1689854f7363SWang Nan } 1690854f7363SWang Nan 1691f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &data.list); 169297f63e4aSNamhyung Kim evlist->nr_groups += data.nr_groups; 169315bfd2ccSWang Nan last = perf_evlist__last(evlist); 169415bfd2ccSWang Nan last->cmdline_group_boundary = true; 169515bfd2ccSWang Nan 169686470930SIngo Molnar return 0; 169786470930SIngo Molnar } 169886470930SIngo Molnar 16995d7be90eSJiri Olsa /* 17005d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 17015d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 17025d7be90eSJiri Olsa * need to bother. 17035d7be90eSJiri Olsa */ 170489812fc8SJiri Olsa return ret; 170589812fc8SJiri Olsa } 170689812fc8SJiri Olsa 1707b39b8393SJiri Olsa #define MAX_WIDTH 1000 1708b39b8393SJiri Olsa static int get_term_width(void) 1709b39b8393SJiri Olsa { 1710b39b8393SJiri Olsa struct winsize ws; 1711b39b8393SJiri Olsa 1712b39b8393SJiri Olsa get_term_dimensions(&ws); 1713b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1714b39b8393SJiri Olsa } 1715b39b8393SJiri Olsa 1716b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err, 1717b39b8393SJiri Olsa const char *event) 1718b39b8393SJiri Olsa { 1719b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1720b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1721b39b8393SJiri Olsa char *buf = (char *) event; 1722b39b8393SJiri Olsa int idx = 0; 1723b39b8393SJiri Olsa 1724b39b8393SJiri Olsa if (err->str) { 1725b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1726b39b8393SJiri Olsa int width = get_term_width() - 2; 1727b39b8393SJiri Olsa int len_event = strlen(event); 1728b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1729b39b8393SJiri Olsa 1730b39b8393SJiri Olsa /* 1731b39b8393SJiri Olsa * Maximum error index indent, we will cut 1732b39b8393SJiri Olsa * the event string if it's bigger. 1733b39b8393SJiri Olsa */ 1734141b2d31SAdrian Hunter int max_err_idx = 13; 1735b39b8393SJiri Olsa 1736b39b8393SJiri Olsa /* 1737b39b8393SJiri Olsa * Let's be specific with the message when 1738b39b8393SJiri Olsa * we have the precise error. 1739b39b8393SJiri Olsa */ 1740b39b8393SJiri Olsa str = "event syntax error: "; 1741b39b8393SJiri Olsa len_str = strlen(str); 1742b39b8393SJiri Olsa max_len = width - len_str; 1743b39b8393SJiri Olsa 1744b39b8393SJiri Olsa buf = _buf; 1745b39b8393SJiri Olsa 1746bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 1747b39b8393SJiri Olsa if (err->idx > max_err_idx) 1748b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1749b39b8393SJiri Olsa 1750b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1751b39b8393SJiri Olsa 1752b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1753b39b8393SJiri Olsa if (cut) 1754b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1755b39b8393SJiri Olsa 1756b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1757b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1758b39b8393SJiri Olsa buf[max_len] = 0; 1759b39b8393SJiri Olsa } 1760b39b8393SJiri Olsa 1761b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1762b39b8393SJiri Olsa } 1763b39b8393SJiri Olsa 1764b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1765b39b8393SJiri Olsa if (idx) { 1766b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1767b39b8393SJiri Olsa if (err->help) 1768b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1769360e071bSTaeung Song zfree(&err->str); 1770360e071bSTaeung Song zfree(&err->help); 1771b39b8393SJiri Olsa } 1772b39b8393SJiri Olsa 1773b39b8393SJiri Olsa fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1774b39b8393SJiri Olsa } 1775b39b8393SJiri Olsa 1776b39b8393SJiri Olsa #undef MAX_WIDTH 1777b39b8393SJiri Olsa 1778f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 17791d037ca1SIrina Tirdea int unset __maybe_unused) 1780f120f9d5SJiri Olsa { 1781f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1782b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1783b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 17849175ce1fSAndi Kleen 1785b39b8393SJiri Olsa if (ret) 1786b39b8393SJiri Olsa parse_events_print_error(&err, str); 1787b39b8393SJiri Olsa 17889175ce1fSAndi Kleen return ret; 1789f120f9d5SJiri Olsa } 1790f120f9d5SJiri Olsa 17914ba1faa1SWang Nan static int 17924ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 17934ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 17944ba1faa1SWang Nan const void *arg), 17954ba1faa1SWang Nan const void *arg) 1796c171b552SLi Zefan { 179769aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 17984ba1faa1SWang Nan int err; 1799c171b552SLi Zefan 1800854f7363SWang Nan /* 1801854f7363SWang Nan * Don't return when list_empty, give func a chance to report 1802854f7363SWang Nan * error when it found last == NULL. 1803854f7363SWang Nan * 1804854f7363SWang Nan * So no need to WARN here, let *func do this. 1805854f7363SWang Nan */ 1806361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 18070c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 180869aad6f1SArnaldo Carvalho de Melo 180915bfd2ccSWang Nan do { 18104ba1faa1SWang Nan err = (*func)(last, arg); 18114ba1faa1SWang Nan if (err) 1812c171b552SLi Zefan return -1; 18134ba1faa1SWang Nan if (!last) 18144ba1faa1SWang Nan return 0; 1815c171b552SLi Zefan 181615bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 181715bfd2ccSWang Nan return 0; 181815bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 181915bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 182015bfd2ccSWang Nan 1821c171b552SLi Zefan return 0; 1822c171b552SLi Zefan } 1823c171b552SLi Zefan 18244ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 18254ba1faa1SWang Nan { 18264ba1faa1SWang Nan const char *str = arg; 18271e857484SMathieu Poirier bool found = false; 18281e857484SMathieu Poirier int nr_addr_filters = 0; 18291e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 18304ba1faa1SWang Nan 18311e857484SMathieu Poirier if (evsel == NULL) 18321e857484SMathieu Poirier goto err; 18334ba1faa1SWang Nan 18341e857484SMathieu Poirier if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 18353541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 18364ba1faa1SWang Nan fprintf(stderr, 18374ba1faa1SWang Nan "not enough memory to hold filter string\n"); 18384ba1faa1SWang Nan return -1; 18394ba1faa1SWang Nan } 18404ba1faa1SWang Nan 18414ba1faa1SWang Nan return 0; 18424ba1faa1SWang Nan } 18434ba1faa1SWang Nan 18441e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 18451e857484SMathieu Poirier if (pmu->type == evsel->attr.type) { 18461e857484SMathieu Poirier found = true; 18471e857484SMathieu Poirier break; 18481e857484SMathieu Poirier } 18491e857484SMathieu Poirier 18501e857484SMathieu Poirier if (found) 18511e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 18521e857484SMathieu Poirier "%d", &nr_addr_filters); 18531e857484SMathieu Poirier 18541e857484SMathieu Poirier if (!nr_addr_filters) 18551e857484SMathieu Poirier goto err; 18561e857484SMathieu Poirier 18571e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 18581e857484SMathieu Poirier fprintf(stderr, 18591e857484SMathieu Poirier "not enough memory to hold filter string\n"); 18601e857484SMathieu Poirier return -1; 18611e857484SMathieu Poirier } 18621e857484SMathieu Poirier 18631e857484SMathieu Poirier return 0; 18641e857484SMathieu Poirier 18651e857484SMathieu Poirier err: 18661e857484SMathieu Poirier fprintf(stderr, 18671e857484SMathieu Poirier "--filter option should follow a -e tracepoint or HW tracer option\n"); 18681e857484SMathieu Poirier 18691e857484SMathieu Poirier return -1; 18701e857484SMathieu Poirier } 18711e857484SMathieu Poirier 18724ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 18734ba1faa1SWang Nan int unset __maybe_unused) 18744ba1faa1SWang Nan { 18754ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 18764ba1faa1SWang Nan 18774ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 18784ba1faa1SWang Nan (const void *)str); 18794ba1faa1SWang Nan } 18804ba1faa1SWang Nan 18814ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 18824ba1faa1SWang Nan const void *arg __maybe_unused) 18834ba1faa1SWang Nan { 18844ba1faa1SWang Nan char new_filter[64]; 18854ba1faa1SWang Nan 18864ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 18874ba1faa1SWang Nan fprintf(stderr, 18884ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 18894ba1faa1SWang Nan return -1; 18904ba1faa1SWang Nan } 18914ba1faa1SWang Nan 18924ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 18934ba1faa1SWang Nan 18943541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 18954ba1faa1SWang Nan fprintf(stderr, 18964ba1faa1SWang Nan "not enough memory to hold filter string\n"); 18974ba1faa1SWang Nan return -1; 18984ba1faa1SWang Nan } 18994ba1faa1SWang Nan 19004ba1faa1SWang Nan return 0; 19014ba1faa1SWang Nan } 19024ba1faa1SWang Nan 19034ba1faa1SWang Nan int exclude_perf(const struct option *opt, 19044ba1faa1SWang Nan const char *arg __maybe_unused, 19054ba1faa1SWang Nan int unset __maybe_unused) 19064ba1faa1SWang Nan { 19074ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 19084ba1faa1SWang Nan 19094ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 19104ba1faa1SWang Nan NULL); 19114ba1faa1SWang Nan } 19124ba1faa1SWang Nan 191386470930SIngo Molnar static const char * const event_type_descriptors[] = { 191486470930SIngo Molnar "Hardware event", 191586470930SIngo Molnar "Software event", 191686470930SIngo Molnar "Tracepoint event", 191786470930SIngo Molnar "Hardware cache event", 191841bdcb23SLiming Wang "Raw hardware event descriptor", 191941bdcb23SLiming Wang "Hardware breakpoint", 192086470930SIngo Molnar }; 192186470930SIngo Molnar 1922ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1923ab0e4800SYunlong Song { 1924ab0e4800SYunlong Song const char * const *as = a; 1925ab0e4800SYunlong Song const char * const *bs = b; 1926ab0e4800SYunlong Song 1927ab0e4800SYunlong Song return strcmp(*as, *bs); 1928ab0e4800SYunlong Song } 1929ab0e4800SYunlong Song 193086470930SIngo Molnar /* 1931f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1932f6bdafefSJason Baron */ 1933f6bdafefSJason Baron 1934a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1935a3277d2dSFrederic Weisbecker bool name_only) 1936f6bdafefSJason Baron { 1937f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 193822a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1939f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1940725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1941ab0e4800SYunlong Song char **evt_list = NULL; 1942ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1943ab0e4800SYunlong Song bool evt_num_known = false; 1944f6bdafefSJason Baron 1945ab0e4800SYunlong Song restart: 1946ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1947f6bdafefSJason Baron if (!sys_dir) 1948725b1368SEric Dumazet return; 1949f6bdafefSJason Baron 1950ab0e4800SYunlong Song if (evt_num_known) { 1951ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1952ab0e4800SYunlong Song if (!evt_list) 1953ab0e4800SYunlong Song goto out_close_sys_dir; 1954ab0e4800SYunlong Song } 1955ab0e4800SYunlong Song 195622a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 1957668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 195822a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 1959668b8788SArnaldo Carvalho de Melo continue; 1960725b1368SEric Dumazet 1961ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 196222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 1963725b1368SEric Dumazet evt_dir = opendir(dir_path); 1964725b1368SEric Dumazet if (!evt_dir) 19656b58e7f1SUlrich Drepper continue; 1966725b1368SEric Dumazet 196722a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 1968668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 196922a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 1970668b8788SArnaldo Carvalho de Melo continue; 1971668b8788SArnaldo Carvalho de Melo 1972ab0e4800SYunlong Song if (!evt_num_known) { 1973ab0e4800SYunlong Song evt_num++; 1974a3277d2dSFrederic Weisbecker continue; 1975a3277d2dSFrederic Weisbecker } 1976a3277d2dSFrederic Weisbecker 1977f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 197822a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 1979ab0e4800SYunlong Song 1980ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 1981ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1982ab0e4800SYunlong Song goto out_close_evt_dir; 1983ab0e4800SYunlong Song evt_i++; 1984f6bdafefSJason Baron } 1985f6bdafefSJason Baron closedir(evt_dir); 1986f6bdafefSJason Baron } 1987f6bdafefSJason Baron closedir(sys_dir); 1988ab0e4800SYunlong Song 1989ab0e4800SYunlong Song if (!evt_num_known) { 1990ab0e4800SYunlong Song evt_num_known = true; 1991ab0e4800SYunlong Song goto restart; 1992ab0e4800SYunlong Song } 1993ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1994ab0e4800SYunlong Song evt_i = 0; 1995ab0e4800SYunlong Song while (evt_i < evt_num) { 1996ab0e4800SYunlong Song if (name_only) { 1997ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1998ab0e4800SYunlong Song continue; 1999ab0e4800SYunlong Song } 2000ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2001ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2002ab0e4800SYunlong Song } 2003dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2004ab0e4800SYunlong Song printf("\n"); 2005ab0e4800SYunlong Song 2006ab0e4800SYunlong Song out_free: 2007ab0e4800SYunlong Song evt_num = evt_i; 2008ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2009ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2010ab0e4800SYunlong Song zfree(&evt_list); 2011ab0e4800SYunlong Song return; 2012ab0e4800SYunlong Song 2013ab0e4800SYunlong Song out_close_evt_dir: 2014ab0e4800SYunlong Song closedir(evt_dir); 2015ab0e4800SYunlong Song out_close_sys_dir: 2016ab0e4800SYunlong Song closedir(sys_dir); 2017ab0e4800SYunlong Song 2018ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2019ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2020ab0e4800SYunlong Song if (evt_list) 2021ab0e4800SYunlong Song goto out_free; 2022f6bdafefSJason Baron } 2023f6bdafefSJason Baron 2024f6bdafefSJason Baron /* 202520c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 202620c457b8SThomas Renninger */ 202720c457b8SThomas Renninger 202820c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 202920c457b8SThomas Renninger { 203020c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 203122a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 203220c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 203320c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 203420c457b8SThomas Renninger 2035ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 203620c457b8SThomas Renninger if (!sys_dir) 203720c457b8SThomas Renninger return 0; 203820c457b8SThomas Renninger 203922a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 204020c457b8SThomas Renninger 2041ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 204222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 204320c457b8SThomas Renninger evt_dir = opendir(dir_path); 204420c457b8SThomas Renninger if (!evt_dir) 204520c457b8SThomas Renninger continue; 204620c457b8SThomas Renninger 204722a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 204820c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 204922a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 205020c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 205120c457b8SThomas Renninger closedir(evt_dir); 205220c457b8SThomas Renninger closedir(sys_dir); 205320c457b8SThomas Renninger return 1; 205420c457b8SThomas Renninger } 205520c457b8SThomas Renninger } 205620c457b8SThomas Renninger closedir(evt_dir); 205720c457b8SThomas Renninger } 205820c457b8SThomas Renninger closedir(sys_dir); 205920c457b8SThomas Renninger return 0; 206020c457b8SThomas Renninger } 206120c457b8SThomas Renninger 2062b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2063b41f1cecSNamhyung Kim { 2064b41f1cecSNamhyung Kim bool ret = true; 206588fee52eSVince Weaver int open_return; 2066b41f1cecSNamhyung Kim struct perf_evsel *evsel; 2067b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2068b41f1cecSNamhyung Kim .type = type, 2069b41f1cecSNamhyung Kim .config = config, 2070b41f1cecSNamhyung Kim .disabled = 1, 2071b41f1cecSNamhyung Kim }; 207289896051SArnaldo Carvalho de Melo struct thread_map *tmap = thread_map__new_by_tid(0); 207389896051SArnaldo Carvalho de Melo 207489896051SArnaldo Carvalho de Melo if (tmap == NULL) 207589896051SArnaldo Carvalho de Melo return false; 2076b41f1cecSNamhyung Kim 2077ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 2078b41f1cecSNamhyung Kim if (evsel) { 207989896051SArnaldo Carvalho de Melo open_return = perf_evsel__open(evsel, NULL, tmap); 208088fee52eSVince Weaver ret = open_return >= 0; 208188fee52eSVince Weaver 208288fee52eSVince Weaver if (open_return == -EACCES) { 208388fee52eSVince Weaver /* 208488fee52eSVince Weaver * This happens if the paranoid value 208588fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 208688fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 208788fee52eSVince Weaver * by default as some ARM machines do not support it. 208888fee52eSVince Weaver * 208988fee52eSVince Weaver */ 209088fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 209189896051SArnaldo Carvalho de Melo ret = perf_evsel__open(evsel, NULL, tmap) >= 0; 209288fee52eSVince Weaver } 2093b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 2094b41f1cecSNamhyung Kim } 2095b41f1cecSNamhyung Kim 2096b41f1cecSNamhyung Kim return ret; 2097b41f1cecSNamhyung Kim } 2098b41f1cecSNamhyung Kim 209940218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 210040218daeSMasami Hiramatsu bool name_only) 210140218daeSMasami Hiramatsu { 210240218daeSMasami Hiramatsu struct probe_cache *pcache; 210340218daeSMasami Hiramatsu struct probe_cache_entry *ent; 210440218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 210540218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 210640218daeSMasami Hiramatsu struct str_node *nd, *nd2; 210740218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 210840218daeSMasami Hiramatsu bool show_detail = false; 210940218daeSMasami Hiramatsu int ret; 211040218daeSMasami Hiramatsu 211140218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 211240218daeSMasami Hiramatsu if (!sdtlist) { 211340218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 211440218daeSMasami Hiramatsu return; 211540218daeSMasami Hiramatsu } 211640218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 211740218daeSMasami Hiramatsu if (!bidlist) { 211840218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 211940218daeSMasami Hiramatsu return; 212040218daeSMasami Hiramatsu } 212140218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 212240218daeSMasami Hiramatsu pcache = probe_cache__new(nd->s); 212340218daeSMasami Hiramatsu if (!pcache) 212440218daeSMasami Hiramatsu continue; 212540218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 212640218daeSMasami Hiramatsu if (!ent->sdt) 212740218daeSMasami Hiramatsu continue; 212840218daeSMasami Hiramatsu if (subsys_glob && 212940218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 213040218daeSMasami Hiramatsu continue; 213140218daeSMasami Hiramatsu if (event_glob && 213240218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 213340218daeSMasami Hiramatsu continue; 213440218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 213540218daeSMasami Hiramatsu ent->pev.event, nd->s); 213640218daeSMasami Hiramatsu if (ret > 0) 213740218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 213840218daeSMasami Hiramatsu } 213940218daeSMasami Hiramatsu probe_cache__delete(pcache); 214040218daeSMasami Hiramatsu } 214140218daeSMasami Hiramatsu strlist__delete(bidlist); 214240218daeSMasami Hiramatsu 214340218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 214440218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 214540218daeSMasami Hiramatsu if (buf) 214640218daeSMasami Hiramatsu *(buf++) = '\0'; 214740218daeSMasami Hiramatsu if (name_only) { 214840218daeSMasami Hiramatsu printf("%s ", nd->s); 214940218daeSMasami Hiramatsu continue; 215040218daeSMasami Hiramatsu } 215140218daeSMasami Hiramatsu nd2 = strlist__next(nd); 215240218daeSMasami Hiramatsu if (nd2) { 215340218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 215440218daeSMasami Hiramatsu if (ptr) 215540218daeSMasami Hiramatsu *ptr = '\0'; 215640218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 215740218daeSMasami Hiramatsu show_detail = true; 215840218daeSMasami Hiramatsu } 215940218daeSMasami Hiramatsu if (show_detail) { 216040218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 216140218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 216240218daeSMasami Hiramatsu if (ret > 0) { 216340218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 216440218daeSMasami Hiramatsu free(buf); 216540218daeSMasami Hiramatsu } 216640218daeSMasami Hiramatsu } else 216740218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 216840218daeSMasami Hiramatsu if (nd2) { 216940218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 217040218daeSMasami Hiramatsu show_detail = false; 217140218daeSMasami Hiramatsu if (ptr) 217240218daeSMasami Hiramatsu *ptr = '@'; 217340218daeSMasami Hiramatsu } 217440218daeSMasami Hiramatsu } 217540218daeSMasami Hiramatsu strlist__delete(sdtlist); 217640218daeSMasami Hiramatsu } 217740218daeSMasami Hiramatsu 2178a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2179668b8788SArnaldo Carvalho de Melo { 2180ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 21810b668bc9SArnaldo Carvalho de Melo char name[64]; 2182ab0e4800SYunlong Song char **evt_list = NULL; 2183ab0e4800SYunlong Song bool evt_num_known = false; 2184ab0e4800SYunlong Song 2185ab0e4800SYunlong Song restart: 2186ab0e4800SYunlong Song if (evt_num_known) { 2187ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2188ab0e4800SYunlong Song if (!evt_list) 2189ab0e4800SYunlong Song goto out_enomem; 2190ab0e4800SYunlong Song } 2191668b8788SArnaldo Carvalho de Melo 2192668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2193668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2194668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 21950b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2196668b8788SArnaldo Carvalho de Melo continue; 2197668b8788SArnaldo Carvalho de Melo 2198668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 21990b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 22000b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 2201947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2202668b8788SArnaldo Carvalho de Melo continue; 2203668b8788SArnaldo Carvalho de Melo 2204b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2205b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2206b41f1cecSNamhyung Kim continue; 2207b41f1cecSNamhyung Kim 2208ab0e4800SYunlong Song if (!evt_num_known) { 2209ab0e4800SYunlong Song evt_num++; 2210ab0e4800SYunlong Song continue; 2211ab0e4800SYunlong Song } 2212ab0e4800SYunlong Song 2213ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2214ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2215ab0e4800SYunlong Song goto out_enomem; 2216ab0e4800SYunlong Song evt_i++; 2217668b8788SArnaldo Carvalho de Melo } 2218668b8788SArnaldo Carvalho de Melo } 2219668b8788SArnaldo Carvalho de Melo } 2220668b8788SArnaldo Carvalho de Melo 2221ab0e4800SYunlong Song if (!evt_num_known) { 2222ab0e4800SYunlong Song evt_num_known = true; 2223ab0e4800SYunlong Song goto restart; 2224ab0e4800SYunlong Song } 2225ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2226ab0e4800SYunlong Song evt_i = 0; 2227ab0e4800SYunlong Song while (evt_i < evt_num) { 2228ab0e4800SYunlong Song if (name_only) { 2229ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2230ab0e4800SYunlong Song continue; 2231ab0e4800SYunlong Song } 2232ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2233ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2234ab0e4800SYunlong Song } 2235dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2236dc098b35SAndi Kleen printf("\n"); 2237ab0e4800SYunlong Song 2238ab0e4800SYunlong Song out_free: 2239ab0e4800SYunlong Song evt_num = evt_i; 2240ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2241ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2242ab0e4800SYunlong Song zfree(&evt_list); 2243ab0e4800SYunlong Song return evt_num; 2244ab0e4800SYunlong Song 2245ab0e4800SYunlong Song out_enomem: 2246ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2247ab0e4800SYunlong Song if (evt_list) 2248ab0e4800SYunlong Song goto out_free; 2249ab0e4800SYunlong Song return evt_num; 2250668b8788SArnaldo Carvalho de Melo } 2251668b8788SArnaldo Carvalho de Melo 2252705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2253a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2254a3277d2dSFrederic Weisbecker bool name_only) 225586470930SIngo Molnar { 2256ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2257947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2258ab0e4800SYunlong Song char **evt_list = NULL; 2259ab0e4800SYunlong Song bool evt_num_known = false; 2260ab0e4800SYunlong Song 2261ab0e4800SYunlong Song restart: 2262ab0e4800SYunlong Song if (evt_num_known) { 2263ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2264ab0e4800SYunlong Song if (!evt_list) 2265ab0e4800SYunlong Song goto out_enomem; 2266ab0e4800SYunlong Song syms -= max; 2267ab0e4800SYunlong Song } 226886470930SIngo Molnar 22691dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2270668b8788SArnaldo Carvalho de Melo 2271e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2272668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2273668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2274668b8788SArnaldo Carvalho de Melo continue; 227586470930SIngo Molnar 2276b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2277b41f1cecSNamhyung Kim continue; 2278b41f1cecSNamhyung Kim 2279ab0e4800SYunlong Song if (!evt_num_known) { 2280ab0e4800SYunlong Song evt_num++; 2281a3277d2dSFrederic Weisbecker continue; 2282a3277d2dSFrederic Weisbecker } 2283a3277d2dSFrederic Weisbecker 2284ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2285947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 228674d5b588SJaswinder Singh Rajput else 2287947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 228886470930SIngo Molnar 2289ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2290ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2291ab0e4800SYunlong Song goto out_enomem; 2292ab0e4800SYunlong Song evt_i++; 229386470930SIngo Molnar } 229486470930SIngo Molnar 2295ab0e4800SYunlong Song if (!evt_num_known) { 2296ab0e4800SYunlong Song evt_num_known = true; 2297ab0e4800SYunlong Song goto restart; 2298ab0e4800SYunlong Song } 2299ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2300ab0e4800SYunlong Song evt_i = 0; 2301ab0e4800SYunlong Song while (evt_i < evt_num) { 2302ab0e4800SYunlong Song if (name_only) { 2303ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2304ab0e4800SYunlong Song continue; 2305ab0e4800SYunlong Song } 2306ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2307ab0e4800SYunlong Song } 2308dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2309689d3018SMarti Raudsepp printf("\n"); 2310ab0e4800SYunlong Song 2311ab0e4800SYunlong Song out_free: 2312ab0e4800SYunlong Song evt_num = evt_i; 2313ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2314ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2315ab0e4800SYunlong Song zfree(&evt_list); 2316ab0e4800SYunlong Song return; 2317ab0e4800SYunlong Song 2318ab0e4800SYunlong Song out_enomem: 2319ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2320ab0e4800SYunlong Song if (evt_list) 2321ab0e4800SYunlong Song goto out_free; 2322668b8788SArnaldo Carvalho de Melo } 23231dc12760SJiri Olsa 23241dc12760SJiri Olsa /* 23251dc12760SJiri Olsa * Print the help text for the event symbols: 23261dc12760SJiri Olsa */ 2327c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2328*bf874fcfSAndi Kleen bool long_desc, bool details_flag) 23291dc12760SJiri Olsa { 23301dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2331a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 23321dc12760SJiri Olsa 23331dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2334a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 23351dc12760SJiri Olsa 2336a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 233773c24cb8SJaswinder Singh Rajput 2338*bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 2339*bf874fcfSAndi Kleen details_flag); 2340dc098b35SAndi Kleen 2341668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2342668b8788SArnaldo Carvalho de Melo return; 234373c24cb8SJaswinder Singh Rajput 2344a3277d2dSFrederic Weisbecker if (!name_only) { 2345947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23465f537a26SJiri Olsa "rNNN", 23471cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 23485f537a26SJiri Olsa printf(" %-50s [%s]\n", 23495f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 23505f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2351dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2352dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 235386470930SIngo Molnar 2354947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23553741eb9fSJacob Shin "mem:<addr>[/len][:access]", 235641bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2357dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 23581b290d67SFrederic Weisbecker printf("\n"); 2359a3277d2dSFrederic Weisbecker } 23601b290d67SFrederic Weisbecker 2361a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 236240218daeSMasami Hiramatsu 236340218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 236486470930SIngo Molnar } 23658f707d84SJiri Olsa 23666cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 23678f707d84SJiri Olsa { 236816fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 23698f707d84SJiri Olsa } 23708f707d84SJiri Olsa 237167b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 237267b49b38SJiri Olsa struct parse_events_term *temp, 237367b49b38SJiri Olsa char *str, u64 num) 23748f707d84SJiri Olsa { 23756cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 23768f707d84SJiri Olsa 237767b49b38SJiri Olsa term = malloc(sizeof(*term)); 23788f707d84SJiri Olsa if (!term) 23798f707d84SJiri Olsa return -ENOMEM; 23808f707d84SJiri Olsa 238167b49b38SJiri Olsa *term = *temp; 23828f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 23838f707d84SJiri Olsa 238467b49b38SJiri Olsa switch (term->type_val) { 23858f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 23868f707d84SJiri Olsa term->val.num = num; 23878f707d84SJiri Olsa break; 23888f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 23898f707d84SJiri Olsa term->val.str = str; 23908f707d84SJiri Olsa break; 23918f707d84SJiri Olsa default: 23924be8be6bSAdrian Hunter free(term); 23938f707d84SJiri Olsa return -EINVAL; 23948f707d84SJiri Olsa } 23958f707d84SJiri Olsa 23968f707d84SJiri Olsa *_term = term; 23978f707d84SJiri Olsa return 0; 23988f707d84SJiri Olsa } 23998f707d84SJiri Olsa 24006cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2401cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 240299e7138eSJiri Olsa bool no_value, 2403bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 240416fa7e82SJiri Olsa { 2405bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2406bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2407bb78ce7dSAdrian Hunter 240867b49b38SJiri Olsa struct parse_events_term temp = { 240967b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 241067b49b38SJiri Olsa .type_term = type_term, 241167b49b38SJiri Olsa .config = config, 241299e7138eSJiri Olsa .no_value = no_value, 241367b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 241467b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 241567b49b38SJiri Olsa }; 241667b49b38SJiri Olsa 241767b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 241816fa7e82SJiri Olsa } 241916fa7e82SJiri Olsa 24206cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2421cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2422bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 242316fa7e82SJiri Olsa { 2424bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2425bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2426bb78ce7dSAdrian Hunter 242767b49b38SJiri Olsa struct parse_events_term temp = { 242867b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 242967b49b38SJiri Olsa .type_term = type_term, 243067b49b38SJiri Olsa .config = config, 243167b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 243267b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 243367b49b38SJiri Olsa }; 243467b49b38SJiri Olsa 243567b49b38SJiri Olsa return new_term(term, &temp, str, 0); 243616fa7e82SJiri Olsa } 243716fa7e82SJiri Olsa 24386cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 24391d33d6dcSJiri Olsa char *config, unsigned idx) 24401d33d6dcSJiri Olsa { 24411d33d6dcSJiri Olsa struct event_symbol *sym; 244267b49b38SJiri Olsa struct parse_events_term temp = { 244367b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 244467b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 244567b49b38SJiri Olsa .config = config ?: (char *) "event", 244667b49b38SJiri Olsa }; 24471d33d6dcSJiri Olsa 24481d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 24491d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 24501d33d6dcSJiri Olsa 245167b49b38SJiri Olsa return new_term(term, &temp, (char *) sym->symbol, 0); 24521d33d6dcSJiri Olsa } 24531d33d6dcSJiri Olsa 24546cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 24556cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2456a6146d50SZheng Yan { 245767b49b38SJiri Olsa struct parse_events_term temp = { 245867b49b38SJiri Olsa .type_val = term->type_val, 245967b49b38SJiri Olsa .type_term = term->type_term, 246067b49b38SJiri Olsa .config = term->config, 246167b49b38SJiri Olsa .err_term = term->err_term, 246267b49b38SJiri Olsa .err_val = term->err_val, 246367b49b38SJiri Olsa }; 246467b49b38SJiri Olsa 246567b49b38SJiri Olsa return new_term(new, &temp, term->val.str, term->val.num); 2466a6146d50SZheng Yan } 2467a6146d50SZheng Yan 24688255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 24698255718fSAndi Kleen struct list_head **new) 24708255718fSAndi Kleen { 24718255718fSAndi Kleen struct parse_events_term *term, *n; 24728255718fSAndi Kleen int ret; 24738255718fSAndi Kleen 24748255718fSAndi Kleen if (!old) { 24758255718fSAndi Kleen *new = NULL; 24768255718fSAndi Kleen return 0; 24778255718fSAndi Kleen } 24788255718fSAndi Kleen 24798255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 24808255718fSAndi Kleen if (!*new) 24818255718fSAndi Kleen return -ENOMEM; 24828255718fSAndi Kleen INIT_LIST_HEAD(*new); 24838255718fSAndi Kleen 24848255718fSAndi Kleen list_for_each_entry (term, old, list) { 24858255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 24868255718fSAndi Kleen if (ret) 24878255718fSAndi Kleen return ret; 24888255718fSAndi Kleen list_add_tail(&n->list, *new); 24898255718fSAndi Kleen } 24908255718fSAndi Kleen return 0; 24918255718fSAndi Kleen } 24928255718fSAndi Kleen 2493fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 24948f707d84SJiri Olsa { 24956cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 24968f707d84SJiri Olsa 2497a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 24982d055bf2SWang Nan if (term->array.nr_ranges) 2499360e071bSTaeung Song zfree(&term->array.ranges); 2500a8adfcebSWang Nan list_del_init(&term->list); 25018f707d84SJiri Olsa free(term); 25028f707d84SJiri Olsa } 2503a8adfcebSWang Nan } 2504b39b8393SJiri Olsa 25052146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2506fc0a2c1dSArnaldo Carvalho de Melo { 25072146afc6SArnaldo Carvalho de Melo if (!terms) 25082146afc6SArnaldo Carvalho de Melo return; 2509fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2510d20a5f2bSWang Nan free(terms); 2511fc0a2c1dSArnaldo Carvalho de Melo } 2512fc0a2c1dSArnaldo Carvalho de Melo 25132d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 25142d055bf2SWang Nan { 2515360e071bSTaeung Song zfree(&a->ranges); 25162d055bf2SWang Nan } 25172d055bf2SWang Nan 2518b39b8393SJiri Olsa void parse_events_evlist_error(struct parse_events_evlist *data, 2519b39b8393SJiri Olsa int idx, const char *str) 2520b39b8393SJiri Olsa { 2521b39b8393SJiri Olsa struct parse_events_error *err = data->error; 2522b39b8393SJiri Olsa 2523a6ced2beSAdrian Hunter if (!err) 2524a6ced2beSAdrian Hunter return; 2525b39b8393SJiri Olsa err->idx = idx; 2526b39b8393SJiri Olsa err->str = strdup(str); 2527b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 2528b39b8393SJiri Olsa } 2529ffeb883eSHe Kuang 253017cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 253117cb5f84SWang Nan { 253217cb5f84SWang Nan int i; 253317cb5f84SWang Nan bool first = true; 253417cb5f84SWang Nan 253517cb5f84SWang Nan buf[0] = '\0'; 253617cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 253717cb5f84SWang Nan const char *name = config_term_names[i]; 253817cb5f84SWang Nan 25391669e509SWang Nan if (!config_term_avail(i, NULL)) 25401669e509SWang Nan continue; 254117cb5f84SWang Nan if (!name) 254217cb5f84SWang Nan continue; 254317cb5f84SWang Nan if (name[0] == '<') 254417cb5f84SWang Nan continue; 254517cb5f84SWang Nan 254617cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 254717cb5f84SWang Nan return; 254817cb5f84SWang Nan 254917cb5f84SWang Nan if (!first) 255017cb5f84SWang Nan strcat(buf, ","); 255117cb5f84SWang Nan else 255217cb5f84SWang Nan first = false; 255317cb5f84SWang Nan strcat(buf, name); 255417cb5f84SWang Nan } 255517cb5f84SWang Nan } 255617cb5f84SWang Nan 2557ffeb883eSHe Kuang /* 2558ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2559ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2560ffeb883eSHe Kuang */ 2561ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2562ffeb883eSHe Kuang { 2563ffeb883eSHe Kuang char *str; 2564626a6b78SWang Nan /* "no-overwrite" is the longest name */ 256517cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2566626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2567ffeb883eSHe Kuang 256817cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2569ffeb883eSHe Kuang /* valid terms */ 2570ffeb883eSHe Kuang if (additional_terms) { 257126dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 257226dee028SWang Nan additional_terms, static_terms) < 0) 2573ffeb883eSHe Kuang goto fail; 2574ffeb883eSHe Kuang } else { 257526dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2576ffeb883eSHe Kuang goto fail; 2577ffeb883eSHe Kuang } 2578ffeb883eSHe Kuang return str; 2579ffeb883eSHe Kuang 2580ffeb883eSHe Kuang fail: 2581ffeb883eSHe Kuang return NULL; 2582ffeb883eSHe Kuang } 2583