1d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 28dd2a131SJiri Olsa #include <linux/err.h> 376b31a29SArnaldo Carvalho de Melo #include <dirent.h> 4a43783aeSArnaldo Carvalho de Melo #include <errno.h> 586a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 6391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 7b0742e90SArnaldo Carvalho de Melo #include "term.h" 86b58e7f1SUlrich Drepper #include "../perf.h" 9361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 1069aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 114b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1286470930SIngo Molnar #include "parse-events.h" 134b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 14a067558eSArnaldo Carvalho de Melo #include "string2.h" 158ec20b17SArnaldo Carvalho de Melo #include "strlist.h" 165aab621bSArnaldo Carvalho de Melo #include "symbol.h" 175beeded1SJason Baron #include "cache.h" 188755a8f2SArjan van de Ven #include "header.h" 1984c86ca1SWang Nan #include "bpf-loader.h" 206e81c74cSMasami Hiramatsu #include "debug.h" 21592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 22ac20de6fSZheng Yan #include "parse-events-bison.h" 2390e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 2489812fc8SJiri Olsa #include "parse-events-flex.h" 255f537a26SJiri Olsa #include "pmu.h" 26b41f1cecSNamhyung Kim #include "thread_map.h" 27f30a79b0SJiri Olsa #include "cpumap.h" 2840218daeSMasami Hiramatsu #include "probe-file.h" 29b39b8393SJiri Olsa #include "asm/bug.h" 30ac12f676SAndi Kleen #include "util/parse-branch-options.h" 3189812fc8SJiri Olsa 3289812fc8SJiri Olsa #define MAX_NAME_LEN 100 3386470930SIngo Molnar 3482ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 3582ba1f2fSJiri Olsa extern int parse_events_debug; 3682ba1f2fSJiri Olsa #endif 375d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 38e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 39e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 40bcd3279fSFrederic Weisbecker 41dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 42dcb4e102SKan Liang /* 43dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 44dcb4e102SKan Liang * 0 means not initialized and ready to init 45dcb4e102SKan Liang * -1 means failed to init, don't try anymore 46dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 47dcb4e102SKan Liang */ 48dcb4e102SKan Liang static int perf_pmu_events_list_num; 49dcb4e102SKan Liang 50705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 511dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 521dc12760SJiri Olsa .symbol = "cpu-cycles", 531dc12760SJiri Olsa .alias = "cycles", 541dc12760SJiri Olsa }, 551dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 561dc12760SJiri Olsa .symbol = "instructions", 571dc12760SJiri Olsa .alias = "", 581dc12760SJiri Olsa }, 591dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 601dc12760SJiri Olsa .symbol = "cache-references", 611dc12760SJiri Olsa .alias = "", 621dc12760SJiri Olsa }, 631dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 641dc12760SJiri Olsa .symbol = "cache-misses", 651dc12760SJiri Olsa .alias = "", 661dc12760SJiri Olsa }, 671dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 681dc12760SJiri Olsa .symbol = "branch-instructions", 691dc12760SJiri Olsa .alias = "branches", 701dc12760SJiri Olsa }, 711dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 721dc12760SJiri Olsa .symbol = "branch-misses", 731dc12760SJiri Olsa .alias = "", 741dc12760SJiri Olsa }, 751dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 761dc12760SJiri Olsa .symbol = "bus-cycles", 771dc12760SJiri Olsa .alias = "", 781dc12760SJiri Olsa }, 791dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 801dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 811dc12760SJiri Olsa .alias = "idle-cycles-frontend", 821dc12760SJiri Olsa }, 831dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 841dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 851dc12760SJiri Olsa .alias = "idle-cycles-backend", 861dc12760SJiri Olsa }, 871dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 881dc12760SJiri Olsa .symbol = "ref-cycles", 891dc12760SJiri Olsa .alias = "", 901dc12760SJiri Olsa }, 911dc12760SJiri Olsa }; 9286470930SIngo Molnar 93705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 941dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 951dc12760SJiri Olsa .symbol = "cpu-clock", 961dc12760SJiri Olsa .alias = "", 971dc12760SJiri Olsa }, 981dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 991dc12760SJiri Olsa .symbol = "task-clock", 1001dc12760SJiri Olsa .alias = "", 1011dc12760SJiri Olsa }, 1021dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1031dc12760SJiri Olsa .symbol = "page-faults", 1041dc12760SJiri Olsa .alias = "faults", 1051dc12760SJiri Olsa }, 1061dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1071dc12760SJiri Olsa .symbol = "context-switches", 1081dc12760SJiri Olsa .alias = "cs", 1091dc12760SJiri Olsa }, 1101dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1111dc12760SJiri Olsa .symbol = "cpu-migrations", 1121dc12760SJiri Olsa .alias = "migrations", 1131dc12760SJiri Olsa }, 1141dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1151dc12760SJiri Olsa .symbol = "minor-faults", 1161dc12760SJiri Olsa .alias = "", 1171dc12760SJiri Olsa }, 1181dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1191dc12760SJiri Olsa .symbol = "major-faults", 1201dc12760SJiri Olsa .alias = "", 1211dc12760SJiri Olsa }, 1221dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1231dc12760SJiri Olsa .symbol = "alignment-faults", 1241dc12760SJiri Olsa .alias = "", 1251dc12760SJiri Olsa }, 1261dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1271dc12760SJiri Olsa .symbol = "emulation-faults", 1281dc12760SJiri Olsa .alias = "", 1291dc12760SJiri Olsa }, 130d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 131d22d1a2aSAdrian Hunter .symbol = "dummy", 132d22d1a2aSAdrian Hunter .alias = "", 133d22d1a2aSAdrian Hunter }, 134bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 135bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 136bae9cc41SArnaldo Carvalho de Melo .alias = "", 137bae9cc41SArnaldo Carvalho de Melo }, 13886470930SIngo Molnar }; 13986470930SIngo Molnar 140cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 141cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 14286470930SIngo Molnar 143cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 144cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 145cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 146cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 14786470930SIngo Molnar 14822a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 14922a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 15022a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 15122a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 15222a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 153f6bdafefSJason Baron 154ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 155ae07b63fSPeter Zijlstra { 156ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 157ae07b63fSPeter Zijlstra int fd; 158ae07b63fSPeter Zijlstra 159ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 160ae07b63fSPeter Zijlstra sys_dir->d_name, evt_dir->d_name); 161ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 162ae07b63fSPeter Zijlstra if (fd < 0) 163ae07b63fSPeter Zijlstra return -EINVAL; 164ae07b63fSPeter Zijlstra close(fd); 165ae07b63fSPeter Zijlstra 166ae07b63fSPeter Zijlstra return 0; 167ae07b63fSPeter Zijlstra } 168ae07b63fSPeter Zijlstra 16922a9f41bSArnaldo Carvalho de Melo #define for_each_event(sys_dirent, evt_dir, evt_dirent) \ 17022a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 17122a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 17222a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 17322a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 17422a9f41bSArnaldo Carvalho de Melo (!tp_event_has_id(sys_dirent, evt_dirent))) 175f6bdafefSJason Baron 176270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 177f6bdafefSJason Baron 178f6bdafefSJason Baron 1791ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 180f6bdafefSJason Baron { 1811ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 182f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 18322a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1848aa8a7c8SStephane Eranian char id_buf[24]; 185725b1368SEric Dumazet int fd; 186f6bdafefSJason Baron u64 id; 187f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 188725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 189f6bdafefSJason Baron 190ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 191f6bdafefSJason Baron if (!sys_dir) 192725b1368SEric Dumazet return NULL; 193f6bdafefSJason Baron 19422a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 195725b1368SEric Dumazet 196ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 19722a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 198725b1368SEric Dumazet evt_dir = opendir(dir_path); 199725b1368SEric Dumazet if (!evt_dir) 2006b58e7f1SUlrich Drepper continue; 201725b1368SEric Dumazet 20222a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 203725b1368SEric Dumazet 204725b1368SEric Dumazet snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 20522a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 206725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 207f6bdafefSJason Baron if (fd < 0) 208f6bdafefSJason Baron continue; 209f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 210f6bdafefSJason Baron close(fd); 211f6bdafefSJason Baron continue; 212f6bdafefSJason Baron } 213f6bdafefSJason Baron close(fd); 214f6bdafefSJason Baron id = atoll(id_buf); 215f6bdafefSJason Baron if (id == config) { 216f6bdafefSJason Baron closedir(evt_dir); 217f6bdafefSJason Baron closedir(sys_dir); 21859b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2195aa365f2STaeung Song if (!path) 2205aa365f2STaeung Song return NULL; 2211ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2221ef2ed10SFrederic Weisbecker if (!path->system) { 2231ef2ed10SFrederic Weisbecker free(path); 2241ef2ed10SFrederic Weisbecker return NULL; 2251ef2ed10SFrederic Weisbecker } 2261ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2271ef2ed10SFrederic Weisbecker if (!path->name) { 22874cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2291ef2ed10SFrederic Weisbecker free(path); 2301ef2ed10SFrederic Weisbecker return NULL; 2311ef2ed10SFrederic Weisbecker } 23222a9f41bSArnaldo Carvalho de Melo strncpy(path->system, sys_dirent->d_name, 2331ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 23422a9f41bSArnaldo Carvalho de Melo strncpy(path->name, evt_dirent->d_name, 2351ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2361ef2ed10SFrederic Weisbecker return path; 237f6bdafefSJason Baron } 238f6bdafefSJason Baron } 239f6bdafefSJason Baron closedir(evt_dir); 240f6bdafefSJason Baron } 241f6bdafefSJason Baron 242f6bdafefSJason Baron closedir(sys_dir); 2431ef2ed10SFrederic Weisbecker return NULL; 2441ef2ed10SFrederic Weisbecker } 2451ef2ed10SFrederic Weisbecker 246e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 247e7c93f09SNamhyung Kim { 248e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 249e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 250e7c93f09SNamhyung Kim 251e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 252e7c93f09SNamhyung Kim free(path); 253e7c93f09SNamhyung Kim return NULL; 254e7c93f09SNamhyung Kim } 255e7c93f09SNamhyung Kim 256e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 257e7c93f09SNamhyung Kim path->name = strdup(str+1); 258e7c93f09SNamhyung Kim 259e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 26074cf249dSArnaldo Carvalho de Melo zfree(&path->system); 26174cf249dSArnaldo Carvalho de Melo zfree(&path->name); 262506fde11STaeung Song zfree(&path); 263e7c93f09SNamhyung Kim } 264e7c93f09SNamhyung Kim 265e7c93f09SNamhyung Kim return path; 266e7c93f09SNamhyung Kim } 267e7c93f09SNamhyung Kim 2681424dc96SDavid Ahern const char *event_type(int type) 2691424dc96SDavid Ahern { 2701424dc96SDavid Ahern switch (type) { 2711424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2721424dc96SDavid Ahern return "hardware"; 2731424dc96SDavid Ahern 2741424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2751424dc96SDavid Ahern return "software"; 2761424dc96SDavid Ahern 2771424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2781424dc96SDavid Ahern return "tracepoint"; 2791424dc96SDavid Ahern 2801424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2811424dc96SDavid Ahern return "hardware-cache"; 2821424dc96SDavid Ahern 2831424dc96SDavid Ahern default: 2841424dc96SDavid Ahern break; 2851424dc96SDavid Ahern } 2861424dc96SDavid Ahern 2871424dc96SDavid Ahern return "unknown"; 2881424dc96SDavid Ahern } 2891424dc96SDavid Ahern 290e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 291e814fdddSWang Nan { 292e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 293e814fdddSWang Nan } 2947ae92e74SYan, Zheng 295e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 296e814fdddSWang Nan { 297e814fdddSWang Nan struct parse_events_term *term; 298e814fdddSWang Nan 299e814fdddSWang Nan if (!head_terms) 300e814fdddSWang Nan return NULL; 301e814fdddSWang Nan 302e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 303e814fdddSWang Nan if (parse_events__is_name_term(term)) 304e814fdddSWang Nan return term->val.str; 305e814fdddSWang Nan 306e814fdddSWang Nan return NULL; 307e814fdddSWang Nan } 3087ae92e74SYan, Zheng 309410136f5SStephane Eranian static struct perf_evsel * 310410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3117ae92e74SYan, Zheng struct perf_event_attr *attr, 312*66ec1191SMark Rutland char *name, struct perf_pmu *pmu, 31363ce8449SArnaldo Carvalho de Melo struct list_head *config_terms, bool auto_merge_stats) 31489812fc8SJiri Olsa { 31589812fc8SJiri Olsa struct perf_evsel *evsel; 316*66ec1191SMark Rutland struct cpu_map *cpus = pmu ? pmu->cpus : NULL; 31789812fc8SJiri Olsa 31889812fc8SJiri Olsa event_attr_init(attr); 31989812fc8SJiri Olsa 32075fc5ae5STaeung Song evsel = perf_evsel__new_idx(attr, *idx); 321c5cd8ac0SDavid Ahern if (!evsel) 322410136f5SStephane Eranian return NULL; 32389812fc8SJiri Olsa 32475fc5ae5STaeung Song (*idx)++; 325f30a79b0SJiri Olsa evsel->cpus = cpu_map__get(cpus); 326fce4d296SAdrian Hunter evsel->own_cpus = cpu_map__get(cpus); 327*66ec1191SMark Rutland evsel->system_wide = pmu ? pmu->is_uncore : false; 32863ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 329f30a79b0SJiri Olsa 3309db1763cSArnaldo Carvalho de Melo if (name) 33189812fc8SJiri Olsa evsel->name = strdup(name); 332930a2e29SJiri Olsa 333930a2e29SJiri Olsa if (config_terms) 334930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 335930a2e29SJiri Olsa 336b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 337410136f5SStephane Eranian return evsel; 33889812fc8SJiri Olsa } 33989812fc8SJiri Olsa 340c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 341930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 342930a2e29SJiri Olsa struct list_head *config_terms) 3437ae92e74SYan, Zheng { 34463ce8449SArnaldo Carvalho de Melo return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM; 3457ae92e74SYan, Zheng } 3467ae92e74SYan, Zheng 3470b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 34886470930SIngo Molnar { 34986470930SIngo Molnar int i, j; 35061c45981SPaul Mackerras int n, longest = -1; 35186470930SIngo Molnar 35286470930SIngo Molnar for (i = 0; i < size; i++) { 3530b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 35461c45981SPaul Mackerras n = strlen(names[i][j]); 35589812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 35661c45981SPaul Mackerras longest = n; 35761c45981SPaul Mackerras } 35889812fc8SJiri Olsa if (longest > 0) 35986470930SIngo Molnar return i; 36086470930SIngo Molnar } 36186470930SIngo Molnar 3628953645fSIngo Molnar return -1; 36386470930SIngo Molnar } 36486470930SIngo Molnar 36543d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 36643d0b978SWang Nan struct parse_events_term *term, 36743d0b978SWang Nan struct parse_events_error *err); 36843d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 36943d0b978SWang Nan struct parse_events_term *term, 37043d0b978SWang Nan struct parse_events_error *err); 37143d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 37243d0b978SWang Nan struct list_head *head, 37343d0b978SWang Nan struct parse_events_error *err, 37443d0b978SWang Nan config_term_func_t config_term); 37543d0b978SWang Nan 376c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 37743d0b978SWang Nan char *type, char *op_result1, char *op_result2, 3782c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 37943d0b978SWang Nan struct list_head *head_config) 38086470930SIngo Molnar { 38189812fc8SJiri Olsa struct perf_event_attr attr; 38243d0b978SWang Nan LIST_HEAD(config_terms); 38343d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 38461c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 38589812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 38689812fc8SJiri Olsa int i, n; 38786470930SIngo Molnar 38886470930SIngo Molnar /* 38986470930SIngo Molnar * No fallback - if we cannot get a clear cache type 39086470930SIngo Molnar * then bail out: 39186470930SIngo Molnar */ 3920b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 39389812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 39486470930SIngo Molnar if (cache_type == -1) 39589812fc8SJiri Olsa return -EINVAL; 39686470930SIngo Molnar 39743d0b978SWang Nan config_name = get_config_name(head_config); 39889812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 39989812fc8SJiri Olsa 40089812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 40189812fc8SJiri Olsa char *str = op_result[i]; 40289812fc8SJiri Olsa 403275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 40461c45981SPaul Mackerras 40561c45981SPaul Mackerras if (cache_op == -1) { 4060b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 40761c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 40861c45981SPaul Mackerras if (cache_op >= 0) { 4090b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 41089812fc8SJiri Olsa return -EINVAL; 41161c45981SPaul Mackerras continue; 41261c45981SPaul Mackerras } 41361c45981SPaul Mackerras } 41461c45981SPaul Mackerras 41561c45981SPaul Mackerras if (cache_result == -1) { 4160b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 41761c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 41861c45981SPaul Mackerras if (cache_result >= 0) 41961c45981SPaul Mackerras continue; 42061c45981SPaul Mackerras } 42161c45981SPaul Mackerras } 42261c45981SPaul Mackerras 42386470930SIngo Molnar /* 42486470930SIngo Molnar * Fall back to reads: 42586470930SIngo Molnar */ 4268953645fSIngo Molnar if (cache_op == -1) 4278953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 42886470930SIngo Molnar 42986470930SIngo Molnar /* 43086470930SIngo Molnar * Fall back to accesses: 43186470930SIngo Molnar */ 43286470930SIngo Molnar if (cache_result == -1) 43386470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 43486470930SIngo Molnar 43589812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 43689812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 43789812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 43843d0b978SWang Nan 43943d0b978SWang Nan if (head_config) { 4402c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 44143d0b978SWang Nan config_term_common)) 44243d0b978SWang Nan return -EINVAL; 44343d0b978SWang Nan 44443d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 44543d0b978SWang Nan return -ENOMEM; 44643d0b978SWang Nan } 44743d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 44886470930SIngo Molnar } 44986470930SIngo Molnar 450272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4518c619d6aSWang Nan const char *sys, const char *name) 45219658171SJiri Olsa { 45319658171SJiri Olsa char help[BUFSIZ]; 45419658171SJiri Olsa 455ec183d22SAdrian Hunter if (!e) 456ec183d22SAdrian Hunter return; 457ec183d22SAdrian Hunter 45819658171SJiri Olsa /* 45919658171SJiri Olsa * We get error directly from syscall errno ( > 0), 46019658171SJiri Olsa * or from encoded pointer's error ( < 0). 46119658171SJiri Olsa */ 46219658171SJiri Olsa err = abs(err); 46319658171SJiri Olsa 46419658171SJiri Olsa switch (err) { 46519658171SJiri Olsa case EACCES: 466272ed29aSJiri Olsa e->str = strdup("can't access trace events"); 46719658171SJiri Olsa break; 46819658171SJiri Olsa case ENOENT: 469272ed29aSJiri Olsa e->str = strdup("unknown tracepoint"); 47019658171SJiri Olsa break; 47119658171SJiri Olsa default: 472272ed29aSJiri Olsa e->str = strdup("failed to add tracepoint"); 47319658171SJiri Olsa break; 47419658171SJiri Olsa } 47519658171SJiri Olsa 47619658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 477272ed29aSJiri Olsa e->help = strdup(help); 47819658171SJiri Olsa } 47919658171SJiri Olsa 480c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 4818c619d6aSWang Nan const char *sys_name, const char *evt_name, 482272ed29aSJiri Olsa struct parse_events_error *err, 483e637d177SHe Kuang struct list_head *head_config) 484bcd3279fSFrederic Weisbecker { 48582fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 486bcd3279fSFrederic Weisbecker 487ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 48819658171SJiri Olsa if (IS_ERR(evsel)) { 489272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 4908dd2a131SJiri Olsa return PTR_ERR(evsel); 49119658171SJiri Olsa } 492bcd3279fSFrederic Weisbecker 493e637d177SHe Kuang if (head_config) { 494e637d177SHe Kuang LIST_HEAD(config_terms); 495e637d177SHe Kuang 496e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 497e637d177SHe Kuang return -ENOMEM; 498e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 499e637d177SHe Kuang } 500e637d177SHe Kuang 50182fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 50282fe1c29SArnaldo Carvalho de Melo return 0; 503bcd3279fSFrederic Weisbecker } 504bcd3279fSFrederic Weisbecker 505c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5068c619d6aSWang Nan const char *sys_name, const char *evt_name, 507272ed29aSJiri Olsa struct parse_events_error *err, 508e637d177SHe Kuang struct list_head *head_config) 509bcd3279fSFrederic Weisbecker { 510bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 511bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 512bcd3279fSFrederic Weisbecker DIR *evt_dir; 51327bf90bfSJiri Olsa int ret = 0, found = 0; 514bcd3279fSFrederic Weisbecker 515ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 516bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 517bcd3279fSFrederic Weisbecker if (!evt_dir) { 518272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 51989812fc8SJiri Olsa return -1; 520bcd3279fSFrederic Weisbecker } 521bcd3279fSFrederic Weisbecker 52289812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 523bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 524bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 525bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 526bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 527bcd3279fSFrederic Weisbecker continue; 528bcd3279fSFrederic Weisbecker 52989812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 530fb1d2edfSMasami Hiramatsu continue; 531fb1d2edfSMasami Hiramatsu 53227bf90bfSJiri Olsa found++; 53327bf90bfSJiri Olsa 534e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 535272ed29aSJiri Olsa err, head_config); 536bcd3279fSFrederic Weisbecker } 537bcd3279fSFrederic Weisbecker 53827bf90bfSJiri Olsa if (!found) { 53927bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 54027bf90bfSJiri Olsa ret = -1; 54127bf90bfSJiri Olsa } 54227bf90bfSJiri Olsa 5430bd3f084SJiri Olsa closedir(evt_dir); 54489812fc8SJiri Olsa return ret; 545bcd3279fSFrederic Weisbecker } 546bcd3279fSFrederic Weisbecker 547c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5488c619d6aSWang Nan const char *sys_name, const char *evt_name, 549272ed29aSJiri Olsa struct parse_events_error *err, 550e637d177SHe Kuang struct list_head *head_config) 551f35488f9SJiri Olsa { 552f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 553e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 554272ed29aSJiri Olsa err, head_config) : 555e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 556272ed29aSJiri Olsa err, head_config); 557f35488f9SJiri Olsa } 558f35488f9SJiri Olsa 559c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5608c619d6aSWang Nan const char *sys_name, const char *evt_name, 561272ed29aSJiri Olsa struct parse_events_error *err, 562e637d177SHe Kuang struct list_head *head_config) 563f35488f9SJiri Olsa { 564f35488f9SJiri Olsa struct dirent *events_ent; 565f35488f9SJiri Olsa DIR *events_dir; 566f35488f9SJiri Olsa int ret = 0; 567f35488f9SJiri Olsa 568f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 569f35488f9SJiri Olsa if (!events_dir) { 570272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 571f35488f9SJiri Olsa return -1; 572f35488f9SJiri Olsa } 573f35488f9SJiri Olsa 574f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 575f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 576f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 577f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 578f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 579f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 580f35488f9SJiri Olsa continue; 581f35488f9SJiri Olsa 582f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 583f35488f9SJiri Olsa continue; 584f35488f9SJiri Olsa 585f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 586272ed29aSJiri Olsa evt_name, err, head_config); 587f35488f9SJiri Olsa } 588f35488f9SJiri Olsa 589f35488f9SJiri Olsa closedir(events_dir); 590f35488f9SJiri Olsa return ret; 591f35488f9SJiri Olsa } 592f35488f9SJiri Olsa 5934edf30e3SWang Nan struct __add_bpf_event_param { 5945d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 5954edf30e3SWang Nan struct list_head *list; 59695088a59SWang Nan struct list_head *head_config; 5974edf30e3SWang Nan }; 5984edf30e3SWang Nan 599cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd, 6004edf30e3SWang Nan void *_param) 6014edf30e3SWang Nan { 6024edf30e3SWang Nan LIST_HEAD(new_evsels); 6034edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6045d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6054edf30e3SWang Nan struct list_head *list = param->list; 6061f45b1d4SWang Nan struct perf_evsel *pos; 6074edf30e3SWang Nan int err; 6084edf30e3SWang Nan 6094edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 610cd102d70SWang Nan group, event, fd); 6114edf30e3SWang Nan 6125d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6135d369a75SArnaldo Carvalho de Melo event, parse_state->error, 61495088a59SWang Nan param->head_config); 6154edf30e3SWang Nan if (err) { 6164edf30e3SWang Nan struct perf_evsel *evsel, *tmp; 6174edf30e3SWang Nan 6184edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 619cd102d70SWang Nan group, event); 6204edf30e3SWang Nan list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { 6214edf30e3SWang Nan list_del(&evsel->node); 6224edf30e3SWang Nan perf_evsel__delete(evsel); 6234edf30e3SWang Nan } 6244edf30e3SWang Nan return err; 6254edf30e3SWang Nan } 626cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6274edf30e3SWang Nan 6281f45b1d4SWang Nan list_for_each_entry(pos, &new_evsels, node) { 6291f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 630cd102d70SWang Nan group, event, pos); 6311f45b1d4SWang Nan pos->bpf_fd = fd; 6321f45b1d4SWang Nan } 6334edf30e3SWang Nan list_splice(&new_evsels, list); 6344edf30e3SWang Nan return 0; 6354edf30e3SWang Nan } 6364edf30e3SWang Nan 6375d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 63884c86ca1SWang Nan struct list_head *list, 63995088a59SWang Nan struct bpf_object *obj, 64095088a59SWang Nan struct list_head *head_config) 64184c86ca1SWang Nan { 64284c86ca1SWang Nan int err; 64384c86ca1SWang Nan char errbuf[BUFSIZ]; 6445d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 645aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 64684c86ca1SWang Nan 64784c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 64884c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 64984c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 65084c86ca1SWang Nan err = -EINVAL; 65184c86ca1SWang Nan goto errout; 65284c86ca1SWang Nan } 65384c86ca1SWang Nan 65484c86ca1SWang Nan /* 655aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 656aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 657aa3abf30SWang Nan * created when failure. 658aa3abf30SWang Nan */ 659aa3abf30SWang Nan if (!registered_unprobe_atexit) { 660aa3abf30SWang Nan atexit(bpf__clear); 661aa3abf30SWang Nan registered_unprobe_atexit = true; 662aa3abf30SWang Nan } 663aa3abf30SWang Nan 664aa3abf30SWang Nan err = bpf__probe(obj); 665aa3abf30SWang Nan if (err) { 666aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 667aa3abf30SWang Nan goto errout; 668aa3abf30SWang Nan } 669aa3abf30SWang Nan 6701e5e3ee8SWang Nan err = bpf__load(obj); 6711e5e3ee8SWang Nan if (err) { 6721e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 6731e5e3ee8SWang Nan goto errout; 6741e5e3ee8SWang Nan } 6751e5e3ee8SWang Nan 676cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 6774edf30e3SWang Nan if (err) { 6784edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 6794edf30e3SWang Nan "Attach events in BPF object failed"); 6804edf30e3SWang Nan goto errout; 6814edf30e3SWang Nan } 6824edf30e3SWang Nan 6834edf30e3SWang Nan return 0; 68484c86ca1SWang Nan errout: 6855d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 6865d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 68784c86ca1SWang Nan return err; 68884c86ca1SWang Nan } 68984c86ca1SWang Nan 690a34f3be7SWang Nan static int 6915d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 692a34f3be7SWang Nan struct bpf_object *obj, 693a34f3be7SWang Nan struct list_head *head_config) 694a34f3be7SWang Nan { 695a34f3be7SWang Nan struct parse_events_term *term; 696a34f3be7SWang Nan int error_pos; 697a34f3be7SWang Nan 698a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 699a34f3be7SWang Nan return 0; 700a34f3be7SWang Nan 701a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 702a34f3be7SWang Nan char errbuf[BUFSIZ]; 703a34f3be7SWang Nan int err; 704a34f3be7SWang Nan 705a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 706a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 707a34f3be7SWang Nan "Invalid config term for BPF object"); 708a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 709a34f3be7SWang Nan 7105d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term; 7115d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 712a34f3be7SWang Nan return -EINVAL; 713a34f3be7SWang Nan } 714a34f3be7SWang Nan 7155d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 716a34f3be7SWang Nan if (err) { 7175d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 718a34f3be7SWang Nan &error_pos, err, errbuf, 719a34f3be7SWang Nan sizeof(errbuf)); 7205d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup( 7217630b3e2SWang Nan "Hint:\tValid config terms:\n" 722e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 723e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7247630b3e2SWang Nan "\n" 725e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 726a34f3be7SWang Nan " \t(add -v to see detail)"); 7275d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 728a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 7295d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_val; 730a34f3be7SWang Nan else 7315d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term + error_pos; 732a34f3be7SWang Nan return err; 733a34f3be7SWang Nan } 734a34f3be7SWang Nan } 735a34f3be7SWang Nan return 0; 736a34f3be7SWang Nan } 737a34f3be7SWang Nan 73895088a59SWang Nan /* 73995088a59SWang Nan * Split config terms: 74095088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 74195088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 74295088a59SWang Nan * events in bpf.c. 74395088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 74495088a59SWang Nan * with parse_events_config_bpf. 74595088a59SWang Nan * 74695088a59SWang Nan * Move object config terms from the first list to obj_head_config. 74795088a59SWang Nan */ 74895088a59SWang Nan static void 74995088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 75095088a59SWang Nan struct list_head *obj_head_config) 75195088a59SWang Nan { 75295088a59SWang Nan struct parse_events_term *term, *temp; 75395088a59SWang Nan 75495088a59SWang Nan /* 75595088a59SWang Nan * Currectly, all possible user config term 75695088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 75795088a59SWang Nan * happends to be a good flag. 75895088a59SWang Nan * 75995088a59SWang Nan * See parse_events_config_bpf() and 76095088a59SWang Nan * config_term_tracepoint(). 76195088a59SWang Nan */ 76295088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 76395088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 76495088a59SWang Nan list_move_tail(&term->list, obj_head_config); 76595088a59SWang Nan } 76695088a59SWang Nan 7675d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 76884c86ca1SWang Nan struct list_head *list, 769d509db04SWang Nan char *bpf_file_name, 770a34f3be7SWang Nan bool source, 771a34f3be7SWang Nan struct list_head *head_config) 77284c86ca1SWang Nan { 773a34f3be7SWang Nan int err; 77495088a59SWang Nan struct bpf_object *obj; 77595088a59SWang Nan LIST_HEAD(obj_head_config); 77695088a59SWang Nan 77795088a59SWang Nan if (head_config) 77895088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 77984c86ca1SWang Nan 780d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 7816371ca3bSWang Nan if (IS_ERR(obj)) { 78284c86ca1SWang Nan char errbuf[BUFSIZ]; 78384c86ca1SWang Nan 7846371ca3bSWang Nan err = PTR_ERR(obj); 78584c86ca1SWang Nan 78684c86ca1SWang Nan if (err == -ENOTSUP) 78784c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 78884c86ca1SWang Nan "BPF support is not compiled"); 78984c86ca1SWang Nan else 790d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 791d3e0ce39SWang Nan source, 792d3e0ce39SWang Nan -err, errbuf, 793d3e0ce39SWang Nan sizeof(errbuf)); 79484c86ca1SWang Nan 7955d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 7965d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 79784c86ca1SWang Nan return err; 79884c86ca1SWang Nan } 79984c86ca1SWang Nan 8005d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 801a34f3be7SWang Nan if (err) 802a34f3be7SWang Nan return err; 8035d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 80495088a59SWang Nan 80595088a59SWang Nan /* 80695088a59SWang Nan * Caller doesn't know anything about obj_head_config, 80795088a59SWang Nan * so combine them together again before returnning. 80895088a59SWang Nan */ 80995088a59SWang Nan if (head_config) 81095088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 81195088a59SWang Nan return err; 81284c86ca1SWang Nan } 81384c86ca1SWang Nan 81489812fc8SJiri Olsa static int 81589812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8161b290d67SFrederic Weisbecker { 8171b290d67SFrederic Weisbecker int i; 8181b290d67SFrederic Weisbecker 8191b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 82089812fc8SJiri Olsa if (!type || !type[i]) 8211b290d67SFrederic Weisbecker break; 8221b290d67SFrederic Weisbecker 8237582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8247582732fSJiri Olsa do { \ 8257582732fSJiri Olsa if (attr->bp_type & bit) \ 8267582732fSJiri Olsa return -EINVAL; \ 8277582732fSJiri Olsa else \ 8287582732fSJiri Olsa attr->bp_type |= bit; \ 8297582732fSJiri Olsa } while (0) 8307582732fSJiri Olsa 8311b290d67SFrederic Weisbecker switch (type[i]) { 8321b290d67SFrederic Weisbecker case 'r': 8337582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8341b290d67SFrederic Weisbecker break; 8351b290d67SFrederic Weisbecker case 'w': 8367582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8371b290d67SFrederic Weisbecker break; 8381b290d67SFrederic Weisbecker case 'x': 8397582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8401b290d67SFrederic Weisbecker break; 8411b290d67SFrederic Weisbecker default: 84289812fc8SJiri Olsa return -EINVAL; 8431b290d67SFrederic Weisbecker } 8441b290d67SFrederic Weisbecker } 84589812fc8SJiri Olsa 8467582732fSJiri Olsa #undef CHECK_SET_TYPE 8477582732fSJiri Olsa 8481b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8491b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8501b290d67SFrederic Weisbecker 85189812fc8SJiri Olsa return 0; 8521b290d67SFrederic Weisbecker } 8531b290d67SFrederic Weisbecker 854c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 8553741eb9fSJacob Shin void *ptr, char *type, u64 len) 8561b290d67SFrederic Weisbecker { 85789812fc8SJiri Olsa struct perf_event_attr attr; 8581b290d67SFrederic Weisbecker 85989812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 8609fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 8611b290d67SFrederic Weisbecker 86289812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 86389812fc8SJiri Olsa return -EINVAL; 8641b290d67SFrederic Weisbecker 8653741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 8663741eb9fSJacob Shin if (!len) { 86789812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 8683741eb9fSJacob Shin len = sizeof(long); 869aa59a485SFrederic Weisbecker else 8703741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 8713741eb9fSJacob Shin } 8723741eb9fSJacob Shin 8733741eb9fSJacob Shin attr.bp_len = len; 874aa59a485SFrederic Weisbecker 87589812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 8764a841d65SJovi Zhang attr.sample_period = 1; 8771b290d67SFrederic Weisbecker 878930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 8791b290d67SFrederic Weisbecker } 8801b290d67SFrederic Weisbecker 8813b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 8823b0e371cSJiri Olsa struct parse_events_error *err, 8833b0e371cSJiri Olsa int type) 8843b0e371cSJiri Olsa { 8853b0e371cSJiri Olsa if (type == term->type_val) 8863b0e371cSJiri Olsa return 0; 8873b0e371cSJiri Olsa 8883b0e371cSJiri Olsa if (err) { 8893b0e371cSJiri Olsa err->idx = term->err_val; 8903b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 8913b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 8923b0e371cSJiri Olsa else 8933b0e371cSJiri Olsa err->str = strdup("expected string value"); 8943b0e371cSJiri Olsa } 8953b0e371cSJiri Olsa return -EINVAL; 8963b0e371cSJiri Olsa } 8973b0e371cSJiri Olsa 89817cb5f84SWang Nan /* 89917cb5f84SWang Nan * Update according to parse-events.l 90017cb5f84SWang Nan */ 90117cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 90217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 90317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 90417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 90517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 90617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 90717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 90817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 90917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 91017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 91117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 91217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 91317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 91417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 915792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 916626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 917626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 918dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 91917cb5f84SWang Nan }; 92017cb5f84SWang Nan 9211669e509SWang Nan static bool config_term_shrinked; 9221669e509SWang Nan 9231669e509SWang Nan static bool 9241669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9251669e509SWang Nan { 9261669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9271669e509SWang Nan err->str = strdup("Invalid term_type"); 9281669e509SWang Nan return false; 9291669e509SWang Nan } 9301669e509SWang Nan if (!config_term_shrinked) 9311669e509SWang Nan return true; 9321669e509SWang Nan 9331669e509SWang Nan switch (term_type) { 9341669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9351669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9361669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9371669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 93806835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 9391669e509SWang Nan return true; 9401669e509SWang Nan default: 9411669e509SWang Nan if (!err) 9421669e509SWang Nan return false; 9431669e509SWang Nan 9441669e509SWang Nan /* term_type is validated so indexing is safe */ 9451669e509SWang Nan if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", 9461669e509SWang Nan config_term_names[term_type]) < 0) 9471669e509SWang Nan err->str = NULL; 9481669e509SWang Nan return false; 9491669e509SWang Nan } 9501669e509SWang Nan } 9511669e509SWang Nan 9521669e509SWang Nan void parse_events__shrink_config_terms(void) 9531669e509SWang Nan { 9541669e509SWang Nan config_term_shrinked = true; 9551669e509SWang Nan } 9561669e509SWang Nan 9570b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 9583b0e371cSJiri Olsa struct parse_events_term *term, 9593b0e371cSJiri Olsa struct parse_events_error *err) 9608f707d84SJiri Olsa { 96116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 96216fa7e82SJiri Olsa do { \ 9633b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 96416fa7e82SJiri Olsa return -EINVAL; \ 96516fa7e82SJiri Olsa } while (0) 96616fa7e82SJiri Olsa 96716fa7e82SJiri Olsa switch (term->type_term) { 9688f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 96916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9708f707d84SJiri Olsa attr->config = term->val.num; 9718f707d84SJiri Olsa break; 9728f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 97316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9748f707d84SJiri Olsa attr->config1 = term->val.num; 9758f707d84SJiri Olsa break; 9768f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 97716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9788f707d84SJiri Olsa attr->config2 = term->val.num; 9798f707d84SJiri Olsa break; 9808f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 98116fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 9828f707d84SJiri Olsa break; 98309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 98409af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 98509af2a55SNamhyung Kim break; 9868f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 987ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 988ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 989ac12f676SAndi Kleen parse_branch_str(term->val.str, &attr->branch_sample_type)) { 990ac12f676SAndi Kleen err->str = strdup("invalid branch sample type"); 991ac12f676SAndi Kleen err->idx = term->err_val; 992ac12f676SAndi Kleen return -EINVAL; 993ac12f676SAndi Kleen } 9948f707d84SJiri Olsa break; 99532067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 99632067712SKan Liang CHECK_TYPE_VAL(NUM); 99732067712SKan Liang if (term->val.num > 1) { 99832067712SKan Liang err->str = strdup("expected 0 or 1"); 99932067712SKan Liang err->idx = term->err_val; 100032067712SKan Liang return -EINVAL; 100132067712SKan Liang } 100232067712SKan Liang break; 1003d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1004d457c963SKan Liang CHECK_TYPE_VAL(STR); 1005d457c963SKan Liang break; 1006d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1007d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1008d457c963SKan Liang break; 1009374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1010374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1011374ce938SWang Nan break; 1012374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1013374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1014374ce938SWang Nan break; 1015626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1016626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1017626a6b78SWang Nan break; 1018626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1019626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1020626a6b78SWang Nan break; 10216b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10226b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10236b5fc39bSJiri Olsa break; 1024792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1025792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1026792d48b4SArnaldo Carvalho de Melo break; 10278f707d84SJiri Olsa default: 1028ffeb883eSHe Kuang err->str = strdup("unknown term"); 1029ffeb883eSHe Kuang err->idx = term->err_term; 1030ffeb883eSHe Kuang err->help = parse_events_formats_error_string(NULL); 10318f707d84SJiri Olsa return -EINVAL; 10328f707d84SJiri Olsa } 103316fa7e82SJiri Olsa 10341669e509SWang Nan /* 10351669e509SWang Nan * Check term availbility after basic checking so 10361669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 10371669e509SWang Nan * 10381669e509SWang Nan * If check availbility at the entry of this function, 10391669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 10401669e509SWang Nan * if an invalid config term is provided for legacy events 10411669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 10421669e509SWang Nan */ 10431669e509SWang Nan if (!config_term_avail(term->type_term, err)) 10441669e509SWang Nan return -EINVAL; 10458f707d84SJiri Olsa return 0; 104616fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 10478f707d84SJiri Olsa } 10488f707d84SJiri Olsa 10490b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 10500b8891a8SHe Kuang struct parse_events_term *term, 10510b8891a8SHe Kuang struct parse_events_error *err) 10520b8891a8SHe Kuang { 1053dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1054dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 10550b8891a8SHe Kuang /* 10560b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 10570b8891a8SHe Kuang * at this point what type they need to have. 10580b8891a8SHe Kuang */ 10590b8891a8SHe Kuang return 0; 10600b8891a8SHe Kuang else 10610b8891a8SHe Kuang return config_term_common(attr, term, err); 10620b8891a8SHe Kuang } 10630b8891a8SHe Kuang 1064e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1065e637d177SHe Kuang struct parse_events_term *term, 1066e637d177SHe Kuang struct parse_events_error *err) 1067e637d177SHe Kuang { 1068e637d177SHe Kuang switch (term->type_term) { 1069e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1070e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1071374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1072374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1073792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1074626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1075626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1076e637d177SHe Kuang return config_term_common(attr, term, err); 1077e637d177SHe Kuang default: 1078e637d177SHe Kuang if (err) { 1079e637d177SHe Kuang err->idx = term->err_term; 1080e637d177SHe Kuang err->str = strdup("unknown term"); 1081e637d177SHe Kuang err->help = strdup("valid terms: call-graph,stack-size\n"); 1082e637d177SHe Kuang } 1083e637d177SHe Kuang return -EINVAL; 1084e637d177SHe Kuang } 1085e637d177SHe Kuang 1086e637d177SHe Kuang return 0; 1087e637d177SHe Kuang } 1088e637d177SHe Kuang 10898f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 10903b0e371cSJiri Olsa struct list_head *head, 10910b8891a8SHe Kuang struct parse_events_error *err, 10920b8891a8SHe Kuang config_term_func_t config_term) 10938f707d84SJiri Olsa { 10946cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 10958f707d84SJiri Olsa 10968f707d84SJiri Olsa list_for_each_entry(term, head, list) 10973b0e371cSJiri Olsa if (config_term(attr, term, err)) 10988f707d84SJiri Olsa return -EINVAL; 10998f707d84SJiri Olsa 11008f707d84SJiri Olsa return 0; 11018f707d84SJiri Olsa } 11028f707d84SJiri Olsa 1103930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1104930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1105930a2e29SJiri Olsa { 1106930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 1107930a2e29SJiri Olsa do { \ 1108930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1109930a2e29SJiri Olsa \ 1110930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1111930a2e29SJiri Olsa if (!__t) \ 1112930a2e29SJiri Olsa return -ENOMEM; \ 1113930a2e29SJiri Olsa \ 1114930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1115930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 1116930a2e29SJiri Olsa __t->val.__name = __val; \ 1117930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 1118930a2e29SJiri Olsa } while (0) 1119930a2e29SJiri Olsa 1120930a2e29SJiri Olsa struct parse_events_term *term; 1121930a2e29SJiri Olsa 1122930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1123930a2e29SJiri Olsa switch (term->type_term) { 1124ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1125ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 112632067712SKan Liang break; 112709af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 112809af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 112909af2a55SNamhyung Kim break; 113032067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 113132067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 113232067712SKan Liang break; 1133d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1134d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 1135d457c963SKan Liang break; 1136ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1137ac12f676SAndi Kleen ADD_CONFIG_TERM(BRANCH, branch, term->val.str); 1138ac12f676SAndi Kleen break; 1139d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1140d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 1141d457c963SKan Liang break; 1142374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1143374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); 1144374ce938SWang Nan break; 1145374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1146374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); 1147374ce938SWang Nan break; 1148792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1149792d48b4SArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num); 1150792d48b4SArnaldo Carvalho de Melo break; 1151626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1152626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0); 1153626a6b78SWang Nan break; 1154626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1155626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1); 1156626a6b78SWang Nan break; 1157dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1158dd60fba7SMathieu Poirier ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str); 1159dd60fba7SMathieu Poirier break; 1160930a2e29SJiri Olsa default: 1161930a2e29SJiri Olsa break; 1162930a2e29SJiri Olsa } 1163930a2e29SJiri Olsa } 1164930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 1165930a2e29SJiri Olsa return 0; 1166930a2e29SJiri Olsa } 1167930a2e29SJiri Olsa 1168e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 11698c619d6aSWang Nan const char *sys, const char *event, 1170272ed29aSJiri Olsa struct parse_events_error *err, 1171e637d177SHe Kuang struct list_head *head_config) 1172e637d177SHe Kuang { 1173e637d177SHe Kuang if (head_config) { 1174e637d177SHe Kuang struct perf_event_attr attr; 1175e637d177SHe Kuang 1176272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1177e637d177SHe Kuang config_term_tracepoint)) 1178e637d177SHe Kuang return -EINVAL; 1179e637d177SHe Kuang } 1180e637d177SHe Kuang 1181e637d177SHe Kuang if (strpbrk(sys, "*?")) 1182e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1183272ed29aSJiri Olsa err, head_config); 1184e637d177SHe Kuang else 1185e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1186272ed29aSJiri Olsa err, head_config); 1187e637d177SHe Kuang } 1188e637d177SHe Kuang 11895d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 119087d650beSJiri Olsa struct list_head *list, 1191b527bab5SRobert Richter u32 type, u64 config, 11928f707d84SJiri Olsa struct list_head *head_config) 119374d5b588SJaswinder Singh Rajput { 119489812fc8SJiri Olsa struct perf_event_attr attr; 1195930a2e29SJiri Olsa LIST_HEAD(config_terms); 119674d5b588SJaswinder Singh Rajput 119789812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 119889812fc8SJiri Olsa attr.type = type; 119989812fc8SJiri Olsa attr.config = config; 12008f707d84SJiri Olsa 1201930a2e29SJiri Olsa if (head_config) { 12025d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 12030b8891a8SHe Kuang config_term_common)) 12048f707d84SJiri Olsa return -EINVAL; 12058f707d84SJiri Olsa 1206930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1207930a2e29SJiri Olsa return -ENOMEM; 1208930a2e29SJiri Olsa } 1209930a2e29SJiri Olsa 12105d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 121110bf358aSWang Nan get_config_name(head_config), &config_terms); 1212b908debdSIngo Molnar } 1213b908debdSIngo Molnar 121463ce8449SArnaldo Carvalho de Melo static int __parse_events_add_pmu(struct parse_events_state *parse_state, 121536adec85SJiri Olsa struct list_head *list, char *name, 121663ce8449SArnaldo Carvalho de Melo struct list_head *head_config, bool auto_merge_stats) 12175f537a26SJiri Olsa { 12185f537a26SJiri Olsa struct perf_event_attr attr; 121946441bdcSMatt Fleming struct perf_pmu_info info; 12205f537a26SJiri Olsa struct perf_pmu *pmu; 1221410136f5SStephane Eranian struct perf_evsel *evsel; 1222930a2e29SJiri Olsa LIST_HEAD(config_terms); 12235f537a26SJiri Olsa 12245f537a26SJiri Olsa pmu = perf_pmu__find(name); 12255f537a26SJiri Olsa if (!pmu) 12265f537a26SJiri Olsa return -EINVAL; 12275f537a26SJiri Olsa 1228dc0a6202SAdrian Hunter if (pmu->default_config) { 1229dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1230dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1231dc0a6202SAdrian Hunter } else { 12325f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1233dc0a6202SAdrian Hunter } 12345f537a26SJiri Olsa 1235ad962273SAdrian Hunter if (!head_config) { 1236ad962273SAdrian Hunter attr.type = pmu->type; 1237*66ec1191SMark Rutland evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats); 1238ad962273SAdrian Hunter return evsel ? 0 : -ENOMEM; 1239ad962273SAdrian Hunter } 1240ad962273SAdrian Hunter 124146441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1242a6146d50SZheng Yan return -EINVAL; 1243a6146d50SZheng Yan 12445f537a26SJiri Olsa /* 12455f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 12465f537a26SJiri Olsa * return value when called with fail == 0 ;) 12475f537a26SJiri Olsa */ 12485d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1249c056ba6aSJiri Olsa return -EINVAL; 12505f537a26SJiri Olsa 1251930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1252930a2e29SJiri Olsa return -ENOMEM; 1253930a2e29SJiri Olsa 12545d9cdc11SArnaldo Carvalho de Melo if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) 12555f537a26SJiri Olsa return -EINVAL; 12565f537a26SJiri Olsa 12575d9cdc11SArnaldo Carvalho de Melo evsel = __add_event(list, &parse_state->idx, &attr, 1258*66ec1191SMark Rutland get_config_name(head_config), pmu, 125963ce8449SArnaldo Carvalho de Melo &config_terms, auto_merge_stats); 1260410136f5SStephane Eranian if (evsel) { 126146441bdcSMatt Fleming evsel->unit = info.unit; 126246441bdcSMatt Fleming evsel->scale = info.scale; 1263044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 12641d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 126537932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 126696284814SAndi Kleen evsel->metric_name = info.metric_name; 1267410136f5SStephane Eranian } 1268410136f5SStephane Eranian 1269410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 12705f537a26SJiri Olsa } 12715f537a26SJiri Olsa 127263ce8449SArnaldo Carvalho de Melo int parse_events_add_pmu(struct parse_events_state *parse_state, 127363ce8449SArnaldo Carvalho de Melo struct list_head *list, char *name, 127463ce8449SArnaldo Carvalho de Melo struct list_head *head_config) 127563ce8449SArnaldo Carvalho de Melo { 127663ce8449SArnaldo Carvalho de Melo return __parse_events_add_pmu(parse_state, list, name, head_config, false); 127763ce8449SArnaldo Carvalho de Melo } 127863ce8449SArnaldo Carvalho de Melo 12795d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 12802073ad33SAndi Kleen char *str, struct list_head **listp) 12812073ad33SAndi Kleen { 12822073ad33SAndi Kleen struct list_head *head; 12832073ad33SAndi Kleen struct parse_events_term *term; 12842073ad33SAndi Kleen struct list_head *list; 12852073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 12862073ad33SAndi Kleen int ok = 0; 12872073ad33SAndi Kleen 12882073ad33SAndi Kleen *listp = NULL; 12892073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 12902073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 12912073ad33SAndi Kleen if (!list) 12922073ad33SAndi Kleen return -1; 12932073ad33SAndi Kleen INIT_LIST_HEAD(list); 12942073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 12952073ad33SAndi Kleen struct perf_pmu_alias *alias; 12962073ad33SAndi Kleen 12972073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 12982073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 12992073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 13002073ad33SAndi Kleen if (!head) 13012073ad33SAndi Kleen return -1; 13022073ad33SAndi Kleen INIT_LIST_HEAD(head); 13032073ad33SAndi Kleen if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 13042073ad33SAndi Kleen str, 1, false, &str, NULL) < 0) 13052073ad33SAndi Kleen return -1; 13062073ad33SAndi Kleen list_add_tail(&term->list, head); 13072073ad33SAndi Kleen 130863ce8449SArnaldo Carvalho de Melo if (!__parse_events_add_pmu(parse_state, list, 130963ce8449SArnaldo Carvalho de Melo pmu->name, head, true)) { 13102073ad33SAndi Kleen pr_debug("%s -> %s/%s/\n", str, 13112073ad33SAndi Kleen pmu->name, alias->str); 13122073ad33SAndi Kleen ok++; 13132073ad33SAndi Kleen } 13142073ad33SAndi Kleen 13152073ad33SAndi Kleen parse_events_terms__delete(head); 13162073ad33SAndi Kleen } 13172073ad33SAndi Kleen } 13182073ad33SAndi Kleen } 13192073ad33SAndi Kleen if (!ok) 13202073ad33SAndi Kleen return -1; 13212073ad33SAndi Kleen *listp = list; 13222073ad33SAndi Kleen return 0; 13232073ad33SAndi Kleen } 13242073ad33SAndi Kleen 13256a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 13266a4bb04cSJiri Olsa char *event_mod) 132789efb029SJiri Olsa { 13286a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 13296a4bb04cSJiri Olsa } 13306a4bb04cSJiri Olsa 133163dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 13326a4bb04cSJiri Olsa { 13336a4bb04cSJiri Olsa struct perf_evsel *leader; 13346a4bb04cSJiri Olsa 1335854f7363SWang Nan if (list_empty(list)) { 1336854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1337854f7363SWang Nan return; 1338854f7363SWang Nan } 1339854f7363SWang Nan 134063dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 134163dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 13426a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 134389efb029SJiri Olsa } 134489efb029SJiri Olsa 1345c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 13465d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 13475d7be90eSJiri Olsa struct list_head *list_all) 13485d7be90eSJiri Olsa { 13495d7be90eSJiri Olsa /* 13505d7be90eSJiri Olsa * Called for single event definition. Update the 135189efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 13525d7be90eSJiri Olsa * list, for next event definition. 13535d7be90eSJiri Olsa */ 13545d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1355b847cbdcSJiri Olsa free(list_event); 13565d7be90eSJiri Olsa } 13575d7be90eSJiri Olsa 1358f5b1135bSJiri Olsa struct event_modifier { 1359f5b1135bSJiri Olsa int eu; 1360f5b1135bSJiri Olsa int ek; 1361f5b1135bSJiri Olsa int eh; 1362f5b1135bSJiri Olsa int eH; 1363f5b1135bSJiri Olsa int eG; 1364a1e12da4SJiri Olsa int eI; 1365f5b1135bSJiri Olsa int precise; 13667f94af7aSJiri Olsa int precise_max; 1367f5b1135bSJiri Olsa int exclude_GH; 13683c176311SJiri Olsa int sample_read; 1369e9a7c414SMichael Ellerman int pinned; 1370f5b1135bSJiri Olsa }; 137161c45981SPaul Mackerras 1372f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 1373f5b1135bSJiri Olsa struct perf_evsel *evsel) 1374f5b1135bSJiri Olsa { 1375f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 1376f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 1377f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 1378f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 1379f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 1380a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 1381f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 13827f94af7aSJiri Olsa int precise_max = 0; 13833c176311SJiri Olsa int sample_read = 0; 1384e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 1385f5b1135bSJiri Olsa 1386f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1387f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 1388f5b1135bSJiri Olsa 1389f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1390ceb53fbfSIngo Molnar 139161c45981SPaul Mackerras while (*str) { 1392ab608344SPeter Zijlstra if (*str == 'u') { 1393ab608344SPeter Zijlstra if (!exclude) 1394ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 139561c45981SPaul Mackerras eu = 0; 1396ab608344SPeter Zijlstra } else if (*str == 'k') { 1397ab608344SPeter Zijlstra if (!exclude) 1398ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 139961c45981SPaul Mackerras ek = 0; 1400ab608344SPeter Zijlstra } else if (*str == 'h') { 1401ab608344SPeter Zijlstra if (!exclude) 1402ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 140361c45981SPaul Mackerras eh = 0; 140499320cc8SJoerg Roedel } else if (*str == 'G') { 140599320cc8SJoerg Roedel if (!exclude_GH) 140699320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 140799320cc8SJoerg Roedel eG = 0; 140899320cc8SJoerg Roedel } else if (*str == 'H') { 140999320cc8SJoerg Roedel if (!exclude_GH) 141099320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 141199320cc8SJoerg Roedel eH = 0; 1412a1e12da4SJiri Olsa } else if (*str == 'I') { 1413a1e12da4SJiri Olsa eI = 1; 1414ab608344SPeter Zijlstra } else if (*str == 'p') { 1415ab608344SPeter Zijlstra precise++; 14161342798cSDavid Ahern /* use of precise requires exclude_guest */ 14171342798cSDavid Ahern if (!exclude_GH) 14181342798cSDavid Ahern eG = 1; 14197f94af7aSJiri Olsa } else if (*str == 'P') { 14207f94af7aSJiri Olsa precise_max = 1; 14213c176311SJiri Olsa } else if (*str == 'S') { 14223c176311SJiri Olsa sample_read = 1; 1423e9a7c414SMichael Ellerman } else if (*str == 'D') { 1424e9a7c414SMichael Ellerman pinned = 1; 1425ab608344SPeter Zijlstra } else 142661c45981SPaul Mackerras break; 1427ab608344SPeter Zijlstra 142861c45981SPaul Mackerras ++str; 142961c45981SPaul Mackerras } 143074d5b588SJaswinder Singh Rajput 143186470930SIngo Molnar /* 143289812fc8SJiri Olsa * precise ip: 143389812fc8SJiri Olsa * 143489812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 143589812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 143689812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 143789812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 143889812fc8SJiri Olsa * 143989812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 144086470930SIngo Molnar */ 144189812fc8SJiri Olsa if (precise > 3) 144289812fc8SJiri Olsa return -EINVAL; 144386470930SIngo Molnar 1444f5b1135bSJiri Olsa mod->eu = eu; 1445f5b1135bSJiri Olsa mod->ek = ek; 1446f5b1135bSJiri Olsa mod->eh = eh; 1447f5b1135bSJiri Olsa mod->eH = eH; 1448f5b1135bSJiri Olsa mod->eG = eG; 1449a1e12da4SJiri Olsa mod->eI = eI; 1450f5b1135bSJiri Olsa mod->precise = precise; 14517f94af7aSJiri Olsa mod->precise_max = precise_max; 1452f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 14533c176311SJiri Olsa mod->sample_read = sample_read; 1454e9a7c414SMichael Ellerman mod->pinned = pinned; 1455e9a7c414SMichael Ellerman 1456f5b1135bSJiri Olsa return 0; 1457f5b1135bSJiri Olsa } 1458f5b1135bSJiri Olsa 1459534123f4SJiri Olsa /* 1460534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1461534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1462534123f4SJiri Olsa */ 1463534123f4SJiri Olsa static int check_modifier(char *str) 1464534123f4SJiri Olsa { 1465534123f4SJiri Olsa char *p = str; 1466534123f4SJiri Olsa 1467534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 14687f94af7aSJiri Olsa if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1)) 1469534123f4SJiri Olsa return -1; 1470534123f4SJiri Olsa 1471534123f4SJiri Olsa while (*p) { 1472534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1473534123f4SJiri Olsa return -1; 1474534123f4SJiri Olsa p++; 1475534123f4SJiri Olsa } 1476534123f4SJiri Olsa 1477534123f4SJiri Olsa return 0; 1478534123f4SJiri Olsa } 1479534123f4SJiri Olsa 1480f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1481f5b1135bSJiri Olsa { 1482f5b1135bSJiri Olsa struct perf_evsel *evsel; 1483f5b1135bSJiri Olsa struct event_modifier mod; 1484f5b1135bSJiri Olsa 1485f5b1135bSJiri Olsa if (str == NULL) 1486f5b1135bSJiri Olsa return 0; 1487f5b1135bSJiri Olsa 1488534123f4SJiri Olsa if (check_modifier(str)) 1489534123f4SJiri Olsa return -EINVAL; 1490534123f4SJiri Olsa 1491f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1492f5b1135bSJiri Olsa return -EINVAL; 1493f5b1135bSJiri Olsa 1494e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1495f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1496f5b1135bSJiri Olsa return -EINVAL; 1497f5b1135bSJiri Olsa 1498f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 1499f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 1500f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 1501f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 1502f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 1503f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 1504a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 1505f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 15063c176311SJiri Olsa evsel->sample_read = mod.sample_read; 15077f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 1508e9a7c414SMichael Ellerman 1509e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 1510e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 1511ceb53fbfSIngo Molnar } 151286470930SIngo Molnar 151389812fc8SJiri Olsa return 0; 151486470930SIngo Molnar } 151586470930SIngo Molnar 1516ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1517ac2ba9f3SRobert Richter { 1518ac2ba9f3SRobert Richter struct perf_evsel *evsel; 1519ac2ba9f3SRobert Richter 1520e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1521ac2ba9f3SRobert Richter if (!evsel->name) 1522ac2ba9f3SRobert Richter evsel->name = strdup(name); 1523ac2ba9f3SRobert Richter } 1524ac2ba9f3SRobert Richter 1525ac2ba9f3SRobert Richter return 0; 1526ac2ba9f3SRobert Richter } 1527ac2ba9f3SRobert Richter 1528dcb4e102SKan Liang static int 1529dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1530dcb4e102SKan Liang { 1531dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1532dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1533dcb4e102SKan Liang 1534e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1535dcb4e102SKan Liang } 1536dcb4e102SKan Liang 1537dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1538dcb4e102SKan Liang { 1539dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1540dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1541dcb4e102SKan Liang int i; 1542dcb4e102SKan Liang 1543dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1544dcb4e102SKan Liang p = perf_pmu_events_list + i; 1545360e071bSTaeung Song zfree(&p->symbol); 1546dcb4e102SKan Liang } 1547506fde11STaeung Song zfree(&perf_pmu_events_list); 1548dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1549dcb4e102SKan Liang } 1550dcb4e102SKan Liang } 1551dcb4e102SKan Liang 1552dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1553dcb4e102SKan Liang do { \ 1554dcb4e102SKan Liang p->symbol = str; \ 1555dcb4e102SKan Liang if (!p->symbol) \ 1556dcb4e102SKan Liang goto err; \ 1557dcb4e102SKan Liang p->type = stype; \ 1558dcb4e102SKan Liang } while (0) 1559dcb4e102SKan Liang 1560dcb4e102SKan Liang /* 1561dcb4e102SKan Liang * Read the pmu events list from sysfs 1562dcb4e102SKan Liang * Save it into perf_pmu_events_list 1563dcb4e102SKan Liang */ 1564dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1565dcb4e102SKan Liang { 1566dcb4e102SKan Liang 1567dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1568dcb4e102SKan Liang struct perf_pmu_alias *alias; 1569dcb4e102SKan Liang int len = 0; 1570dcb4e102SKan Liang 1571231bb2aaSAndi Kleen pmu = NULL; 1572231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1573dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1574dcb4e102SKan Liang if (strchr(alias->name, '-')) 1575dcb4e102SKan Liang len++; 1576dcb4e102SKan Liang len++; 1577dcb4e102SKan Liang } 1578231bb2aaSAndi Kleen } 1579231bb2aaSAndi Kleen 1580231bb2aaSAndi Kleen if (len == 0) { 1581231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1582231bb2aaSAndi Kleen return; 1583231bb2aaSAndi Kleen } 1584dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1585dcb4e102SKan Liang if (!perf_pmu_events_list) 1586dcb4e102SKan Liang return; 1587dcb4e102SKan Liang perf_pmu_events_list_num = len; 1588dcb4e102SKan Liang 1589dcb4e102SKan Liang len = 0; 1590231bb2aaSAndi Kleen pmu = NULL; 1591231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1592dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1593dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1594dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1595dcb4e102SKan Liang 1596dcb4e102SKan Liang if (tmp != NULL) { 1597dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1598dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1599dcb4e102SKan Liang p++; 1600dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1601dcb4e102SKan Liang len += 2; 1602dcb4e102SKan Liang } else { 1603dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1604dcb4e102SKan Liang len++; 1605dcb4e102SKan Liang } 1606dcb4e102SKan Liang } 1607231bb2aaSAndi Kleen } 1608dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1609dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1610dcb4e102SKan Liang 1611dcb4e102SKan Liang return; 1612dcb4e102SKan Liang err: 1613dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1614dcb4e102SKan Liang } 1615dcb4e102SKan Liang 1616dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1617dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1618dcb4e102SKan Liang { 1619dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1620dcb4e102SKan Liang 1621dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1622dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1623dcb4e102SKan Liang perf_pmu__parse_init(); 1624dcb4e102SKan Liang /* 1625dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1626dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1627dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1628dcb4e102SKan Liang */ 1629dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1630dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1631dcb4e102SKan Liang 1632dcb4e102SKan Liang p.symbol = strdup(name); 1633dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1634dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1635dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1636360e071bSTaeung Song zfree(&p.symbol); 1637dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1638dcb4e102SKan Liang } 1639dcb4e102SKan Liang 16405d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token) 1641ac20de6fSZheng Yan { 1642ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1643ac20de6fSZheng Yan void *scanner; 1644ac20de6fSZheng Yan int ret; 1645ac20de6fSZheng Yan 164690e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1647ac20de6fSZheng Yan if (ret) 1648ac20de6fSZheng Yan return ret; 1649ac20de6fSZheng Yan 1650ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1651ac20de6fSZheng Yan 1652ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1653ac20de6fSZheng Yan parse_events_debug = 1; 1654ac20de6fSZheng Yan #endif 16555d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 1656ac20de6fSZheng Yan 1657ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1658ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1659ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1660ac20de6fSZheng Yan return ret; 1661ac20de6fSZheng Yan } 1662ac20de6fSZheng Yan 166390e2b22dSJiri Olsa /* 166490e2b22dSJiri Olsa * parse event config string, return a list of event terms. 166590e2b22dSJiri Olsa */ 166690e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 166790e2b22dSJiri Olsa { 16685d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 166990e2b22dSJiri Olsa .terms = NULL, 167090e2b22dSJiri Olsa }; 167190e2b22dSJiri Olsa int ret; 167290e2b22dSJiri Olsa 16735d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_TERMS); 167490e2b22dSJiri Olsa if (!ret) { 16755d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 16765d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 167790e2b22dSJiri Olsa return 0; 167890e2b22dSJiri Olsa } 167990e2b22dSJiri Olsa 16805d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 168190e2b22dSJiri Olsa return ret; 168290e2b22dSJiri Olsa } 168390e2b22dSJiri Olsa 1684b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1685b39b8393SJiri Olsa struct parse_events_error *err) 168686470930SIngo Molnar { 16875d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 16885d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 168946010ab2SJiri Olsa .idx = evlist->nr_entries, 1690b39b8393SJiri Olsa .error = err, 16917630b3e2SWang Nan .evlist = evlist, 169246010ab2SJiri Olsa }; 169346010ab2SJiri Olsa int ret; 169486470930SIngo Molnar 16955d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS); 1696dcb4e102SKan Liang perf_pmu__parse_cleanup(); 169789812fc8SJiri Olsa if (!ret) { 169815bfd2ccSWang Nan struct perf_evsel *last; 169915bfd2ccSWang Nan 17005d9cdc11SArnaldo Carvalho de Melo if (list_empty(&parse_state.list)) { 1701854f7363SWang Nan WARN_ONCE(true, "WARNING: event parser found nothing"); 1702854f7363SWang Nan return -1; 1703854f7363SWang Nan } 1704854f7363SWang Nan 17055d9cdc11SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &parse_state.list); 17065d9cdc11SArnaldo Carvalho de Melo evlist->nr_groups += parse_state.nr_groups; 170715bfd2ccSWang Nan last = perf_evlist__last(evlist); 170815bfd2ccSWang Nan last->cmdline_group_boundary = true; 170915bfd2ccSWang Nan 171086470930SIngo Molnar return 0; 171186470930SIngo Molnar } 171286470930SIngo Molnar 17135d7be90eSJiri Olsa /* 17145d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 17155d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 17165d7be90eSJiri Olsa * need to bother. 17175d7be90eSJiri Olsa */ 171889812fc8SJiri Olsa return ret; 171989812fc8SJiri Olsa } 172089812fc8SJiri Olsa 1721b39b8393SJiri Olsa #define MAX_WIDTH 1000 1722b39b8393SJiri Olsa static int get_term_width(void) 1723b39b8393SJiri Olsa { 1724b39b8393SJiri Olsa struct winsize ws; 1725b39b8393SJiri Olsa 1726b39b8393SJiri Olsa get_term_dimensions(&ws); 1727b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1728b39b8393SJiri Olsa } 1729b39b8393SJiri Olsa 1730b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err, 1731b39b8393SJiri Olsa const char *event) 1732b39b8393SJiri Olsa { 1733b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1734b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1735b39b8393SJiri Olsa char *buf = (char *) event; 1736b39b8393SJiri Olsa int idx = 0; 1737b39b8393SJiri Olsa 1738b39b8393SJiri Olsa if (err->str) { 1739b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1740b39b8393SJiri Olsa int width = get_term_width() - 2; 1741b39b8393SJiri Olsa int len_event = strlen(event); 1742b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1743b39b8393SJiri Olsa 1744b39b8393SJiri Olsa /* 1745b39b8393SJiri Olsa * Maximum error index indent, we will cut 1746b39b8393SJiri Olsa * the event string if it's bigger. 1747b39b8393SJiri Olsa */ 1748141b2d31SAdrian Hunter int max_err_idx = 13; 1749b39b8393SJiri Olsa 1750b39b8393SJiri Olsa /* 1751b39b8393SJiri Olsa * Let's be specific with the message when 1752b39b8393SJiri Olsa * we have the precise error. 1753b39b8393SJiri Olsa */ 1754b39b8393SJiri Olsa str = "event syntax error: "; 1755b39b8393SJiri Olsa len_str = strlen(str); 1756b39b8393SJiri Olsa max_len = width - len_str; 1757b39b8393SJiri Olsa 1758b39b8393SJiri Olsa buf = _buf; 1759b39b8393SJiri Olsa 1760bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 1761b39b8393SJiri Olsa if (err->idx > max_err_idx) 1762b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1763b39b8393SJiri Olsa 1764b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1765b39b8393SJiri Olsa 1766b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1767b39b8393SJiri Olsa if (cut) 1768b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1769b39b8393SJiri Olsa 1770b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1771b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1772b39b8393SJiri Olsa buf[max_len] = 0; 1773b39b8393SJiri Olsa } 1774b39b8393SJiri Olsa 1775b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1776b39b8393SJiri Olsa } 1777b39b8393SJiri Olsa 1778b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1779b39b8393SJiri Olsa if (idx) { 1780b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1781b39b8393SJiri Olsa if (err->help) 1782b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1783360e071bSTaeung Song zfree(&err->str); 1784360e071bSTaeung Song zfree(&err->help); 1785b39b8393SJiri Olsa } 1786b39b8393SJiri Olsa 1787b39b8393SJiri Olsa fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1788b39b8393SJiri Olsa } 1789b39b8393SJiri Olsa 1790b39b8393SJiri Olsa #undef MAX_WIDTH 1791b39b8393SJiri Olsa 1792f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 17931d037ca1SIrina Tirdea int unset __maybe_unused) 1794f120f9d5SJiri Olsa { 1795f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1796b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1797b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 17989175ce1fSAndi Kleen 1799b39b8393SJiri Olsa if (ret) 1800b39b8393SJiri Olsa parse_events_print_error(&err, str); 1801b39b8393SJiri Olsa 18029175ce1fSAndi Kleen return ret; 1803f120f9d5SJiri Olsa } 1804f120f9d5SJiri Olsa 18054ba1faa1SWang Nan static int 18064ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 18074ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 18084ba1faa1SWang Nan const void *arg), 18094ba1faa1SWang Nan const void *arg) 1810c171b552SLi Zefan { 181169aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 18124ba1faa1SWang Nan int err; 1813c171b552SLi Zefan 1814854f7363SWang Nan /* 1815854f7363SWang Nan * Don't return when list_empty, give func a chance to report 1816854f7363SWang Nan * error when it found last == NULL. 1817854f7363SWang Nan * 1818854f7363SWang Nan * So no need to WARN here, let *func do this. 1819854f7363SWang Nan */ 1820361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 18210c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 182269aad6f1SArnaldo Carvalho de Melo 182315bfd2ccSWang Nan do { 18244ba1faa1SWang Nan err = (*func)(last, arg); 18254ba1faa1SWang Nan if (err) 1826c171b552SLi Zefan return -1; 18274ba1faa1SWang Nan if (!last) 18284ba1faa1SWang Nan return 0; 1829c171b552SLi Zefan 183015bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 183115bfd2ccSWang Nan return 0; 183215bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 183315bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 183415bfd2ccSWang Nan 1835c171b552SLi Zefan return 0; 1836c171b552SLi Zefan } 1837c171b552SLi Zefan 18384ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 18394ba1faa1SWang Nan { 18404ba1faa1SWang Nan const char *str = arg; 18411e857484SMathieu Poirier bool found = false; 18421e857484SMathieu Poirier int nr_addr_filters = 0; 18431e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 18444ba1faa1SWang Nan 18451e857484SMathieu Poirier if (evsel == NULL) 18461e857484SMathieu Poirier goto err; 18474ba1faa1SWang Nan 18481e857484SMathieu Poirier if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 18493541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 18504ba1faa1SWang Nan fprintf(stderr, 18514ba1faa1SWang Nan "not enough memory to hold filter string\n"); 18524ba1faa1SWang Nan return -1; 18534ba1faa1SWang Nan } 18544ba1faa1SWang Nan 18554ba1faa1SWang Nan return 0; 18564ba1faa1SWang Nan } 18574ba1faa1SWang Nan 18581e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 18591e857484SMathieu Poirier if (pmu->type == evsel->attr.type) { 18601e857484SMathieu Poirier found = true; 18611e857484SMathieu Poirier break; 18621e857484SMathieu Poirier } 18631e857484SMathieu Poirier 18641e857484SMathieu Poirier if (found) 18651e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 18661e857484SMathieu Poirier "%d", &nr_addr_filters); 18671e857484SMathieu Poirier 18681e857484SMathieu Poirier if (!nr_addr_filters) 18691e857484SMathieu Poirier goto err; 18701e857484SMathieu Poirier 18711e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 18721e857484SMathieu Poirier fprintf(stderr, 18731e857484SMathieu Poirier "not enough memory to hold filter string\n"); 18741e857484SMathieu Poirier return -1; 18751e857484SMathieu Poirier } 18761e857484SMathieu Poirier 18771e857484SMathieu Poirier return 0; 18781e857484SMathieu Poirier 18791e857484SMathieu Poirier err: 18801e857484SMathieu Poirier fprintf(stderr, 18811e857484SMathieu Poirier "--filter option should follow a -e tracepoint or HW tracer option\n"); 18821e857484SMathieu Poirier 18831e857484SMathieu Poirier return -1; 18841e857484SMathieu Poirier } 18851e857484SMathieu Poirier 18864ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 18874ba1faa1SWang Nan int unset __maybe_unused) 18884ba1faa1SWang Nan { 18894ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 18904ba1faa1SWang Nan 18914ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 18924ba1faa1SWang Nan (const void *)str); 18934ba1faa1SWang Nan } 18944ba1faa1SWang Nan 18954ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 18964ba1faa1SWang Nan const void *arg __maybe_unused) 18974ba1faa1SWang Nan { 18984ba1faa1SWang Nan char new_filter[64]; 18994ba1faa1SWang Nan 19004ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 19014ba1faa1SWang Nan fprintf(stderr, 19024ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 19034ba1faa1SWang Nan return -1; 19044ba1faa1SWang Nan } 19054ba1faa1SWang Nan 19064ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 19074ba1faa1SWang Nan 19083541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 19094ba1faa1SWang Nan fprintf(stderr, 19104ba1faa1SWang Nan "not enough memory to hold filter string\n"); 19114ba1faa1SWang Nan return -1; 19124ba1faa1SWang Nan } 19134ba1faa1SWang Nan 19144ba1faa1SWang Nan return 0; 19154ba1faa1SWang Nan } 19164ba1faa1SWang Nan 19174ba1faa1SWang Nan int exclude_perf(const struct option *opt, 19184ba1faa1SWang Nan const char *arg __maybe_unused, 19194ba1faa1SWang Nan int unset __maybe_unused) 19204ba1faa1SWang Nan { 19214ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 19224ba1faa1SWang Nan 19234ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 19244ba1faa1SWang Nan NULL); 19254ba1faa1SWang Nan } 19264ba1faa1SWang Nan 192786470930SIngo Molnar static const char * const event_type_descriptors[] = { 192886470930SIngo Molnar "Hardware event", 192986470930SIngo Molnar "Software event", 193086470930SIngo Molnar "Tracepoint event", 193186470930SIngo Molnar "Hardware cache event", 193241bdcb23SLiming Wang "Raw hardware event descriptor", 193341bdcb23SLiming Wang "Hardware breakpoint", 193486470930SIngo Molnar }; 193586470930SIngo Molnar 1936ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1937ab0e4800SYunlong Song { 1938ab0e4800SYunlong Song const char * const *as = a; 1939ab0e4800SYunlong Song const char * const *bs = b; 1940ab0e4800SYunlong Song 1941ab0e4800SYunlong Song return strcmp(*as, *bs); 1942ab0e4800SYunlong Song } 1943ab0e4800SYunlong Song 194486470930SIngo Molnar /* 1945f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1946f6bdafefSJason Baron */ 1947f6bdafefSJason Baron 1948a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1949a3277d2dSFrederic Weisbecker bool name_only) 1950f6bdafefSJason Baron { 1951f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 195222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1953f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1954725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1955ab0e4800SYunlong Song char **evt_list = NULL; 1956ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1957ab0e4800SYunlong Song bool evt_num_known = false; 1958f6bdafefSJason Baron 1959ab0e4800SYunlong Song restart: 1960ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1961f6bdafefSJason Baron if (!sys_dir) 1962725b1368SEric Dumazet return; 1963f6bdafefSJason Baron 1964ab0e4800SYunlong Song if (evt_num_known) { 1965ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1966ab0e4800SYunlong Song if (!evt_list) 1967ab0e4800SYunlong Song goto out_close_sys_dir; 1968ab0e4800SYunlong Song } 1969ab0e4800SYunlong Song 197022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 1971668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 197222a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 1973668b8788SArnaldo Carvalho de Melo continue; 1974725b1368SEric Dumazet 1975ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 197622a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 1977725b1368SEric Dumazet evt_dir = opendir(dir_path); 1978725b1368SEric Dumazet if (!evt_dir) 19796b58e7f1SUlrich Drepper continue; 1980725b1368SEric Dumazet 198122a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 1982668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 198322a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 1984668b8788SArnaldo Carvalho de Melo continue; 1985668b8788SArnaldo Carvalho de Melo 1986ab0e4800SYunlong Song if (!evt_num_known) { 1987ab0e4800SYunlong Song evt_num++; 1988a3277d2dSFrederic Weisbecker continue; 1989a3277d2dSFrederic Weisbecker } 1990a3277d2dSFrederic Weisbecker 1991f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 199222a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 1993ab0e4800SYunlong Song 1994ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 1995ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1996ab0e4800SYunlong Song goto out_close_evt_dir; 1997ab0e4800SYunlong Song evt_i++; 1998f6bdafefSJason Baron } 1999f6bdafefSJason Baron closedir(evt_dir); 2000f6bdafefSJason Baron } 2001f6bdafefSJason Baron closedir(sys_dir); 2002ab0e4800SYunlong Song 2003ab0e4800SYunlong Song if (!evt_num_known) { 2004ab0e4800SYunlong Song evt_num_known = true; 2005ab0e4800SYunlong Song goto restart; 2006ab0e4800SYunlong Song } 2007ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2008ab0e4800SYunlong Song evt_i = 0; 2009ab0e4800SYunlong Song while (evt_i < evt_num) { 2010ab0e4800SYunlong Song if (name_only) { 2011ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2012ab0e4800SYunlong Song continue; 2013ab0e4800SYunlong Song } 2014ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2015ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2016ab0e4800SYunlong Song } 2017dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2018ab0e4800SYunlong Song printf("\n"); 2019ab0e4800SYunlong Song 2020ab0e4800SYunlong Song out_free: 2021ab0e4800SYunlong Song evt_num = evt_i; 2022ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2023ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2024ab0e4800SYunlong Song zfree(&evt_list); 2025ab0e4800SYunlong Song return; 2026ab0e4800SYunlong Song 2027ab0e4800SYunlong Song out_close_evt_dir: 2028ab0e4800SYunlong Song closedir(evt_dir); 2029ab0e4800SYunlong Song out_close_sys_dir: 2030ab0e4800SYunlong Song closedir(sys_dir); 2031ab0e4800SYunlong Song 2032ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2033ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2034ab0e4800SYunlong Song if (evt_list) 2035ab0e4800SYunlong Song goto out_free; 2036f6bdafefSJason Baron } 2037f6bdafefSJason Baron 2038f6bdafefSJason Baron /* 203920c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 204020c457b8SThomas Renninger */ 204120c457b8SThomas Renninger 204220c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 204320c457b8SThomas Renninger { 204420c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 204522a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 204620c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 204720c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 204820c457b8SThomas Renninger 2049ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 205020c457b8SThomas Renninger if (!sys_dir) 205120c457b8SThomas Renninger return 0; 205220c457b8SThomas Renninger 205322a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 205420c457b8SThomas Renninger 2055ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 205622a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name); 205720c457b8SThomas Renninger evt_dir = opendir(dir_path); 205820c457b8SThomas Renninger if (!evt_dir) 205920c457b8SThomas Renninger continue; 206020c457b8SThomas Renninger 206122a9f41bSArnaldo Carvalho de Melo for_each_event(sys_dirent, evt_dir, evt_dirent) { 206220c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 206322a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 206420c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 206520c457b8SThomas Renninger closedir(evt_dir); 206620c457b8SThomas Renninger closedir(sys_dir); 206720c457b8SThomas Renninger return 1; 206820c457b8SThomas Renninger } 206920c457b8SThomas Renninger } 207020c457b8SThomas Renninger closedir(evt_dir); 207120c457b8SThomas Renninger } 207220c457b8SThomas Renninger closedir(sys_dir); 207320c457b8SThomas Renninger return 0; 207420c457b8SThomas Renninger } 207520c457b8SThomas Renninger 2076b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2077b41f1cecSNamhyung Kim { 2078b41f1cecSNamhyung Kim bool ret = true; 207988fee52eSVince Weaver int open_return; 2080b41f1cecSNamhyung Kim struct perf_evsel *evsel; 2081b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2082b41f1cecSNamhyung Kim .type = type, 2083b41f1cecSNamhyung Kim .config = config, 2084b41f1cecSNamhyung Kim .disabled = 1, 2085b41f1cecSNamhyung Kim }; 208689896051SArnaldo Carvalho de Melo struct thread_map *tmap = thread_map__new_by_tid(0); 208789896051SArnaldo Carvalho de Melo 208889896051SArnaldo Carvalho de Melo if (tmap == NULL) 208989896051SArnaldo Carvalho de Melo return false; 2090b41f1cecSNamhyung Kim 2091ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 2092b41f1cecSNamhyung Kim if (evsel) { 209389896051SArnaldo Carvalho de Melo open_return = perf_evsel__open(evsel, NULL, tmap); 209488fee52eSVince Weaver ret = open_return >= 0; 209588fee52eSVince Weaver 209688fee52eSVince Weaver if (open_return == -EACCES) { 209788fee52eSVince Weaver /* 209888fee52eSVince Weaver * This happens if the paranoid value 209988fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 210088fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 210188fee52eSVince Weaver * by default as some ARM machines do not support it. 210288fee52eSVince Weaver * 210388fee52eSVince Weaver */ 210488fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 210589896051SArnaldo Carvalho de Melo ret = perf_evsel__open(evsel, NULL, tmap) >= 0; 210688fee52eSVince Weaver } 2107b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 2108b41f1cecSNamhyung Kim } 2109b41f1cecSNamhyung Kim 2110b41f1cecSNamhyung Kim return ret; 2111b41f1cecSNamhyung Kim } 2112b41f1cecSNamhyung Kim 211340218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 211440218daeSMasami Hiramatsu bool name_only) 211540218daeSMasami Hiramatsu { 211640218daeSMasami Hiramatsu struct probe_cache *pcache; 211740218daeSMasami Hiramatsu struct probe_cache_entry *ent; 211840218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 211940218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 212040218daeSMasami Hiramatsu struct str_node *nd, *nd2; 212140218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 212240218daeSMasami Hiramatsu bool show_detail = false; 212340218daeSMasami Hiramatsu int ret; 212440218daeSMasami Hiramatsu 212540218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 212640218daeSMasami Hiramatsu if (!sdtlist) { 212740218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 212840218daeSMasami Hiramatsu return; 212940218daeSMasami Hiramatsu } 213040218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 213140218daeSMasami Hiramatsu if (!bidlist) { 213240218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 213340218daeSMasami Hiramatsu return; 213440218daeSMasami Hiramatsu } 213540218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2136f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 213740218daeSMasami Hiramatsu if (!pcache) 213840218daeSMasami Hiramatsu continue; 213940218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 214040218daeSMasami Hiramatsu if (!ent->sdt) 214140218daeSMasami Hiramatsu continue; 214240218daeSMasami Hiramatsu if (subsys_glob && 214340218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 214440218daeSMasami Hiramatsu continue; 214540218daeSMasami Hiramatsu if (event_glob && 214640218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 214740218daeSMasami Hiramatsu continue; 214840218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 214940218daeSMasami Hiramatsu ent->pev.event, nd->s); 215040218daeSMasami Hiramatsu if (ret > 0) 215140218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 215240218daeSMasami Hiramatsu } 215340218daeSMasami Hiramatsu probe_cache__delete(pcache); 215440218daeSMasami Hiramatsu } 215540218daeSMasami Hiramatsu strlist__delete(bidlist); 215640218daeSMasami Hiramatsu 215740218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 215840218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 215940218daeSMasami Hiramatsu if (buf) 216040218daeSMasami Hiramatsu *(buf++) = '\0'; 216140218daeSMasami Hiramatsu if (name_only) { 216240218daeSMasami Hiramatsu printf("%s ", nd->s); 216340218daeSMasami Hiramatsu continue; 216440218daeSMasami Hiramatsu } 216540218daeSMasami Hiramatsu nd2 = strlist__next(nd); 216640218daeSMasami Hiramatsu if (nd2) { 216740218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 216840218daeSMasami Hiramatsu if (ptr) 216940218daeSMasami Hiramatsu *ptr = '\0'; 217040218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 217140218daeSMasami Hiramatsu show_detail = true; 217240218daeSMasami Hiramatsu } 217340218daeSMasami Hiramatsu if (show_detail) { 217440218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 217540218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 217640218daeSMasami Hiramatsu if (ret > 0) { 217740218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 217840218daeSMasami Hiramatsu free(buf); 217940218daeSMasami Hiramatsu } 218040218daeSMasami Hiramatsu } else 218140218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 218240218daeSMasami Hiramatsu if (nd2) { 218340218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 218440218daeSMasami Hiramatsu show_detail = false; 218540218daeSMasami Hiramatsu if (ptr) 218640218daeSMasami Hiramatsu *ptr = '@'; 218740218daeSMasami Hiramatsu } 218840218daeSMasami Hiramatsu } 218940218daeSMasami Hiramatsu strlist__delete(sdtlist); 219040218daeSMasami Hiramatsu } 219140218daeSMasami Hiramatsu 2192a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2193668b8788SArnaldo Carvalho de Melo { 2194ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 21950b668bc9SArnaldo Carvalho de Melo char name[64]; 2196ab0e4800SYunlong Song char **evt_list = NULL; 2197ab0e4800SYunlong Song bool evt_num_known = false; 2198ab0e4800SYunlong Song 2199ab0e4800SYunlong Song restart: 2200ab0e4800SYunlong Song if (evt_num_known) { 2201ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2202ab0e4800SYunlong Song if (!evt_list) 2203ab0e4800SYunlong Song goto out_enomem; 2204ab0e4800SYunlong Song } 2205668b8788SArnaldo Carvalho de Melo 2206668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2207668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2208668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 22090b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2210668b8788SArnaldo Carvalho de Melo continue; 2211668b8788SArnaldo Carvalho de Melo 2212668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 22130b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 22140b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 2215947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2216668b8788SArnaldo Carvalho de Melo continue; 2217668b8788SArnaldo Carvalho de Melo 2218b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2219b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2220b41f1cecSNamhyung Kim continue; 2221b41f1cecSNamhyung Kim 2222ab0e4800SYunlong Song if (!evt_num_known) { 2223ab0e4800SYunlong Song evt_num++; 2224ab0e4800SYunlong Song continue; 2225ab0e4800SYunlong Song } 2226ab0e4800SYunlong Song 2227ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2228ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2229ab0e4800SYunlong Song goto out_enomem; 2230ab0e4800SYunlong Song evt_i++; 2231668b8788SArnaldo Carvalho de Melo } 2232668b8788SArnaldo Carvalho de Melo } 2233668b8788SArnaldo Carvalho de Melo } 2234668b8788SArnaldo Carvalho de Melo 2235ab0e4800SYunlong Song if (!evt_num_known) { 2236ab0e4800SYunlong Song evt_num_known = true; 2237ab0e4800SYunlong Song goto restart; 2238ab0e4800SYunlong Song } 2239ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2240ab0e4800SYunlong Song evt_i = 0; 2241ab0e4800SYunlong Song while (evt_i < evt_num) { 2242ab0e4800SYunlong Song if (name_only) { 2243ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2244ab0e4800SYunlong Song continue; 2245ab0e4800SYunlong Song } 2246ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2247ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2248ab0e4800SYunlong Song } 2249dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2250dc098b35SAndi Kleen printf("\n"); 2251ab0e4800SYunlong Song 2252ab0e4800SYunlong Song out_free: 2253ab0e4800SYunlong Song evt_num = evt_i; 2254ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2255ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2256ab0e4800SYunlong Song zfree(&evt_list); 2257ab0e4800SYunlong Song return evt_num; 2258ab0e4800SYunlong Song 2259ab0e4800SYunlong Song out_enomem: 2260ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2261ab0e4800SYunlong Song if (evt_list) 2262ab0e4800SYunlong Song goto out_free; 2263ab0e4800SYunlong Song return evt_num; 2264668b8788SArnaldo Carvalho de Melo } 2265668b8788SArnaldo Carvalho de Melo 2266705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2267a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2268a3277d2dSFrederic Weisbecker bool name_only) 226986470930SIngo Molnar { 2270ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2271947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2272ab0e4800SYunlong Song char **evt_list = NULL; 2273ab0e4800SYunlong Song bool evt_num_known = false; 2274ab0e4800SYunlong Song 2275ab0e4800SYunlong Song restart: 2276ab0e4800SYunlong Song if (evt_num_known) { 2277ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2278ab0e4800SYunlong Song if (!evt_list) 2279ab0e4800SYunlong Song goto out_enomem; 2280ab0e4800SYunlong Song syms -= max; 2281ab0e4800SYunlong Song } 228286470930SIngo Molnar 22831dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2284668b8788SArnaldo Carvalho de Melo 2285e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2286668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2287668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2288668b8788SArnaldo Carvalho de Melo continue; 228986470930SIngo Molnar 2290b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2291b41f1cecSNamhyung Kim continue; 2292b41f1cecSNamhyung Kim 2293ab0e4800SYunlong Song if (!evt_num_known) { 2294ab0e4800SYunlong Song evt_num++; 2295a3277d2dSFrederic Weisbecker continue; 2296a3277d2dSFrederic Weisbecker } 2297a3277d2dSFrederic Weisbecker 2298ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2299947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 230074d5b588SJaswinder Singh Rajput else 2301947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 230286470930SIngo Molnar 2303ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2304ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2305ab0e4800SYunlong Song goto out_enomem; 2306ab0e4800SYunlong Song evt_i++; 230786470930SIngo Molnar } 230886470930SIngo Molnar 2309ab0e4800SYunlong Song if (!evt_num_known) { 2310ab0e4800SYunlong Song evt_num_known = true; 2311ab0e4800SYunlong Song goto restart; 2312ab0e4800SYunlong Song } 2313ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2314ab0e4800SYunlong Song evt_i = 0; 2315ab0e4800SYunlong Song while (evt_i < evt_num) { 2316ab0e4800SYunlong Song if (name_only) { 2317ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2318ab0e4800SYunlong Song continue; 2319ab0e4800SYunlong Song } 2320ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2321ab0e4800SYunlong Song } 2322dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2323689d3018SMarti Raudsepp printf("\n"); 2324ab0e4800SYunlong Song 2325ab0e4800SYunlong Song out_free: 2326ab0e4800SYunlong Song evt_num = evt_i; 2327ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2328ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2329ab0e4800SYunlong Song zfree(&evt_list); 2330ab0e4800SYunlong Song return; 2331ab0e4800SYunlong Song 2332ab0e4800SYunlong Song out_enomem: 2333ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2334ab0e4800SYunlong Song if (evt_list) 2335ab0e4800SYunlong Song goto out_free; 2336668b8788SArnaldo Carvalho de Melo } 23371dc12760SJiri Olsa 23381dc12760SJiri Olsa /* 23391dc12760SJiri Olsa * Print the help text for the event symbols: 23401dc12760SJiri Olsa */ 2341c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2342bf874fcfSAndi Kleen bool long_desc, bool details_flag) 23431dc12760SJiri Olsa { 23441dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2345a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 23461dc12760SJiri Olsa 23471dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2348a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 23491dc12760SJiri Olsa 2350a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 235173c24cb8SJaswinder Singh Rajput 2352bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 2353bf874fcfSAndi Kleen details_flag); 2354dc098b35SAndi Kleen 2355668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2356668b8788SArnaldo Carvalho de Melo return; 235773c24cb8SJaswinder Singh Rajput 2358a3277d2dSFrederic Weisbecker if (!name_only) { 2359947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23605f537a26SJiri Olsa "rNNN", 23611cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 23625f537a26SJiri Olsa printf(" %-50s [%s]\n", 23635f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 23645f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2365dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2366dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 236786470930SIngo Molnar 2368947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 23693741eb9fSJacob Shin "mem:<addr>[/len][:access]", 237041bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2371dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 23721b290d67SFrederic Weisbecker printf("\n"); 2373a3277d2dSFrederic Weisbecker } 23741b290d67SFrederic Weisbecker 2375a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 237640218daeSMasami Hiramatsu 237740218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 237886470930SIngo Molnar } 23798f707d84SJiri Olsa 23806cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 23818f707d84SJiri Olsa { 238216fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 23838f707d84SJiri Olsa } 23848f707d84SJiri Olsa 238567b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 238667b49b38SJiri Olsa struct parse_events_term *temp, 238767b49b38SJiri Olsa char *str, u64 num) 23888f707d84SJiri Olsa { 23896cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 23908f707d84SJiri Olsa 239167b49b38SJiri Olsa term = malloc(sizeof(*term)); 23928f707d84SJiri Olsa if (!term) 23938f707d84SJiri Olsa return -ENOMEM; 23948f707d84SJiri Olsa 239567b49b38SJiri Olsa *term = *temp; 23968f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 23978f707d84SJiri Olsa 239867b49b38SJiri Olsa switch (term->type_val) { 23998f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 24008f707d84SJiri Olsa term->val.num = num; 24018f707d84SJiri Olsa break; 24028f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 24038f707d84SJiri Olsa term->val.str = str; 24048f707d84SJiri Olsa break; 24058f707d84SJiri Olsa default: 24064be8be6bSAdrian Hunter free(term); 24078f707d84SJiri Olsa return -EINVAL; 24088f707d84SJiri Olsa } 24098f707d84SJiri Olsa 24108f707d84SJiri Olsa *_term = term; 24118f707d84SJiri Olsa return 0; 24128f707d84SJiri Olsa } 24138f707d84SJiri Olsa 24146cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2415cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 241699e7138eSJiri Olsa bool no_value, 2417bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 241816fa7e82SJiri Olsa { 2419bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2420bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2421bb78ce7dSAdrian Hunter 242267b49b38SJiri Olsa struct parse_events_term temp = { 242367b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 242467b49b38SJiri Olsa .type_term = type_term, 242567b49b38SJiri Olsa .config = config, 242699e7138eSJiri Olsa .no_value = no_value, 242767b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 242867b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 242967b49b38SJiri Olsa }; 243067b49b38SJiri Olsa 243167b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 243216fa7e82SJiri Olsa } 243316fa7e82SJiri Olsa 24346cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2435cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2436bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 243716fa7e82SJiri Olsa { 2438bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2439bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2440bb78ce7dSAdrian Hunter 244167b49b38SJiri Olsa struct parse_events_term temp = { 244267b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 244367b49b38SJiri Olsa .type_term = type_term, 244467b49b38SJiri Olsa .config = config, 244567b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 244667b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 244767b49b38SJiri Olsa }; 244867b49b38SJiri Olsa 244967b49b38SJiri Olsa return new_term(term, &temp, str, 0); 245016fa7e82SJiri Olsa } 245116fa7e82SJiri Olsa 24526cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 24531d33d6dcSJiri Olsa char *config, unsigned idx) 24541d33d6dcSJiri Olsa { 24551d33d6dcSJiri Olsa struct event_symbol *sym; 245667b49b38SJiri Olsa struct parse_events_term temp = { 245767b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 245867b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 245967b49b38SJiri Olsa .config = config ?: (char *) "event", 246067b49b38SJiri Olsa }; 24611d33d6dcSJiri Olsa 24621d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 24631d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 24641d33d6dcSJiri Olsa 246567b49b38SJiri Olsa return new_term(term, &temp, (char *) sym->symbol, 0); 24661d33d6dcSJiri Olsa } 24671d33d6dcSJiri Olsa 24686cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 24696cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2470a6146d50SZheng Yan { 247167b49b38SJiri Olsa struct parse_events_term temp = { 247267b49b38SJiri Olsa .type_val = term->type_val, 247367b49b38SJiri Olsa .type_term = term->type_term, 247467b49b38SJiri Olsa .config = term->config, 247567b49b38SJiri Olsa .err_term = term->err_term, 247667b49b38SJiri Olsa .err_val = term->err_val, 247767b49b38SJiri Olsa }; 247867b49b38SJiri Olsa 247967b49b38SJiri Olsa return new_term(new, &temp, term->val.str, term->val.num); 2480a6146d50SZheng Yan } 2481a6146d50SZheng Yan 24828255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 24838255718fSAndi Kleen struct list_head **new) 24848255718fSAndi Kleen { 24858255718fSAndi Kleen struct parse_events_term *term, *n; 24868255718fSAndi Kleen int ret; 24878255718fSAndi Kleen 24888255718fSAndi Kleen if (!old) { 24898255718fSAndi Kleen *new = NULL; 24908255718fSAndi Kleen return 0; 24918255718fSAndi Kleen } 24928255718fSAndi Kleen 24938255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 24948255718fSAndi Kleen if (!*new) 24958255718fSAndi Kleen return -ENOMEM; 24968255718fSAndi Kleen INIT_LIST_HEAD(*new); 24978255718fSAndi Kleen 24988255718fSAndi Kleen list_for_each_entry (term, old, list) { 24998255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 25008255718fSAndi Kleen if (ret) 25018255718fSAndi Kleen return ret; 25028255718fSAndi Kleen list_add_tail(&n->list, *new); 25038255718fSAndi Kleen } 25048255718fSAndi Kleen return 0; 25058255718fSAndi Kleen } 25068255718fSAndi Kleen 2507fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 25088f707d84SJiri Olsa { 25096cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 25108f707d84SJiri Olsa 2511a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 25122d055bf2SWang Nan if (term->array.nr_ranges) 2513360e071bSTaeung Song zfree(&term->array.ranges); 2514a8adfcebSWang Nan list_del_init(&term->list); 25158f707d84SJiri Olsa free(term); 25168f707d84SJiri Olsa } 2517a8adfcebSWang Nan } 2518b39b8393SJiri Olsa 25192146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2520fc0a2c1dSArnaldo Carvalho de Melo { 25212146afc6SArnaldo Carvalho de Melo if (!terms) 25222146afc6SArnaldo Carvalho de Melo return; 2523fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2524d20a5f2bSWang Nan free(terms); 2525fc0a2c1dSArnaldo Carvalho de Melo } 2526fc0a2c1dSArnaldo Carvalho de Melo 25272d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 25282d055bf2SWang Nan { 2529360e071bSTaeung Song zfree(&a->ranges); 25302d055bf2SWang Nan } 25312d055bf2SWang Nan 25325d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2533b39b8393SJiri Olsa int idx, const char *str) 2534b39b8393SJiri Olsa { 25355d9cdc11SArnaldo Carvalho de Melo struct parse_events_error *err = parse_state->error; 2536b39b8393SJiri Olsa 2537a6ced2beSAdrian Hunter if (!err) 2538a6ced2beSAdrian Hunter return; 2539b39b8393SJiri Olsa err->idx = idx; 2540b39b8393SJiri Olsa err->str = strdup(str); 2541b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 2542b39b8393SJiri Olsa } 2543ffeb883eSHe Kuang 254417cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 254517cb5f84SWang Nan { 254617cb5f84SWang Nan int i; 254717cb5f84SWang Nan bool first = true; 254817cb5f84SWang Nan 254917cb5f84SWang Nan buf[0] = '\0'; 255017cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 255117cb5f84SWang Nan const char *name = config_term_names[i]; 255217cb5f84SWang Nan 25531669e509SWang Nan if (!config_term_avail(i, NULL)) 25541669e509SWang Nan continue; 255517cb5f84SWang Nan if (!name) 255617cb5f84SWang Nan continue; 255717cb5f84SWang Nan if (name[0] == '<') 255817cb5f84SWang Nan continue; 255917cb5f84SWang Nan 256017cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 256117cb5f84SWang Nan return; 256217cb5f84SWang Nan 256317cb5f84SWang Nan if (!first) 256417cb5f84SWang Nan strcat(buf, ","); 256517cb5f84SWang Nan else 256617cb5f84SWang Nan first = false; 256717cb5f84SWang Nan strcat(buf, name); 256817cb5f84SWang Nan } 256917cb5f84SWang Nan } 257017cb5f84SWang Nan 2571ffeb883eSHe Kuang /* 2572ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2573ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2574ffeb883eSHe Kuang */ 2575ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2576ffeb883eSHe Kuang { 2577ffeb883eSHe Kuang char *str; 2578626a6b78SWang Nan /* "no-overwrite" is the longest name */ 257917cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2580626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2581ffeb883eSHe Kuang 258217cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2583ffeb883eSHe Kuang /* valid terms */ 2584ffeb883eSHe Kuang if (additional_terms) { 258526dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 258626dee028SWang Nan additional_terms, static_terms) < 0) 2587ffeb883eSHe Kuang goto fail; 2588ffeb883eSHe Kuang } else { 258926dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2590ffeb883eSHe Kuang goto fail; 2591ffeb883eSHe Kuang } 2592ffeb883eSHe Kuang return str; 2593ffeb883eSHe Kuang 2594ffeb883eSHe Kuang fail: 2595ffeb883eSHe Kuang return NULL; 2596ffeb883eSHe Kuang } 2597