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); 257*506fde11STaeung 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); 321f30a79b0SJiri Olsa 3229db1763cSArnaldo Carvalho de Melo if (name) 32389812fc8SJiri Olsa evsel->name = strdup(name); 324930a2e29SJiri Olsa 325930a2e29SJiri Olsa if (config_terms) 326930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 327930a2e29SJiri Olsa 328b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 329410136f5SStephane Eranian return evsel; 33089812fc8SJiri Olsa } 33189812fc8SJiri Olsa 332c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 333930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 334930a2e29SJiri Olsa struct list_head *config_terms) 3357ae92e74SYan, Zheng { 336930a2e29SJiri Olsa return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; 3377ae92e74SYan, Zheng } 3387ae92e74SYan, Zheng 3390b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 34086470930SIngo Molnar { 34186470930SIngo Molnar int i, j; 34261c45981SPaul Mackerras int n, longest = -1; 34386470930SIngo Molnar 34486470930SIngo Molnar for (i = 0; i < size; i++) { 3450b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 34661c45981SPaul Mackerras n = strlen(names[i][j]); 34789812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 34861c45981SPaul Mackerras longest = n; 34961c45981SPaul Mackerras } 35089812fc8SJiri Olsa if (longest > 0) 35186470930SIngo Molnar return i; 35286470930SIngo Molnar } 35386470930SIngo Molnar 3548953645fSIngo Molnar return -1; 35586470930SIngo Molnar } 35686470930SIngo Molnar 35743d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 35843d0b978SWang Nan struct parse_events_term *term, 35943d0b978SWang Nan struct parse_events_error *err); 36043d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 36143d0b978SWang Nan struct parse_events_term *term, 36243d0b978SWang Nan struct parse_events_error *err); 36343d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 36443d0b978SWang Nan struct list_head *head, 36543d0b978SWang Nan struct parse_events_error *err, 36643d0b978SWang Nan config_term_func_t config_term); 36743d0b978SWang Nan 368c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 36943d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3702c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 37143d0b978SWang Nan struct list_head *head_config) 37286470930SIngo Molnar { 37389812fc8SJiri Olsa struct perf_event_attr attr; 37443d0b978SWang Nan LIST_HEAD(config_terms); 37543d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 37661c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 37789812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 37889812fc8SJiri Olsa int i, n; 37986470930SIngo Molnar 38086470930SIngo Molnar /* 38186470930SIngo Molnar * No fallback - if we cannot get a clear cache type 38286470930SIngo Molnar * then bail out: 38386470930SIngo Molnar */ 3840b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 38589812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 38686470930SIngo Molnar if (cache_type == -1) 38789812fc8SJiri Olsa return -EINVAL; 38886470930SIngo Molnar 38943d0b978SWang Nan config_name = get_config_name(head_config); 39089812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 39189812fc8SJiri Olsa 39289812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 39389812fc8SJiri Olsa char *str = op_result[i]; 39489812fc8SJiri Olsa 395275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 39661c45981SPaul Mackerras 39761c45981SPaul Mackerras if (cache_op == -1) { 3980b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 39961c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 40061c45981SPaul Mackerras if (cache_op >= 0) { 4010b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 40289812fc8SJiri Olsa return -EINVAL; 40361c45981SPaul Mackerras continue; 40461c45981SPaul Mackerras } 40561c45981SPaul Mackerras } 40661c45981SPaul Mackerras 40761c45981SPaul Mackerras if (cache_result == -1) { 4080b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 40961c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 41061c45981SPaul Mackerras if (cache_result >= 0) 41161c45981SPaul Mackerras continue; 41261c45981SPaul Mackerras } 41361c45981SPaul Mackerras } 41461c45981SPaul Mackerras 41586470930SIngo Molnar /* 41686470930SIngo Molnar * Fall back to reads: 41786470930SIngo Molnar */ 4188953645fSIngo Molnar if (cache_op == -1) 4198953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 42086470930SIngo Molnar 42186470930SIngo Molnar /* 42286470930SIngo Molnar * Fall back to accesses: 42386470930SIngo Molnar */ 42486470930SIngo Molnar if (cache_result == -1) 42586470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 42686470930SIngo Molnar 42789812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 42889812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 42989812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 43043d0b978SWang Nan 43143d0b978SWang Nan if (head_config) { 4322c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 43343d0b978SWang Nan config_term_common)) 43443d0b978SWang Nan return -EINVAL; 43543d0b978SWang Nan 43643d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 43743d0b978SWang Nan return -ENOMEM; 43843d0b978SWang Nan } 43943d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 44086470930SIngo Molnar } 44186470930SIngo Molnar 442272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4438c619d6aSWang Nan const char *sys, const char *name) 44419658171SJiri Olsa { 44519658171SJiri Olsa char help[BUFSIZ]; 44619658171SJiri Olsa 447ec183d22SAdrian Hunter if (!e) 448ec183d22SAdrian Hunter return; 449ec183d22SAdrian Hunter 45019658171SJiri Olsa /* 45119658171SJiri Olsa * We get error directly from syscall errno ( > 0), 45219658171SJiri Olsa * or from encoded pointer's error ( < 0). 45319658171SJiri Olsa */ 45419658171SJiri Olsa err = abs(err); 45519658171SJiri Olsa 45619658171SJiri Olsa switch (err) { 45719658171SJiri Olsa case EACCES: 458272ed29aSJiri Olsa e->str = strdup("can't access trace events"); 45919658171SJiri Olsa break; 46019658171SJiri Olsa case ENOENT: 461272ed29aSJiri Olsa e->str = strdup("unknown tracepoint"); 46219658171SJiri Olsa break; 46319658171SJiri Olsa default: 464272ed29aSJiri Olsa e->str = strdup("failed to add tracepoint"); 46519658171SJiri Olsa break; 46619658171SJiri Olsa } 46719658171SJiri Olsa 46819658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 469272ed29aSJiri Olsa e->help = strdup(help); 47019658171SJiri Olsa } 47119658171SJiri Olsa 472c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4738c619d6aSWang Nan const char *sys_name, const char *evt_name, 474272ed29aSJiri Olsa struct parse_events_error *err, 475e637d177SHe Kuang struct list_head *head_config) 476bcd3279fSFrederic Weisbecker { 47782fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 478bcd3279fSFrederic Weisbecker 479ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 48019658171SJiri Olsa if (IS_ERR(evsel)) { 481272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4828dd2a131SJiri Olsa return PTR_ERR(evsel); 48319658171SJiri Olsa } 484bcd3279fSFrederic Weisbecker 485e637d177SHe Kuang if (head_config) { 486e637d177SHe Kuang LIST_HEAD(config_terms); 487e637d177SHe Kuang 488e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 489e637d177SHe Kuang return -ENOMEM; 490e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 491e637d177SHe Kuang } 492e637d177SHe Kuang 49382fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 49482fe1c29SArnaldo Carvalho de Melo return 0; 495bcd3279fSFrederic Weisbecker } 496bcd3279fSFrederic Weisbecker 497c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 4988c619d6aSWang Nan const char *sys_name, const char *evt_name, 499272ed29aSJiri Olsa struct parse_events_error *err, 500e637d177SHe Kuang struct list_head *head_config) 501bcd3279fSFrederic Weisbecker { 502bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 503bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 504bcd3279fSFrederic Weisbecker DIR *evt_dir; 50527bf90bfSJiri Olsa int ret = 0, found = 0; 506bcd3279fSFrederic Weisbecker 507ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 508bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 509bcd3279fSFrederic Weisbecker if (!evt_dir) { 510272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 51189812fc8SJiri Olsa return -1; 512bcd3279fSFrederic Weisbecker } 513bcd3279fSFrederic Weisbecker 51489812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 515bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 516bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 517bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 518bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 519bcd3279fSFrederic Weisbecker continue; 520bcd3279fSFrederic Weisbecker 52189812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 522fb1d2edfSMasami Hiramatsu continue; 523fb1d2edfSMasami Hiramatsu 52427bf90bfSJiri Olsa found++; 52527bf90bfSJiri Olsa 526e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 527272ed29aSJiri Olsa err, head_config); 528bcd3279fSFrederic Weisbecker } 529bcd3279fSFrederic Weisbecker 53027bf90bfSJiri Olsa if (!found) { 53127bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 53227bf90bfSJiri Olsa ret = -1; 53327bf90bfSJiri Olsa } 53427bf90bfSJiri Olsa 5350bd3f084SJiri Olsa closedir(evt_dir); 53689812fc8SJiri Olsa return ret; 537bcd3279fSFrederic Weisbecker } 538bcd3279fSFrederic Weisbecker 539c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5408c619d6aSWang Nan const char *sys_name, const char *evt_name, 541272ed29aSJiri Olsa struct parse_events_error *err, 542e637d177SHe Kuang struct list_head *head_config) 543f35488f9SJiri Olsa { 544f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 545e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 546272ed29aSJiri Olsa err, head_config) : 547e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 548272ed29aSJiri Olsa err, head_config); 549f35488f9SJiri Olsa } 550f35488f9SJiri Olsa 551c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5528c619d6aSWang Nan const char *sys_name, const char *evt_name, 553272ed29aSJiri Olsa struct parse_events_error *err, 554e637d177SHe Kuang struct list_head *head_config) 555f35488f9SJiri Olsa { 556f35488f9SJiri Olsa struct dirent *events_ent; 557f35488f9SJiri Olsa DIR *events_dir; 558f35488f9SJiri Olsa int ret = 0; 559f35488f9SJiri Olsa 560f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 561f35488f9SJiri Olsa if (!events_dir) { 562272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 563f35488f9SJiri Olsa return -1; 564f35488f9SJiri Olsa } 565f35488f9SJiri Olsa 566f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 567f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 568f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 569f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 570f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 571f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 572f35488f9SJiri Olsa continue; 573f35488f9SJiri Olsa 574f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 575f35488f9SJiri Olsa continue; 576f35488f9SJiri Olsa 577f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 578272ed29aSJiri Olsa evt_name, err, head_config); 579f35488f9SJiri Olsa } 580f35488f9SJiri Olsa 581f35488f9SJiri Olsa closedir(events_dir); 582f35488f9SJiri Olsa return ret; 583f35488f9SJiri Olsa } 584f35488f9SJiri Olsa 5854edf30e3SWang Nan struct __add_bpf_event_param { 5864edf30e3SWang Nan struct parse_events_evlist *data; 5874edf30e3SWang Nan struct list_head *list; 58895088a59SWang Nan struct list_head *head_config; 5894edf30e3SWang Nan }; 5904edf30e3SWang Nan 591cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd, 5924edf30e3SWang Nan void *_param) 5934edf30e3SWang Nan { 5944edf30e3SWang Nan LIST_HEAD(new_evsels); 5954edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 5964edf30e3SWang Nan struct parse_events_evlist *evlist = param->data; 5974edf30e3SWang Nan struct list_head *list = param->list; 5981f45b1d4SWang Nan struct perf_evsel *pos; 5994edf30e3SWang Nan int err; 6004edf30e3SWang Nan 6014edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 602cd102d70SWang Nan group, event, fd); 6034edf30e3SWang Nan 604cd102d70SWang Nan err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, group, 605cd102d70SWang Nan event, evlist->error, 60695088a59SWang Nan param->head_config); 6074edf30e3SWang Nan if (err) { 6084edf30e3SWang Nan struct perf_evsel *evsel, *tmp; 6094edf30e3SWang Nan 6104edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 611cd102d70SWang Nan group, event); 6124edf30e3SWang Nan list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { 6134edf30e3SWang Nan list_del(&evsel->node); 6144edf30e3SWang Nan perf_evsel__delete(evsel); 6154edf30e3SWang Nan } 6164edf30e3SWang Nan return err; 6174edf30e3SWang Nan } 618cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6194edf30e3SWang Nan 6201f45b1d4SWang Nan list_for_each_entry(pos, &new_evsels, node) { 6211f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 622cd102d70SWang Nan group, event, pos); 6231f45b1d4SWang Nan pos->bpf_fd = fd; 6241f45b1d4SWang Nan } 6254edf30e3SWang Nan list_splice(&new_evsels, list); 6264edf30e3SWang Nan return 0; 6274edf30e3SWang Nan } 6284edf30e3SWang Nan 62984c86ca1SWang Nan int parse_events_load_bpf_obj(struct parse_events_evlist *data, 63084c86ca1SWang Nan struct list_head *list, 63195088a59SWang Nan struct bpf_object *obj, 63295088a59SWang Nan struct list_head *head_config) 63384c86ca1SWang Nan { 63484c86ca1SWang Nan int err; 63584c86ca1SWang Nan char errbuf[BUFSIZ]; 63695088a59SWang Nan struct __add_bpf_event_param param = {data, list, head_config}; 637aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 63884c86ca1SWang Nan 63984c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 64084c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 64184c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 64284c86ca1SWang Nan err = -EINVAL; 64384c86ca1SWang Nan goto errout; 64484c86ca1SWang Nan } 64584c86ca1SWang Nan 64684c86ca1SWang Nan /* 647aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 648aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 649aa3abf30SWang Nan * created when failure. 650aa3abf30SWang Nan */ 651aa3abf30SWang Nan if (!registered_unprobe_atexit) { 652aa3abf30SWang Nan atexit(bpf__clear); 653aa3abf30SWang Nan registered_unprobe_atexit = true; 654aa3abf30SWang Nan } 655aa3abf30SWang Nan 656aa3abf30SWang Nan err = bpf__probe(obj); 657aa3abf30SWang Nan if (err) { 658aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 659aa3abf30SWang Nan goto errout; 660aa3abf30SWang Nan } 661aa3abf30SWang Nan 6621e5e3ee8SWang Nan err = bpf__load(obj); 6631e5e3ee8SWang Nan if (err) { 6641e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6651e5e3ee8SWang Nan goto errout; 6661e5e3ee8SWang Nan } 6671e5e3ee8SWang Nan 668cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6694edf30e3SWang Nan if (err) { 6704edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6714edf30e3SWang Nan "Attach events in BPF object failed"); 6724edf30e3SWang Nan goto errout; 6734edf30e3SWang Nan } 6744edf30e3SWang Nan 6754edf30e3SWang Nan return 0; 67684c86ca1SWang Nan errout: 67784c86ca1SWang Nan data->error->help = strdup("(add -v to see detail)"); 67884c86ca1SWang Nan data->error->str = strdup(errbuf); 67984c86ca1SWang Nan return err; 68084c86ca1SWang Nan } 68184c86ca1SWang Nan 682a34f3be7SWang Nan static int 683a34f3be7SWang Nan parse_events_config_bpf(struct parse_events_evlist *data, 684a34f3be7SWang Nan struct bpf_object *obj, 685a34f3be7SWang Nan struct list_head *head_config) 686a34f3be7SWang Nan { 687a34f3be7SWang Nan struct parse_events_term *term; 688a34f3be7SWang Nan int error_pos; 689a34f3be7SWang Nan 690a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 691a34f3be7SWang Nan return 0; 692a34f3be7SWang Nan 693a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 694a34f3be7SWang Nan char errbuf[BUFSIZ]; 695a34f3be7SWang Nan int err; 696a34f3be7SWang Nan 697a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 698a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 699a34f3be7SWang Nan "Invalid config term for BPF object"); 700a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 701a34f3be7SWang Nan 702a34f3be7SWang Nan data->error->idx = term->err_term; 703a34f3be7SWang Nan data->error->str = strdup(errbuf); 704a34f3be7SWang Nan return -EINVAL; 705a34f3be7SWang Nan } 706a34f3be7SWang Nan 7077630b3e2SWang Nan err = bpf__config_obj(obj, term, data->evlist, &error_pos); 708a34f3be7SWang Nan if (err) { 7097630b3e2SWang Nan bpf__strerror_config_obj(obj, term, data->evlist, 710a34f3be7SWang Nan &error_pos, err, errbuf, 711a34f3be7SWang Nan sizeof(errbuf)); 712a34f3be7SWang Nan data->error->help = strdup( 7137630b3e2SWang Nan "Hint:\tValid config terms:\n" 714e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 715e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7167630b3e2SWang Nan "\n" 717e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 718a34f3be7SWang Nan " \t(add -v to see detail)"); 719a34f3be7SWang Nan data->error->str = strdup(errbuf); 720a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 721a34f3be7SWang Nan data->error->idx = term->err_val; 722a34f3be7SWang Nan else 723a34f3be7SWang Nan data->error->idx = term->err_term + error_pos; 724a34f3be7SWang Nan return err; 725a34f3be7SWang Nan } 726a34f3be7SWang Nan } 727a34f3be7SWang Nan return 0; 728a34f3be7SWang Nan } 729a34f3be7SWang Nan 73095088a59SWang Nan /* 73195088a59SWang Nan * Split config terms: 73295088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 73395088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 73495088a59SWang Nan * events in bpf.c. 73595088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 73695088a59SWang Nan * with parse_events_config_bpf. 73795088a59SWang Nan * 73895088a59SWang Nan * Move object config terms from the first list to obj_head_config. 73995088a59SWang Nan */ 74095088a59SWang Nan static void 74195088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 74295088a59SWang Nan struct list_head *obj_head_config) 74395088a59SWang Nan { 74495088a59SWang Nan struct parse_events_term *term, *temp; 74595088a59SWang Nan 74695088a59SWang Nan /* 74795088a59SWang Nan * Currectly, all possible user config term 74895088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 74995088a59SWang Nan * happends to be a good flag. 75095088a59SWang Nan * 75195088a59SWang Nan * See parse_events_config_bpf() and 75295088a59SWang Nan * config_term_tracepoint(). 75395088a59SWang Nan */ 75495088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 75595088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 75695088a59SWang Nan list_move_tail(&term->list, obj_head_config); 75795088a59SWang Nan } 75895088a59SWang Nan 75984c86ca1SWang Nan int parse_events_load_bpf(struct parse_events_evlist *data, 76084c86ca1SWang Nan struct list_head *list, 761d509db04SWang Nan char *bpf_file_name, 762a34f3be7SWang Nan bool source, 763a34f3be7SWang Nan struct list_head *head_config) 76484c86ca1SWang Nan { 765a34f3be7SWang Nan int err; 76695088a59SWang Nan struct bpf_object *obj; 76795088a59SWang Nan LIST_HEAD(obj_head_config); 76895088a59SWang Nan 76995088a59SWang Nan if (head_config) 77095088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 77184c86ca1SWang Nan 772d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 7736371ca3bSWang Nan if (IS_ERR(obj)) { 77484c86ca1SWang Nan char errbuf[BUFSIZ]; 77584c86ca1SWang Nan 7766371ca3bSWang Nan err = PTR_ERR(obj); 77784c86ca1SWang Nan 77884c86ca1SWang Nan if (err == -ENOTSUP) 77984c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 78084c86ca1SWang Nan "BPF support is not compiled"); 78184c86ca1SWang Nan else 782d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 783d3e0ce39SWang Nan source, 784d3e0ce39SWang Nan -err, errbuf, 785d3e0ce39SWang Nan sizeof(errbuf)); 78684c86ca1SWang Nan 78784c86ca1SWang Nan data->error->help = strdup("(add -v to see detail)"); 78884c86ca1SWang Nan data->error->str = strdup(errbuf); 78984c86ca1SWang Nan return err; 79084c86ca1SWang Nan } 79184c86ca1SWang Nan 79295088a59SWang Nan err = parse_events_load_bpf_obj(data, list, obj, head_config); 793a34f3be7SWang Nan if (err) 794a34f3be7SWang Nan return err; 79595088a59SWang Nan err = parse_events_config_bpf(data, obj, &obj_head_config); 79695088a59SWang Nan 79795088a59SWang Nan /* 79895088a59SWang Nan * Caller doesn't know anything about obj_head_config, 79995088a59SWang Nan * so combine them together again before returnning. 80095088a59SWang Nan */ 80195088a59SWang Nan if (head_config) 80295088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 80395088a59SWang Nan return err; 80484c86ca1SWang Nan } 80584c86ca1SWang Nan 80689812fc8SJiri Olsa static int 80789812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8081b290d67SFrederic Weisbecker { 8091b290d67SFrederic Weisbecker int i; 8101b290d67SFrederic Weisbecker 8111b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 81289812fc8SJiri Olsa if (!type || !type[i]) 8131b290d67SFrederic Weisbecker break; 8141b290d67SFrederic Weisbecker 8157582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8167582732fSJiri Olsa do { \ 8177582732fSJiri Olsa if (attr->bp_type & bit) \ 8187582732fSJiri Olsa return -EINVAL; \ 8197582732fSJiri Olsa else \ 8207582732fSJiri Olsa attr->bp_type |= bit; \ 8217582732fSJiri Olsa } while (0) 8227582732fSJiri Olsa 8231b290d67SFrederic Weisbecker switch (type[i]) { 8241b290d67SFrederic Weisbecker case 'r': 8257582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8261b290d67SFrederic Weisbecker break; 8271b290d67SFrederic Weisbecker case 'w': 8287582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8291b290d67SFrederic Weisbecker break; 8301b290d67SFrederic Weisbecker case 'x': 8317582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8321b290d67SFrederic Weisbecker break; 8331b290d67SFrederic Weisbecker default: 83489812fc8SJiri Olsa return -EINVAL; 8351b290d67SFrederic Weisbecker } 8361b290d67SFrederic Weisbecker } 83789812fc8SJiri Olsa 8387582732fSJiri Olsa #undef CHECK_SET_TYPE 8397582732fSJiri Olsa 8401b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8411b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8421b290d67SFrederic Weisbecker 84389812fc8SJiri Olsa return 0; 8441b290d67SFrederic Weisbecker } 8451b290d67SFrederic Weisbecker 846c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 8473741eb9fSJacob Shin void *ptr, char *type, u64 len) 8481b290d67SFrederic Weisbecker { 84989812fc8SJiri Olsa struct perf_event_attr attr; 8501b290d67SFrederic Weisbecker 85189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 8529fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 8531b290d67SFrederic Weisbecker 85489812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 85589812fc8SJiri Olsa return -EINVAL; 8561b290d67SFrederic Weisbecker 8573741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 8583741eb9fSJacob Shin if (!len) { 85989812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 8603741eb9fSJacob Shin len = sizeof(long); 861aa59a485SFrederic Weisbecker else 8623741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 8633741eb9fSJacob Shin } 8643741eb9fSJacob Shin 8653741eb9fSJacob Shin attr.bp_len = len; 866aa59a485SFrederic Weisbecker 86789812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 8684a841d65SJovi Zhang attr.sample_period = 1; 8691b290d67SFrederic Weisbecker 870930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 8711b290d67SFrederic Weisbecker } 8721b290d67SFrederic Weisbecker 8733b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 8743b0e371cSJiri Olsa struct parse_events_error *err, 8753b0e371cSJiri Olsa int type) 8763b0e371cSJiri Olsa { 8773b0e371cSJiri Olsa if (type == term->type_val) 8783b0e371cSJiri Olsa return 0; 8793b0e371cSJiri Olsa 8803b0e371cSJiri Olsa if (err) { 8813b0e371cSJiri Olsa err->idx = term->err_val; 8823b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 8833b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 8843b0e371cSJiri Olsa else 8853b0e371cSJiri Olsa err->str = strdup("expected string value"); 8863b0e371cSJiri Olsa } 8873b0e371cSJiri Olsa return -EINVAL; 8883b0e371cSJiri Olsa } 8893b0e371cSJiri Olsa 89017cb5f84SWang Nan /* 89117cb5f84SWang Nan * Update according to parse-events.l 89217cb5f84SWang Nan */ 89317cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 89417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 89517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 89617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 89717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 89817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 89917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 90017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 90117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 90217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 90317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 90417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 90517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 90617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 907792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 908626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 909626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 910dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 91117cb5f84SWang Nan }; 91217cb5f84SWang Nan 9131669e509SWang Nan static bool config_term_shrinked; 9141669e509SWang Nan 9151669e509SWang Nan static bool 9161669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9171669e509SWang Nan { 9181669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9191669e509SWang Nan err->str = strdup("Invalid term_type"); 9201669e509SWang Nan return false; 9211669e509SWang Nan } 9221669e509SWang Nan if (!config_term_shrinked) 9231669e509SWang Nan return true; 9241669e509SWang Nan 9251669e509SWang Nan switch (term_type) { 9261669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9271669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9281669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9291669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 93006835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 9311669e509SWang Nan return true; 9321669e509SWang Nan default: 9331669e509SWang Nan if (!err) 9341669e509SWang Nan return false; 9351669e509SWang Nan 9361669e509SWang Nan /* term_type is validated so indexing is safe */ 9371669e509SWang Nan if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", 9381669e509SWang Nan config_term_names[term_type]) < 0) 9391669e509SWang Nan err->str = NULL; 9401669e509SWang Nan return false; 9411669e509SWang Nan } 9421669e509SWang Nan } 9431669e509SWang Nan 9441669e509SWang Nan void parse_events__shrink_config_terms(void) 9451669e509SWang Nan { 9461669e509SWang Nan config_term_shrinked = true; 9471669e509SWang Nan } 9481669e509SWang Nan 9490b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 9503b0e371cSJiri Olsa struct parse_events_term *term, 9513b0e371cSJiri Olsa struct parse_events_error *err) 9528f707d84SJiri Olsa { 95316fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 95416fa7e82SJiri Olsa do { \ 9553b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 95616fa7e82SJiri Olsa return -EINVAL; \ 95716fa7e82SJiri Olsa } while (0) 95816fa7e82SJiri Olsa 95916fa7e82SJiri Olsa switch (term->type_term) { 9608f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 96116fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9628f707d84SJiri Olsa attr->config = term->val.num; 9638f707d84SJiri Olsa break; 9648f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 96516fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9668f707d84SJiri Olsa attr->config1 = term->val.num; 9678f707d84SJiri Olsa break; 9688f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 96916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9708f707d84SJiri Olsa attr->config2 = term->val.num; 9718f707d84SJiri Olsa break; 9728f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 97316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9748f707d84SJiri Olsa break; 97509af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 97609af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 97709af2a55SNamhyung Kim break; 9788f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 979ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 980ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 981ac12f676SAndi Kleen parse_branch_str(term->val.str, &attr->branch_sample_type)) { 982ac12f676SAndi Kleen err->str = strdup("invalid branch sample type"); 983ac12f676SAndi Kleen err->idx = term->err_val; 984ac12f676SAndi Kleen return -EINVAL; 985ac12f676SAndi Kleen } 9868f707d84SJiri Olsa break; 98732067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 98832067712SKan Liang CHECK_TYPE_VAL(NUM); 98932067712SKan Liang if (term->val.num > 1) { 99032067712SKan Liang err->str = strdup("expected 0 or 1"); 99132067712SKan Liang err->idx = term->err_val; 99232067712SKan Liang return -EINVAL; 99332067712SKan Liang } 99432067712SKan Liang break; 995d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 996d457c963SKan Liang CHECK_TYPE_VAL(STR); 997d457c963SKan Liang break; 998d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 999d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1000d457c963SKan Liang break; 1001374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1002374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1003374ce938SWang Nan break; 1004374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1005374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1006374ce938SWang Nan break; 1007626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1008626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1009626a6b78SWang Nan break; 1010626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1011626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1012626a6b78SWang Nan break; 10136b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10146b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10156b5fc39bSJiri Olsa break; 1016792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1017792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1018792d48b4SArnaldo Carvalho de Melo break; 10198f707d84SJiri Olsa default: 1020ffeb883eSHe Kuang err->str = strdup("unknown term"); 1021ffeb883eSHe Kuang err->idx = term->err_term; 1022ffeb883eSHe Kuang err->help = parse_events_formats_error_string(NULL); 10238f707d84SJiri Olsa return -EINVAL; 10248f707d84SJiri Olsa } 102516fa7e82SJiri Olsa 10261669e509SWang Nan /* 10271669e509SWang Nan * Check term availbility after basic checking so 10281669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 10291669e509SWang Nan * 10301669e509SWang Nan * If check availbility at the entry of this function, 10311669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 10321669e509SWang Nan * if an invalid config term is provided for legacy events 10331669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 10341669e509SWang Nan */ 10351669e509SWang Nan if (!config_term_avail(term->type_term, err)) 10361669e509SWang Nan return -EINVAL; 10378f707d84SJiri Olsa return 0; 103816fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 10398f707d84SJiri Olsa } 10408f707d84SJiri Olsa 10410b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 10420b8891a8SHe Kuang struct parse_events_term *term, 10430b8891a8SHe Kuang struct parse_events_error *err) 10440b8891a8SHe Kuang { 1045dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1046dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 10470b8891a8SHe Kuang /* 10480b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 10490b8891a8SHe Kuang * at this point what type they need to have. 10500b8891a8SHe Kuang */ 10510b8891a8SHe Kuang return 0; 10520b8891a8SHe Kuang else 10530b8891a8SHe Kuang return config_term_common(attr, term, err); 10540b8891a8SHe Kuang } 10550b8891a8SHe Kuang 1056e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1057e637d177SHe Kuang struct parse_events_term *term, 1058e637d177SHe Kuang struct parse_events_error *err) 1059e637d177SHe Kuang { 1060e637d177SHe Kuang switch (term->type_term) { 1061e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1062e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1063374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1064374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1065792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1066626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1067626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1068e637d177SHe Kuang return config_term_common(attr, term, err); 1069e637d177SHe Kuang default: 1070e637d177SHe Kuang if (err) { 1071e637d177SHe Kuang err->idx = term->err_term; 1072e637d177SHe Kuang err->str = strdup("unknown term"); 1073e637d177SHe Kuang err->help = strdup("valid terms: call-graph,stack-size\n"); 1074e637d177SHe Kuang } 1075e637d177SHe Kuang return -EINVAL; 1076e637d177SHe Kuang } 1077e637d177SHe Kuang 1078e637d177SHe Kuang return 0; 1079e637d177SHe Kuang } 1080e637d177SHe Kuang 10818f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 10823b0e371cSJiri Olsa struct list_head *head, 10830b8891a8SHe Kuang struct parse_events_error *err, 10840b8891a8SHe Kuang config_term_func_t config_term) 10858f707d84SJiri Olsa { 10866cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 10878f707d84SJiri Olsa 10888f707d84SJiri Olsa list_for_each_entry(term, head, list) 10893b0e371cSJiri Olsa if (config_term(attr, term, err)) 10908f707d84SJiri Olsa return -EINVAL; 10918f707d84SJiri Olsa 10928f707d84SJiri Olsa return 0; 10938f707d84SJiri Olsa } 10948f707d84SJiri Olsa 1095930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1096930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1097930a2e29SJiri Olsa { 1098930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 1099930a2e29SJiri Olsa do { \ 1100930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1101930a2e29SJiri Olsa \ 1102930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1103930a2e29SJiri Olsa if (!__t) \ 1104930a2e29SJiri Olsa return -ENOMEM; \ 1105930a2e29SJiri Olsa \ 1106930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1107930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 1108930a2e29SJiri Olsa __t->val.__name = __val; \ 1109930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 1110930a2e29SJiri Olsa } while (0) 1111930a2e29SJiri Olsa 1112930a2e29SJiri Olsa struct parse_events_term *term; 1113930a2e29SJiri Olsa 1114930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1115930a2e29SJiri Olsa switch (term->type_term) { 1116ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1117ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 111832067712SKan Liang break; 111909af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 112009af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 112109af2a55SNamhyung Kim break; 112232067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 112332067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 112432067712SKan Liang break; 1125d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1126d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 1127d457c963SKan Liang break; 1128ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1129ac12f676SAndi Kleen ADD_CONFIG_TERM(BRANCH, branch, term->val.str); 1130ac12f676SAndi Kleen break; 1131d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1132d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 1133d457c963SKan Liang break; 1134374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1135374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); 1136374ce938SWang Nan break; 1137374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1138374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); 1139374ce938SWang Nan break; 1140792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1141792d48b4SArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num); 1142792d48b4SArnaldo Carvalho de Melo break; 1143626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1144626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0); 1145626a6b78SWang Nan break; 1146626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1147626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1); 1148626a6b78SWang Nan break; 1149dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1150dd60fba7SMathieu Poirier ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str); 1151dd60fba7SMathieu Poirier break; 1152930a2e29SJiri Olsa default: 1153930a2e29SJiri Olsa break; 1154930a2e29SJiri Olsa } 1155930a2e29SJiri Olsa } 1156930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 1157930a2e29SJiri Olsa return 0; 1158930a2e29SJiri Olsa } 1159930a2e29SJiri Olsa 1160e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 11618c619d6aSWang Nan const char *sys, const char *event, 1162272ed29aSJiri Olsa struct parse_events_error *err, 1163e637d177SHe Kuang struct list_head *head_config) 1164e637d177SHe Kuang { 1165e637d177SHe Kuang if (head_config) { 1166e637d177SHe Kuang struct perf_event_attr attr; 1167e637d177SHe Kuang 1168272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1169e637d177SHe Kuang config_term_tracepoint)) 1170e637d177SHe Kuang return -EINVAL; 1171e637d177SHe Kuang } 1172e637d177SHe Kuang 1173e637d177SHe Kuang if (strpbrk(sys, "*?")) 1174e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1175272ed29aSJiri Olsa err, head_config); 1176e637d177SHe Kuang else 1177e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1178272ed29aSJiri Olsa err, head_config); 1179e637d177SHe Kuang } 1180e637d177SHe Kuang 118187d650beSJiri Olsa int parse_events_add_numeric(struct parse_events_evlist *data, 118287d650beSJiri Olsa struct list_head *list, 1183b527bab5SRobert Richter u32 type, u64 config, 11848f707d84SJiri Olsa struct list_head *head_config) 118574d5b588SJaswinder Singh Rajput { 118689812fc8SJiri Olsa struct perf_event_attr attr; 1187930a2e29SJiri Olsa LIST_HEAD(config_terms); 118874d5b588SJaswinder Singh Rajput 118989812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 119089812fc8SJiri Olsa attr.type = type; 119189812fc8SJiri Olsa attr.config = config; 11928f707d84SJiri Olsa 1193930a2e29SJiri Olsa if (head_config) { 11940b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, 11950b8891a8SHe Kuang config_term_common)) 11968f707d84SJiri Olsa return -EINVAL; 11978f707d84SJiri Olsa 1198930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1199930a2e29SJiri Olsa return -ENOMEM; 1200930a2e29SJiri Olsa } 1201930a2e29SJiri Olsa 120210bf358aSWang Nan return add_event(list, &data->idx, &attr, 120310bf358aSWang Nan get_config_name(head_config), &config_terms); 1204b908debdSIngo Molnar } 1205b908debdSIngo Molnar 120636adec85SJiri Olsa int parse_events_add_pmu(struct parse_events_evlist *data, 120736adec85SJiri Olsa struct list_head *list, char *name, 120836adec85SJiri Olsa struct list_head *head_config) 12095f537a26SJiri Olsa { 12105f537a26SJiri Olsa struct perf_event_attr attr; 121146441bdcSMatt Fleming struct perf_pmu_info info; 12125f537a26SJiri Olsa struct perf_pmu *pmu; 1213410136f5SStephane Eranian struct perf_evsel *evsel; 1214930a2e29SJiri Olsa LIST_HEAD(config_terms); 12155f537a26SJiri Olsa 12165f537a26SJiri Olsa pmu = perf_pmu__find(name); 12175f537a26SJiri Olsa if (!pmu) 12185f537a26SJiri Olsa return -EINVAL; 12195f537a26SJiri Olsa 1220dc0a6202SAdrian Hunter if (pmu->default_config) { 1221dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1222dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1223dc0a6202SAdrian Hunter } else { 12245f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1225dc0a6202SAdrian Hunter } 12265f537a26SJiri Olsa 1227ad962273SAdrian Hunter if (!head_config) { 1228ad962273SAdrian Hunter attr.type = pmu->type; 1229930a2e29SJiri Olsa evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); 1230ad962273SAdrian Hunter return evsel ? 0 : -ENOMEM; 1231ad962273SAdrian Hunter } 1232ad962273SAdrian Hunter 123346441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1234a6146d50SZheng Yan return -EINVAL; 1235a6146d50SZheng Yan 12365f537a26SJiri Olsa /* 12375f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 12385f537a26SJiri Olsa * return value when called with fail == 0 ;) 12395f537a26SJiri Olsa */ 12400b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, config_term_pmu)) 1241c056ba6aSJiri Olsa return -EINVAL; 12425f537a26SJiri Olsa 1243930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1244930a2e29SJiri Olsa return -ENOMEM; 1245930a2e29SJiri Olsa 1246e64b020bSJiri Olsa if (perf_pmu__config(pmu, &attr, head_config, data->error)) 12475f537a26SJiri Olsa return -EINVAL; 12485f537a26SJiri Olsa 124936adec85SJiri Olsa evsel = __add_event(list, &data->idx, &attr, 1250e814fdddSWang Nan get_config_name(head_config), pmu->cpus, 1251930a2e29SJiri Olsa &config_terms); 1252410136f5SStephane Eranian if (evsel) { 125346441bdcSMatt Fleming evsel->unit = info.unit; 125446441bdcSMatt Fleming evsel->scale = info.scale; 1255044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 12561d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 1257410136f5SStephane Eranian } 1258410136f5SStephane Eranian 1259410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 12605f537a26SJiri Olsa } 12615f537a26SJiri Olsa 12626a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 12636a4bb04cSJiri Olsa char *event_mod) 126489efb029SJiri Olsa { 12656a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 12666a4bb04cSJiri Olsa } 12676a4bb04cSJiri Olsa 126863dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 12696a4bb04cSJiri Olsa { 12706a4bb04cSJiri Olsa struct perf_evsel *leader; 12716a4bb04cSJiri Olsa 1272854f7363SWang Nan if (list_empty(list)) { 1273854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1274854f7363SWang Nan return; 1275854f7363SWang Nan } 1276854f7363SWang Nan 127763dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 127863dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 12796a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 128089efb029SJiri Olsa } 128189efb029SJiri Olsa 1282c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 12835d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 12845d7be90eSJiri Olsa struct list_head *list_all) 12855d7be90eSJiri Olsa { 12865d7be90eSJiri Olsa /* 12875d7be90eSJiri Olsa * Called for single event definition. Update the 128889efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 12895d7be90eSJiri Olsa * list, for next event definition. 12905d7be90eSJiri Olsa */ 12915d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1292b847cbdcSJiri Olsa free(list_event); 12935d7be90eSJiri Olsa } 12945d7be90eSJiri Olsa 1295f5b1135bSJiri Olsa struct event_modifier { 1296f5b1135bSJiri Olsa int eu; 1297f5b1135bSJiri Olsa int ek; 1298f5b1135bSJiri Olsa int eh; 1299f5b1135bSJiri Olsa int eH; 1300f5b1135bSJiri Olsa int eG; 1301a1e12da4SJiri Olsa int eI; 1302f5b1135bSJiri Olsa int precise; 13037f94af7aSJiri Olsa int precise_max; 1304f5b1135bSJiri Olsa int exclude_GH; 13053c176311SJiri Olsa int sample_read; 1306e9a7c414SMichael Ellerman int pinned; 1307f5b1135bSJiri Olsa }; 130861c45981SPaul Mackerras 1309f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 1310f5b1135bSJiri Olsa struct perf_evsel *evsel) 1311f5b1135bSJiri Olsa { 1312f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 1313f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 1314f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 1315f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 1316f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 1317a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 1318f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 13197f94af7aSJiri Olsa int precise_max = 0; 13203c176311SJiri Olsa int sample_read = 0; 1321e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 1322f5b1135bSJiri Olsa 1323f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1324f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 1325f5b1135bSJiri Olsa 1326f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1327ceb53fbfSIngo Molnar 132861c45981SPaul Mackerras while (*str) { 1329ab608344SPeter Zijlstra if (*str == 'u') { 1330ab608344SPeter Zijlstra if (!exclude) 1331ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 133261c45981SPaul Mackerras eu = 0; 1333ab608344SPeter Zijlstra } else if (*str == 'k') { 1334ab608344SPeter Zijlstra if (!exclude) 1335ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 133661c45981SPaul Mackerras ek = 0; 1337ab608344SPeter Zijlstra } else if (*str == 'h') { 1338ab608344SPeter Zijlstra if (!exclude) 1339ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 134061c45981SPaul Mackerras eh = 0; 134199320cc8SJoerg Roedel } else if (*str == 'G') { 134299320cc8SJoerg Roedel if (!exclude_GH) 134399320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 134499320cc8SJoerg Roedel eG = 0; 134599320cc8SJoerg Roedel } else if (*str == 'H') { 134699320cc8SJoerg Roedel if (!exclude_GH) 134799320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 134899320cc8SJoerg Roedel eH = 0; 1349a1e12da4SJiri Olsa } else if (*str == 'I') { 1350a1e12da4SJiri Olsa eI = 1; 1351ab608344SPeter Zijlstra } else if (*str == 'p') { 1352ab608344SPeter Zijlstra precise++; 13531342798cSDavid Ahern /* use of precise requires exclude_guest */ 13541342798cSDavid Ahern if (!exclude_GH) 13551342798cSDavid Ahern eG = 1; 13567f94af7aSJiri Olsa } else if (*str == 'P') { 13577f94af7aSJiri Olsa precise_max = 1; 13583c176311SJiri Olsa } else if (*str == 'S') { 13593c176311SJiri Olsa sample_read = 1; 1360e9a7c414SMichael Ellerman } else if (*str == 'D') { 1361e9a7c414SMichael Ellerman pinned = 1; 1362ab608344SPeter Zijlstra } else 136361c45981SPaul Mackerras break; 1364ab608344SPeter Zijlstra 136561c45981SPaul Mackerras ++str; 136661c45981SPaul Mackerras } 136774d5b588SJaswinder Singh Rajput 136886470930SIngo Molnar /* 136989812fc8SJiri Olsa * precise ip: 137089812fc8SJiri Olsa * 137189812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 137289812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 137389812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 137489812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 137589812fc8SJiri Olsa * 137689812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 137786470930SIngo Molnar */ 137889812fc8SJiri Olsa if (precise > 3) 137989812fc8SJiri Olsa return -EINVAL; 138086470930SIngo Molnar 1381f5b1135bSJiri Olsa mod->eu = eu; 1382f5b1135bSJiri Olsa mod->ek = ek; 1383f5b1135bSJiri Olsa mod->eh = eh; 1384f5b1135bSJiri Olsa mod->eH = eH; 1385f5b1135bSJiri Olsa mod->eG = eG; 1386a1e12da4SJiri Olsa mod->eI = eI; 1387f5b1135bSJiri Olsa mod->precise = precise; 13887f94af7aSJiri Olsa mod->precise_max = precise_max; 1389f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 13903c176311SJiri Olsa mod->sample_read = sample_read; 1391e9a7c414SMichael Ellerman mod->pinned = pinned; 1392e9a7c414SMichael Ellerman 1393f5b1135bSJiri Olsa return 0; 1394f5b1135bSJiri Olsa } 1395f5b1135bSJiri Olsa 1396534123f4SJiri Olsa /* 1397534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1398534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1399534123f4SJiri Olsa */ 1400534123f4SJiri Olsa static int check_modifier(char *str) 1401534123f4SJiri Olsa { 1402534123f4SJiri Olsa char *p = str; 1403534123f4SJiri Olsa 1404534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 14057f94af7aSJiri Olsa if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1)) 1406534123f4SJiri Olsa return -1; 1407534123f4SJiri Olsa 1408534123f4SJiri Olsa while (*p) { 1409534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1410534123f4SJiri Olsa return -1; 1411534123f4SJiri Olsa p++; 1412534123f4SJiri Olsa } 1413534123f4SJiri Olsa 1414534123f4SJiri Olsa return 0; 1415534123f4SJiri Olsa } 1416534123f4SJiri Olsa 1417f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1418f5b1135bSJiri Olsa { 1419f5b1135bSJiri Olsa struct perf_evsel *evsel; 1420f5b1135bSJiri Olsa struct event_modifier mod; 1421f5b1135bSJiri Olsa 1422f5b1135bSJiri Olsa if (str == NULL) 1423f5b1135bSJiri Olsa return 0; 1424f5b1135bSJiri Olsa 1425534123f4SJiri Olsa if (check_modifier(str)) 1426534123f4SJiri Olsa return -EINVAL; 1427534123f4SJiri Olsa 1428f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1429f5b1135bSJiri Olsa return -EINVAL; 1430f5b1135bSJiri Olsa 1431e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1432f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1433f5b1135bSJiri Olsa return -EINVAL; 1434f5b1135bSJiri Olsa 1435f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 1436f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 1437f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 1438f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 1439f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 1440f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 1441a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 1442f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 14433c176311SJiri Olsa evsel->sample_read = mod.sample_read; 14447f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 1445e9a7c414SMichael Ellerman 1446e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 1447e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 1448ceb53fbfSIngo Molnar } 144986470930SIngo Molnar 145089812fc8SJiri Olsa return 0; 145186470930SIngo Molnar } 145286470930SIngo Molnar 1453ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1454ac2ba9f3SRobert Richter { 1455ac2ba9f3SRobert Richter struct perf_evsel *evsel; 1456ac2ba9f3SRobert Richter 1457e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1458ac2ba9f3SRobert Richter if (!evsel->name) 1459ac2ba9f3SRobert Richter evsel->name = strdup(name); 1460ac2ba9f3SRobert Richter } 1461ac2ba9f3SRobert Richter 1462ac2ba9f3SRobert Richter return 0; 1463ac2ba9f3SRobert Richter } 1464ac2ba9f3SRobert Richter 1465dcb4e102SKan Liang static int 1466dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1467dcb4e102SKan Liang { 1468dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1469dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1470dcb4e102SKan Liang 1471e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1472dcb4e102SKan Liang } 1473dcb4e102SKan Liang 1474dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1475dcb4e102SKan Liang { 1476dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1477dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1478dcb4e102SKan Liang int i; 1479dcb4e102SKan Liang 1480dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1481dcb4e102SKan Liang p = perf_pmu_events_list + i; 1482dcb4e102SKan Liang free(p->symbol); 1483dcb4e102SKan Liang } 1484*506fde11STaeung Song zfree(&perf_pmu_events_list); 1485dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1486dcb4e102SKan Liang } 1487dcb4e102SKan Liang } 1488dcb4e102SKan Liang 1489dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1490dcb4e102SKan Liang do { \ 1491dcb4e102SKan Liang p->symbol = str; \ 1492dcb4e102SKan Liang if (!p->symbol) \ 1493dcb4e102SKan Liang goto err; \ 1494dcb4e102SKan Liang p->type = stype; \ 1495dcb4e102SKan Liang } while (0) 1496dcb4e102SKan Liang 1497dcb4e102SKan Liang /* 1498dcb4e102SKan Liang * Read the pmu events list from sysfs 1499dcb4e102SKan Liang * Save it into perf_pmu_events_list 1500dcb4e102SKan Liang */ 1501dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1502dcb4e102SKan Liang { 1503dcb4e102SKan Liang 1504dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1505dcb4e102SKan Liang struct perf_pmu_alias *alias; 1506dcb4e102SKan Liang int len = 0; 1507dcb4e102SKan Liang 1508231bb2aaSAndi Kleen pmu = NULL; 1509231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1510dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1511dcb4e102SKan Liang if (strchr(alias->name, '-')) 1512dcb4e102SKan Liang len++; 1513dcb4e102SKan Liang len++; 1514dcb4e102SKan Liang } 1515231bb2aaSAndi Kleen } 1516231bb2aaSAndi Kleen 1517231bb2aaSAndi Kleen if (len == 0) { 1518231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1519231bb2aaSAndi Kleen return; 1520231bb2aaSAndi Kleen } 1521dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1522dcb4e102SKan Liang if (!perf_pmu_events_list) 1523dcb4e102SKan Liang return; 1524dcb4e102SKan Liang perf_pmu_events_list_num = len; 1525dcb4e102SKan Liang 1526dcb4e102SKan Liang len = 0; 1527231bb2aaSAndi Kleen pmu = NULL; 1528231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1529dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1530dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1531dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1532dcb4e102SKan Liang 1533dcb4e102SKan Liang if (tmp != NULL) { 1534dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1535dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1536dcb4e102SKan Liang p++; 1537dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1538dcb4e102SKan Liang len += 2; 1539dcb4e102SKan Liang } else { 1540dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1541dcb4e102SKan Liang len++; 1542dcb4e102SKan Liang } 1543dcb4e102SKan Liang } 1544231bb2aaSAndi Kleen } 1545dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1546dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1547dcb4e102SKan Liang 1548dcb4e102SKan Liang return; 1549dcb4e102SKan Liang err: 1550dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1551dcb4e102SKan Liang } 1552dcb4e102SKan Liang 1553dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1554dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1555dcb4e102SKan Liang { 1556dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1557dcb4e102SKan Liang 1558dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1559dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1560dcb4e102SKan Liang perf_pmu__parse_init(); 1561dcb4e102SKan Liang /* 1562dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1563dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1564dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1565dcb4e102SKan Liang */ 1566dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1567dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1568dcb4e102SKan Liang 1569dcb4e102SKan Liang p.symbol = strdup(name); 1570dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1571dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1572dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1573dcb4e102SKan Liang free(p.symbol); 1574dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1575dcb4e102SKan Liang } 1576dcb4e102SKan Liang 157790e2b22dSJiri Olsa static int parse_events__scanner(const char *str, void *data, int start_token) 1578ac20de6fSZheng Yan { 1579ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1580ac20de6fSZheng Yan void *scanner; 1581ac20de6fSZheng Yan int ret; 1582ac20de6fSZheng Yan 158390e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1584ac20de6fSZheng Yan if (ret) 1585ac20de6fSZheng Yan return ret; 1586ac20de6fSZheng Yan 1587ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1588ac20de6fSZheng Yan 1589ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1590ac20de6fSZheng Yan parse_events_debug = 1; 1591ac20de6fSZheng Yan #endif 1592ac20de6fSZheng Yan ret = parse_events_parse(data, scanner); 1593ac20de6fSZheng Yan 1594ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1595ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1596ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1597ac20de6fSZheng Yan return ret; 1598ac20de6fSZheng Yan } 1599ac20de6fSZheng Yan 160090e2b22dSJiri Olsa /* 160190e2b22dSJiri Olsa * parse event config string, return a list of event terms. 160290e2b22dSJiri Olsa */ 160390e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 160490e2b22dSJiri Olsa { 160523b6339bSArnaldo Carvalho de Melo struct parse_events_terms data = { 160690e2b22dSJiri Olsa .terms = NULL, 160790e2b22dSJiri Olsa }; 160890e2b22dSJiri Olsa int ret; 160990e2b22dSJiri Olsa 161090e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_TERMS); 161190e2b22dSJiri Olsa if (!ret) { 161290e2b22dSJiri Olsa list_splice(data.terms, terms); 161374cf249dSArnaldo Carvalho de Melo zfree(&data.terms); 161490e2b22dSJiri Olsa return 0; 161590e2b22dSJiri Olsa } 161690e2b22dSJiri Olsa 16172146afc6SArnaldo Carvalho de Melo parse_events_terms__delete(data.terms); 161890e2b22dSJiri Olsa return ret; 161990e2b22dSJiri Olsa } 162090e2b22dSJiri Olsa 1621b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1622b39b8393SJiri Olsa struct parse_events_error *err) 162386470930SIngo Molnar { 162423b6339bSArnaldo Carvalho de Melo struct parse_events_evlist data = { 162546010ab2SJiri Olsa .list = LIST_HEAD_INIT(data.list), 162646010ab2SJiri Olsa .idx = evlist->nr_entries, 1627b39b8393SJiri Olsa .error = err, 16287630b3e2SWang Nan .evlist = evlist, 162946010ab2SJiri Olsa }; 163046010ab2SJiri Olsa int ret; 163186470930SIngo Molnar 163290e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1633dcb4e102SKan Liang perf_pmu__parse_cleanup(); 163489812fc8SJiri Olsa if (!ret) { 163515bfd2ccSWang Nan struct perf_evsel *last; 163615bfd2ccSWang Nan 1637854f7363SWang Nan if (list_empty(&data.list)) { 1638854f7363SWang Nan WARN_ONCE(true, "WARNING: event parser found nothing"); 1639854f7363SWang Nan return -1; 1640854f7363SWang Nan } 1641854f7363SWang Nan 1642f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &data.list); 164397f63e4aSNamhyung Kim evlist->nr_groups += data.nr_groups; 164415bfd2ccSWang Nan last = perf_evlist__last(evlist); 164515bfd2ccSWang Nan last->cmdline_group_boundary = true; 164615bfd2ccSWang Nan 164786470930SIngo Molnar return 0; 164886470930SIngo Molnar } 164986470930SIngo Molnar 16505d7be90eSJiri Olsa /* 16515d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 16525d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 16535d7be90eSJiri Olsa * need to bother. 16545d7be90eSJiri Olsa */ 165589812fc8SJiri Olsa return ret; 165689812fc8SJiri Olsa } 165789812fc8SJiri Olsa 1658b39b8393SJiri Olsa #define MAX_WIDTH 1000 1659b39b8393SJiri Olsa static int get_term_width(void) 1660b39b8393SJiri Olsa { 1661b39b8393SJiri Olsa struct winsize ws; 1662b39b8393SJiri Olsa 1663b39b8393SJiri Olsa get_term_dimensions(&ws); 1664b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1665b39b8393SJiri Olsa } 1666b39b8393SJiri Olsa 1667b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err, 1668b39b8393SJiri Olsa const char *event) 1669b39b8393SJiri Olsa { 1670b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1671b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1672b39b8393SJiri Olsa char *buf = (char *) event; 1673b39b8393SJiri Olsa int idx = 0; 1674b39b8393SJiri Olsa 1675b39b8393SJiri Olsa if (err->str) { 1676b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1677b39b8393SJiri Olsa int width = get_term_width() - 2; 1678b39b8393SJiri Olsa int len_event = strlen(event); 1679b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1680b39b8393SJiri Olsa 1681b39b8393SJiri Olsa /* 1682b39b8393SJiri Olsa * Maximum error index indent, we will cut 1683b39b8393SJiri Olsa * the event string if it's bigger. 1684b39b8393SJiri Olsa */ 1685141b2d31SAdrian Hunter int max_err_idx = 13; 1686b39b8393SJiri Olsa 1687b39b8393SJiri Olsa /* 1688b39b8393SJiri Olsa * Let's be specific with the message when 1689b39b8393SJiri Olsa * we have the precise error. 1690b39b8393SJiri Olsa */ 1691b39b8393SJiri Olsa str = "event syntax error: "; 1692b39b8393SJiri Olsa len_str = strlen(str); 1693b39b8393SJiri Olsa max_len = width - len_str; 1694b39b8393SJiri Olsa 1695b39b8393SJiri Olsa buf = _buf; 1696b39b8393SJiri Olsa 1697bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 1698b39b8393SJiri Olsa if (err->idx > max_err_idx) 1699b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1700b39b8393SJiri Olsa 1701b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1702b39b8393SJiri Olsa 1703b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1704b39b8393SJiri Olsa if (cut) 1705b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1706b39b8393SJiri Olsa 1707b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1708b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1709b39b8393SJiri Olsa buf[max_len] = 0; 1710b39b8393SJiri Olsa } 1711b39b8393SJiri Olsa 1712b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1713b39b8393SJiri Olsa } 1714b39b8393SJiri Olsa 1715b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1716b39b8393SJiri Olsa if (idx) { 1717b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1718b39b8393SJiri Olsa if (err->help) 1719b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1720b39b8393SJiri Olsa free(err->str); 1721b39b8393SJiri Olsa free(err->help); 1722b39b8393SJiri Olsa } 1723b39b8393SJiri Olsa 1724b39b8393SJiri Olsa fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1725b39b8393SJiri Olsa } 1726b39b8393SJiri Olsa 1727b39b8393SJiri Olsa #undef MAX_WIDTH 1728b39b8393SJiri Olsa 1729f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 17301d037ca1SIrina Tirdea int unset __maybe_unused) 1731f120f9d5SJiri Olsa { 1732f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1733b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1734b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 17359175ce1fSAndi Kleen 1736b39b8393SJiri Olsa if (ret) 1737b39b8393SJiri Olsa parse_events_print_error(&err, str); 1738b39b8393SJiri Olsa 17399175ce1fSAndi Kleen return ret; 1740f120f9d5SJiri Olsa } 1741f120f9d5SJiri Olsa 17424ba1faa1SWang Nan static int 17434ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 17444ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 17454ba1faa1SWang Nan const void *arg), 17464ba1faa1SWang Nan const void *arg) 1747c171b552SLi Zefan { 174869aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 17494ba1faa1SWang Nan int err; 1750c171b552SLi Zefan 1751854f7363SWang Nan /* 1752854f7363SWang Nan * Don't return when list_empty, give func a chance to report 1753854f7363SWang Nan * error when it found last == NULL. 1754854f7363SWang Nan * 1755854f7363SWang Nan * So no need to WARN here, let *func do this. 1756854f7363SWang Nan */ 1757361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 17580c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 175969aad6f1SArnaldo Carvalho de Melo 176015bfd2ccSWang Nan do { 17614ba1faa1SWang Nan err = (*func)(last, arg); 17624ba1faa1SWang Nan if (err) 1763c171b552SLi Zefan return -1; 17644ba1faa1SWang Nan if (!last) 17654ba1faa1SWang Nan return 0; 1766c171b552SLi Zefan 176715bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 176815bfd2ccSWang Nan return 0; 176915bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 177015bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 177115bfd2ccSWang Nan 1772c171b552SLi Zefan return 0; 1773c171b552SLi Zefan } 1774c171b552SLi Zefan 17754ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 17764ba1faa1SWang Nan { 17774ba1faa1SWang Nan const char *str = arg; 17781e857484SMathieu Poirier bool found = false; 17791e857484SMathieu Poirier int nr_addr_filters = 0; 17801e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 17814ba1faa1SWang Nan 17821e857484SMathieu Poirier if (evsel == NULL) 17831e857484SMathieu Poirier goto err; 17844ba1faa1SWang Nan 17851e857484SMathieu Poirier if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 17863541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 17874ba1faa1SWang Nan fprintf(stderr, 17884ba1faa1SWang Nan "not enough memory to hold filter string\n"); 17894ba1faa1SWang Nan return -1; 17904ba1faa1SWang Nan } 17914ba1faa1SWang Nan 17924ba1faa1SWang Nan return 0; 17934ba1faa1SWang Nan } 17944ba1faa1SWang Nan 17951e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 17961e857484SMathieu Poirier if (pmu->type == evsel->attr.type) { 17971e857484SMathieu Poirier found = true; 17981e857484SMathieu Poirier break; 17991e857484SMathieu Poirier } 18001e857484SMathieu Poirier 18011e857484SMathieu Poirier if (found) 18021e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 18031e857484SMathieu Poirier "%d", &nr_addr_filters); 18041e857484SMathieu Poirier 18051e857484SMathieu Poirier if (!nr_addr_filters) 18061e857484SMathieu Poirier goto err; 18071e857484SMathieu Poirier 18081e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 18091e857484SMathieu Poirier fprintf(stderr, 18101e857484SMathieu Poirier "not enough memory to hold filter string\n"); 18111e857484SMathieu Poirier return -1; 18121e857484SMathieu Poirier } 18131e857484SMathieu Poirier 18141e857484SMathieu Poirier return 0; 18151e857484SMathieu Poirier 18161e857484SMathieu Poirier err: 18171e857484SMathieu Poirier fprintf(stderr, 18181e857484SMathieu Poirier "--filter option should follow a -e tracepoint or HW tracer option\n"); 18191e857484SMathieu Poirier 18201e857484SMathieu Poirier return -1; 18211e857484SMathieu Poirier } 18221e857484SMathieu Poirier 18234ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 18244ba1faa1SWang Nan int unset __maybe_unused) 18254ba1faa1SWang Nan { 18264ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 18274ba1faa1SWang Nan 18284ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 18294ba1faa1SWang Nan (const void *)str); 18304ba1faa1SWang Nan } 18314ba1faa1SWang Nan 18324ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 18334ba1faa1SWang Nan const void *arg __maybe_unused) 18344ba1faa1SWang Nan { 18354ba1faa1SWang Nan char new_filter[64]; 18364ba1faa1SWang Nan 18374ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 18384ba1faa1SWang Nan fprintf(stderr, 18394ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 18404ba1faa1SWang Nan return -1; 18414ba1faa1SWang Nan } 18424ba1faa1SWang Nan 18434ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 18444ba1faa1SWang Nan 18453541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 18464ba1faa1SWang Nan fprintf(stderr, 18474ba1faa1SWang Nan "not enough memory to hold filter string\n"); 18484ba1faa1SWang Nan return -1; 18494ba1faa1SWang Nan } 18504ba1faa1SWang Nan 18514ba1faa1SWang Nan return 0; 18524ba1faa1SWang Nan } 18534ba1faa1SWang Nan 18544ba1faa1SWang Nan int exclude_perf(const struct option *opt, 18554ba1faa1SWang Nan const char *arg __maybe_unused, 18564ba1faa1SWang Nan int unset __maybe_unused) 18574ba1faa1SWang Nan { 18584ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 18594ba1faa1SWang Nan 18604ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 18614ba1faa1SWang Nan NULL); 18624ba1faa1SWang Nan } 18634ba1faa1SWang Nan 186486470930SIngo Molnar static const char * const event_type_descriptors[] = { 186586470930SIngo Molnar "Hardware event", 186686470930SIngo Molnar "Software event", 186786470930SIngo Molnar "Tracepoint event", 186886470930SIngo Molnar "Hardware cache event", 186941bdcb23SLiming Wang "Raw hardware event descriptor", 187041bdcb23SLiming Wang "Hardware breakpoint", 187186470930SIngo Molnar }; 187286470930SIngo Molnar 1873ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1874ab0e4800SYunlong Song { 1875ab0e4800SYunlong Song const char * const *as = a; 1876ab0e4800SYunlong Song const char * const *bs = b; 1877ab0e4800SYunlong Song 1878ab0e4800SYunlong Song return strcmp(*as, *bs); 1879ab0e4800SYunlong Song } 1880ab0e4800SYunlong Song 188186470930SIngo Molnar /* 1882f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1883f6bdafefSJason Baron */ 1884f6bdafefSJason Baron 1885a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1886a3277d2dSFrederic Weisbecker bool name_only) 1887f6bdafefSJason Baron { 1888f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 188922a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1890f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1891725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1892ab0e4800SYunlong Song char **evt_list = NULL; 1893ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1894ab0e4800SYunlong Song bool evt_num_known = false; 1895f6bdafefSJason Baron 1896ab0e4800SYunlong Song restart: 1897ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1898f6bdafefSJason Baron if (!sys_dir) 1899725b1368SEric Dumazet return; 1900f6bdafefSJason Baron 1901ab0e4800SYunlong Song if (evt_num_known) { 1902ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1903ab0e4800SYunlong Song if (!evt_list) 1904ab0e4800SYunlong Song goto out_close_sys_dir; 1905ab0e4800SYunlong Song } 1906ab0e4800SYunlong Song 190722a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 1908668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 190922a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 1910668b8788SArnaldo Carvalho de Melo continue; 1911725b1368SEric Dumazet 1912ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 191322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 1914725b1368SEric Dumazet evt_dir = opendir(dir_path); 1915725b1368SEric Dumazet if (!evt_dir) 19166b58e7f1SUlrich Drepper continue; 1917725b1368SEric Dumazet 191822a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 1919668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 192022a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 1921668b8788SArnaldo Carvalho de Melo continue; 1922668b8788SArnaldo Carvalho de Melo 1923ab0e4800SYunlong Song if (!evt_num_known) { 1924ab0e4800SYunlong Song evt_num++; 1925a3277d2dSFrederic Weisbecker continue; 1926a3277d2dSFrederic Weisbecker } 1927a3277d2dSFrederic Weisbecker 1928f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 192922a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 1930ab0e4800SYunlong Song 1931ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 1932ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1933ab0e4800SYunlong Song goto out_close_evt_dir; 1934ab0e4800SYunlong Song evt_i++; 1935f6bdafefSJason Baron } 1936f6bdafefSJason Baron closedir(evt_dir); 1937f6bdafefSJason Baron } 1938f6bdafefSJason Baron closedir(sys_dir); 1939ab0e4800SYunlong Song 1940ab0e4800SYunlong Song if (!evt_num_known) { 1941ab0e4800SYunlong Song evt_num_known = true; 1942ab0e4800SYunlong Song goto restart; 1943ab0e4800SYunlong Song } 1944ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1945ab0e4800SYunlong Song evt_i = 0; 1946ab0e4800SYunlong Song while (evt_i < evt_num) { 1947ab0e4800SYunlong Song if (name_only) { 1948ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1949ab0e4800SYunlong Song continue; 1950ab0e4800SYunlong Song } 1951ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 1952ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1953ab0e4800SYunlong Song } 1954dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 1955ab0e4800SYunlong Song printf("\n"); 1956ab0e4800SYunlong Song 1957ab0e4800SYunlong Song out_free: 1958ab0e4800SYunlong Song evt_num = evt_i; 1959ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1960ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1961ab0e4800SYunlong Song zfree(&evt_list); 1962ab0e4800SYunlong Song return; 1963ab0e4800SYunlong Song 1964ab0e4800SYunlong Song out_close_evt_dir: 1965ab0e4800SYunlong Song closedir(evt_dir); 1966ab0e4800SYunlong Song out_close_sys_dir: 1967ab0e4800SYunlong Song closedir(sys_dir); 1968ab0e4800SYunlong Song 1969ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 1970ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1971ab0e4800SYunlong Song if (evt_list) 1972ab0e4800SYunlong Song goto out_free; 1973f6bdafefSJason Baron } 1974f6bdafefSJason Baron 1975f6bdafefSJason Baron /* 197620c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 197720c457b8SThomas Renninger */ 197820c457b8SThomas Renninger 197920c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 198020c457b8SThomas Renninger { 198120c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 198222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 198320c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 198420c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 198520c457b8SThomas Renninger 1986ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 198720c457b8SThomas Renninger if (!sys_dir) 198820c457b8SThomas Renninger return 0; 198920c457b8SThomas Renninger 199022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 199120c457b8SThomas Renninger 1992ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 199322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 199420c457b8SThomas Renninger evt_dir = opendir(dir_path); 199520c457b8SThomas Renninger if (!evt_dir) 199620c457b8SThomas Renninger continue; 199720c457b8SThomas Renninger 199822a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 199920c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 200022a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 200120c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 200220c457b8SThomas Renninger closedir(evt_dir); 200320c457b8SThomas Renninger closedir(sys_dir); 200420c457b8SThomas Renninger return 1; 200520c457b8SThomas Renninger } 200620c457b8SThomas Renninger } 200720c457b8SThomas Renninger closedir(evt_dir); 200820c457b8SThomas Renninger } 200920c457b8SThomas Renninger closedir(sys_dir); 201020c457b8SThomas Renninger return 0; 201120c457b8SThomas Renninger } 201220c457b8SThomas Renninger 2013b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2014b41f1cecSNamhyung Kim { 2015b41f1cecSNamhyung Kim bool ret = true; 201688fee52eSVince Weaver int open_return; 2017b41f1cecSNamhyung Kim struct perf_evsel *evsel; 2018b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2019b41f1cecSNamhyung Kim .type = type, 2020b41f1cecSNamhyung Kim .config = config, 2021b41f1cecSNamhyung Kim .disabled = 1, 2022b41f1cecSNamhyung Kim }; 2023b41f1cecSNamhyung Kim struct { 2024b41f1cecSNamhyung Kim struct thread_map map; 2025b41f1cecSNamhyung Kim int threads[1]; 2026b41f1cecSNamhyung Kim } tmap = { 2027b41f1cecSNamhyung Kim .map.nr = 1, 2028b41f1cecSNamhyung Kim .threads = { 0 }, 2029b41f1cecSNamhyung Kim }; 2030b41f1cecSNamhyung Kim 2031ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 2032b41f1cecSNamhyung Kim if (evsel) { 203388fee52eSVince Weaver open_return = perf_evsel__open(evsel, NULL, &tmap.map); 203488fee52eSVince Weaver ret = open_return >= 0; 203588fee52eSVince Weaver 203688fee52eSVince Weaver if (open_return == -EACCES) { 203788fee52eSVince Weaver /* 203888fee52eSVince Weaver * This happens if the paranoid value 203988fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 204088fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 204188fee52eSVince Weaver * by default as some ARM machines do not support it. 204288fee52eSVince Weaver * 204388fee52eSVince Weaver */ 204488fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 2045b41f1cecSNamhyung Kim ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; 204688fee52eSVince Weaver } 2047b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 2048b41f1cecSNamhyung Kim } 2049b41f1cecSNamhyung Kim 2050b41f1cecSNamhyung Kim return ret; 2051b41f1cecSNamhyung Kim } 2052b41f1cecSNamhyung Kim 205340218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 205440218daeSMasami Hiramatsu bool name_only) 205540218daeSMasami Hiramatsu { 205640218daeSMasami Hiramatsu struct probe_cache *pcache; 205740218daeSMasami Hiramatsu struct probe_cache_entry *ent; 205840218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 205940218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 206040218daeSMasami Hiramatsu struct str_node *nd, *nd2; 206140218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 206240218daeSMasami Hiramatsu bool show_detail = false; 206340218daeSMasami Hiramatsu int ret; 206440218daeSMasami Hiramatsu 206540218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 206640218daeSMasami Hiramatsu if (!sdtlist) { 206740218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 206840218daeSMasami Hiramatsu return; 206940218daeSMasami Hiramatsu } 207040218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 207140218daeSMasami Hiramatsu if (!bidlist) { 207240218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 207340218daeSMasami Hiramatsu return; 207440218daeSMasami Hiramatsu } 207540218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 207640218daeSMasami Hiramatsu pcache = probe_cache__new(nd->s); 207740218daeSMasami Hiramatsu if (!pcache) 207840218daeSMasami Hiramatsu continue; 207940218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 208040218daeSMasami Hiramatsu if (!ent->sdt) 208140218daeSMasami Hiramatsu continue; 208240218daeSMasami Hiramatsu if (subsys_glob && 208340218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 208440218daeSMasami Hiramatsu continue; 208540218daeSMasami Hiramatsu if (event_glob && 208640218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 208740218daeSMasami Hiramatsu continue; 208840218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 208940218daeSMasami Hiramatsu ent->pev.event, nd->s); 209040218daeSMasami Hiramatsu if (ret > 0) 209140218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 209240218daeSMasami Hiramatsu } 209340218daeSMasami Hiramatsu probe_cache__delete(pcache); 209440218daeSMasami Hiramatsu } 209540218daeSMasami Hiramatsu strlist__delete(bidlist); 209640218daeSMasami Hiramatsu 209740218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 209840218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 209940218daeSMasami Hiramatsu if (buf) 210040218daeSMasami Hiramatsu *(buf++) = '\0'; 210140218daeSMasami Hiramatsu if (name_only) { 210240218daeSMasami Hiramatsu printf("%s ", nd->s); 210340218daeSMasami Hiramatsu continue; 210440218daeSMasami Hiramatsu } 210540218daeSMasami Hiramatsu nd2 = strlist__next(nd); 210640218daeSMasami Hiramatsu if (nd2) { 210740218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 210840218daeSMasami Hiramatsu if (ptr) 210940218daeSMasami Hiramatsu *ptr = '\0'; 211040218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 211140218daeSMasami Hiramatsu show_detail = true; 211240218daeSMasami Hiramatsu } 211340218daeSMasami Hiramatsu if (show_detail) { 211440218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 211540218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 211640218daeSMasami Hiramatsu if (ret > 0) { 211740218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 211840218daeSMasami Hiramatsu free(buf); 211940218daeSMasami Hiramatsu } 212040218daeSMasami Hiramatsu } else 212140218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 212240218daeSMasami Hiramatsu if (nd2) { 212340218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 212440218daeSMasami Hiramatsu show_detail = false; 212540218daeSMasami Hiramatsu if (ptr) 212640218daeSMasami Hiramatsu *ptr = '@'; 212740218daeSMasami Hiramatsu } 212840218daeSMasami Hiramatsu } 212940218daeSMasami Hiramatsu strlist__delete(sdtlist); 213040218daeSMasami Hiramatsu } 213140218daeSMasami Hiramatsu 2132a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2133668b8788SArnaldo Carvalho de Melo { 2134ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 21350b668bc9SArnaldo Carvalho de Melo char name[64]; 2136ab0e4800SYunlong Song char **evt_list = NULL; 2137ab0e4800SYunlong Song bool evt_num_known = false; 2138ab0e4800SYunlong Song 2139ab0e4800SYunlong Song restart: 2140ab0e4800SYunlong Song if (evt_num_known) { 2141ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2142ab0e4800SYunlong Song if (!evt_list) 2143ab0e4800SYunlong Song goto out_enomem; 2144ab0e4800SYunlong Song } 2145668b8788SArnaldo Carvalho de Melo 2146668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2147668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2148668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 21490b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2150668b8788SArnaldo Carvalho de Melo continue; 2151668b8788SArnaldo Carvalho de Melo 2152668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 21530b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 21540b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 2155947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2156668b8788SArnaldo Carvalho de Melo continue; 2157668b8788SArnaldo Carvalho de Melo 2158b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2159b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2160b41f1cecSNamhyung Kim continue; 2161b41f1cecSNamhyung Kim 2162ab0e4800SYunlong Song if (!evt_num_known) { 2163ab0e4800SYunlong Song evt_num++; 2164ab0e4800SYunlong Song continue; 2165ab0e4800SYunlong Song } 2166ab0e4800SYunlong Song 2167ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2168ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2169ab0e4800SYunlong Song goto out_enomem; 2170ab0e4800SYunlong Song evt_i++; 2171668b8788SArnaldo Carvalho de Melo } 2172668b8788SArnaldo Carvalho de Melo } 2173668b8788SArnaldo Carvalho de Melo } 2174668b8788SArnaldo Carvalho de Melo 2175ab0e4800SYunlong Song if (!evt_num_known) { 2176ab0e4800SYunlong Song evt_num_known = true; 2177ab0e4800SYunlong Song goto restart; 2178ab0e4800SYunlong Song } 2179ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2180ab0e4800SYunlong Song evt_i = 0; 2181ab0e4800SYunlong Song while (evt_i < evt_num) { 2182ab0e4800SYunlong Song if (name_only) { 2183ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2184ab0e4800SYunlong Song continue; 2185ab0e4800SYunlong Song } 2186ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2187ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2188ab0e4800SYunlong Song } 2189dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2190dc098b35SAndi Kleen printf("\n"); 2191ab0e4800SYunlong Song 2192ab0e4800SYunlong Song out_free: 2193ab0e4800SYunlong Song evt_num = evt_i; 2194ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2195ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2196ab0e4800SYunlong Song zfree(&evt_list); 2197ab0e4800SYunlong Song return evt_num; 2198ab0e4800SYunlong Song 2199ab0e4800SYunlong Song out_enomem: 2200ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2201ab0e4800SYunlong Song if (evt_list) 2202ab0e4800SYunlong Song goto out_free; 2203ab0e4800SYunlong Song return evt_num; 2204668b8788SArnaldo Carvalho de Melo } 2205668b8788SArnaldo Carvalho de Melo 2206705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2207a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2208a3277d2dSFrederic Weisbecker bool name_only) 220986470930SIngo Molnar { 2210ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2211947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2212ab0e4800SYunlong Song char **evt_list = NULL; 2213ab0e4800SYunlong Song bool evt_num_known = false; 2214ab0e4800SYunlong Song 2215ab0e4800SYunlong Song restart: 2216ab0e4800SYunlong Song if (evt_num_known) { 2217ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2218ab0e4800SYunlong Song if (!evt_list) 2219ab0e4800SYunlong Song goto out_enomem; 2220ab0e4800SYunlong Song syms -= max; 2221ab0e4800SYunlong Song } 222286470930SIngo Molnar 22231dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2224668b8788SArnaldo Carvalho de Melo 2225e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2226668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2227668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2228668b8788SArnaldo Carvalho de Melo continue; 222986470930SIngo Molnar 2230b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2231b41f1cecSNamhyung Kim continue; 2232b41f1cecSNamhyung Kim 2233ab0e4800SYunlong Song if (!evt_num_known) { 2234ab0e4800SYunlong Song evt_num++; 2235a3277d2dSFrederic Weisbecker continue; 2236a3277d2dSFrederic Weisbecker } 2237a3277d2dSFrederic Weisbecker 2238ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2239947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 224074d5b588SJaswinder Singh Rajput else 2241947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 224286470930SIngo Molnar 2243ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2244ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2245ab0e4800SYunlong Song goto out_enomem; 2246ab0e4800SYunlong Song evt_i++; 224786470930SIngo Molnar } 224886470930SIngo Molnar 2249ab0e4800SYunlong Song if (!evt_num_known) { 2250ab0e4800SYunlong Song evt_num_known = true; 2251ab0e4800SYunlong Song goto restart; 2252ab0e4800SYunlong Song } 2253ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2254ab0e4800SYunlong Song evt_i = 0; 2255ab0e4800SYunlong Song while (evt_i < evt_num) { 2256ab0e4800SYunlong Song if (name_only) { 2257ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2258ab0e4800SYunlong Song continue; 2259ab0e4800SYunlong Song } 2260ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2261ab0e4800SYunlong Song } 2262dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2263689d3018SMarti Raudsepp printf("\n"); 2264ab0e4800SYunlong Song 2265ab0e4800SYunlong Song out_free: 2266ab0e4800SYunlong Song evt_num = evt_i; 2267ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2268ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2269ab0e4800SYunlong Song zfree(&evt_list); 2270ab0e4800SYunlong Song return; 2271ab0e4800SYunlong Song 2272ab0e4800SYunlong Song out_enomem: 2273ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2274ab0e4800SYunlong Song if (evt_list) 2275ab0e4800SYunlong Song goto out_free; 2276668b8788SArnaldo Carvalho de Melo } 22771dc12760SJiri Olsa 22781dc12760SJiri Olsa /* 22791dc12760SJiri Olsa * Print the help text for the event symbols: 22801dc12760SJiri Olsa */ 2281c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2282c8d6828aSSukadev Bhattiprolu bool long_desc) 22831dc12760SJiri Olsa { 22841dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2285a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 22861dc12760SJiri Olsa 22871dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2288a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 22891dc12760SJiri Olsa 2290a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 229173c24cb8SJaswinder Singh Rajput 2292c8d6828aSSukadev Bhattiprolu print_pmu_events(event_glob, name_only, quiet_flag, long_desc); 2293dc098b35SAndi Kleen 2294668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2295668b8788SArnaldo Carvalho de Melo return; 229673c24cb8SJaswinder Singh Rajput 2297a3277d2dSFrederic Weisbecker if (!name_only) { 2298947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 22995f537a26SJiri Olsa "rNNN", 23001cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 23015f537a26SJiri Olsa printf(" %-50s [%s]\n", 23025f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 23035f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2304dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2305dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 230686470930SIngo Molnar 2307947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23083741eb9fSJacob Shin "mem:<addr>[/len][:access]", 230941bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2310dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 23111b290d67SFrederic Weisbecker printf("\n"); 2312a3277d2dSFrederic Weisbecker } 23131b290d67SFrederic Weisbecker 2314a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 231540218daeSMasami Hiramatsu 231640218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 231786470930SIngo Molnar } 23188f707d84SJiri Olsa 23196cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 23208f707d84SJiri Olsa { 232116fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 23228f707d84SJiri Olsa } 23238f707d84SJiri Olsa 23246cee6cd3SArnaldo Carvalho de Melo static int new_term(struct parse_events_term **_term, int type_val, 232516fa7e82SJiri Olsa int type_term, char *config, 2326cecf3a2eSJiri Olsa char *str, u64 num, int err_term, int err_val) 23278f707d84SJiri Olsa { 23286cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 23298f707d84SJiri Olsa 23308f707d84SJiri Olsa term = zalloc(sizeof(*term)); 23318f707d84SJiri Olsa if (!term) 23328f707d84SJiri Olsa return -ENOMEM; 23338f707d84SJiri Olsa 23348f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 233516fa7e82SJiri Olsa term->type_val = type_val; 233616fa7e82SJiri Olsa term->type_term = type_term; 23378f707d84SJiri Olsa term->config = config; 2338cecf3a2eSJiri Olsa term->err_term = err_term; 2339cecf3a2eSJiri Olsa term->err_val = err_val; 23408f707d84SJiri Olsa 234116fa7e82SJiri Olsa switch (type_val) { 23428f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 23438f707d84SJiri Olsa term->val.num = num; 23448f707d84SJiri Olsa break; 23458f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 23468f707d84SJiri Olsa term->val.str = str; 23478f707d84SJiri Olsa break; 23488f707d84SJiri Olsa default: 23494be8be6bSAdrian Hunter free(term); 23508f707d84SJiri Olsa return -EINVAL; 23518f707d84SJiri Olsa } 23528f707d84SJiri Olsa 23538f707d84SJiri Olsa *_term = term; 23548f707d84SJiri Olsa return 0; 23558f707d84SJiri Olsa } 23568f707d84SJiri Olsa 23576cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2358cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 2359bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 236016fa7e82SJiri Olsa { 2361bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2362bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2363bb78ce7dSAdrian Hunter 236416fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, 2365cecf3a2eSJiri Olsa config, NULL, num, 2366cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 2367cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 236816fa7e82SJiri Olsa } 236916fa7e82SJiri Olsa 23706cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2371cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2372bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 237316fa7e82SJiri Olsa { 2374bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2375bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2376bb78ce7dSAdrian Hunter 237716fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, 2378cecf3a2eSJiri Olsa config, str, 0, 2379cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 2380cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 238116fa7e82SJiri Olsa } 238216fa7e82SJiri Olsa 23836cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 23841d33d6dcSJiri Olsa char *config, unsigned idx) 23851d33d6dcSJiri Olsa { 23861d33d6dcSJiri Olsa struct event_symbol *sym; 23871d33d6dcSJiri Olsa 23881d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 23891d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 23901d33d6dcSJiri Olsa 23911d33d6dcSJiri Olsa if (config) 23921d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 23931d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, config, 2394cecf3a2eSJiri Olsa (char *) sym->symbol, 0, 0, 0); 23951d33d6dcSJiri Olsa else 23961d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 23971d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, 2398cecf3a2eSJiri Olsa (char *) "event", (char *) sym->symbol, 2399cecf3a2eSJiri Olsa 0, 0, 0); 24001d33d6dcSJiri Olsa } 24011d33d6dcSJiri Olsa 24026cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 24036cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2404a6146d50SZheng Yan { 2405a6146d50SZheng Yan return new_term(new, term->type_val, term->type_term, term->config, 2406cecf3a2eSJiri Olsa term->val.str, term->val.num, 2407cecf3a2eSJiri Olsa term->err_term, term->err_val); 2408a6146d50SZheng Yan } 2409a6146d50SZheng Yan 2410fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 24118f707d84SJiri Olsa { 24126cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 24138f707d84SJiri Olsa 2414a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 24152d055bf2SWang Nan if (term->array.nr_ranges) 24162d055bf2SWang Nan free(term->array.ranges); 2417a8adfcebSWang Nan list_del_init(&term->list); 24188f707d84SJiri Olsa free(term); 24198f707d84SJiri Olsa } 2420a8adfcebSWang Nan } 2421b39b8393SJiri Olsa 24222146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2423fc0a2c1dSArnaldo Carvalho de Melo { 24242146afc6SArnaldo Carvalho de Melo if (!terms) 24252146afc6SArnaldo Carvalho de Melo return; 2426fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2427d20a5f2bSWang Nan free(terms); 2428fc0a2c1dSArnaldo Carvalho de Melo } 2429fc0a2c1dSArnaldo Carvalho de Melo 24302d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 24312d055bf2SWang Nan { 24322d055bf2SWang Nan free(a->ranges); 24332d055bf2SWang Nan } 24342d055bf2SWang Nan 2435b39b8393SJiri Olsa void parse_events_evlist_error(struct parse_events_evlist *data, 2436b39b8393SJiri Olsa int idx, const char *str) 2437b39b8393SJiri Olsa { 2438b39b8393SJiri Olsa struct parse_events_error *err = data->error; 2439b39b8393SJiri Olsa 2440a6ced2beSAdrian Hunter if (!err) 2441a6ced2beSAdrian Hunter return; 2442b39b8393SJiri Olsa err->idx = idx; 2443b39b8393SJiri Olsa err->str = strdup(str); 2444b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 2445b39b8393SJiri Olsa } 2446ffeb883eSHe Kuang 244717cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 244817cb5f84SWang Nan { 244917cb5f84SWang Nan int i; 245017cb5f84SWang Nan bool first = true; 245117cb5f84SWang Nan 245217cb5f84SWang Nan buf[0] = '\0'; 245317cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 245417cb5f84SWang Nan const char *name = config_term_names[i]; 245517cb5f84SWang Nan 24561669e509SWang Nan if (!config_term_avail(i, NULL)) 24571669e509SWang Nan continue; 245817cb5f84SWang Nan if (!name) 245917cb5f84SWang Nan continue; 246017cb5f84SWang Nan if (name[0] == '<') 246117cb5f84SWang Nan continue; 246217cb5f84SWang Nan 246317cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 246417cb5f84SWang Nan return; 246517cb5f84SWang Nan 246617cb5f84SWang Nan if (!first) 246717cb5f84SWang Nan strcat(buf, ","); 246817cb5f84SWang Nan else 246917cb5f84SWang Nan first = false; 247017cb5f84SWang Nan strcat(buf, name); 247117cb5f84SWang Nan } 247217cb5f84SWang Nan } 247317cb5f84SWang Nan 2474ffeb883eSHe Kuang /* 2475ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2476ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2477ffeb883eSHe Kuang */ 2478ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2479ffeb883eSHe Kuang { 2480ffeb883eSHe Kuang char *str; 2481626a6b78SWang Nan /* "no-overwrite" is the longest name */ 248217cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2483626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2484ffeb883eSHe Kuang 248517cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2486ffeb883eSHe Kuang /* valid terms */ 2487ffeb883eSHe Kuang if (additional_terms) { 248826dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 248926dee028SWang Nan additional_terms, static_terms) < 0) 2490ffeb883eSHe Kuang goto fail; 2491ffeb883eSHe Kuang } else { 249226dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2493ffeb883eSHe Kuang goto fail; 2494ffeb883eSHe Kuang } 2495ffeb883eSHe Kuang return str; 2496ffeb883eSHe Kuang 2497ffeb883eSHe Kuang fail: 2498ffeb883eSHe Kuang return NULL; 2499ffeb883eSHe Kuang } 2500