1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 38dd2a131SJiri Olsa #include <linux/err.h> 47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 576b31a29SArnaldo Carvalho de Melo #include <dirent.h> 6a43783aeSArnaldo Carvalho de Melo #include <errno.h> 786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h> 9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h> 10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 11391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 12b0742e90SArnaldo Carvalho de Melo #include "term.h" 134a3cec84SArnaldo Carvalho de Melo #include "build-id.h" 14361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 16fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h> 174b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1886470930SIngo Molnar #include "parse-events.h" 194b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 20a067558eSArnaldo Carvalho de Melo #include "string2.h" 218ec20b17SArnaldo Carvalho de Melo #include "strlist.h" 225aab621bSArnaldo Carvalho de Melo #include "symbol.h" 238755a8f2SArjan van de Ven #include "header.h" 2484c86ca1SWang Nan #include "bpf-loader.h" 256e81c74cSMasami Hiramatsu #include "debug.h" 26592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 279c3516d1SJiri Olsa #include <perf/cpumap.h> 28ac20de6fSZheng Yan #include "parse-events-bison.h" 2990e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 3089812fc8SJiri Olsa #include "parse-events-flex.h" 315f537a26SJiri Olsa #include "pmu.h" 32b41f1cecSNamhyung Kim #include "thread_map.h" 3340218daeSMasami Hiramatsu #include "probe-file.h" 34b39b8393SJiri Olsa #include "asm/bug.h" 35ac12f676SAndi Kleen #include "util/parse-branch-options.h" 3671b0acceSAndi Kleen #include "metricgroup.h" 3795be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h" 38252a2fdcSArnaldo Carvalho de Melo #include "util/event.h" 3989812fc8SJiri Olsa 4089812fc8SJiri Olsa #define MAX_NAME_LEN 100 4186470930SIngo Molnar 4282ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4382ba1f2fSJiri Olsa extern int parse_events_debug; 4482ba1f2fSJiri Olsa #endif 455d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 46e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 47e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 48bcd3279fSFrederic Weisbecker 49dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 50dcb4e102SKan Liang /* 51dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 52dcb4e102SKan Liang * 0 means not initialized and ready to init 53dcb4e102SKan Liang * -1 means failed to init, don't try anymore 54dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 55dcb4e102SKan Liang */ 56dcb4e102SKan Liang static int perf_pmu_events_list_num; 57dcb4e102SKan Liang 58705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 591dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 601dc12760SJiri Olsa .symbol = "cpu-cycles", 611dc12760SJiri Olsa .alias = "cycles", 621dc12760SJiri Olsa }, 631dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 641dc12760SJiri Olsa .symbol = "instructions", 651dc12760SJiri Olsa .alias = "", 661dc12760SJiri Olsa }, 671dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 681dc12760SJiri Olsa .symbol = "cache-references", 691dc12760SJiri Olsa .alias = "", 701dc12760SJiri Olsa }, 711dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 721dc12760SJiri Olsa .symbol = "cache-misses", 731dc12760SJiri Olsa .alias = "", 741dc12760SJiri Olsa }, 751dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 761dc12760SJiri Olsa .symbol = "branch-instructions", 771dc12760SJiri Olsa .alias = "branches", 781dc12760SJiri Olsa }, 791dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 801dc12760SJiri Olsa .symbol = "branch-misses", 811dc12760SJiri Olsa .alias = "", 821dc12760SJiri Olsa }, 831dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 841dc12760SJiri Olsa .symbol = "bus-cycles", 851dc12760SJiri Olsa .alias = "", 861dc12760SJiri Olsa }, 871dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 881dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 891dc12760SJiri Olsa .alias = "idle-cycles-frontend", 901dc12760SJiri Olsa }, 911dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 921dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 931dc12760SJiri Olsa .alias = "idle-cycles-backend", 941dc12760SJiri Olsa }, 951dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 961dc12760SJiri Olsa .symbol = "ref-cycles", 971dc12760SJiri Olsa .alias = "", 981dc12760SJiri Olsa }, 991dc12760SJiri Olsa }; 10086470930SIngo Molnar 101705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 1021dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1031dc12760SJiri Olsa .symbol = "cpu-clock", 1041dc12760SJiri Olsa .alias = "", 1051dc12760SJiri Olsa }, 1061dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1071dc12760SJiri Olsa .symbol = "task-clock", 1081dc12760SJiri Olsa .alias = "", 1091dc12760SJiri Olsa }, 1101dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1111dc12760SJiri Olsa .symbol = "page-faults", 1121dc12760SJiri Olsa .alias = "faults", 1131dc12760SJiri Olsa }, 1141dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1151dc12760SJiri Olsa .symbol = "context-switches", 1161dc12760SJiri Olsa .alias = "cs", 1171dc12760SJiri Olsa }, 1181dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1191dc12760SJiri Olsa .symbol = "cpu-migrations", 1201dc12760SJiri Olsa .alias = "migrations", 1211dc12760SJiri Olsa }, 1221dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1231dc12760SJiri Olsa .symbol = "minor-faults", 1241dc12760SJiri Olsa .alias = "", 1251dc12760SJiri Olsa }, 1261dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1271dc12760SJiri Olsa .symbol = "major-faults", 1281dc12760SJiri Olsa .alias = "", 1291dc12760SJiri Olsa }, 1301dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1311dc12760SJiri Olsa .symbol = "alignment-faults", 1321dc12760SJiri Olsa .alias = "", 1331dc12760SJiri Olsa }, 1341dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1351dc12760SJiri Olsa .symbol = "emulation-faults", 1361dc12760SJiri Olsa .alias = "", 1371dc12760SJiri Olsa }, 138d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 139d22d1a2aSAdrian Hunter .symbol = "dummy", 140d22d1a2aSAdrian Hunter .alias = "", 141d22d1a2aSAdrian Hunter }, 142bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 143bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 144bae9cc41SArnaldo Carvalho de Melo .alias = "", 145bae9cc41SArnaldo Carvalho de Melo }, 14686470930SIngo Molnar }; 14786470930SIngo Molnar 148cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 149cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 15086470930SIngo Molnar 151cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 152cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 153cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 154cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 15586470930SIngo Molnar 15622a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 15722a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 15822a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 15922a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 16022a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 161f6bdafefSJason Baron 162c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) 163ae07b63fSPeter Zijlstra { 164ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 165ae07b63fSPeter Zijlstra int fd; 166ae07b63fSPeter Zijlstra 167c02cab22SArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name); 168ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 169ae07b63fSPeter Zijlstra if (fd < 0) 170ae07b63fSPeter Zijlstra return -EINVAL; 171ae07b63fSPeter Zijlstra close(fd); 172ae07b63fSPeter Zijlstra 173ae07b63fSPeter Zijlstra return 0; 174ae07b63fSPeter Zijlstra } 175ae07b63fSPeter Zijlstra 176c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent) \ 17722a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 17822a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 17922a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 18022a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 181c02cab22SArnaldo Carvalho de Melo (!tp_event_has_id(dir_path, evt_dirent))) 182f6bdafefSJason Baron 183270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 184f6bdafefSJason Baron 185448d732cSIan Rogers void parse_events__handle_error(struct parse_events_error *err, int idx, 186448d732cSIan Rogers char *str, char *help) 187448d732cSIan Rogers { 188448d732cSIan Rogers if (WARN(!str, "WARNING: failed to provide error string\n")) { 189448d732cSIan Rogers free(help); 190448d732cSIan Rogers return; 191448d732cSIan Rogers } 192a910e466SIan Rogers switch (err->num_errors) { 193a910e466SIan Rogers case 0: 194448d732cSIan Rogers err->idx = idx; 195a910e466SIan Rogers err->str = str; 196a910e466SIan Rogers err->help = help; 197a910e466SIan Rogers break; 198a910e466SIan Rogers case 1: 199a910e466SIan Rogers err->first_idx = err->idx; 200a910e466SIan Rogers err->idx = idx; 201a910e466SIan Rogers err->first_str = err->str; 202a910e466SIan Rogers err->str = str; 203a910e466SIan Rogers err->first_help = err->help; 204a910e466SIan Rogers err->help = help; 205a910e466SIan Rogers break; 206a910e466SIan Rogers default: 207a910e466SIan Rogers WARN_ONCE(1, "WARNING: multiple event parsing errors\n"); 208448d732cSIan Rogers free(err->str); 209448d732cSIan Rogers err->str = str; 210448d732cSIan Rogers free(err->help); 211448d732cSIan Rogers err->help = help; 212a910e466SIan Rogers break; 213a910e466SIan Rogers } 214a910e466SIan Rogers err->num_errors++; 215448d732cSIan Rogers } 216f6bdafefSJason Baron 2171ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 218f6bdafefSJason Baron { 2191ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 220f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 22122a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2228aa8a7c8SStephane Eranian char id_buf[24]; 223725b1368SEric Dumazet int fd; 224f6bdafefSJason Baron u64 id; 225f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 22625a7d914SArnaldo Carvalho de Melo char *dir_path; 227f6bdafefSJason Baron 2287014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 229f6bdafefSJason Baron if (!sys_dir) 230725b1368SEric Dumazet return NULL; 231f6bdafefSJason Baron 23222a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 23325a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 23425a7d914SArnaldo Carvalho de Melo if (!dir_path) 23525a7d914SArnaldo Carvalho de Melo continue; 236725b1368SEric Dumazet evt_dir = opendir(dir_path); 237725b1368SEric Dumazet if (!evt_dir) 23825a7d914SArnaldo Carvalho de Melo goto next; 239725b1368SEric Dumazet 240c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 241725b1368SEric Dumazet 24277f18153SJiri Olsa scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 24322a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 244725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 245f6bdafefSJason Baron if (fd < 0) 246f6bdafefSJason Baron continue; 247f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 248f6bdafefSJason Baron close(fd); 249f6bdafefSJason Baron continue; 250f6bdafefSJason Baron } 251f6bdafefSJason Baron close(fd); 252f6bdafefSJason Baron id = atoll(id_buf); 253f6bdafefSJason Baron if (id == config) { 25425a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 255f6bdafefSJason Baron closedir(evt_dir); 256f6bdafefSJason Baron closedir(sys_dir); 25759b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2585aa365f2STaeung Song if (!path) 2595aa365f2STaeung Song return NULL; 2607125f204SArnaldo Carvalho de Melo if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) { 2611ef2ed10SFrederic Weisbecker free(path); 2621ef2ed10SFrederic Weisbecker return NULL; 2631ef2ed10SFrederic Weisbecker } 2647125f204SArnaldo Carvalho de Melo if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) { 26574cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2661ef2ed10SFrederic Weisbecker free(path); 2671ef2ed10SFrederic Weisbecker return NULL; 2681ef2ed10SFrederic Weisbecker } 2691ef2ed10SFrederic Weisbecker return path; 270f6bdafefSJason Baron } 271f6bdafefSJason Baron } 272f6bdafefSJason Baron closedir(evt_dir); 27325a7d914SArnaldo Carvalho de Melo next: 27425a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 275f6bdafefSJason Baron } 276f6bdafefSJason Baron 277f6bdafefSJason Baron closedir(sys_dir); 2781ef2ed10SFrederic Weisbecker return NULL; 2791ef2ed10SFrederic Weisbecker } 2801ef2ed10SFrederic Weisbecker 281e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 282e7c93f09SNamhyung Kim { 283e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 284e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 285e7c93f09SNamhyung Kim 286e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 287e7c93f09SNamhyung Kim free(path); 288e7c93f09SNamhyung Kim return NULL; 289e7c93f09SNamhyung Kim } 290e7c93f09SNamhyung Kim 291e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 292e7c93f09SNamhyung Kim path->name = strdup(str+1); 293e7c93f09SNamhyung Kim 294e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 29574cf249dSArnaldo Carvalho de Melo zfree(&path->system); 29674cf249dSArnaldo Carvalho de Melo zfree(&path->name); 297506fde11STaeung Song zfree(&path); 298e7c93f09SNamhyung Kim } 299e7c93f09SNamhyung Kim 300e7c93f09SNamhyung Kim return path; 301e7c93f09SNamhyung Kim } 302e7c93f09SNamhyung Kim 3031424dc96SDavid Ahern const char *event_type(int type) 3041424dc96SDavid Ahern { 3051424dc96SDavid Ahern switch (type) { 3061424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 3071424dc96SDavid Ahern return "hardware"; 3081424dc96SDavid Ahern 3091424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 3101424dc96SDavid Ahern return "software"; 3111424dc96SDavid Ahern 3121424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 3131424dc96SDavid Ahern return "tracepoint"; 3141424dc96SDavid Ahern 3151424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 3161424dc96SDavid Ahern return "hardware-cache"; 3171424dc96SDavid Ahern 3181424dc96SDavid Ahern default: 3191424dc96SDavid Ahern break; 3201424dc96SDavid Ahern } 3211424dc96SDavid Ahern 3221424dc96SDavid Ahern return "unknown"; 3231424dc96SDavid Ahern } 3241424dc96SDavid Ahern 325e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 326e814fdddSWang Nan { 327e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 328e814fdddSWang Nan } 3297ae92e74SYan, Zheng 330e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 331e814fdddSWang Nan { 332e814fdddSWang Nan struct parse_events_term *term; 333e814fdddSWang Nan 334e814fdddSWang Nan if (!head_terms) 335e814fdddSWang Nan return NULL; 336e814fdddSWang Nan 337e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 338e814fdddSWang Nan if (parse_events__is_name_term(term)) 339e814fdddSWang Nan return term->val.str; 340e814fdddSWang Nan 341e814fdddSWang Nan return NULL; 342e814fdddSWang Nan } 3437ae92e74SYan, Zheng 34432dcd021SJiri Olsa static struct evsel * 345410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3467ae92e74SYan, Zheng struct perf_event_attr *attr, 34766ec1191SMark Rutland char *name, struct perf_pmu *pmu, 348f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 349f0fbb114SAndi Kleen const char *cpu_list) 35089812fc8SJiri Olsa { 35132dcd021SJiri Olsa struct evsel *evsel; 352f854839bSJiri Olsa struct perf_cpu_map *cpus = pmu ? pmu->cpus : 3539c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 35489812fc8SJiri Olsa 35589812fc8SJiri Olsa event_attr_init(attr); 35689812fc8SJiri Olsa 35775fc5ae5STaeung Song evsel = perf_evsel__new_idx(attr, *idx); 358c5cd8ac0SDavid Ahern if (!evsel) 359410136f5SStephane Eranian return NULL; 36089812fc8SJiri Olsa 36175fc5ae5STaeung Song (*idx)++; 362d400bd3aSJiri Olsa evsel->core.cpus = perf_cpu_map__get(cpus); 363fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 364648b5af3SJiri Olsa evsel->core.system_wide = pmu ? pmu->is_uncore : false; 36563ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 366f30a79b0SJiri Olsa 3679db1763cSArnaldo Carvalho de Melo if (name) 36889812fc8SJiri Olsa evsel->name = strdup(name); 369930a2e29SJiri Olsa 370930a2e29SJiri Olsa if (config_terms) 371930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 372930a2e29SJiri Olsa 373b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 374410136f5SStephane Eranian return evsel; 37589812fc8SJiri Olsa } 37689812fc8SJiri Olsa 377c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 378930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 379930a2e29SJiri Olsa struct list_head *config_terms) 3807ae92e74SYan, Zheng { 381f0fbb114SAndi Kleen return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM; 382f0fbb114SAndi Kleen } 383f0fbb114SAndi Kleen 384f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 385f0fbb114SAndi Kleen enum perf_tool_event tool_event) 386f0fbb114SAndi Kleen { 38732dcd021SJiri Olsa struct evsel *evsel; 388f0fbb114SAndi Kleen struct perf_event_attr attr = { 389f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 390f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 391f0fbb114SAndi Kleen }; 392f0fbb114SAndi Kleen 393f0fbb114SAndi Kleen evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0"); 394f0fbb114SAndi Kleen if (!evsel) 395f0fbb114SAndi Kleen return -ENOMEM; 396f0fbb114SAndi Kleen evsel->tool_event = tool_event; 397f0fbb114SAndi Kleen if (tool_event == PERF_TOOL_DURATION_TIME) 398f0fbb114SAndi Kleen evsel->unit = strdup("ns"); 399f0fbb114SAndi Kleen return 0; 4007ae92e74SYan, Zheng } 4017ae92e74SYan, Zheng 4020b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 40386470930SIngo Molnar { 40486470930SIngo Molnar int i, j; 40561c45981SPaul Mackerras int n, longest = -1; 40686470930SIngo Molnar 40786470930SIngo Molnar for (i = 0; i < size; i++) { 4080b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 40961c45981SPaul Mackerras n = strlen(names[i][j]); 41089812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 41161c45981SPaul Mackerras longest = n; 41261c45981SPaul Mackerras } 41389812fc8SJiri Olsa if (longest > 0) 41486470930SIngo Molnar return i; 41586470930SIngo Molnar } 41686470930SIngo Molnar 4178953645fSIngo Molnar return -1; 41886470930SIngo Molnar } 41986470930SIngo Molnar 42043d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 42143d0b978SWang Nan struct parse_events_term *term, 42243d0b978SWang Nan struct parse_events_error *err); 42343d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 42443d0b978SWang Nan struct parse_events_term *term, 42543d0b978SWang Nan struct parse_events_error *err); 42643d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 42743d0b978SWang Nan struct list_head *head, 42843d0b978SWang Nan struct parse_events_error *err, 42943d0b978SWang Nan config_term_func_t config_term); 43043d0b978SWang Nan 431c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 43243d0b978SWang Nan char *type, char *op_result1, char *op_result2, 4332c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 43443d0b978SWang Nan struct list_head *head_config) 43586470930SIngo Molnar { 43689812fc8SJiri Olsa struct perf_event_attr attr; 43743d0b978SWang Nan LIST_HEAD(config_terms); 43843d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 43961c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 44089812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 44189812fc8SJiri Olsa int i, n; 44286470930SIngo Molnar 44386470930SIngo Molnar /* 44486470930SIngo Molnar * No fallback - if we cannot get a clear cache type 44586470930SIngo Molnar * then bail out: 44686470930SIngo Molnar */ 4470b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 44889812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 44986470930SIngo Molnar if (cache_type == -1) 45089812fc8SJiri Olsa return -EINVAL; 45186470930SIngo Molnar 45243d0b978SWang Nan config_name = get_config_name(head_config); 45389812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 45489812fc8SJiri Olsa 45589812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 45689812fc8SJiri Olsa char *str = op_result[i]; 45789812fc8SJiri Olsa 458275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 45961c45981SPaul Mackerras 46061c45981SPaul Mackerras if (cache_op == -1) { 4610b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 46261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 46361c45981SPaul Mackerras if (cache_op >= 0) { 4640b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 46589812fc8SJiri Olsa return -EINVAL; 46661c45981SPaul Mackerras continue; 46761c45981SPaul Mackerras } 46861c45981SPaul Mackerras } 46961c45981SPaul Mackerras 47061c45981SPaul Mackerras if (cache_result == -1) { 4710b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 47261c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 47361c45981SPaul Mackerras if (cache_result >= 0) 47461c45981SPaul Mackerras continue; 47561c45981SPaul Mackerras } 47661c45981SPaul Mackerras } 47761c45981SPaul Mackerras 47886470930SIngo Molnar /* 47986470930SIngo Molnar * Fall back to reads: 48086470930SIngo Molnar */ 4818953645fSIngo Molnar if (cache_op == -1) 4828953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 48386470930SIngo Molnar 48486470930SIngo Molnar /* 48586470930SIngo Molnar * Fall back to accesses: 48686470930SIngo Molnar */ 48786470930SIngo Molnar if (cache_result == -1) 48886470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 48986470930SIngo Molnar 49089812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 49189812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 49289812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 49343d0b978SWang Nan 49443d0b978SWang Nan if (head_config) { 4952c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 49643d0b978SWang Nan config_term_common)) 49743d0b978SWang Nan return -EINVAL; 49843d0b978SWang Nan 49943d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 50043d0b978SWang Nan return -ENOMEM; 50143d0b978SWang Nan } 50243d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 50386470930SIngo Molnar } 50486470930SIngo Molnar 505272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 5068c619d6aSWang Nan const char *sys, const char *name) 50719658171SJiri Olsa { 5084584f084SIan Rogers const char *str; 50919658171SJiri Olsa char help[BUFSIZ]; 51019658171SJiri Olsa 511ec183d22SAdrian Hunter if (!e) 512ec183d22SAdrian Hunter return; 513ec183d22SAdrian Hunter 51419658171SJiri Olsa /* 51519658171SJiri Olsa * We get error directly from syscall errno ( > 0), 51619658171SJiri Olsa * or from encoded pointer's error ( < 0). 51719658171SJiri Olsa */ 51819658171SJiri Olsa err = abs(err); 51919658171SJiri Olsa 52019658171SJiri Olsa switch (err) { 52119658171SJiri Olsa case EACCES: 5224584f084SIan Rogers str = "can't access trace events"; 52319658171SJiri Olsa break; 52419658171SJiri Olsa case ENOENT: 5254584f084SIan Rogers str = "unknown tracepoint"; 52619658171SJiri Olsa break; 52719658171SJiri Olsa default: 5284584f084SIan Rogers str = "failed to add tracepoint"; 52919658171SJiri Olsa break; 53019658171SJiri Olsa } 53119658171SJiri Olsa 53219658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 5334584f084SIan Rogers parse_events__handle_error(e, 0, strdup(str), strdup(help)); 53419658171SJiri Olsa } 53519658171SJiri Olsa 536c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 5378c619d6aSWang Nan const char *sys_name, const char *evt_name, 538272ed29aSJiri Olsa struct parse_events_error *err, 539e637d177SHe Kuang struct list_head *head_config) 540bcd3279fSFrederic Weisbecker { 54132dcd021SJiri Olsa struct evsel *evsel; 542bcd3279fSFrederic Weisbecker 543ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 54419658171SJiri Olsa if (IS_ERR(evsel)) { 545272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 5468dd2a131SJiri Olsa return PTR_ERR(evsel); 54719658171SJiri Olsa } 548bcd3279fSFrederic Weisbecker 549e637d177SHe Kuang if (head_config) { 550e637d177SHe Kuang LIST_HEAD(config_terms); 551e637d177SHe Kuang 552e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 553e637d177SHe Kuang return -ENOMEM; 554e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 555e637d177SHe Kuang } 556e637d177SHe Kuang 557b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 55882fe1c29SArnaldo Carvalho de Melo return 0; 559bcd3279fSFrederic Weisbecker } 560bcd3279fSFrederic Weisbecker 561c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5628c619d6aSWang Nan const char *sys_name, const char *evt_name, 563272ed29aSJiri Olsa struct parse_events_error *err, 564e637d177SHe Kuang struct list_head *head_config) 565bcd3279fSFrederic Weisbecker { 56625a7d914SArnaldo Carvalho de Melo char *evt_path; 567bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 568bcd3279fSFrederic Weisbecker DIR *evt_dir; 56927bf90bfSJiri Olsa int ret = 0, found = 0; 570bcd3279fSFrederic Weisbecker 57125a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 57225a7d914SArnaldo Carvalho de Melo if (!evt_path) { 57325a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 57425a7d914SArnaldo Carvalho de Melo return -1; 57525a7d914SArnaldo Carvalho de Melo } 576bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 577bcd3279fSFrederic Weisbecker if (!evt_dir) { 57825a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 579272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 58089812fc8SJiri Olsa return -1; 581bcd3279fSFrederic Weisbecker } 582bcd3279fSFrederic Weisbecker 58389812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 584bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 585bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 586bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 587bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 588bcd3279fSFrederic Weisbecker continue; 589bcd3279fSFrederic Weisbecker 59089812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 591fb1d2edfSMasami Hiramatsu continue; 592fb1d2edfSMasami Hiramatsu 59327bf90bfSJiri Olsa found++; 59427bf90bfSJiri Olsa 595e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 596272ed29aSJiri Olsa err, head_config); 597bcd3279fSFrederic Weisbecker } 598bcd3279fSFrederic Weisbecker 59927bf90bfSJiri Olsa if (!found) { 60027bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 60127bf90bfSJiri Olsa ret = -1; 60227bf90bfSJiri Olsa } 60327bf90bfSJiri Olsa 60425a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 6050bd3f084SJiri Olsa closedir(evt_dir); 60689812fc8SJiri Olsa return ret; 607bcd3279fSFrederic Weisbecker } 608bcd3279fSFrederic Weisbecker 609c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 6108c619d6aSWang Nan const char *sys_name, const char *evt_name, 611272ed29aSJiri Olsa struct parse_events_error *err, 612e637d177SHe Kuang struct list_head *head_config) 613f35488f9SJiri Olsa { 614f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 615e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 616272ed29aSJiri Olsa err, head_config) : 617e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 618272ed29aSJiri Olsa err, head_config); 619f35488f9SJiri Olsa } 620f35488f9SJiri Olsa 621c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 6228c619d6aSWang Nan const char *sys_name, const char *evt_name, 623272ed29aSJiri Olsa struct parse_events_error *err, 624e637d177SHe Kuang struct list_head *head_config) 625f35488f9SJiri Olsa { 626f35488f9SJiri Olsa struct dirent *events_ent; 627f35488f9SJiri Olsa DIR *events_dir; 628f35488f9SJiri Olsa int ret = 0; 629f35488f9SJiri Olsa 6307014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 631f35488f9SJiri Olsa if (!events_dir) { 632272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 633f35488f9SJiri Olsa return -1; 634f35488f9SJiri Olsa } 635f35488f9SJiri Olsa 636f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 637f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 638f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 639f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 640f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 641f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 642f35488f9SJiri Olsa continue; 643f35488f9SJiri Olsa 644f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 645f35488f9SJiri Olsa continue; 646f35488f9SJiri Olsa 647f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 648272ed29aSJiri Olsa evt_name, err, head_config); 649f35488f9SJiri Olsa } 650f35488f9SJiri Olsa 651f35488f9SJiri Olsa closedir(events_dir); 652f35488f9SJiri Olsa return ret; 653f35488f9SJiri Olsa } 654f35488f9SJiri Olsa 6554edf30e3SWang Nan struct __add_bpf_event_param { 6565d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6574edf30e3SWang Nan struct list_head *list; 65895088a59SWang Nan struct list_head *head_config; 6594edf30e3SWang Nan }; 6604edf30e3SWang Nan 661af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 6624edf30e3SWang Nan void *_param) 6634edf30e3SWang Nan { 6644edf30e3SWang Nan LIST_HEAD(new_evsels); 6654edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6665d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6674edf30e3SWang Nan struct list_head *list = param->list; 66832dcd021SJiri Olsa struct evsel *pos; 6694edf30e3SWang Nan int err; 6702620b7e3SArnaldo Carvalho de Melo /* 6712620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 6722620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 6732620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 6742620b7e3SArnaldo Carvalho de Melo * 6752620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 6762620b7e3SArnaldo Carvalho de Melo */ 6772620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 6782620b7e3SArnaldo Carvalho de Melo return 0; 6794edf30e3SWang Nan 6804edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 681cd102d70SWang Nan group, event, fd); 6824edf30e3SWang Nan 6835d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6845d369a75SArnaldo Carvalho de Melo event, parse_state->error, 68595088a59SWang Nan param->head_config); 6864edf30e3SWang Nan if (err) { 68732dcd021SJiri Olsa struct evsel *evsel, *tmp; 6884edf30e3SWang Nan 6894edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 690cd102d70SWang Nan group, event); 691b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 692b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 6935eb2dd2aSJiri Olsa evsel__delete(evsel); 6944edf30e3SWang Nan } 6954edf30e3SWang Nan return err; 6964edf30e3SWang Nan } 697cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6984edf30e3SWang Nan 699b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 7001f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 701cd102d70SWang Nan group, event, pos); 7021f45b1d4SWang Nan pos->bpf_fd = fd; 703af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 7041f45b1d4SWang Nan } 7054edf30e3SWang Nan list_splice(&new_evsels, list); 7064edf30e3SWang Nan return 0; 7074edf30e3SWang Nan } 7084edf30e3SWang Nan 7095d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 71084c86ca1SWang Nan struct list_head *list, 71195088a59SWang Nan struct bpf_object *obj, 71295088a59SWang Nan struct list_head *head_config) 71384c86ca1SWang Nan { 71484c86ca1SWang Nan int err; 71584c86ca1SWang Nan char errbuf[BUFSIZ]; 7165d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 717aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 71884c86ca1SWang Nan 71984c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 72084c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 72184c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 72284c86ca1SWang Nan err = -EINVAL; 72384c86ca1SWang Nan goto errout; 72484c86ca1SWang Nan } 72584c86ca1SWang Nan 72684c86ca1SWang Nan /* 727aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 728aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 729aa3abf30SWang Nan * created when failure. 730aa3abf30SWang Nan */ 731aa3abf30SWang Nan if (!registered_unprobe_atexit) { 732aa3abf30SWang Nan atexit(bpf__clear); 733aa3abf30SWang Nan registered_unprobe_atexit = true; 734aa3abf30SWang Nan } 735aa3abf30SWang Nan 736aa3abf30SWang Nan err = bpf__probe(obj); 737aa3abf30SWang Nan if (err) { 738aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 739aa3abf30SWang Nan goto errout; 740aa3abf30SWang Nan } 741aa3abf30SWang Nan 7421e5e3ee8SWang Nan err = bpf__load(obj); 7431e5e3ee8SWang Nan if (err) { 7441e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 7451e5e3ee8SWang Nan goto errout; 7461e5e3ee8SWang Nan } 7471e5e3ee8SWang Nan 748cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 7494edf30e3SWang Nan if (err) { 7504edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 7514edf30e3SWang Nan "Attach events in BPF object failed"); 7524edf30e3SWang Nan goto errout; 7534edf30e3SWang Nan } 7544edf30e3SWang Nan 7554edf30e3SWang Nan return 0; 75684c86ca1SWang Nan errout: 7575d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 7585d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 75984c86ca1SWang Nan return err; 76084c86ca1SWang Nan } 76184c86ca1SWang Nan 762a34f3be7SWang Nan static int 7635d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 764a34f3be7SWang Nan struct bpf_object *obj, 765a34f3be7SWang Nan struct list_head *head_config) 766a34f3be7SWang Nan { 767a34f3be7SWang Nan struct parse_events_term *term; 768a34f3be7SWang Nan int error_pos; 769a34f3be7SWang Nan 770a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 771a34f3be7SWang Nan return 0; 772a34f3be7SWang Nan 773a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 774a34f3be7SWang Nan char errbuf[BUFSIZ]; 775a34f3be7SWang Nan int err; 776a34f3be7SWang Nan 777a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 778a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 779a34f3be7SWang Nan "Invalid config term for BPF object"); 780a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 781a34f3be7SWang Nan 7825d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term; 7835d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 784a34f3be7SWang Nan return -EINVAL; 785a34f3be7SWang Nan } 786a34f3be7SWang Nan 7875d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 788a34f3be7SWang Nan if (err) { 7895d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 790a34f3be7SWang Nan &error_pos, err, errbuf, 791a34f3be7SWang Nan sizeof(errbuf)); 7925d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup( 7937630b3e2SWang Nan "Hint:\tValid config terms:\n" 794e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 795e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7967630b3e2SWang Nan "\n" 797e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 798a34f3be7SWang Nan " \t(add -v to see detail)"); 7995d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 800a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 8015d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_val; 802a34f3be7SWang Nan else 8035d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term + error_pos; 804a34f3be7SWang Nan return err; 805a34f3be7SWang Nan } 806a34f3be7SWang Nan } 807a34f3be7SWang Nan return 0; 808a34f3be7SWang Nan } 809a34f3be7SWang Nan 81095088a59SWang Nan /* 81195088a59SWang Nan * Split config terms: 81295088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 81395088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 81495088a59SWang Nan * events in bpf.c. 81595088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 81695088a59SWang Nan * with parse_events_config_bpf. 81795088a59SWang Nan * 81895088a59SWang Nan * Move object config terms from the first list to obj_head_config. 81995088a59SWang Nan */ 82095088a59SWang Nan static void 82195088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 82295088a59SWang Nan struct list_head *obj_head_config) 82395088a59SWang Nan { 82495088a59SWang Nan struct parse_events_term *term, *temp; 82595088a59SWang Nan 82695088a59SWang Nan /* 82795088a59SWang Nan * Currectly, all possible user config term 82895088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 82995088a59SWang Nan * happends to be a good flag. 83095088a59SWang Nan * 83195088a59SWang Nan * See parse_events_config_bpf() and 83295088a59SWang Nan * config_term_tracepoint(). 83395088a59SWang Nan */ 83495088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 83595088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 83695088a59SWang Nan list_move_tail(&term->list, obj_head_config); 83795088a59SWang Nan } 83895088a59SWang Nan 8395d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 84084c86ca1SWang Nan struct list_head *list, 841d509db04SWang Nan char *bpf_file_name, 842a34f3be7SWang Nan bool source, 843a34f3be7SWang Nan struct list_head *head_config) 84484c86ca1SWang Nan { 845a34f3be7SWang Nan int err; 84695088a59SWang Nan struct bpf_object *obj; 84795088a59SWang Nan LIST_HEAD(obj_head_config); 84895088a59SWang Nan 84995088a59SWang Nan if (head_config) 85095088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 85184c86ca1SWang Nan 852d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8536371ca3bSWang Nan if (IS_ERR(obj)) { 85484c86ca1SWang Nan char errbuf[BUFSIZ]; 85584c86ca1SWang Nan 8566371ca3bSWang Nan err = PTR_ERR(obj); 85784c86ca1SWang Nan 85884c86ca1SWang Nan if (err == -ENOTSUP) 85984c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 86084c86ca1SWang Nan "BPF support is not compiled"); 86184c86ca1SWang Nan else 862d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 863d3e0ce39SWang Nan source, 864d3e0ce39SWang Nan -err, errbuf, 865d3e0ce39SWang Nan sizeof(errbuf)); 86684c86ca1SWang Nan 8675d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 8685d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 86984c86ca1SWang Nan return err; 87084c86ca1SWang Nan } 87184c86ca1SWang Nan 8725d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 873a34f3be7SWang Nan if (err) 874a34f3be7SWang Nan return err; 8755d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 87695088a59SWang Nan 87795088a59SWang Nan /* 87895088a59SWang Nan * Caller doesn't know anything about obj_head_config, 87995088a59SWang Nan * so combine them together again before returnning. 88095088a59SWang Nan */ 88195088a59SWang Nan if (head_config) 88295088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 88395088a59SWang Nan return err; 88484c86ca1SWang Nan } 88584c86ca1SWang Nan 88689812fc8SJiri Olsa static int 88789812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8881b290d67SFrederic Weisbecker { 8891b290d67SFrederic Weisbecker int i; 8901b290d67SFrederic Weisbecker 8911b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 89289812fc8SJiri Olsa if (!type || !type[i]) 8931b290d67SFrederic Weisbecker break; 8941b290d67SFrederic Weisbecker 8957582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8967582732fSJiri Olsa do { \ 8977582732fSJiri Olsa if (attr->bp_type & bit) \ 8987582732fSJiri Olsa return -EINVAL; \ 8997582732fSJiri Olsa else \ 9007582732fSJiri Olsa attr->bp_type |= bit; \ 9017582732fSJiri Olsa } while (0) 9027582732fSJiri Olsa 9031b290d67SFrederic Weisbecker switch (type[i]) { 9041b290d67SFrederic Weisbecker case 'r': 9057582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 9061b290d67SFrederic Weisbecker break; 9071b290d67SFrederic Weisbecker case 'w': 9087582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 9091b290d67SFrederic Weisbecker break; 9101b290d67SFrederic Weisbecker case 'x': 9117582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 9121b290d67SFrederic Weisbecker break; 9131b290d67SFrederic Weisbecker default: 91489812fc8SJiri Olsa return -EINVAL; 9151b290d67SFrederic Weisbecker } 9161b290d67SFrederic Weisbecker } 91789812fc8SJiri Olsa 9187582732fSJiri Olsa #undef CHECK_SET_TYPE 9197582732fSJiri Olsa 9201b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 9211b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 9221b290d67SFrederic Weisbecker 92389812fc8SJiri Olsa return 0; 9241b290d67SFrederic Weisbecker } 9251b290d67SFrederic Weisbecker 926c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 9273741eb9fSJacob Shin void *ptr, char *type, u64 len) 9281b290d67SFrederic Weisbecker { 92989812fc8SJiri Olsa struct perf_event_attr attr; 9301b290d67SFrederic Weisbecker 93189812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 9329fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 9331b290d67SFrederic Weisbecker 93489812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 93589812fc8SJiri Olsa return -EINVAL; 9361b290d67SFrederic Weisbecker 9373741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 9383741eb9fSJacob Shin if (!len) { 93989812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 9403741eb9fSJacob Shin len = sizeof(long); 941aa59a485SFrederic Weisbecker else 9423741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 9433741eb9fSJacob Shin } 9443741eb9fSJacob Shin 9453741eb9fSJacob Shin attr.bp_len = len; 946aa59a485SFrederic Weisbecker 94789812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 9484a841d65SJovi Zhang attr.sample_period = 1; 9491b290d67SFrederic Weisbecker 950930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 9511b290d67SFrederic Weisbecker } 9521b290d67SFrederic Weisbecker 9533b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 9543b0e371cSJiri Olsa struct parse_events_error *err, 9553b0e371cSJiri Olsa int type) 9563b0e371cSJiri Olsa { 9573b0e371cSJiri Olsa if (type == term->type_val) 9583b0e371cSJiri Olsa return 0; 9593b0e371cSJiri Olsa 9603b0e371cSJiri Olsa if (err) { 961448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 962448d732cSIan Rogers type == PARSE_EVENTS__TERM_TYPE_NUM 963448d732cSIan Rogers ? strdup("expected numeric value") 964448d732cSIan Rogers : strdup("expected string value"), 965448d732cSIan Rogers NULL); 9663b0e371cSJiri Olsa } 9673b0e371cSJiri Olsa return -EINVAL; 9683b0e371cSJiri Olsa } 9693b0e371cSJiri Olsa 97017cb5f84SWang Nan /* 97117cb5f84SWang Nan * Update according to parse-events.l 97217cb5f84SWang Nan */ 97317cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 97417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 97517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 97617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 97717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 97817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 97917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 98017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 98117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 98217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 98317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 98417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 98517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 98617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 987792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 9882fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 989626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 990626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 991dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 992064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 9931b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 994eb7a52d4SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 99517cb5f84SWang Nan }; 99617cb5f84SWang Nan 9971669e509SWang Nan static bool config_term_shrinked; 9981669e509SWang Nan 9991669e509SWang Nan static bool 10001669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 10011669e509SWang Nan { 1002448d732cSIan Rogers char *err_str; 1003448d732cSIan Rogers 10041669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 1005448d732cSIan Rogers parse_events__handle_error(err, -1, 1006448d732cSIan Rogers strdup("Invalid term_type"), NULL); 10071669e509SWang Nan return false; 10081669e509SWang Nan } 10091669e509SWang Nan if (!config_term_shrinked) 10101669e509SWang Nan return true; 10111669e509SWang Nan 10121669e509SWang Nan switch (term_type) { 10131669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 10141669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 10151669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 10161669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 101706835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1018064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 10191669e509SWang Nan return true; 10201669e509SWang Nan default: 10211669e509SWang Nan if (!err) 10221669e509SWang Nan return false; 10231669e509SWang Nan 10241669e509SWang Nan /* term_type is validated so indexing is safe */ 1025448d732cSIan Rogers if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 1026448d732cSIan Rogers config_term_names[term_type]) >= 0) 1027448d732cSIan Rogers parse_events__handle_error(err, -1, err_str, NULL); 10281669e509SWang Nan return false; 10291669e509SWang Nan } 10301669e509SWang Nan } 10311669e509SWang Nan 10321669e509SWang Nan void parse_events__shrink_config_terms(void) 10331669e509SWang Nan { 10341669e509SWang Nan config_term_shrinked = true; 10351669e509SWang Nan } 10361669e509SWang Nan 10370b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 10383b0e371cSJiri Olsa struct parse_events_term *term, 10393b0e371cSJiri Olsa struct parse_events_error *err) 10408f707d84SJiri Olsa { 104116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 104216fa7e82SJiri Olsa do { \ 10433b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 104416fa7e82SJiri Olsa return -EINVAL; \ 104516fa7e82SJiri Olsa } while (0) 104616fa7e82SJiri Olsa 104716fa7e82SJiri Olsa switch (term->type_term) { 10488f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 104916fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10508f707d84SJiri Olsa attr->config = term->val.num; 10518f707d84SJiri Olsa break; 10528f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 105316fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10548f707d84SJiri Olsa attr->config1 = term->val.num; 10558f707d84SJiri Olsa break; 10568f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 105716fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10588f707d84SJiri Olsa attr->config2 = term->val.num; 10598f707d84SJiri Olsa break; 10608f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 106116fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10628f707d84SJiri Olsa break; 106309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 106409af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 106509af2a55SNamhyung Kim break; 10668f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1067ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1068ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1069448d732cSIan Rogers parse_branch_str(term->val.str, 1070448d732cSIan Rogers &attr->branch_sample_type)) { 1071448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1072448d732cSIan Rogers strdup("invalid branch sample type"), 1073448d732cSIan Rogers NULL); 1074ac12f676SAndi Kleen return -EINVAL; 1075ac12f676SAndi Kleen } 10768f707d84SJiri Olsa break; 107732067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 107832067712SKan Liang CHECK_TYPE_VAL(NUM); 107932067712SKan Liang if (term->val.num > 1) { 1080448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1081448d732cSIan Rogers strdup("expected 0 or 1"), 1082448d732cSIan Rogers NULL); 108332067712SKan Liang return -EINVAL; 108432067712SKan Liang } 108532067712SKan Liang break; 1086d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1087d457c963SKan Liang CHECK_TYPE_VAL(STR); 1088d457c963SKan Liang break; 1089d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1090d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1091d457c963SKan Liang break; 1092374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1093374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1094374ce938SWang Nan break; 1095374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1096374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1097374ce938SWang Nan break; 1098626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1099626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1100626a6b78SWang Nan break; 1101626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1102626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1103626a6b78SWang Nan break; 11046b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 11056b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 11066b5fc39bSJiri Olsa break; 1107792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1108792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1109792d48b4SArnaldo Carvalho de Melo break; 11102fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 11112fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 11122fda5adaSArnaldo Carvalho de Melo break; 1113064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1114064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1115064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 1116448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1117448d732cSIan Rogers strdup("expected 0 or 1"), 1118448d732cSIan Rogers NULL); 1119064b4e82SJin Yao return -EINVAL; 1120064b4e82SJin Yao } 1121064b4e82SJin Yao break; 11221b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 11231b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 11241b992154SAdrian Hunter break; 1125eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1126eb7a52d4SAdrian Hunter CHECK_TYPE_VAL(NUM); 1127eb7a52d4SAdrian Hunter if (term->val.num > UINT_MAX) { 1128eb7a52d4SAdrian Hunter parse_events__handle_error(err, term->err_val, 1129eb7a52d4SAdrian Hunter strdup("too big"), 1130eb7a52d4SAdrian Hunter NULL); 1131eb7a52d4SAdrian Hunter return -EINVAL; 1132eb7a52d4SAdrian Hunter } 1133eb7a52d4SAdrian Hunter break; 11348f707d84SJiri Olsa default: 1135448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1136448d732cSIan Rogers strdup("unknown term"), 1137448d732cSIan Rogers parse_events_formats_error_string(NULL)); 11388f707d84SJiri Olsa return -EINVAL; 11398f707d84SJiri Olsa } 114016fa7e82SJiri Olsa 11411669e509SWang Nan /* 11421669e509SWang Nan * Check term availbility after basic checking so 11431669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 11441669e509SWang Nan * 11451669e509SWang Nan * If check availbility at the entry of this function, 11461669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 11471669e509SWang Nan * if an invalid config term is provided for legacy events 11481669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 11491669e509SWang Nan */ 11501669e509SWang Nan if (!config_term_avail(term->type_term, err)) 11511669e509SWang Nan return -EINVAL; 11528f707d84SJiri Olsa return 0; 115316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 11548f707d84SJiri Olsa } 11558f707d84SJiri Olsa 11560b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 11570b8891a8SHe Kuang struct parse_events_term *term, 11580b8891a8SHe Kuang struct parse_events_error *err) 11590b8891a8SHe Kuang { 1160dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1161dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 11620b8891a8SHe Kuang /* 11630b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 11640b8891a8SHe Kuang * at this point what type they need to have. 11650b8891a8SHe Kuang */ 11660b8891a8SHe Kuang return 0; 11670b8891a8SHe Kuang else 11680b8891a8SHe Kuang return config_term_common(attr, term, err); 11690b8891a8SHe Kuang } 11700b8891a8SHe Kuang 1171e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1172e637d177SHe Kuang struct parse_events_term *term, 1173e637d177SHe Kuang struct parse_events_error *err) 1174e637d177SHe Kuang { 1175e637d177SHe Kuang switch (term->type_term) { 1176e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1177e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1178374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1179374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1180792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 11812fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1182626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1183626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 11841b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1185eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1186e637d177SHe Kuang return config_term_common(attr, term, err); 1187e637d177SHe Kuang default: 1188e637d177SHe Kuang if (err) { 1189448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1190448d732cSIan Rogers strdup("unknown term"), 1191448d732cSIan Rogers strdup("valid terms: call-graph,stack-size\n")); 1192e637d177SHe Kuang } 1193e637d177SHe Kuang return -EINVAL; 1194e637d177SHe Kuang } 1195e637d177SHe Kuang 1196e637d177SHe Kuang return 0; 1197e637d177SHe Kuang } 1198e637d177SHe Kuang 11998f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 12003b0e371cSJiri Olsa struct list_head *head, 12010b8891a8SHe Kuang struct parse_events_error *err, 12020b8891a8SHe Kuang config_term_func_t config_term) 12038f707d84SJiri Olsa { 12046cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 12058f707d84SJiri Olsa 12068f707d84SJiri Olsa list_for_each_entry(term, head, list) 12073b0e371cSJiri Olsa if (config_term(attr, term, err)) 12088f707d84SJiri Olsa return -EINVAL; 12098f707d84SJiri Olsa 12108f707d84SJiri Olsa return 0; 12118f707d84SJiri Olsa } 12128f707d84SJiri Olsa 1213930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1214930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1215930a2e29SJiri Olsa { 121605e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak) \ 1217930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1218930a2e29SJiri Olsa \ 1219930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1220930a2e29SJiri Olsa if (!__t) \ 1221930a2e29SJiri Olsa return -ENOMEM; \ 1222930a2e29SJiri Olsa \ 1223930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1224930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 122505e54e23SIan Rogers __t->weak = __weak; \ 1226e884602bSLeo Yan list_add_tail(&__t->list, head_terms) 1227e884602bSLeo Yan 122805e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1229e884602bSLeo Yan do { \ 123005e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 1231e884602bSLeo Yan __t->val.__name = __val; \ 1232e884602bSLeo Yan } while (0) 1233e884602bSLeo Yan 123405e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1235e884602bSLeo Yan do { \ 123605e54e23SIan Rogers ADD_CONFIG_TERM(__type, __weak); \ 12373220fb8dSLeo Yan __t->val.str = strdup(__val); \ 12383220fb8dSLeo Yan if (!__t->val.str) { \ 12393220fb8dSLeo Yan zfree(&__t); \ 12403220fb8dSLeo Yan return -ENOMEM; \ 12413220fb8dSLeo Yan } \ 12423220fb8dSLeo Yan __t->free_str = true; \ 1243930a2e29SJiri Olsa } while (0) 1244930a2e29SJiri Olsa 1245930a2e29SJiri Olsa struct parse_events_term *term; 1246930a2e29SJiri Olsa 1247930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1248930a2e29SJiri Olsa switch (term->type_term) { 1249ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 125005e54e23SIan Rogers ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 125132067712SKan Liang break; 125209af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 125305e54e23SIan Rogers ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 125409af2a55SNamhyung Kim break; 125532067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 125605e54e23SIan Rogers ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 125732067712SKan Liang break; 1258d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 125905e54e23SIan Rogers ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1260d457c963SKan Liang break; 1261ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 126205e54e23SIan Rogers ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1263ac12f676SAndi Kleen break; 1264d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1265e884602bSLeo Yan ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 126605e54e23SIan Rogers term->val.num, term->weak); 1267d457c963SKan Liang break; 1268374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1269e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 127005e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1271374ce938SWang Nan break; 1272374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1273e884602bSLeo Yan ADD_CONFIG_TERM_VAL(INHERIT, inherit, 127405e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1275374ce938SWang Nan break; 1276792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1277e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 127805e54e23SIan Rogers term->val.num, term->weak); 1279792d48b4SArnaldo Carvalho de Melo break; 12802fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1281e884602bSLeo Yan ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 128205e54e23SIan Rogers term->val.num, term->weak); 12832fda5adaSArnaldo Carvalho de Melo break; 1284626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1285e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 128605e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 1287626a6b78SWang Nan break; 1288626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1289e884602bSLeo Yan ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 129005e54e23SIan Rogers term->val.num ? 0 : 1, term->weak); 1291626a6b78SWang Nan break; 1292dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 129305e54e23SIan Rogers ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1294dd60fba7SMathieu Poirier break; 1295064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1296e884602bSLeo Yan ADD_CONFIG_TERM_VAL(PERCORE, percore, 129705e54e23SIan Rogers term->val.num ? true : false, term->weak); 1298064b4e82SJin Yao break; 12991b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1300e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 130105e54e23SIan Rogers term->val.num ? 1 : 0, term->weak); 13021b992154SAdrian Hunter break; 1303eb7a52d4SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1304e884602bSLeo Yan ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 130505e54e23SIan Rogers term->val.num, term->weak); 1306eb7a52d4SAdrian Hunter break; 1307930a2e29SJiri Olsa default: 1308930a2e29SJiri Olsa break; 1309930a2e29SJiri Olsa } 1310930a2e29SJiri Olsa } 1311a1ac7de6SAdrian Hunter return 0; 1312a1ac7de6SAdrian Hunter } 1313a1ac7de6SAdrian Hunter 1314a1ac7de6SAdrian Hunter /* 1315a1ac7de6SAdrian Hunter * Add PERF_EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1316a1ac7de6SAdrian Hunter * each bit of attr->config that the user has changed. 1317a1ac7de6SAdrian Hunter */ 1318a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1319a1ac7de6SAdrian Hunter struct list_head *head_terms) 1320a1ac7de6SAdrian Hunter { 1321a1ac7de6SAdrian Hunter struct parse_events_term *term; 1322a1ac7de6SAdrian Hunter u64 bits = 0; 1323a1ac7de6SAdrian Hunter int type; 1324a1ac7de6SAdrian Hunter 1325a1ac7de6SAdrian Hunter list_for_each_entry(term, head_config, list) { 1326a1ac7de6SAdrian Hunter switch (term->type_term) { 1327a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_USER: 1328a1ac7de6SAdrian Hunter type = perf_pmu__format_type(&pmu->format, term->config); 1329a1ac7de6SAdrian Hunter if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1330a1ac7de6SAdrian Hunter continue; 1331a1ac7de6SAdrian Hunter bits |= perf_pmu__format_bits(&pmu->format, term->config); 1332a1ac7de6SAdrian Hunter break; 1333a1ac7de6SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_CONFIG: 1334a1ac7de6SAdrian Hunter bits = ~(u64)0; 1335a1ac7de6SAdrian Hunter break; 1336a1ac7de6SAdrian Hunter default: 1337a1ac7de6SAdrian Hunter break; 1338a1ac7de6SAdrian Hunter } 1339a1ac7de6SAdrian Hunter } 1340a1ac7de6SAdrian Hunter 1341a1ac7de6SAdrian Hunter if (bits) 134205e54e23SIan Rogers ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1343a1ac7de6SAdrian Hunter 1344a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM 1345930a2e29SJiri Olsa return 0; 1346930a2e29SJiri Olsa } 1347930a2e29SJiri Olsa 1348e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 13498c619d6aSWang Nan const char *sys, const char *event, 1350272ed29aSJiri Olsa struct parse_events_error *err, 1351e637d177SHe Kuang struct list_head *head_config) 1352e637d177SHe Kuang { 1353e637d177SHe Kuang if (head_config) { 1354e637d177SHe Kuang struct perf_event_attr attr; 1355e637d177SHe Kuang 1356272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1357e637d177SHe Kuang config_term_tracepoint)) 1358e637d177SHe Kuang return -EINVAL; 1359e637d177SHe Kuang } 1360e637d177SHe Kuang 1361e637d177SHe Kuang if (strpbrk(sys, "*?")) 1362e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1363272ed29aSJiri Olsa err, head_config); 1364e637d177SHe Kuang else 1365e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1366272ed29aSJiri Olsa err, head_config); 1367e637d177SHe Kuang } 1368e637d177SHe Kuang 13695d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 137087d650beSJiri Olsa struct list_head *list, 1371b527bab5SRobert Richter u32 type, u64 config, 13728f707d84SJiri Olsa struct list_head *head_config) 137374d5b588SJaswinder Singh Rajput { 137489812fc8SJiri Olsa struct perf_event_attr attr; 1375930a2e29SJiri Olsa LIST_HEAD(config_terms); 137674d5b588SJaswinder Singh Rajput 137789812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 137889812fc8SJiri Olsa attr.type = type; 137989812fc8SJiri Olsa attr.config = config; 13808f707d84SJiri Olsa 1381930a2e29SJiri Olsa if (head_config) { 13825d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 13830b8891a8SHe Kuang config_term_common)) 13848f707d84SJiri Olsa return -EINVAL; 13858f707d84SJiri Olsa 1386930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1387930a2e29SJiri Olsa return -ENOMEM; 1388930a2e29SJiri Olsa } 1389930a2e29SJiri Olsa 13905d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 139110bf358aSWang Nan get_config_name(head_config), &config_terms); 1392b908debdSIngo Molnar } 1393b908debdSIngo Molnar 1394f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1395f0fbb114SAndi Kleen struct list_head *list, 1396f0fbb114SAndi Kleen enum perf_tool_event tool_event) 1397f0fbb114SAndi Kleen { 1398f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1399f0fbb114SAndi Kleen } 1400f0fbb114SAndi Kleen 1401064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1402064b4e82SJin Yao { 1403064b4e82SJin Yao struct perf_evsel_config_term *term; 1404064b4e82SJin Yao 1405064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 1406064b4e82SJin Yao if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE) 1407064b4e82SJin Yao return term->val.percore; 1408064b4e82SJin Yao } 1409064b4e82SJin Yao 1410064b4e82SJin Yao return false; 1411064b4e82SJin Yao } 1412064b4e82SJin Yao 1413c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 141436adec85SJiri Olsa struct list_head *list, char *name, 14153cdc5c2cSKan Liang struct list_head *head_config, 14163cdc5c2cSKan Liang bool auto_merge_stats, 14173cdc5c2cSKan Liang bool use_alias) 14185f537a26SJiri Olsa { 14195f537a26SJiri Olsa struct perf_event_attr attr; 142046441bdcSMatt Fleming struct perf_pmu_info info; 14215f537a26SJiri Olsa struct perf_pmu *pmu; 142232dcd021SJiri Olsa struct evsel *evsel; 1423333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 14243cdc5c2cSKan Liang bool use_uncore_alias; 1425930a2e29SJiri Olsa LIST_HEAD(config_terms); 14265f537a26SJiri Olsa 14275f537a26SJiri Olsa pmu = perf_pmu__find(name); 1428333b5665SAndi Kleen if (!pmu) { 1429448d732cSIan Rogers char *err_str; 1430448d732cSIan Rogers 1431448d732cSIan Rogers if (asprintf(&err_str, 1432333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1433448d732cSIan Rogers name) >= 0) 1434a910e466SIan Rogers parse_events__handle_error(err, 0, err_str, NULL); 14355f537a26SJiri Olsa return -EINVAL; 1436333b5665SAndi Kleen } 14375f537a26SJiri Olsa 1438dc0a6202SAdrian Hunter if (pmu->default_config) { 1439dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1440dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1441dc0a6202SAdrian Hunter } else { 14425f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1443dc0a6202SAdrian Hunter } 14445f537a26SJiri Olsa 14453cdc5c2cSKan Liang use_uncore_alias = (pmu->is_uncore && use_alias); 14463cdc5c2cSKan Liang 1447ad962273SAdrian Hunter if (!head_config) { 1448ad962273SAdrian Hunter attr.type = pmu->type; 1449f0fbb114SAndi Kleen evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, 1450f0fbb114SAndi Kleen auto_merge_stats, NULL); 14518c5421c0SAgustin Vega-Frias if (evsel) { 1452d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 14533cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 14548c5421c0SAgustin Vega-Frias return 0; 14558c5421c0SAgustin Vega-Frias } else { 14568c5421c0SAgustin Vega-Frias return -ENOMEM; 14578c5421c0SAgustin Vega-Frias } 1458ad962273SAdrian Hunter } 1459ad962273SAdrian Hunter 146046441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1461a6146d50SZheng Yan return -EINVAL; 1462a6146d50SZheng Yan 14635f537a26SJiri Olsa /* 14645f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 14655f537a26SJiri Olsa * return value when called with fail == 0 ;) 14665f537a26SJiri Olsa */ 14675d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1468c056ba6aSJiri Olsa return -EINVAL; 14695f537a26SJiri Olsa 1470930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1471930a2e29SJiri Olsa return -ENOMEM; 1472930a2e29SJiri Olsa 1473a1ac7de6SAdrian Hunter /* 1474a1ac7de6SAdrian Hunter * When using default config, record which bits of attr->config were 1475a1ac7de6SAdrian Hunter * changed by the user. 1476a1ac7de6SAdrian Hunter */ 1477a1ac7de6SAdrian Hunter if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1478a1ac7de6SAdrian Hunter return -ENOMEM; 1479a1ac7de6SAdrian Hunter 148038f2c422SIan Rogers if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 148138f2c422SIan Rogers struct perf_evsel_config_term *pos, *tmp; 148238f2c422SIan Rogers 148338f2c422SIan Rogers list_for_each_entry_safe(pos, tmp, &config_terms, list) { 148438f2c422SIan Rogers list_del_init(&pos->list); 1485e8dfb818SIan Rogers zfree(&pos->val.str); 148638f2c422SIan Rogers free(pos); 148738f2c422SIan Rogers } 14885f537a26SJiri Olsa return -EINVAL; 148938f2c422SIan Rogers } 14905f537a26SJiri Olsa 14915d9cdc11SArnaldo Carvalho de Melo evsel = __add_event(list, &parse_state->idx, &attr, 149266ec1191SMark Rutland get_config_name(head_config), pmu, 1493f0fbb114SAndi Kleen &config_terms, auto_merge_stats, NULL); 1494410136f5SStephane Eranian if (evsel) { 149546441bdcSMatt Fleming evsel->unit = info.unit; 149646441bdcSMatt Fleming evsel->scale = info.scale; 1497044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 14981d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 149937932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 150096284814SAndi Kleen evsel->metric_name = info.metric_name; 1501d4953f7eSIan Rogers evsel->pmu_name = name ? strdup(name) : NULL; 15023cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 1503064b4e82SJin Yao evsel->percore = config_term_percore(&evsel->config_terms); 1504410136f5SStephane Eranian } 1505410136f5SStephane Eranian 1506410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 15075f537a26SJiri Olsa } 15085f537a26SJiri Olsa 15095d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 15102073ad33SAndi Kleen char *str, struct list_head **listp) 15112073ad33SAndi Kleen { 15122073ad33SAndi Kleen struct parse_events_term *term; 15132073ad33SAndi Kleen struct list_head *list; 15142073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 15152073ad33SAndi Kleen int ok = 0; 15162073ad33SAndi Kleen 15172073ad33SAndi Kleen *listp = NULL; 15182073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 15192073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 15202073ad33SAndi Kleen if (!list) 15212073ad33SAndi Kleen return -1; 15222073ad33SAndi Kleen INIT_LIST_HEAD(list); 15232073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 15242073ad33SAndi Kleen struct perf_pmu_alias *alias; 15252073ad33SAndi Kleen 15262073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 15272073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 1528b6645a72SIan Rogers struct list_head *head; 1529b6645a72SIan Rogers char *config; 1530b6645a72SIan Rogers 15312073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 15322073ad33SAndi Kleen if (!head) 15332073ad33SAndi Kleen return -1; 15342073ad33SAndi Kleen INIT_LIST_HEAD(head); 1535b6645a72SIan Rogers config = strdup(str); 1536b6645a72SIan Rogers if (!config) 15372073ad33SAndi Kleen return -1; 1538b6645a72SIan Rogers if (parse_events_term__num(&term, 1539b6645a72SIan Rogers PARSE_EVENTS__TERM_TYPE_USER, 1540b6645a72SIan Rogers config, 1, false, &config, 1541b6645a72SIan Rogers NULL) < 0) { 1542b6645a72SIan Rogers free(list); 1543b6645a72SIan Rogers free(config); 1544b6645a72SIan Rogers return -1; 1545b6645a72SIan Rogers } 15462073ad33SAndi Kleen list_add_tail(&term->list, head); 15472073ad33SAndi Kleen 1548c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 15493cdc5c2cSKan Liang pmu->name, head, 15503cdc5c2cSKan Liang true, true)) { 1551d4953f7eSIan Rogers pr_debug("%s -> %s/%s/\n", str, 15522073ad33SAndi Kleen pmu->name, alias->str); 15532073ad33SAndi Kleen ok++; 15542073ad33SAndi Kleen } 15552073ad33SAndi Kleen 15562073ad33SAndi Kleen parse_events_terms__delete(head); 15572073ad33SAndi Kleen } 15582073ad33SAndi Kleen } 15592073ad33SAndi Kleen } 1560b6645a72SIan Rogers if (!ok) { 1561b6645a72SIan Rogers free(list); 15622073ad33SAndi Kleen return -1; 1563b6645a72SIan Rogers } 15642073ad33SAndi Kleen *listp = list; 15652073ad33SAndi Kleen return 0; 15662073ad33SAndi Kleen } 15672073ad33SAndi Kleen 15686a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 15696a4bb04cSJiri Olsa char *event_mod) 157089efb029SJiri Olsa { 15716a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 15726a4bb04cSJiri Olsa } 15736a4bb04cSJiri Olsa 15743cdc5c2cSKan Liang /* 15753cdc5c2cSKan Liang * Check if the two uncore PMUs are from the same uncore block 15763cdc5c2cSKan Liang * The format of the uncore PMU name is uncore_#blockname_#pmuidx 15773cdc5c2cSKan Liang */ 15783cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b) 15793cdc5c2cSKan Liang { 15803cdc5c2cSKan Liang char *end_a, *end_b; 15813cdc5c2cSKan Liang 15823cdc5c2cSKan Liang end_a = strrchr(pmu_name_a, '_'); 15833cdc5c2cSKan Liang end_b = strrchr(pmu_name_b, '_'); 15843cdc5c2cSKan Liang 15853cdc5c2cSKan Liang if (!end_a || !end_b) 15863cdc5c2cSKan Liang return false; 15873cdc5c2cSKan Liang 15883cdc5c2cSKan Liang if ((end_a - pmu_name_a) != (end_b - pmu_name_b)) 15893cdc5c2cSKan Liang return false; 15903cdc5c2cSKan Liang 15913cdc5c2cSKan Liang return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0); 15923cdc5c2cSKan Liang } 15933cdc5c2cSKan Liang 15943cdc5c2cSKan Liang static int 15953cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list, 15963cdc5c2cSKan Liang struct parse_events_state *parse_state) 15973cdc5c2cSKan Liang { 159832dcd021SJiri Olsa struct evsel *evsel, *leader; 15993cdc5c2cSKan Liang uintptr_t *leaders; 16003cdc5c2cSKan Liang bool is_leader = true; 16013cdc5c2cSKan Liang int i, nr_pmu = 0, total_members, ret = 0; 16023cdc5c2cSKan Liang 1603b27c4eceSJiri Olsa leader = list_first_entry(list, struct evsel, core.node); 1604b27c4eceSJiri Olsa evsel = list_last_entry(list, struct evsel, core.node); 16053cdc5c2cSKan Liang total_members = evsel->idx - leader->idx + 1; 16063cdc5c2cSKan Liang 16073cdc5c2cSKan Liang leaders = calloc(total_members, sizeof(uintptr_t)); 16083cdc5c2cSKan Liang if (WARN_ON(!leaders)) 16093cdc5c2cSKan Liang return 0; 16103cdc5c2cSKan Liang 16113cdc5c2cSKan Liang /* 16123cdc5c2cSKan Liang * Going through the whole group and doing sanity check. 16133cdc5c2cSKan Liang * All members must use alias, and be from the same uncore block. 16143cdc5c2cSKan Liang * Also, storing the leader events in an array. 16153cdc5c2cSKan Liang */ 16163cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 16173cdc5c2cSKan Liang 16183cdc5c2cSKan Liang /* Only split the uncore group which members use alias */ 16193cdc5c2cSKan Liang if (!evsel->use_uncore_alias) 16203cdc5c2cSKan Liang goto out; 16213cdc5c2cSKan Liang 16223cdc5c2cSKan Liang /* The events must be from the same uncore block */ 16233cdc5c2cSKan Liang if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name)) 16243cdc5c2cSKan Liang goto out; 16253cdc5c2cSKan Liang 16263cdc5c2cSKan Liang if (!is_leader) 16273cdc5c2cSKan Liang continue; 16283cdc5c2cSKan Liang /* 16293cdc5c2cSKan Liang * If the event's PMU name starts to repeat, it must be a new 16303cdc5c2cSKan Liang * event. That can be used to distinguish the leader from 16313cdc5c2cSKan Liang * other members, even they have the same event name. 16323cdc5c2cSKan Liang */ 16333cdc5c2cSKan Liang if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) { 16343cdc5c2cSKan Liang is_leader = false; 16353cdc5c2cSKan Liang continue; 16363cdc5c2cSKan Liang } 16373cdc5c2cSKan Liang /* The name is always alias name */ 16383cdc5c2cSKan Liang WARN_ON(strcmp(leader->name, evsel->name)); 16393cdc5c2cSKan Liang 16403cdc5c2cSKan Liang /* Store the leader event for each PMU */ 16413cdc5c2cSKan Liang leaders[nr_pmu++] = (uintptr_t) evsel; 16423cdc5c2cSKan Liang } 16433cdc5c2cSKan Liang 16443cdc5c2cSKan Liang /* only one event alias */ 16453cdc5c2cSKan Liang if (nr_pmu == total_members) { 16463cdc5c2cSKan Liang parse_state->nr_groups--; 16473cdc5c2cSKan Liang goto handled; 16483cdc5c2cSKan Liang } 16493cdc5c2cSKan Liang 16503cdc5c2cSKan Liang /* 16513cdc5c2cSKan Liang * An uncore event alias is a joint name which means the same event 16523cdc5c2cSKan Liang * runs on all PMUs of a block. 16533cdc5c2cSKan Liang * Perf doesn't support mixed events from different PMUs in the same 16543cdc5c2cSKan Liang * group. The big group has to be split into multiple small groups 16553cdc5c2cSKan Liang * which only include the events from the same PMU. 16563cdc5c2cSKan Liang * 16573cdc5c2cSKan Liang * Here the uncore event aliases must be from the same uncore block. 16583cdc5c2cSKan Liang * The number of PMUs must be same for each alias. The number of new 16593cdc5c2cSKan Liang * small groups equals to the number of PMUs. 16603cdc5c2cSKan Liang * Setting the leader event for corresponding members in each group. 16613cdc5c2cSKan Liang */ 16623cdc5c2cSKan Liang i = 0; 16633cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 16643cdc5c2cSKan Liang if (i >= nr_pmu) 16653cdc5c2cSKan Liang i = 0; 166632dcd021SJiri Olsa evsel->leader = (struct evsel *) leaders[i++]; 16673cdc5c2cSKan Liang } 16683cdc5c2cSKan Liang 16693cdc5c2cSKan Liang /* The number of members and group name are same for each group */ 16703cdc5c2cSKan Liang for (i = 0; i < nr_pmu; i++) { 167132dcd021SJiri Olsa evsel = (struct evsel *) leaders[i]; 16725643b1a5SJiri Olsa evsel->core.nr_members = total_members / nr_pmu; 16733cdc5c2cSKan Liang evsel->group_name = name ? strdup(name) : NULL; 16743cdc5c2cSKan Liang } 16753cdc5c2cSKan Liang 16763cdc5c2cSKan Liang /* Take the new small groups into account */ 16773cdc5c2cSKan Liang parse_state->nr_groups += nr_pmu - 1; 16783cdc5c2cSKan Liang 16793cdc5c2cSKan Liang handled: 16803cdc5c2cSKan Liang ret = 1; 16813cdc5c2cSKan Liang out: 16823cdc5c2cSKan Liang free(leaders); 16833cdc5c2cSKan Liang return ret; 16843cdc5c2cSKan Liang } 16853cdc5c2cSKan Liang 16863cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list, 16873cdc5c2cSKan Liang struct parse_events_state *parse_state) 16886a4bb04cSJiri Olsa { 168932dcd021SJiri Olsa struct evsel *leader; 16906a4bb04cSJiri Olsa 1691854f7363SWang Nan if (list_empty(list)) { 1692854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1693854f7363SWang Nan return; 1694854f7363SWang Nan } 1695854f7363SWang Nan 16963cdc5c2cSKan Liang if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state)) 16973cdc5c2cSKan Liang return; 16983cdc5c2cSKan Liang 169963dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 1700b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 17016a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 170289efb029SJiri Olsa } 170389efb029SJiri Olsa 1704c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 17055d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 17065d7be90eSJiri Olsa struct list_head *list_all) 17075d7be90eSJiri Olsa { 17085d7be90eSJiri Olsa /* 17095d7be90eSJiri Olsa * Called for single event definition. Update the 171089efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 17115d7be90eSJiri Olsa * list, for next event definition. 17125d7be90eSJiri Olsa */ 17135d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1714b847cbdcSJiri Olsa free(list_event); 17155d7be90eSJiri Olsa } 17165d7be90eSJiri Olsa 1717f5b1135bSJiri Olsa struct event_modifier { 1718f5b1135bSJiri Olsa int eu; 1719f5b1135bSJiri Olsa int ek; 1720f5b1135bSJiri Olsa int eh; 1721f5b1135bSJiri Olsa int eH; 1722f5b1135bSJiri Olsa int eG; 1723a1e12da4SJiri Olsa int eI; 1724f5b1135bSJiri Olsa int precise; 17257f94af7aSJiri Olsa int precise_max; 1726f5b1135bSJiri Olsa int exclude_GH; 17273c176311SJiri Olsa int sample_read; 1728e9a7c414SMichael Ellerman int pinned; 17295a5dfe4bSAndi Kleen int weak; 1730f5b1135bSJiri Olsa }; 173161c45981SPaul Mackerras 1732f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 173332dcd021SJiri Olsa struct evsel *evsel) 1734f5b1135bSJiri Olsa { 17351fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 17361fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 17371fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 17381fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 17391fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 17401fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 17411fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 17427f94af7aSJiri Olsa int precise_max = 0; 17433c176311SJiri Olsa int sample_read = 0; 17441fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 1745f5b1135bSJiri Olsa 1746f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1747f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 17485a5dfe4bSAndi Kleen int weak = 0; 1749f5b1135bSJiri Olsa 1750f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1751ceb53fbfSIngo Molnar 175261c45981SPaul Mackerras while (*str) { 1753ab608344SPeter Zijlstra if (*str == 'u') { 1754ab608344SPeter Zijlstra if (!exclude) 1755ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 175661c45981SPaul Mackerras eu = 0; 1757ab608344SPeter Zijlstra } else if (*str == 'k') { 1758ab608344SPeter Zijlstra if (!exclude) 1759ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 176061c45981SPaul Mackerras ek = 0; 1761ab608344SPeter Zijlstra } else if (*str == 'h') { 1762ab608344SPeter Zijlstra if (!exclude) 1763ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 176461c45981SPaul Mackerras eh = 0; 176599320cc8SJoerg Roedel } else if (*str == 'G') { 176699320cc8SJoerg Roedel if (!exclude_GH) 176799320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 176899320cc8SJoerg Roedel eG = 0; 176999320cc8SJoerg Roedel } else if (*str == 'H') { 177099320cc8SJoerg Roedel if (!exclude_GH) 177199320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 177299320cc8SJoerg Roedel eH = 0; 1773a1e12da4SJiri Olsa } else if (*str == 'I') { 1774a1e12da4SJiri Olsa eI = 1; 1775ab608344SPeter Zijlstra } else if (*str == 'p') { 1776ab608344SPeter Zijlstra precise++; 17771342798cSDavid Ahern /* use of precise requires exclude_guest */ 17781342798cSDavid Ahern if (!exclude_GH) 17791342798cSDavid Ahern eG = 1; 17807f94af7aSJiri Olsa } else if (*str == 'P') { 17817f94af7aSJiri Olsa precise_max = 1; 17823c176311SJiri Olsa } else if (*str == 'S') { 17833c176311SJiri Olsa sample_read = 1; 1784e9a7c414SMichael Ellerman } else if (*str == 'D') { 1785e9a7c414SMichael Ellerman pinned = 1; 17865a5dfe4bSAndi Kleen } else if (*str == 'W') { 17875a5dfe4bSAndi Kleen weak = 1; 1788ab608344SPeter Zijlstra } else 178961c45981SPaul Mackerras break; 1790ab608344SPeter Zijlstra 179161c45981SPaul Mackerras ++str; 179261c45981SPaul Mackerras } 179374d5b588SJaswinder Singh Rajput 179486470930SIngo Molnar /* 179589812fc8SJiri Olsa * precise ip: 179689812fc8SJiri Olsa * 179789812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 179889812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 179989812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 180089812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 180189812fc8SJiri Olsa * 180289812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 180386470930SIngo Molnar */ 180489812fc8SJiri Olsa if (precise > 3) 180589812fc8SJiri Olsa return -EINVAL; 180686470930SIngo Molnar 1807f5b1135bSJiri Olsa mod->eu = eu; 1808f5b1135bSJiri Olsa mod->ek = ek; 1809f5b1135bSJiri Olsa mod->eh = eh; 1810f5b1135bSJiri Olsa mod->eH = eH; 1811f5b1135bSJiri Olsa mod->eG = eG; 1812a1e12da4SJiri Olsa mod->eI = eI; 1813f5b1135bSJiri Olsa mod->precise = precise; 18147f94af7aSJiri Olsa mod->precise_max = precise_max; 1815f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 18163c176311SJiri Olsa mod->sample_read = sample_read; 1817e9a7c414SMichael Ellerman mod->pinned = pinned; 18185a5dfe4bSAndi Kleen mod->weak = weak; 1819e9a7c414SMichael Ellerman 1820f5b1135bSJiri Olsa return 0; 1821f5b1135bSJiri Olsa } 1822f5b1135bSJiri Olsa 1823534123f4SJiri Olsa /* 1824534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1825534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1826534123f4SJiri Olsa */ 1827534123f4SJiri Olsa static int check_modifier(char *str) 1828534123f4SJiri Olsa { 1829534123f4SJiri Olsa char *p = str; 1830534123f4SJiri Olsa 1831534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 18325a5dfe4bSAndi Kleen if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1)) 1833534123f4SJiri Olsa return -1; 1834534123f4SJiri Olsa 1835534123f4SJiri Olsa while (*p) { 1836534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1837534123f4SJiri Olsa return -1; 1838534123f4SJiri Olsa p++; 1839534123f4SJiri Olsa } 1840534123f4SJiri Olsa 1841534123f4SJiri Olsa return 0; 1842534123f4SJiri Olsa } 1843534123f4SJiri Olsa 1844f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1845f5b1135bSJiri Olsa { 184632dcd021SJiri Olsa struct evsel *evsel; 1847f5b1135bSJiri Olsa struct event_modifier mod; 1848f5b1135bSJiri Olsa 1849f5b1135bSJiri Olsa if (str == NULL) 1850f5b1135bSJiri Olsa return 0; 1851f5b1135bSJiri Olsa 1852534123f4SJiri Olsa if (check_modifier(str)) 1853534123f4SJiri Olsa return -EINVAL; 1854534123f4SJiri Olsa 1855f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1856f5b1135bSJiri Olsa return -EINVAL; 1857f5b1135bSJiri Olsa 1858e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1859f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1860f5b1135bSJiri Olsa return -EINVAL; 1861f5b1135bSJiri Olsa 18621fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 18631fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 18641fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 18651fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 18661fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 18671fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 18681fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 1869f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 18703c176311SJiri Olsa evsel->sample_read = mod.sample_read; 18717f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 18725a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 1873e9a7c414SMichael Ellerman 1874e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 18751fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 1876ceb53fbfSIngo Molnar } 187786470930SIngo Molnar 187889812fc8SJiri Olsa return 0; 187986470930SIngo Molnar } 188086470930SIngo Molnar 1881ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1882ac2ba9f3SRobert Richter { 188332dcd021SJiri Olsa struct evsel *evsel; 1884ac2ba9f3SRobert Richter 1885e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1886ac2ba9f3SRobert Richter if (!evsel->name) 1887ac2ba9f3SRobert Richter evsel->name = strdup(name); 1888ac2ba9f3SRobert Richter } 1889ac2ba9f3SRobert Richter 1890ac2ba9f3SRobert Richter return 0; 1891ac2ba9f3SRobert Richter } 1892ac2ba9f3SRobert Richter 1893dcb4e102SKan Liang static int 1894dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1895dcb4e102SKan Liang { 1896dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1897dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1898dcb4e102SKan Liang 1899e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1900dcb4e102SKan Liang } 1901dcb4e102SKan Liang 1902dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1903dcb4e102SKan Liang { 1904dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1905dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1906dcb4e102SKan Liang int i; 1907dcb4e102SKan Liang 1908dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1909dcb4e102SKan Liang p = perf_pmu_events_list + i; 1910360e071bSTaeung Song zfree(&p->symbol); 1911dcb4e102SKan Liang } 1912506fde11STaeung Song zfree(&perf_pmu_events_list); 1913dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1914dcb4e102SKan Liang } 1915dcb4e102SKan Liang } 1916dcb4e102SKan Liang 1917dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1918dcb4e102SKan Liang do { \ 1919dcb4e102SKan Liang p->symbol = str; \ 1920dcb4e102SKan Liang if (!p->symbol) \ 1921dcb4e102SKan Liang goto err; \ 1922dcb4e102SKan Liang p->type = stype; \ 1923dcb4e102SKan Liang } while (0) 1924dcb4e102SKan Liang 1925dcb4e102SKan Liang /* 1926dcb4e102SKan Liang * Read the pmu events list from sysfs 1927dcb4e102SKan Liang * Save it into perf_pmu_events_list 1928dcb4e102SKan Liang */ 1929dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1930dcb4e102SKan Liang { 1931dcb4e102SKan Liang 1932dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1933dcb4e102SKan Liang struct perf_pmu_alias *alias; 1934dcb4e102SKan Liang int len = 0; 1935dcb4e102SKan Liang 1936231bb2aaSAndi Kleen pmu = NULL; 1937231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1938dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1939dcb4e102SKan Liang if (strchr(alias->name, '-')) 1940dcb4e102SKan Liang len++; 1941dcb4e102SKan Liang len++; 1942dcb4e102SKan Liang } 1943231bb2aaSAndi Kleen } 1944231bb2aaSAndi Kleen 1945231bb2aaSAndi Kleen if (len == 0) { 1946231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1947231bb2aaSAndi Kleen return; 1948231bb2aaSAndi Kleen } 1949dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1950dcb4e102SKan Liang if (!perf_pmu_events_list) 1951dcb4e102SKan Liang return; 1952dcb4e102SKan Liang perf_pmu_events_list_num = len; 1953dcb4e102SKan Liang 1954dcb4e102SKan Liang len = 0; 1955231bb2aaSAndi Kleen pmu = NULL; 1956231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1957dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1958dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1959dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1960dcb4e102SKan Liang 1961dcb4e102SKan Liang if (tmp != NULL) { 1962dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1963dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1964dcb4e102SKan Liang p++; 1965dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1966dcb4e102SKan Liang len += 2; 1967dcb4e102SKan Liang } else { 1968dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1969dcb4e102SKan Liang len++; 1970dcb4e102SKan Liang } 1971dcb4e102SKan Liang } 1972231bb2aaSAndi Kleen } 1973dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1974dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1975dcb4e102SKan Liang 1976dcb4e102SKan Liang return; 1977dcb4e102SKan Liang err: 1978dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1979dcb4e102SKan Liang } 1980dcb4e102SKan Liang 1981dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1982dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1983dcb4e102SKan Liang { 1984dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1985dcb4e102SKan Liang 1986dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1987dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1988dcb4e102SKan Liang perf_pmu__parse_init(); 1989dcb4e102SKan Liang /* 1990dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1991dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1992dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1993dcb4e102SKan Liang */ 1994dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1995dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1996dcb4e102SKan Liang 1997dcb4e102SKan Liang p.symbol = strdup(name); 1998dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1999dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 2000dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 2001360e071bSTaeung Song zfree(&p.symbol); 2002dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 2003dcb4e102SKan Liang } 2004dcb4e102SKan Liang 20055d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token) 2006ac20de6fSZheng Yan { 2007ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 2008ac20de6fSZheng Yan void *scanner; 2009ac20de6fSZheng Yan int ret; 2010ac20de6fSZheng Yan 201190e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 2012ac20de6fSZheng Yan if (ret) 2013ac20de6fSZheng Yan return ret; 2014ac20de6fSZheng Yan 2015ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 2016ac20de6fSZheng Yan 2017ac20de6fSZheng Yan #ifdef PARSER_DEBUG 2018ac20de6fSZheng Yan parse_events_debug = 1; 2019ac20de6fSZheng Yan #endif 20205d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 2021ac20de6fSZheng Yan 2022ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 2023ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 2024ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 2025ac20de6fSZheng Yan return ret; 2026ac20de6fSZheng Yan } 2027ac20de6fSZheng Yan 202890e2b22dSJiri Olsa /* 202990e2b22dSJiri Olsa * parse event config string, return a list of event terms. 203090e2b22dSJiri Olsa */ 203190e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 203290e2b22dSJiri Olsa { 20335d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 203490e2b22dSJiri Olsa .terms = NULL, 203590e2b22dSJiri Olsa }; 203690e2b22dSJiri Olsa int ret; 203790e2b22dSJiri Olsa 20385d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_TERMS); 203990e2b22dSJiri Olsa if (!ret) { 20405d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 20415d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 204290e2b22dSJiri Olsa return 0; 204390e2b22dSJiri Olsa } 204490e2b22dSJiri Olsa 20455d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 204690e2b22dSJiri Olsa return ret; 204790e2b22dSJiri Olsa } 204890e2b22dSJiri Olsa 204963503dbaSJiri Olsa int parse_events(struct evlist *evlist, const char *str, 2050b39b8393SJiri Olsa struct parse_events_error *err) 205186470930SIngo Molnar { 20525d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 20535d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 20546484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 2055b39b8393SJiri Olsa .error = err, 20567630b3e2SWang Nan .evlist = evlist, 205746010ab2SJiri Olsa }; 205846010ab2SJiri Olsa int ret; 205986470930SIngo Molnar 20605d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS); 2061dcb4e102SKan Liang perf_pmu__parse_cleanup(); 206215bfd2ccSWang Nan 20638e8714c3SIan Rogers if (!ret && list_empty(&parse_state.list)) { 2064c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2065854f7363SWang Nan return -1; 2066854f7363SWang Nan } 2067854f7363SWang Nan 20688e8714c3SIan Rogers /* 20698e8714c3SIan Rogers * Add list to the evlist even with errors to allow callers to clean up. 20708e8714c3SIan Rogers */ 20715d9cdc11SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &parse_state.list); 20728e8714c3SIan Rogers 20738e8714c3SIan Rogers if (!ret) { 20748e8714c3SIan Rogers struct evsel *last; 20758e8714c3SIan Rogers 20765d9cdc11SArnaldo Carvalho de Melo evlist->nr_groups += parse_state.nr_groups; 2077515dbe48SJiri Olsa last = evlist__last(evlist); 207815bfd2ccSWang Nan last->cmdline_group_boundary = true; 207915bfd2ccSWang Nan 208086470930SIngo Molnar return 0; 208186470930SIngo Molnar } 208286470930SIngo Molnar 20835d7be90eSJiri Olsa /* 20845d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 2085c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 20865d7be90eSJiri Olsa * need to bother. 20875d7be90eSJiri Olsa */ 208889812fc8SJiri Olsa return ret; 208989812fc8SJiri Olsa } 209089812fc8SJiri Olsa 2091b39b8393SJiri Olsa #define MAX_WIDTH 1000 2092b39b8393SJiri Olsa static int get_term_width(void) 2093b39b8393SJiri Olsa { 2094b39b8393SJiri Olsa struct winsize ws; 2095b39b8393SJiri Olsa 2096b39b8393SJiri Olsa get_term_dimensions(&ws); 2097b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2098b39b8393SJiri Olsa } 2099b39b8393SJiri Olsa 2100a910e466SIan Rogers static void __parse_events_print_error(int err_idx, const char *err_str, 2101a910e466SIan Rogers const char *err_help, const char *event) 2102b39b8393SJiri Olsa { 2103b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 2104b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 2105b39b8393SJiri Olsa char *buf = (char *) event; 2106b39b8393SJiri Olsa int idx = 0; 2107a910e466SIan Rogers if (err_str) { 2108b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 2109b39b8393SJiri Olsa int width = get_term_width() - 2; 2110b39b8393SJiri Olsa int len_event = strlen(event); 2111b39b8393SJiri Olsa int len_str, max_len, cut = 0; 2112b39b8393SJiri Olsa 2113b39b8393SJiri Olsa /* 2114b39b8393SJiri Olsa * Maximum error index indent, we will cut 2115b39b8393SJiri Olsa * the event string if it's bigger. 2116b39b8393SJiri Olsa */ 2117141b2d31SAdrian Hunter int max_err_idx = 13; 2118b39b8393SJiri Olsa 2119b39b8393SJiri Olsa /* 2120b39b8393SJiri Olsa * Let's be specific with the message when 2121b39b8393SJiri Olsa * we have the precise error. 2122b39b8393SJiri Olsa */ 2123b39b8393SJiri Olsa str = "event syntax error: "; 2124b39b8393SJiri Olsa len_str = strlen(str); 2125b39b8393SJiri Olsa max_len = width - len_str; 2126b39b8393SJiri Olsa 2127b39b8393SJiri Olsa buf = _buf; 2128b39b8393SJiri Olsa 2129bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 2130a910e466SIan Rogers if (err_idx > max_err_idx) 2131a910e466SIan Rogers cut = err_idx - max_err_idx; 2132b39b8393SJiri Olsa 2133b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 2134b39b8393SJiri Olsa 2135b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 2136b39b8393SJiri Olsa if (cut) 2137b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2138b39b8393SJiri Olsa 2139b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2140b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2141b39b8393SJiri Olsa buf[max_len] = 0; 2142b39b8393SJiri Olsa } 2143b39b8393SJiri Olsa 2144a910e466SIan Rogers idx = len_str + err_idx - cut; 2145b39b8393SJiri Olsa } 2146b39b8393SJiri Olsa 2147b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2148b39b8393SJiri Olsa if (idx) { 2149a910e466SIan Rogers fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2150a910e466SIan Rogers if (err_help) 2151a910e466SIan Rogers fprintf(stderr, "\n%s\n", err_help); 2152a910e466SIan Rogers } 2153a910e466SIan Rogers } 2154a910e466SIan Rogers 2155a910e466SIan Rogers void parse_events_print_error(struct parse_events_error *err, 2156a910e466SIan Rogers const char *event) 2157a910e466SIan Rogers { 2158a910e466SIan Rogers if (!err->num_errors) 2159a910e466SIan Rogers return; 2160a910e466SIan Rogers 2161a910e466SIan Rogers __parse_events_print_error(err->idx, err->str, err->help, event); 2162360e071bSTaeung Song zfree(&err->str); 2163360e071bSTaeung Song zfree(&err->help); 2164a910e466SIan Rogers 2165a910e466SIan Rogers if (err->num_errors > 1) { 2166a910e466SIan Rogers fputs("\nInitial error:\n", stderr); 2167a910e466SIan Rogers __parse_events_print_error(err->first_idx, err->first_str, 2168a910e466SIan Rogers err->first_help, event); 2169a910e466SIan Rogers zfree(&err->first_str); 2170a910e466SIan Rogers zfree(&err->first_help); 2171b39b8393SJiri Olsa } 2172b39b8393SJiri Olsa } 2173b39b8393SJiri Olsa 2174b39b8393SJiri Olsa #undef MAX_WIDTH 2175b39b8393SJiri Olsa 2176f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 21771d037ca1SIrina Tirdea int unset __maybe_unused) 2178f120f9d5SJiri Olsa { 217963503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2180a910e466SIan Rogers struct parse_events_error err; 2181a910e466SIan Rogers int ret; 2182a910e466SIan Rogers 2183a910e466SIan Rogers bzero(&err, sizeof(err)); 2184a910e466SIan Rogers ret = parse_events(evlist, str, &err); 21859175ce1fSAndi Kleen 2186333b5665SAndi Kleen if (ret) { 2187b39b8393SJiri Olsa parse_events_print_error(&err, str); 2188333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2189333b5665SAndi Kleen } 2190b39b8393SJiri Olsa 21919175ce1fSAndi Kleen return ret; 2192f120f9d5SJiri Olsa } 2193f120f9d5SJiri Olsa 2194d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2195d0abbc3cSArnaldo Carvalho de Melo { 2196d0abbc3cSArnaldo Carvalho de Melo struct evlist **evlistp = opt->value; 2197d0abbc3cSArnaldo Carvalho de Melo int ret; 2198d0abbc3cSArnaldo Carvalho de Melo 2199d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2200d0abbc3cSArnaldo Carvalho de Melo *evlistp = evlist__new(); 2201d0abbc3cSArnaldo Carvalho de Melo 2202d0abbc3cSArnaldo Carvalho de Melo if (*evlistp == NULL) { 2203d0abbc3cSArnaldo Carvalho de Melo fprintf(stderr, "Not enough memory to create evlist\n"); 2204d0abbc3cSArnaldo Carvalho de Melo return -1; 2205d0abbc3cSArnaldo Carvalho de Melo } 2206d0abbc3cSArnaldo Carvalho de Melo } 2207d0abbc3cSArnaldo Carvalho de Melo 2208d0abbc3cSArnaldo Carvalho de Melo ret = parse_events_option(opt, str, unset); 2209d0abbc3cSArnaldo Carvalho de Melo if (ret) { 2210d0abbc3cSArnaldo Carvalho de Melo evlist__delete(*evlistp); 2211d0abbc3cSArnaldo Carvalho de Melo *evlistp = NULL; 2212d0abbc3cSArnaldo Carvalho de Melo } 2213d0abbc3cSArnaldo Carvalho de Melo 2214d0abbc3cSArnaldo Carvalho de Melo return ret; 2215d0abbc3cSArnaldo Carvalho de Melo } 2216d0abbc3cSArnaldo Carvalho de Melo 22174ba1faa1SWang Nan static int 221863503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 221932dcd021SJiri Olsa int (*func)(struct evsel *evsel, 22204ba1faa1SWang Nan const void *arg), 22214ba1faa1SWang Nan const void *arg) 2222c171b552SLi Zefan { 222332dcd021SJiri Olsa struct evsel *last = NULL; 22244ba1faa1SWang Nan int err; 2225c171b552SLi Zefan 2226854f7363SWang Nan /* 2227854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2228854f7363SWang Nan * error when it found last == NULL. 2229854f7363SWang Nan * 2230854f7363SWang Nan * So no need to WARN here, let *func do this. 2231854f7363SWang Nan */ 22326484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 2233515dbe48SJiri Olsa last = evlist__last(evlist); 223469aad6f1SArnaldo Carvalho de Melo 223515bfd2ccSWang Nan do { 22364ba1faa1SWang Nan err = (*func)(last, arg); 22374ba1faa1SWang Nan if (err) 2238c171b552SLi Zefan return -1; 22394ba1faa1SWang Nan if (!last) 22404ba1faa1SWang Nan return 0; 2241c171b552SLi Zefan 2242ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 224315bfd2ccSWang Nan return 0; 2244b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 224515bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 224615bfd2ccSWang Nan 2247c171b552SLi Zefan return 0; 2248c171b552SLi Zefan } 2249c171b552SLi Zefan 225032dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 22514ba1faa1SWang Nan { 22524ba1faa1SWang Nan const char *str = arg; 22531e857484SMathieu Poirier bool found = false; 22541e857484SMathieu Poirier int nr_addr_filters = 0; 22551e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 22564ba1faa1SWang Nan 225749836f78SJack Henschel if (evsel == NULL) { 225849836f78SJack Henschel fprintf(stderr, 225949836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 226049836f78SJack Henschel return -1; 226149836f78SJack Henschel } 22624ba1faa1SWang Nan 22631fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 22643541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 22654ba1faa1SWang Nan fprintf(stderr, 22664ba1faa1SWang Nan "not enough memory to hold filter string\n"); 22674ba1faa1SWang Nan return -1; 22684ba1faa1SWang Nan } 22694ba1faa1SWang Nan 22704ba1faa1SWang Nan return 0; 22714ba1faa1SWang Nan } 22724ba1faa1SWang Nan 22731e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 22741fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 22751e857484SMathieu Poirier found = true; 22761e857484SMathieu Poirier break; 22771e857484SMathieu Poirier } 22781e857484SMathieu Poirier 22791e857484SMathieu Poirier if (found) 22801e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 22811e857484SMathieu Poirier "%d", &nr_addr_filters); 22821e857484SMathieu Poirier 228349836f78SJack Henschel if (!nr_addr_filters) { 228449836f78SJack Henschel fprintf(stderr, 228549836f78SJack Henschel "This CPU does not support address filtering\n"); 228649836f78SJack Henschel return -1; 228749836f78SJack Henschel } 22881e857484SMathieu Poirier 22891e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 22901e857484SMathieu Poirier fprintf(stderr, 22911e857484SMathieu Poirier "not enough memory to hold filter string\n"); 22921e857484SMathieu Poirier return -1; 22931e857484SMathieu Poirier } 22941e857484SMathieu Poirier 22951e857484SMathieu Poirier return 0; 22961e857484SMathieu Poirier } 22971e857484SMathieu Poirier 22984ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 22994ba1faa1SWang Nan int unset __maybe_unused) 23004ba1faa1SWang Nan { 230163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 23024ba1faa1SWang Nan 23034ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 23044ba1faa1SWang Nan (const void *)str); 23054ba1faa1SWang Nan } 23064ba1faa1SWang Nan 230732dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 23084ba1faa1SWang Nan const void *arg __maybe_unused) 23094ba1faa1SWang Nan { 23104ba1faa1SWang Nan char new_filter[64]; 23114ba1faa1SWang Nan 23121fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 23134ba1faa1SWang Nan fprintf(stderr, 23144ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 23154ba1faa1SWang Nan return -1; 23164ba1faa1SWang Nan } 23174ba1faa1SWang Nan 23184ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 23194ba1faa1SWang Nan 23203541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 23214ba1faa1SWang Nan fprintf(stderr, 23224ba1faa1SWang Nan "not enough memory to hold filter string\n"); 23234ba1faa1SWang Nan return -1; 23244ba1faa1SWang Nan } 23254ba1faa1SWang Nan 23264ba1faa1SWang Nan return 0; 23274ba1faa1SWang Nan } 23284ba1faa1SWang Nan 23294ba1faa1SWang Nan int exclude_perf(const struct option *opt, 23304ba1faa1SWang Nan const char *arg __maybe_unused, 23314ba1faa1SWang Nan int unset __maybe_unused) 23324ba1faa1SWang Nan { 233363503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 23344ba1faa1SWang Nan 23354ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 23364ba1faa1SWang Nan NULL); 23374ba1faa1SWang Nan } 23384ba1faa1SWang Nan 233986470930SIngo Molnar static const char * const event_type_descriptors[] = { 234086470930SIngo Molnar "Hardware event", 234186470930SIngo Molnar "Software event", 234286470930SIngo Molnar "Tracepoint event", 234386470930SIngo Molnar "Hardware cache event", 234441bdcb23SLiming Wang "Raw hardware event descriptor", 234541bdcb23SLiming Wang "Hardware breakpoint", 234686470930SIngo Molnar }; 234786470930SIngo Molnar 2348ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 2349ab0e4800SYunlong Song { 2350ab0e4800SYunlong Song const char * const *as = a; 2351ab0e4800SYunlong Song const char * const *bs = b; 2352ab0e4800SYunlong Song 2353ab0e4800SYunlong Song return strcmp(*as, *bs); 2354ab0e4800SYunlong Song } 2355ab0e4800SYunlong Song 235686470930SIngo Molnar /* 2357f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 2358f6bdafefSJason Baron */ 2359f6bdafefSJason Baron 2360a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 2361a3277d2dSFrederic Weisbecker bool name_only) 2362f6bdafefSJason Baron { 2363f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 236422a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2365f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 236625a7d914SArnaldo Carvalho de Melo char *dir_path; 2367ab0e4800SYunlong Song char **evt_list = NULL; 2368ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 2369ab0e4800SYunlong Song bool evt_num_known = false; 2370f6bdafefSJason Baron 2371ab0e4800SYunlong Song restart: 23727014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 2373f6bdafefSJason Baron if (!sys_dir) 2374725b1368SEric Dumazet return; 2375f6bdafefSJason Baron 2376ab0e4800SYunlong Song if (evt_num_known) { 2377ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2378ab0e4800SYunlong Song if (!evt_list) 2379ab0e4800SYunlong Song goto out_close_sys_dir; 2380ab0e4800SYunlong Song } 2381ab0e4800SYunlong Song 238222a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 2383668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 238422a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 2385668b8788SArnaldo Carvalho de Melo continue; 2386725b1368SEric Dumazet 238725a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 238825a7d914SArnaldo Carvalho de Melo if (!dir_path) 238925a7d914SArnaldo Carvalho de Melo continue; 2390725b1368SEric Dumazet evt_dir = opendir(dir_path); 2391725b1368SEric Dumazet if (!evt_dir) 239225a7d914SArnaldo Carvalho de Melo goto next; 2393725b1368SEric Dumazet 2394c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 2395668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 239622a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 2397668b8788SArnaldo Carvalho de Melo continue; 2398668b8788SArnaldo Carvalho de Melo 2399ab0e4800SYunlong Song if (!evt_num_known) { 2400ab0e4800SYunlong Song evt_num++; 2401a3277d2dSFrederic Weisbecker continue; 2402a3277d2dSFrederic Weisbecker } 2403a3277d2dSFrederic Weisbecker 2404f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 240522a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 2406ab0e4800SYunlong Song 2407ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 240825a7d914SArnaldo Carvalho de Melo if (evt_list[evt_i] == NULL) { 240925a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2410ab0e4800SYunlong Song goto out_close_evt_dir; 241125a7d914SArnaldo Carvalho de Melo } 2412ab0e4800SYunlong Song evt_i++; 2413f6bdafefSJason Baron } 2414f6bdafefSJason Baron closedir(evt_dir); 241525a7d914SArnaldo Carvalho de Melo next: 241625a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2417f6bdafefSJason Baron } 2418f6bdafefSJason Baron closedir(sys_dir); 2419ab0e4800SYunlong Song 2420ab0e4800SYunlong Song if (!evt_num_known) { 2421ab0e4800SYunlong Song evt_num_known = true; 2422ab0e4800SYunlong Song goto restart; 2423ab0e4800SYunlong Song } 2424ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2425ab0e4800SYunlong Song evt_i = 0; 2426ab0e4800SYunlong Song while (evt_i < evt_num) { 2427ab0e4800SYunlong Song if (name_only) { 2428ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2429ab0e4800SYunlong Song continue; 2430ab0e4800SYunlong Song } 2431ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2432ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2433ab0e4800SYunlong Song } 2434dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2435ab0e4800SYunlong Song printf("\n"); 2436ab0e4800SYunlong Song 2437ab0e4800SYunlong Song out_free: 2438ab0e4800SYunlong Song evt_num = evt_i; 2439ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2440ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2441ab0e4800SYunlong Song zfree(&evt_list); 2442ab0e4800SYunlong Song return; 2443ab0e4800SYunlong Song 2444ab0e4800SYunlong Song out_close_evt_dir: 2445ab0e4800SYunlong Song closedir(evt_dir); 2446ab0e4800SYunlong Song out_close_sys_dir: 2447ab0e4800SYunlong Song closedir(sys_dir); 2448ab0e4800SYunlong Song 2449ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2450ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2451ab0e4800SYunlong Song if (evt_list) 2452ab0e4800SYunlong Song goto out_free; 2453f6bdafefSJason Baron } 2454f6bdafefSJason Baron 2455f6bdafefSJason Baron /* 245620c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 245720c457b8SThomas Renninger */ 245820c457b8SThomas Renninger 245920c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 246020c457b8SThomas Renninger { 246120c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 246222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 246320c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 246425a7d914SArnaldo Carvalho de Melo char *dir_path; 246520c457b8SThomas Renninger 24667014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 246720c457b8SThomas Renninger if (!sys_dir) 246820c457b8SThomas Renninger return 0; 246920c457b8SThomas Renninger 247022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 247125a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 247225a7d914SArnaldo Carvalho de Melo if (!dir_path) 247325a7d914SArnaldo Carvalho de Melo continue; 247420c457b8SThomas Renninger evt_dir = opendir(dir_path); 247520c457b8SThomas Renninger if (!evt_dir) 247625a7d914SArnaldo Carvalho de Melo goto next; 247720c457b8SThomas Renninger 2478c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 247920c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 248022a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 248120c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 248220c457b8SThomas Renninger closedir(evt_dir); 248320c457b8SThomas Renninger closedir(sys_dir); 248420c457b8SThomas Renninger return 1; 248520c457b8SThomas Renninger } 248620c457b8SThomas Renninger } 248720c457b8SThomas Renninger closedir(evt_dir); 248825a7d914SArnaldo Carvalho de Melo next: 248925a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 249020c457b8SThomas Renninger } 249120c457b8SThomas Renninger closedir(sys_dir); 249220c457b8SThomas Renninger return 0; 249320c457b8SThomas Renninger } 249420c457b8SThomas Renninger 2495b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2496b41f1cecSNamhyung Kim { 2497b41f1cecSNamhyung Kim bool ret = true; 249888fee52eSVince Weaver int open_return; 249932dcd021SJiri Olsa struct evsel *evsel; 2500b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2501b41f1cecSNamhyung Kim .type = type, 2502b41f1cecSNamhyung Kim .config = config, 2503b41f1cecSNamhyung Kim .disabled = 1, 2504b41f1cecSNamhyung Kim }; 25059749b90eSJiri Olsa struct perf_thread_map *tmap = thread_map__new_by_tid(0); 250689896051SArnaldo Carvalho de Melo 250789896051SArnaldo Carvalho de Melo if (tmap == NULL) 250889896051SArnaldo Carvalho de Melo return false; 2509b41f1cecSNamhyung Kim 2510365c3ae7SJiri Olsa evsel = evsel__new(&attr); 2511b41f1cecSNamhyung Kim if (evsel) { 25125972d1e0SJiri Olsa open_return = evsel__open(evsel, NULL, tmap); 251388fee52eSVince Weaver ret = open_return >= 0; 251488fee52eSVince Weaver 251588fee52eSVince Weaver if (open_return == -EACCES) { 251688fee52eSVince Weaver /* 251788fee52eSVince Weaver * This happens if the paranoid value 251888fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 251988fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 252088fee52eSVince Weaver * by default as some ARM machines do not support it. 252188fee52eSVince Weaver * 252288fee52eSVince Weaver */ 25231fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = 1; 25245972d1e0SJiri Olsa ret = evsel__open(evsel, NULL, tmap) >= 0; 252588fee52eSVince Weaver } 25265eb2dd2aSJiri Olsa evsel__delete(evsel); 2527b41f1cecSNamhyung Kim } 2528b41f1cecSNamhyung Kim 25297836e52eSJiri Olsa perf_thread_map__put(tmap); 2530b41f1cecSNamhyung Kim return ret; 2531b41f1cecSNamhyung Kim } 2532b41f1cecSNamhyung Kim 253340218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 253440218daeSMasami Hiramatsu bool name_only) 253540218daeSMasami Hiramatsu { 253640218daeSMasami Hiramatsu struct probe_cache *pcache; 253740218daeSMasami Hiramatsu struct probe_cache_entry *ent; 253840218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 253940218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 254040218daeSMasami Hiramatsu struct str_node *nd, *nd2; 254140218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 254240218daeSMasami Hiramatsu bool show_detail = false; 254340218daeSMasami Hiramatsu int ret; 254440218daeSMasami Hiramatsu 254540218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 254640218daeSMasami Hiramatsu if (!sdtlist) { 254740218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 254840218daeSMasami Hiramatsu return; 254940218daeSMasami Hiramatsu } 255040218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 255140218daeSMasami Hiramatsu if (!bidlist) { 255240218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 255340218daeSMasami Hiramatsu return; 255440218daeSMasami Hiramatsu } 255540218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2556f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 255740218daeSMasami Hiramatsu if (!pcache) 255840218daeSMasami Hiramatsu continue; 255940218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 256040218daeSMasami Hiramatsu if (!ent->sdt) 256140218daeSMasami Hiramatsu continue; 256240218daeSMasami Hiramatsu if (subsys_glob && 256340218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 256440218daeSMasami Hiramatsu continue; 256540218daeSMasami Hiramatsu if (event_glob && 256640218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 256740218daeSMasami Hiramatsu continue; 256840218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 256940218daeSMasami Hiramatsu ent->pev.event, nd->s); 257040218daeSMasami Hiramatsu if (ret > 0) 257140218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 257240218daeSMasami Hiramatsu } 257340218daeSMasami Hiramatsu probe_cache__delete(pcache); 257440218daeSMasami Hiramatsu } 257540218daeSMasami Hiramatsu strlist__delete(bidlist); 257640218daeSMasami Hiramatsu 257740218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 257840218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 257940218daeSMasami Hiramatsu if (buf) 258040218daeSMasami Hiramatsu *(buf++) = '\0'; 258140218daeSMasami Hiramatsu if (name_only) { 258240218daeSMasami Hiramatsu printf("%s ", nd->s); 258340218daeSMasami Hiramatsu continue; 258440218daeSMasami Hiramatsu } 258540218daeSMasami Hiramatsu nd2 = strlist__next(nd); 258640218daeSMasami Hiramatsu if (nd2) { 258740218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 258840218daeSMasami Hiramatsu if (ptr) 258940218daeSMasami Hiramatsu *ptr = '\0'; 259040218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 259140218daeSMasami Hiramatsu show_detail = true; 259240218daeSMasami Hiramatsu } 259340218daeSMasami Hiramatsu if (show_detail) { 259440218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 259540218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 259640218daeSMasami Hiramatsu if (ret > 0) { 259740218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 259840218daeSMasami Hiramatsu free(buf); 259940218daeSMasami Hiramatsu } 26008bde8516SChangbin Du free(path); 260140218daeSMasami Hiramatsu } else 260240218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 260340218daeSMasami Hiramatsu if (nd2) { 260440218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 260540218daeSMasami Hiramatsu show_detail = false; 260640218daeSMasami Hiramatsu if (ptr) 260740218daeSMasami Hiramatsu *ptr = '@'; 260840218daeSMasami Hiramatsu } 260940218daeSMasami Hiramatsu } 261040218daeSMasami Hiramatsu strlist__delete(sdtlist); 261140218daeSMasami Hiramatsu } 261240218daeSMasami Hiramatsu 2613a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2614668b8788SArnaldo Carvalho de Melo { 2615ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 26160b668bc9SArnaldo Carvalho de Melo char name[64]; 2617ab0e4800SYunlong Song char **evt_list = NULL; 2618ab0e4800SYunlong Song bool evt_num_known = false; 2619ab0e4800SYunlong Song 2620ab0e4800SYunlong Song restart: 2621ab0e4800SYunlong Song if (evt_num_known) { 2622ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2623ab0e4800SYunlong Song if (!evt_list) 2624ab0e4800SYunlong Song goto out_enomem; 2625ab0e4800SYunlong Song } 2626668b8788SArnaldo Carvalho de Melo 2627668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2628668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2629668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 26300b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2631668b8788SArnaldo Carvalho de Melo continue; 2632668b8788SArnaldo Carvalho de Melo 2633668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 2634*8ab2e96dSArnaldo Carvalho de Melo __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); 2635947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2636668b8788SArnaldo Carvalho de Melo continue; 2637668b8788SArnaldo Carvalho de Melo 2638b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2639b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2640b41f1cecSNamhyung Kim continue; 2641b41f1cecSNamhyung Kim 2642ab0e4800SYunlong Song if (!evt_num_known) { 2643ab0e4800SYunlong Song evt_num++; 2644ab0e4800SYunlong Song continue; 2645ab0e4800SYunlong Song } 2646ab0e4800SYunlong Song 2647ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2648ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2649ab0e4800SYunlong Song goto out_enomem; 2650ab0e4800SYunlong Song evt_i++; 2651668b8788SArnaldo Carvalho de Melo } 2652668b8788SArnaldo Carvalho de Melo } 2653668b8788SArnaldo Carvalho de Melo } 2654668b8788SArnaldo Carvalho de Melo 2655ab0e4800SYunlong Song if (!evt_num_known) { 2656ab0e4800SYunlong Song evt_num_known = true; 2657ab0e4800SYunlong Song goto restart; 2658ab0e4800SYunlong Song } 2659ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2660ab0e4800SYunlong Song evt_i = 0; 2661ab0e4800SYunlong Song while (evt_i < evt_num) { 2662ab0e4800SYunlong Song if (name_only) { 2663ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2664ab0e4800SYunlong Song continue; 2665ab0e4800SYunlong Song } 2666ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2667ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2668ab0e4800SYunlong Song } 2669dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2670dc098b35SAndi Kleen printf("\n"); 2671ab0e4800SYunlong Song 2672ab0e4800SYunlong Song out_free: 2673ab0e4800SYunlong Song evt_num = evt_i; 2674ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2675ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2676ab0e4800SYunlong Song zfree(&evt_list); 2677ab0e4800SYunlong Song return evt_num; 2678ab0e4800SYunlong Song 2679ab0e4800SYunlong Song out_enomem: 2680ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2681ab0e4800SYunlong Song if (evt_list) 2682ab0e4800SYunlong Song goto out_free; 2683ab0e4800SYunlong Song return evt_num; 2684668b8788SArnaldo Carvalho de Melo } 2685668b8788SArnaldo Carvalho de Melo 26865e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob, 26875e0861baSAndi Kleen bool name_only) 26885e0861baSAndi Kleen { 26895e0861baSAndi Kleen if (event_glob && !strglobmatch(name, event_glob)) 26905e0861baSAndi Kleen return; 26915e0861baSAndi Kleen if (name_only) 26925e0861baSAndi Kleen printf("%s ", name); 26935e0861baSAndi Kleen else 26945e0861baSAndi Kleen printf(" %-50s [%s]\n", name, "Tool event"); 26955e0861baSAndi Kleen 26965e0861baSAndi Kleen } 26975e0861baSAndi Kleen 26985e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only) 26995e0861baSAndi Kleen { 27005e0861baSAndi Kleen print_tool_event("duration_time", event_glob, name_only); 27015e0861baSAndi Kleen if (pager_in_use()) 27025e0861baSAndi Kleen printf("\n"); 27035e0861baSAndi Kleen } 27045e0861baSAndi Kleen 2705705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2706a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2707a3277d2dSFrederic Weisbecker bool name_only) 270886470930SIngo Molnar { 2709ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2710947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2711ab0e4800SYunlong Song char **evt_list = NULL; 2712ab0e4800SYunlong Song bool evt_num_known = false; 2713ab0e4800SYunlong Song 2714ab0e4800SYunlong Song restart: 2715ab0e4800SYunlong Song if (evt_num_known) { 2716ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2717ab0e4800SYunlong Song if (!evt_list) 2718ab0e4800SYunlong Song goto out_enomem; 2719ab0e4800SYunlong Song syms -= max; 2720ab0e4800SYunlong Song } 272186470930SIngo Molnar 27221dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2723668b8788SArnaldo Carvalho de Melo 2724e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2725668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2726668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2727668b8788SArnaldo Carvalho de Melo continue; 272886470930SIngo Molnar 2729b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2730b41f1cecSNamhyung Kim continue; 2731b41f1cecSNamhyung Kim 2732ab0e4800SYunlong Song if (!evt_num_known) { 2733ab0e4800SYunlong Song evt_num++; 2734a3277d2dSFrederic Weisbecker continue; 2735a3277d2dSFrederic Weisbecker } 2736a3277d2dSFrederic Weisbecker 2737ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2738947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 273974d5b588SJaswinder Singh Rajput else 2740bd8d57fbSArnaldo Carvalho de Melo strlcpy(name, syms->symbol, MAX_NAME_LEN); 274186470930SIngo Molnar 2742ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2743ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2744ab0e4800SYunlong Song goto out_enomem; 2745ab0e4800SYunlong Song evt_i++; 274686470930SIngo Molnar } 274786470930SIngo Molnar 2748ab0e4800SYunlong Song if (!evt_num_known) { 2749ab0e4800SYunlong Song evt_num_known = true; 2750ab0e4800SYunlong Song goto restart; 2751ab0e4800SYunlong Song } 2752ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2753ab0e4800SYunlong Song evt_i = 0; 2754ab0e4800SYunlong Song while (evt_i < evt_num) { 2755ab0e4800SYunlong Song if (name_only) { 2756ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2757ab0e4800SYunlong Song continue; 2758ab0e4800SYunlong Song } 2759ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2760ab0e4800SYunlong Song } 2761dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2762689d3018SMarti Raudsepp printf("\n"); 2763ab0e4800SYunlong Song 2764ab0e4800SYunlong Song out_free: 2765ab0e4800SYunlong Song evt_num = evt_i; 2766ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2767ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2768ab0e4800SYunlong Song zfree(&evt_list); 2769ab0e4800SYunlong Song return; 2770ab0e4800SYunlong Song 2771ab0e4800SYunlong Song out_enomem: 2772ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2773ab0e4800SYunlong Song if (evt_list) 2774ab0e4800SYunlong Song goto out_free; 2775668b8788SArnaldo Carvalho de Melo } 27761dc12760SJiri Olsa 27771dc12760SJiri Olsa /* 27781dc12760SJiri Olsa * Print the help text for the event symbols: 27791dc12760SJiri Olsa */ 2780c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2781a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 27821dc12760SJiri Olsa { 27831dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2784a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 27851dc12760SJiri Olsa 27861dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2787a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 27885e0861baSAndi Kleen print_tool_events(event_glob, name_only); 27891dc12760SJiri Olsa 2790a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 279173c24cb8SJaswinder Singh Rajput 2792bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 2793a7f6c8c8SJin Yao details_flag, deprecated); 2794dc098b35SAndi Kleen 2795668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2796668b8788SArnaldo Carvalho de Melo return; 279773c24cb8SJaswinder Singh Rajput 2798a3277d2dSFrederic Weisbecker if (!name_only) { 2799947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 28005f537a26SJiri Olsa "rNNN", 28011cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 28025f537a26SJiri Olsa printf(" %-50s [%s]\n", 28035f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 28045f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2805dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2806dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 280786470930SIngo Molnar 2808947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 28093741eb9fSJacob Shin "mem:<addr>[/len][:access]", 281041bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2811dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 28121b290d67SFrederic Weisbecker printf("\n"); 2813a3277d2dSFrederic Weisbecker } 28141b290d67SFrederic Weisbecker 2815a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 281640218daeSMasami Hiramatsu 281740218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 281871b0acceSAndi Kleen 281933bbc571SJiri Olsa metricgroup__print(true, true, NULL, name_only, details_flag); 282086470930SIngo Molnar } 28218f707d84SJiri Olsa 28226cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 28238f707d84SJiri Olsa { 282416fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 28258f707d84SJiri Olsa } 28268f707d84SJiri Olsa 282767b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 282867b49b38SJiri Olsa struct parse_events_term *temp, 282967b49b38SJiri Olsa char *str, u64 num) 28308f707d84SJiri Olsa { 28316cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 28328f707d84SJiri Olsa 283367b49b38SJiri Olsa term = malloc(sizeof(*term)); 28348f707d84SJiri Olsa if (!term) 28358f707d84SJiri Olsa return -ENOMEM; 28368f707d84SJiri Olsa 283767b49b38SJiri Olsa *term = *temp; 28388f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2839c2f1ceadSAndi Kleen term->weak = false; 28408f707d84SJiri Olsa 284167b49b38SJiri Olsa switch (term->type_val) { 28428f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 28438f707d84SJiri Olsa term->val.num = num; 28448f707d84SJiri Olsa break; 28458f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 28468f707d84SJiri Olsa term->val.str = str; 28478f707d84SJiri Olsa break; 28488f707d84SJiri Olsa default: 28494be8be6bSAdrian Hunter free(term); 28508f707d84SJiri Olsa return -EINVAL; 28518f707d84SJiri Olsa } 28528f707d84SJiri Olsa 28538f707d84SJiri Olsa *_term = term; 28548f707d84SJiri Olsa return 0; 28558f707d84SJiri Olsa } 28568f707d84SJiri Olsa 28576cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2858cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 285999e7138eSJiri Olsa bool no_value, 2860bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 286116fa7e82SJiri Olsa { 2862bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2863bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2864bb78ce7dSAdrian Hunter 286567b49b38SJiri Olsa struct parse_events_term temp = { 286667b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 286767b49b38SJiri Olsa .type_term = type_term, 286867b49b38SJiri Olsa .config = config, 286999e7138eSJiri Olsa .no_value = no_value, 287067b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 287167b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 287267b49b38SJiri Olsa }; 287367b49b38SJiri Olsa 287467b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 287516fa7e82SJiri Olsa } 287616fa7e82SJiri Olsa 28776cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2878cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2879bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 288016fa7e82SJiri Olsa { 2881bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2882bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2883bb78ce7dSAdrian Hunter 288467b49b38SJiri Olsa struct parse_events_term temp = { 288567b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 288667b49b38SJiri Olsa .type_term = type_term, 288767b49b38SJiri Olsa .config = config, 288867b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 288967b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 289067b49b38SJiri Olsa }; 289167b49b38SJiri Olsa 289267b49b38SJiri Olsa return new_term(term, &temp, str, 0); 289316fa7e82SJiri Olsa } 289416fa7e82SJiri Olsa 28956cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 28961d33d6dcSJiri Olsa char *config, unsigned idx) 28971d33d6dcSJiri Olsa { 28981d33d6dcSJiri Olsa struct event_symbol *sym; 2899b6645a72SIan Rogers char *str; 290067b49b38SJiri Olsa struct parse_events_term temp = { 290167b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 290267b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 2903b6645a72SIan Rogers .config = config, 290467b49b38SJiri Olsa }; 29051d33d6dcSJiri Olsa 2906b6645a72SIan Rogers if (!temp.config) { 2907b6645a72SIan Rogers temp.config = strdup("event"); 2908b6645a72SIan Rogers if (!temp.config) 2909b6645a72SIan Rogers return -ENOMEM; 2910b6645a72SIan Rogers } 29111d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 29121d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 29131d33d6dcSJiri Olsa 2914b6645a72SIan Rogers str = strdup(sym->symbol); 2915b6645a72SIan Rogers if (!str) 2916b6645a72SIan Rogers return -ENOMEM; 2917b6645a72SIan Rogers return new_term(term, &temp, str, 0); 29181d33d6dcSJiri Olsa } 29191d33d6dcSJiri Olsa 29206cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 29216cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2922a6146d50SZheng Yan { 2923b6645a72SIan Rogers char *str; 292467b49b38SJiri Olsa struct parse_events_term temp = { 292567b49b38SJiri Olsa .type_val = term->type_val, 292667b49b38SJiri Olsa .type_term = term->type_term, 2927b6645a72SIan Rogers .config = NULL, 292867b49b38SJiri Olsa .err_term = term->err_term, 292967b49b38SJiri Olsa .err_val = term->err_val, 293067b49b38SJiri Olsa }; 293167b49b38SJiri Olsa 2932b6645a72SIan Rogers if (term->config) { 2933b6645a72SIan Rogers temp.config = strdup(term->config); 2934b6645a72SIan Rogers if (!temp.config) 2935b6645a72SIan Rogers return -ENOMEM; 2936b6645a72SIan Rogers } 2937b6645a72SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 2938b6645a72SIan Rogers return new_term(new, &temp, NULL, term->val.num); 2939b6645a72SIan Rogers 2940b6645a72SIan Rogers str = strdup(term->val.str); 2941b6645a72SIan Rogers if (!str) 2942b6645a72SIan Rogers return -ENOMEM; 2943b6645a72SIan Rogers return new_term(new, &temp, str, 0); 2944a6146d50SZheng Yan } 2945a6146d50SZheng Yan 29461dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term) 29471dc92556SIan Rogers { 29481dc92556SIan Rogers if (term->array.nr_ranges) 29491dc92556SIan Rogers zfree(&term->array.ranges); 29501dc92556SIan Rogers 29511dc92556SIan Rogers if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 29521dc92556SIan Rogers zfree(&term->val.str); 29531dc92556SIan Rogers 29541dc92556SIan Rogers zfree(&term->config); 29551dc92556SIan Rogers free(term); 29561dc92556SIan Rogers } 29571dc92556SIan Rogers 29588255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 29598255718fSAndi Kleen struct list_head **new) 29608255718fSAndi Kleen { 29618255718fSAndi Kleen struct parse_events_term *term, *n; 29628255718fSAndi Kleen int ret; 29638255718fSAndi Kleen 29648255718fSAndi Kleen if (!old) { 29658255718fSAndi Kleen *new = NULL; 29668255718fSAndi Kleen return 0; 29678255718fSAndi Kleen } 29688255718fSAndi Kleen 29698255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 29708255718fSAndi Kleen if (!*new) 29718255718fSAndi Kleen return -ENOMEM; 29728255718fSAndi Kleen INIT_LIST_HEAD(*new); 29738255718fSAndi Kleen 29748255718fSAndi Kleen list_for_each_entry (term, old, list) { 29758255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 29768255718fSAndi Kleen if (ret) 29778255718fSAndi Kleen return ret; 29788255718fSAndi Kleen list_add_tail(&n->list, *new); 29798255718fSAndi Kleen } 29808255718fSAndi Kleen return 0; 29818255718fSAndi Kleen } 29828255718fSAndi Kleen 2983fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 29848f707d84SJiri Olsa { 29856cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 29868f707d84SJiri Olsa 2987a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 2988a8adfcebSWang Nan list_del_init(&term->list); 29891dc92556SIan Rogers parse_events_term__delete(term); 29908f707d84SJiri Olsa } 2991a8adfcebSWang Nan } 2992b39b8393SJiri Olsa 29932146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2994fc0a2c1dSArnaldo Carvalho de Melo { 29952146afc6SArnaldo Carvalho de Melo if (!terms) 29962146afc6SArnaldo Carvalho de Melo return; 2997fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2998d20a5f2bSWang Nan free(terms); 2999fc0a2c1dSArnaldo Carvalho de Melo } 3000fc0a2c1dSArnaldo Carvalho de Melo 30012d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 30022d055bf2SWang Nan { 3003360e071bSTaeung Song zfree(&a->ranges); 30042d055bf2SWang Nan } 30052d055bf2SWang Nan 30065d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 3007b39b8393SJiri Olsa int idx, const char *str) 3008b39b8393SJiri Olsa { 3009448d732cSIan Rogers if (!parse_state->error) 3010a6ced2beSAdrian Hunter return; 3011448d732cSIan Rogers 3012448d732cSIan Rogers parse_events__handle_error(parse_state->error, idx, strdup(str), NULL); 3013b39b8393SJiri Olsa } 3014ffeb883eSHe Kuang 301517cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 301617cb5f84SWang Nan { 301717cb5f84SWang Nan int i; 301817cb5f84SWang Nan bool first = true; 301917cb5f84SWang Nan 302017cb5f84SWang Nan buf[0] = '\0'; 302117cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 302217cb5f84SWang Nan const char *name = config_term_names[i]; 302317cb5f84SWang Nan 30241669e509SWang Nan if (!config_term_avail(i, NULL)) 30251669e509SWang Nan continue; 302617cb5f84SWang Nan if (!name) 302717cb5f84SWang Nan continue; 302817cb5f84SWang Nan if (name[0] == '<') 302917cb5f84SWang Nan continue; 303017cb5f84SWang Nan 303117cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 303217cb5f84SWang Nan return; 303317cb5f84SWang Nan 303417cb5f84SWang Nan if (!first) 303517cb5f84SWang Nan strcat(buf, ","); 303617cb5f84SWang Nan else 303717cb5f84SWang Nan first = false; 303817cb5f84SWang Nan strcat(buf, name); 303917cb5f84SWang Nan } 304017cb5f84SWang Nan } 304117cb5f84SWang Nan 3042ffeb883eSHe Kuang /* 3043ffeb883eSHe Kuang * Return string contains valid config terms of an event. 3044ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 3045ffeb883eSHe Kuang */ 3046ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 3047ffeb883eSHe Kuang { 3048ffeb883eSHe Kuang char *str; 3049626a6b78SWang Nan /* "no-overwrite" is the longest name */ 305017cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 3051626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 3052ffeb883eSHe Kuang 305317cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 3054ffeb883eSHe Kuang /* valid terms */ 3055ffeb883eSHe Kuang if (additional_terms) { 305626dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 305726dee028SWang Nan additional_terms, static_terms) < 0) 3058ffeb883eSHe Kuang goto fail; 3059ffeb883eSHe Kuang } else { 306026dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 3061ffeb883eSHe Kuang goto fail; 3062ffeb883eSHe Kuang } 3063ffeb883eSHe Kuang return str; 3064ffeb883eSHe Kuang 3065ffeb883eSHe Kuang fail: 3066ffeb883eSHe Kuang return NULL; 3067ffeb883eSHe Kuang } 3068