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" 37e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 3889812fc8SJiri Olsa 3989812fc8SJiri Olsa #define MAX_NAME_LEN 100 4086470930SIngo Molnar 4182ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 4282ba1f2fSJiri Olsa extern int parse_events_debug; 4382ba1f2fSJiri Olsa #endif 445d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner); 45e637d177SHe Kuang static int get_config_terms(struct list_head *head_config, 46e637d177SHe Kuang struct list_head *head_terms __maybe_unused); 47bcd3279fSFrederic Weisbecker 48dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 49dcb4e102SKan Liang /* 50dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 51dcb4e102SKan Liang * 0 means not initialized and ready to init 52dcb4e102SKan Liang * -1 means failed to init, don't try anymore 53dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 54dcb4e102SKan Liang */ 55dcb4e102SKan Liang static int perf_pmu_events_list_num; 56dcb4e102SKan Liang 57705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 581dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 591dc12760SJiri Olsa .symbol = "cpu-cycles", 601dc12760SJiri Olsa .alias = "cycles", 611dc12760SJiri Olsa }, 621dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 631dc12760SJiri Olsa .symbol = "instructions", 641dc12760SJiri Olsa .alias = "", 651dc12760SJiri Olsa }, 661dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 671dc12760SJiri Olsa .symbol = "cache-references", 681dc12760SJiri Olsa .alias = "", 691dc12760SJiri Olsa }, 701dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 711dc12760SJiri Olsa .symbol = "cache-misses", 721dc12760SJiri Olsa .alias = "", 731dc12760SJiri Olsa }, 741dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 751dc12760SJiri Olsa .symbol = "branch-instructions", 761dc12760SJiri Olsa .alias = "branches", 771dc12760SJiri Olsa }, 781dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 791dc12760SJiri Olsa .symbol = "branch-misses", 801dc12760SJiri Olsa .alias = "", 811dc12760SJiri Olsa }, 821dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 831dc12760SJiri Olsa .symbol = "bus-cycles", 841dc12760SJiri Olsa .alias = "", 851dc12760SJiri Olsa }, 861dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 871dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 881dc12760SJiri Olsa .alias = "idle-cycles-frontend", 891dc12760SJiri Olsa }, 901dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 911dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 921dc12760SJiri Olsa .alias = "idle-cycles-backend", 931dc12760SJiri Olsa }, 941dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 951dc12760SJiri Olsa .symbol = "ref-cycles", 961dc12760SJiri Olsa .alias = "", 971dc12760SJiri Olsa }, 981dc12760SJiri Olsa }; 9986470930SIngo Molnar 100705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 1011dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 1021dc12760SJiri Olsa .symbol = "cpu-clock", 1031dc12760SJiri Olsa .alias = "", 1041dc12760SJiri Olsa }, 1051dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 1061dc12760SJiri Olsa .symbol = "task-clock", 1071dc12760SJiri Olsa .alias = "", 1081dc12760SJiri Olsa }, 1091dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 1101dc12760SJiri Olsa .symbol = "page-faults", 1111dc12760SJiri Olsa .alias = "faults", 1121dc12760SJiri Olsa }, 1131dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 1141dc12760SJiri Olsa .symbol = "context-switches", 1151dc12760SJiri Olsa .alias = "cs", 1161dc12760SJiri Olsa }, 1171dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1181dc12760SJiri Olsa .symbol = "cpu-migrations", 1191dc12760SJiri Olsa .alias = "migrations", 1201dc12760SJiri Olsa }, 1211dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1221dc12760SJiri Olsa .symbol = "minor-faults", 1231dc12760SJiri Olsa .alias = "", 1241dc12760SJiri Olsa }, 1251dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1261dc12760SJiri Olsa .symbol = "major-faults", 1271dc12760SJiri Olsa .alias = "", 1281dc12760SJiri Olsa }, 1291dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1301dc12760SJiri Olsa .symbol = "alignment-faults", 1311dc12760SJiri Olsa .alias = "", 1321dc12760SJiri Olsa }, 1331dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1341dc12760SJiri Olsa .symbol = "emulation-faults", 1351dc12760SJiri Olsa .alias = "", 1361dc12760SJiri Olsa }, 137d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 138d22d1a2aSAdrian Hunter .symbol = "dummy", 139d22d1a2aSAdrian Hunter .alias = "", 140d22d1a2aSAdrian Hunter }, 141bae9cc41SArnaldo Carvalho de Melo [PERF_COUNT_SW_BPF_OUTPUT] = { 142bae9cc41SArnaldo Carvalho de Melo .symbol = "bpf-output", 143bae9cc41SArnaldo Carvalho de Melo .alias = "", 144bae9cc41SArnaldo Carvalho de Melo }, 14586470930SIngo Molnar }; 14686470930SIngo Molnar 147cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 148cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 14986470930SIngo Molnar 150cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 151cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 152cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 153cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 15486470930SIngo Molnar 15522a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent) \ 15622a9f41bSArnaldo Carvalho de Melo while ((sys_dirent = readdir(sys_dir)) != NULL) \ 15722a9f41bSArnaldo Carvalho de Melo if (sys_dirent->d_type == DT_DIR && \ 15822a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".")) && \ 15922a9f41bSArnaldo Carvalho de Melo (strcmp(sys_dirent->d_name, ".."))) 160f6bdafefSJason Baron 161c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) 162ae07b63fSPeter Zijlstra { 163ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 164ae07b63fSPeter Zijlstra int fd; 165ae07b63fSPeter Zijlstra 166c02cab22SArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name); 167ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 168ae07b63fSPeter Zijlstra if (fd < 0) 169ae07b63fSPeter Zijlstra return -EINVAL; 170ae07b63fSPeter Zijlstra close(fd); 171ae07b63fSPeter Zijlstra 172ae07b63fSPeter Zijlstra return 0; 173ae07b63fSPeter Zijlstra } 174ae07b63fSPeter Zijlstra 175c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent) \ 17622a9f41bSArnaldo Carvalho de Melo while ((evt_dirent = readdir(evt_dir)) != NULL) \ 17722a9f41bSArnaldo Carvalho de Melo if (evt_dirent->d_type == DT_DIR && \ 17822a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, ".")) && \ 17922a9f41bSArnaldo Carvalho de Melo (strcmp(evt_dirent->d_name, "..")) && \ 180c02cab22SArnaldo Carvalho de Melo (!tp_event_has_id(dir_path, evt_dirent))) 181f6bdafefSJason Baron 182270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 183f6bdafefSJason Baron 184f6bdafefSJason Baron 1851ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 186f6bdafefSJason Baron { 1871ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 188f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 18922a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 1908aa8a7c8SStephane Eranian char id_buf[24]; 191725b1368SEric Dumazet int fd; 192f6bdafefSJason Baron u64 id; 193f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 19425a7d914SArnaldo Carvalho de Melo char *dir_path; 195f6bdafefSJason Baron 1967014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 197f6bdafefSJason Baron if (!sys_dir) 198725b1368SEric Dumazet return NULL; 199f6bdafefSJason Baron 20022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 20125a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 20225a7d914SArnaldo Carvalho de Melo if (!dir_path) 20325a7d914SArnaldo Carvalho de Melo continue; 204725b1368SEric Dumazet evt_dir = opendir(dir_path); 205725b1368SEric Dumazet if (!evt_dir) 20625a7d914SArnaldo Carvalho de Melo goto next; 207725b1368SEric Dumazet 208c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 209725b1368SEric Dumazet 21077f18153SJiri Olsa scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 21122a9f41bSArnaldo Carvalho de Melo evt_dirent->d_name); 212725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 213f6bdafefSJason Baron if (fd < 0) 214f6bdafefSJason Baron continue; 215f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 216f6bdafefSJason Baron close(fd); 217f6bdafefSJason Baron continue; 218f6bdafefSJason Baron } 219f6bdafefSJason Baron close(fd); 220f6bdafefSJason Baron id = atoll(id_buf); 221f6bdafefSJason Baron if (id == config) { 22225a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 223f6bdafefSJason Baron closedir(evt_dir); 224f6bdafefSJason Baron closedir(sys_dir); 22559b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2265aa365f2STaeung Song if (!path) 2275aa365f2STaeung Song return NULL; 2281ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2291ef2ed10SFrederic Weisbecker if (!path->system) { 2301ef2ed10SFrederic Weisbecker free(path); 2311ef2ed10SFrederic Weisbecker return NULL; 2321ef2ed10SFrederic Weisbecker } 2331ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2341ef2ed10SFrederic Weisbecker if (!path->name) { 23574cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2361ef2ed10SFrederic Weisbecker free(path); 2371ef2ed10SFrederic Weisbecker return NULL; 2381ef2ed10SFrederic Weisbecker } 23922a9f41bSArnaldo Carvalho de Melo strncpy(path->system, sys_dirent->d_name, 2401ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 24122a9f41bSArnaldo Carvalho de Melo strncpy(path->name, evt_dirent->d_name, 2421ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2431ef2ed10SFrederic Weisbecker return path; 244f6bdafefSJason Baron } 245f6bdafefSJason Baron } 246f6bdafefSJason Baron closedir(evt_dir); 24725a7d914SArnaldo Carvalho de Melo next: 24825a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 249f6bdafefSJason Baron } 250f6bdafefSJason Baron 251f6bdafefSJason Baron closedir(sys_dir); 2521ef2ed10SFrederic Weisbecker return NULL; 2531ef2ed10SFrederic Weisbecker } 2541ef2ed10SFrederic Weisbecker 255e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 256e7c93f09SNamhyung Kim { 257e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 258e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 259e7c93f09SNamhyung Kim 260e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 261e7c93f09SNamhyung Kim free(path); 262e7c93f09SNamhyung Kim return NULL; 263e7c93f09SNamhyung Kim } 264e7c93f09SNamhyung Kim 265e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 266e7c93f09SNamhyung Kim path->name = strdup(str+1); 267e7c93f09SNamhyung Kim 268e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 26974cf249dSArnaldo Carvalho de Melo zfree(&path->system); 27074cf249dSArnaldo Carvalho de Melo zfree(&path->name); 271506fde11STaeung Song zfree(&path); 272e7c93f09SNamhyung Kim } 273e7c93f09SNamhyung Kim 274e7c93f09SNamhyung Kim return path; 275e7c93f09SNamhyung Kim } 276e7c93f09SNamhyung Kim 2771424dc96SDavid Ahern const char *event_type(int type) 2781424dc96SDavid Ahern { 2791424dc96SDavid Ahern switch (type) { 2801424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2811424dc96SDavid Ahern return "hardware"; 2821424dc96SDavid Ahern 2831424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2841424dc96SDavid Ahern return "software"; 2851424dc96SDavid Ahern 2861424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2871424dc96SDavid Ahern return "tracepoint"; 2881424dc96SDavid Ahern 2891424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2901424dc96SDavid Ahern return "hardware-cache"; 2911424dc96SDavid Ahern 2921424dc96SDavid Ahern default: 2931424dc96SDavid Ahern break; 2941424dc96SDavid Ahern } 2951424dc96SDavid Ahern 2961424dc96SDavid Ahern return "unknown"; 2971424dc96SDavid Ahern } 2981424dc96SDavid Ahern 299e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term) 300e814fdddSWang Nan { 301e814fdddSWang Nan return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 302e814fdddSWang Nan } 3037ae92e74SYan, Zheng 304e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms) 305e814fdddSWang Nan { 306e814fdddSWang Nan struct parse_events_term *term; 307e814fdddSWang Nan 308e814fdddSWang Nan if (!head_terms) 309e814fdddSWang Nan return NULL; 310e814fdddSWang Nan 311e814fdddSWang Nan list_for_each_entry(term, head_terms, list) 312e814fdddSWang Nan if (parse_events__is_name_term(term)) 313e814fdddSWang Nan return term->val.str; 314e814fdddSWang Nan 315e814fdddSWang Nan return NULL; 316e814fdddSWang Nan } 3177ae92e74SYan, Zheng 31832dcd021SJiri Olsa static struct evsel * 319410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 3207ae92e74SYan, Zheng struct perf_event_attr *attr, 32166ec1191SMark Rutland char *name, struct perf_pmu *pmu, 322f0fbb114SAndi Kleen struct list_head *config_terms, bool auto_merge_stats, 323f0fbb114SAndi Kleen const char *cpu_list) 32489812fc8SJiri Olsa { 32532dcd021SJiri Olsa struct evsel *evsel; 326f854839bSJiri Olsa struct perf_cpu_map *cpus = pmu ? pmu->cpus : 3279c3516d1SJiri Olsa cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 32889812fc8SJiri Olsa 32989812fc8SJiri Olsa event_attr_init(attr); 33089812fc8SJiri Olsa 33175fc5ae5STaeung Song evsel = perf_evsel__new_idx(attr, *idx); 332c5cd8ac0SDavid Ahern if (!evsel) 333410136f5SStephane Eranian return NULL; 33489812fc8SJiri Olsa 33575fc5ae5STaeung Song (*idx)++; 336d400bd3aSJiri Olsa evsel->core.cpus = perf_cpu_map__get(cpus); 337fe1f61b3SJiri Olsa evsel->core.own_cpus = perf_cpu_map__get(cpus); 338*648b5af3SJiri Olsa evsel->core.system_wide = pmu ? pmu->is_uncore : false; 33963ce8449SArnaldo Carvalho de Melo evsel->auto_merge_stats = auto_merge_stats; 340f30a79b0SJiri Olsa 3419db1763cSArnaldo Carvalho de Melo if (name) 34289812fc8SJiri Olsa evsel->name = strdup(name); 343930a2e29SJiri Olsa 344930a2e29SJiri Olsa if (config_terms) 345930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 346930a2e29SJiri Olsa 347b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 348410136f5SStephane Eranian return evsel; 34989812fc8SJiri Olsa } 35089812fc8SJiri Olsa 351c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 352930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 353930a2e29SJiri Olsa struct list_head *config_terms) 3547ae92e74SYan, Zheng { 355f0fbb114SAndi Kleen return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM; 356f0fbb114SAndi Kleen } 357f0fbb114SAndi Kleen 358f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx, 359f0fbb114SAndi Kleen enum perf_tool_event tool_event) 360f0fbb114SAndi Kleen { 36132dcd021SJiri Olsa struct evsel *evsel; 362f0fbb114SAndi Kleen struct perf_event_attr attr = { 363f0fbb114SAndi Kleen .type = PERF_TYPE_SOFTWARE, 364f0fbb114SAndi Kleen .config = PERF_COUNT_SW_DUMMY, 365f0fbb114SAndi Kleen }; 366f0fbb114SAndi Kleen 367f0fbb114SAndi Kleen evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0"); 368f0fbb114SAndi Kleen if (!evsel) 369f0fbb114SAndi Kleen return -ENOMEM; 370f0fbb114SAndi Kleen evsel->tool_event = tool_event; 371f0fbb114SAndi Kleen if (tool_event == PERF_TOOL_DURATION_TIME) 372f0fbb114SAndi Kleen evsel->unit = strdup("ns"); 373f0fbb114SAndi Kleen return 0; 3747ae92e74SYan, Zheng } 3757ae92e74SYan, Zheng 3760b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 37786470930SIngo Molnar { 37886470930SIngo Molnar int i, j; 37961c45981SPaul Mackerras int n, longest = -1; 38086470930SIngo Molnar 38186470930SIngo Molnar for (i = 0; i < size; i++) { 3820b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 38361c45981SPaul Mackerras n = strlen(names[i][j]); 38489812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 38561c45981SPaul Mackerras longest = n; 38661c45981SPaul Mackerras } 38789812fc8SJiri Olsa if (longest > 0) 38886470930SIngo Molnar return i; 38986470930SIngo Molnar } 39086470930SIngo Molnar 3918953645fSIngo Molnar return -1; 39286470930SIngo Molnar } 39386470930SIngo Molnar 39443d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr, 39543d0b978SWang Nan struct parse_events_term *term, 39643d0b978SWang Nan struct parse_events_error *err); 39743d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr, 39843d0b978SWang Nan struct parse_events_term *term, 39943d0b978SWang Nan struct parse_events_error *err); 40043d0b978SWang Nan static int config_attr(struct perf_event_attr *attr, 40143d0b978SWang Nan struct list_head *head, 40243d0b978SWang Nan struct parse_events_error *err, 40343d0b978SWang Nan config_term_func_t config_term); 40443d0b978SWang Nan 405c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 40643d0b978SWang Nan char *type, char *op_result1, char *op_result2, 4072c97b0d4SArnaldo Carvalho de Melo struct parse_events_error *err, 40843d0b978SWang Nan struct list_head *head_config) 40986470930SIngo Molnar { 41089812fc8SJiri Olsa struct perf_event_attr attr; 41143d0b978SWang Nan LIST_HEAD(config_terms); 41243d0b978SWang Nan char name[MAX_NAME_LEN], *config_name; 41361c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 41489812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 41589812fc8SJiri Olsa int i, n; 41686470930SIngo Molnar 41786470930SIngo Molnar /* 41886470930SIngo Molnar * No fallback - if we cannot get a clear cache type 41986470930SIngo Molnar * then bail out: 42086470930SIngo Molnar */ 4210b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 42289812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 42386470930SIngo Molnar if (cache_type == -1) 42489812fc8SJiri Olsa return -EINVAL; 42586470930SIngo Molnar 42643d0b978SWang Nan config_name = get_config_name(head_config); 42789812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 42889812fc8SJiri Olsa 42989812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 43089812fc8SJiri Olsa char *str = op_result[i]; 43189812fc8SJiri Olsa 432275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 43361c45981SPaul Mackerras 43461c45981SPaul Mackerras if (cache_op == -1) { 4350b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 43661c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 43761c45981SPaul Mackerras if (cache_op >= 0) { 4380b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 43989812fc8SJiri Olsa return -EINVAL; 44061c45981SPaul Mackerras continue; 44161c45981SPaul Mackerras } 44261c45981SPaul Mackerras } 44361c45981SPaul Mackerras 44461c45981SPaul Mackerras if (cache_result == -1) { 4450b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 44661c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 44761c45981SPaul Mackerras if (cache_result >= 0) 44861c45981SPaul Mackerras continue; 44961c45981SPaul Mackerras } 45061c45981SPaul Mackerras } 45161c45981SPaul Mackerras 45286470930SIngo Molnar /* 45386470930SIngo Molnar * Fall back to reads: 45486470930SIngo Molnar */ 4558953645fSIngo Molnar if (cache_op == -1) 4568953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 45786470930SIngo Molnar 45886470930SIngo Molnar /* 45986470930SIngo Molnar * Fall back to accesses: 46086470930SIngo Molnar */ 46186470930SIngo Molnar if (cache_result == -1) 46286470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 46386470930SIngo Molnar 46489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 46589812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 46689812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 46743d0b978SWang Nan 46843d0b978SWang Nan if (head_config) { 4692c97b0d4SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, err, 47043d0b978SWang Nan config_term_common)) 47143d0b978SWang Nan return -EINVAL; 47243d0b978SWang Nan 47343d0b978SWang Nan if (get_config_terms(head_config, &config_terms)) 47443d0b978SWang Nan return -ENOMEM; 47543d0b978SWang Nan } 47643d0b978SWang Nan return add_event(list, idx, &attr, config_name ? : name, &config_terms); 47786470930SIngo Molnar } 47886470930SIngo Molnar 479272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err, 4808c619d6aSWang Nan const char *sys, const char *name) 48119658171SJiri Olsa { 48219658171SJiri Olsa char help[BUFSIZ]; 48319658171SJiri Olsa 484ec183d22SAdrian Hunter if (!e) 485ec183d22SAdrian Hunter return; 486ec183d22SAdrian Hunter 48719658171SJiri Olsa /* 48819658171SJiri Olsa * We get error directly from syscall errno ( > 0), 48919658171SJiri Olsa * or from encoded pointer's error ( < 0). 49019658171SJiri Olsa */ 49119658171SJiri Olsa err = abs(err); 49219658171SJiri Olsa 49319658171SJiri Olsa switch (err) { 49419658171SJiri Olsa case EACCES: 495272ed29aSJiri Olsa e->str = strdup("can't access trace events"); 49619658171SJiri Olsa break; 49719658171SJiri Olsa case ENOENT: 498272ed29aSJiri Olsa e->str = strdup("unknown tracepoint"); 49919658171SJiri Olsa break; 50019658171SJiri Olsa default: 501272ed29aSJiri Olsa e->str = strdup("failed to add tracepoint"); 50219658171SJiri Olsa break; 50319658171SJiri Olsa } 50419658171SJiri Olsa 50519658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 506272ed29aSJiri Olsa e->help = strdup(help); 50719658171SJiri Olsa } 50819658171SJiri Olsa 509c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 5108c619d6aSWang Nan const char *sys_name, const char *evt_name, 511272ed29aSJiri Olsa struct parse_events_error *err, 512e637d177SHe Kuang struct list_head *head_config) 513bcd3279fSFrederic Weisbecker { 51432dcd021SJiri Olsa struct evsel *evsel; 515bcd3279fSFrederic Weisbecker 516ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 51719658171SJiri Olsa if (IS_ERR(evsel)) { 518272ed29aSJiri Olsa tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 5198dd2a131SJiri Olsa return PTR_ERR(evsel); 52019658171SJiri Olsa } 521bcd3279fSFrederic Weisbecker 522e637d177SHe Kuang if (head_config) { 523e637d177SHe Kuang LIST_HEAD(config_terms); 524e637d177SHe Kuang 525e637d177SHe Kuang if (get_config_terms(head_config, &config_terms)) 526e637d177SHe Kuang return -ENOMEM; 527e637d177SHe Kuang list_splice(&config_terms, &evsel->config_terms); 528e637d177SHe Kuang } 529e637d177SHe Kuang 530b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, list); 53182fe1c29SArnaldo Carvalho de Melo return 0; 532bcd3279fSFrederic Weisbecker } 533bcd3279fSFrederic Weisbecker 534c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 5358c619d6aSWang Nan const char *sys_name, const char *evt_name, 536272ed29aSJiri Olsa struct parse_events_error *err, 537e637d177SHe Kuang struct list_head *head_config) 538bcd3279fSFrederic Weisbecker { 53925a7d914SArnaldo Carvalho de Melo char *evt_path; 540bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 541bcd3279fSFrederic Weisbecker DIR *evt_dir; 54227bf90bfSJiri Olsa int ret = 0, found = 0; 543bcd3279fSFrederic Weisbecker 54425a7d914SArnaldo Carvalho de Melo evt_path = get_events_file(sys_name); 54525a7d914SArnaldo Carvalho de Melo if (!evt_path) { 54625a7d914SArnaldo Carvalho de Melo tracepoint_error(err, errno, sys_name, evt_name); 54725a7d914SArnaldo Carvalho de Melo return -1; 54825a7d914SArnaldo Carvalho de Melo } 549bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 550bcd3279fSFrederic Weisbecker if (!evt_dir) { 55125a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 552272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 55389812fc8SJiri Olsa return -1; 554bcd3279fSFrederic Weisbecker } 555bcd3279fSFrederic Weisbecker 55689812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 557bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 558bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 559bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 560bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 561bcd3279fSFrederic Weisbecker continue; 562bcd3279fSFrederic Weisbecker 56389812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 564fb1d2edfSMasami Hiramatsu continue; 565fb1d2edfSMasami Hiramatsu 56627bf90bfSJiri Olsa found++; 56727bf90bfSJiri Olsa 568e637d177SHe Kuang ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 569272ed29aSJiri Olsa err, head_config); 570bcd3279fSFrederic Weisbecker } 571bcd3279fSFrederic Weisbecker 57227bf90bfSJiri Olsa if (!found) { 57327bf90bfSJiri Olsa tracepoint_error(err, ENOENT, sys_name, evt_name); 57427bf90bfSJiri Olsa ret = -1; 57527bf90bfSJiri Olsa } 57627bf90bfSJiri Olsa 57725a7d914SArnaldo Carvalho de Melo put_events_file(evt_path); 5780bd3f084SJiri Olsa closedir(evt_dir); 57989812fc8SJiri Olsa return ret; 580bcd3279fSFrederic Weisbecker } 581bcd3279fSFrederic Weisbecker 582c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 5838c619d6aSWang Nan const char *sys_name, const char *evt_name, 584272ed29aSJiri Olsa struct parse_events_error *err, 585e637d177SHe Kuang struct list_head *head_config) 586f35488f9SJiri Olsa { 587f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 588e637d177SHe Kuang add_tracepoint_multi_event(list, idx, sys_name, evt_name, 589272ed29aSJiri Olsa err, head_config) : 590e637d177SHe Kuang add_tracepoint(list, idx, sys_name, evt_name, 591272ed29aSJiri Olsa err, head_config); 592f35488f9SJiri Olsa } 593f35488f9SJiri Olsa 594c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 5958c619d6aSWang Nan const char *sys_name, const char *evt_name, 596272ed29aSJiri Olsa struct parse_events_error *err, 597e637d177SHe Kuang struct list_head *head_config) 598f35488f9SJiri Olsa { 599f35488f9SJiri Olsa struct dirent *events_ent; 600f35488f9SJiri Olsa DIR *events_dir; 601f35488f9SJiri Olsa int ret = 0; 602f35488f9SJiri Olsa 6037014e0e3SArnaldo Carvalho de Melo events_dir = tracing_events__opendir(); 604f35488f9SJiri Olsa if (!events_dir) { 605272ed29aSJiri Olsa tracepoint_error(err, errno, sys_name, evt_name); 606f35488f9SJiri Olsa return -1; 607f35488f9SJiri Olsa } 608f35488f9SJiri Olsa 609f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 610f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 611f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 612f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 613f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 614f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 615f35488f9SJiri Olsa continue; 616f35488f9SJiri Olsa 617f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 618f35488f9SJiri Olsa continue; 619f35488f9SJiri Olsa 620f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 621272ed29aSJiri Olsa evt_name, err, head_config); 622f35488f9SJiri Olsa } 623f35488f9SJiri Olsa 624f35488f9SJiri Olsa closedir(events_dir); 625f35488f9SJiri Olsa return ret; 626f35488f9SJiri Olsa } 627f35488f9SJiri Olsa 6284edf30e3SWang Nan struct __add_bpf_event_param { 6295d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state; 6304edf30e3SWang Nan struct list_head *list; 63195088a59SWang Nan struct list_head *head_config; 6324edf30e3SWang Nan }; 6334edf30e3SWang Nan 634af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, 6354edf30e3SWang Nan void *_param) 6364edf30e3SWang Nan { 6374edf30e3SWang Nan LIST_HEAD(new_evsels); 6384edf30e3SWang Nan struct __add_bpf_event_param *param = _param; 6395d9cdc11SArnaldo Carvalho de Melo struct parse_events_state *parse_state = param->parse_state; 6404edf30e3SWang Nan struct list_head *list = param->list; 64132dcd021SJiri Olsa struct evsel *pos; 6424edf30e3SWang Nan int err; 6432620b7e3SArnaldo Carvalho de Melo /* 6442620b7e3SArnaldo Carvalho de Melo * Check if we should add the event, i.e. if it is a TP but starts with a '!', 6452620b7e3SArnaldo Carvalho de Melo * then don't add the tracepoint, this will be used for something else, like 6462620b7e3SArnaldo Carvalho de Melo * adding to a BPF_MAP_TYPE_PROG_ARRAY. 6472620b7e3SArnaldo Carvalho de Melo * 6482620b7e3SArnaldo Carvalho de Melo * See tools/perf/examples/bpf/augmented_raw_syscalls.c 6492620b7e3SArnaldo Carvalho de Melo */ 6502620b7e3SArnaldo Carvalho de Melo if (group[0] == '!') 6512620b7e3SArnaldo Carvalho de Melo return 0; 6524edf30e3SWang Nan 6534edf30e3SWang Nan pr_debug("add bpf event %s:%s and attach bpf program %d\n", 654cd102d70SWang Nan group, event, fd); 6554edf30e3SWang Nan 6565d369a75SArnaldo Carvalho de Melo err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, 6575d369a75SArnaldo Carvalho de Melo event, parse_state->error, 65895088a59SWang Nan param->head_config); 6594edf30e3SWang Nan if (err) { 66032dcd021SJiri Olsa struct evsel *evsel, *tmp; 6614edf30e3SWang Nan 6624edf30e3SWang Nan pr_debug("Failed to add BPF event %s:%s\n", 663cd102d70SWang Nan group, event); 664b27c4eceSJiri Olsa list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { 665b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 6665eb2dd2aSJiri Olsa evsel__delete(evsel); 6674edf30e3SWang Nan } 6684edf30e3SWang Nan return err; 6694edf30e3SWang Nan } 670cd102d70SWang Nan pr_debug("adding %s:%s\n", group, event); 6714edf30e3SWang Nan 672b27c4eceSJiri Olsa list_for_each_entry(pos, &new_evsels, core.node) { 6731f45b1d4SWang Nan pr_debug("adding %s:%s to %p\n", 674cd102d70SWang Nan group, event, pos); 6751f45b1d4SWang Nan pos->bpf_fd = fd; 676af4a0991SArnaldo Carvalho de Melo pos->bpf_obj = obj; 6771f45b1d4SWang Nan } 6784edf30e3SWang Nan list_splice(&new_evsels, list); 6794edf30e3SWang Nan return 0; 6804edf30e3SWang Nan } 6814edf30e3SWang Nan 6825d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state, 68384c86ca1SWang Nan struct list_head *list, 68495088a59SWang Nan struct bpf_object *obj, 68595088a59SWang Nan struct list_head *head_config) 68684c86ca1SWang Nan { 68784c86ca1SWang Nan int err; 68884c86ca1SWang Nan char errbuf[BUFSIZ]; 6895d9cdc11SArnaldo Carvalho de Melo struct __add_bpf_event_param param = {parse_state, list, head_config}; 690aa3abf30SWang Nan static bool registered_unprobe_atexit = false; 69184c86ca1SWang Nan 69284c86ca1SWang Nan if (IS_ERR(obj) || !obj) { 69384c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 69484c86ca1SWang Nan "Internal error: load bpf obj with NULL"); 69584c86ca1SWang Nan err = -EINVAL; 69684c86ca1SWang Nan goto errout; 69784c86ca1SWang Nan } 69884c86ca1SWang Nan 69984c86ca1SWang Nan /* 700aa3abf30SWang Nan * Register atexit handler before calling bpf__probe() so 701aa3abf30SWang Nan * bpf__probe() don't need to unprobe probe points its already 702aa3abf30SWang Nan * created when failure. 703aa3abf30SWang Nan */ 704aa3abf30SWang Nan if (!registered_unprobe_atexit) { 705aa3abf30SWang Nan atexit(bpf__clear); 706aa3abf30SWang Nan registered_unprobe_atexit = true; 707aa3abf30SWang Nan } 708aa3abf30SWang Nan 709aa3abf30SWang Nan err = bpf__probe(obj); 710aa3abf30SWang Nan if (err) { 711aa3abf30SWang Nan bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); 712aa3abf30SWang Nan goto errout; 713aa3abf30SWang Nan } 714aa3abf30SWang Nan 7151e5e3ee8SWang Nan err = bpf__load(obj); 7161e5e3ee8SWang Nan if (err) { 7171e5e3ee8SWang Nan bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); 7181e5e3ee8SWang Nan goto errout; 7191e5e3ee8SWang Nan } 7201e5e3ee8SWang Nan 721cd102d70SWang Nan err = bpf__foreach_event(obj, add_bpf_event, ¶m); 7224edf30e3SWang Nan if (err) { 7234edf30e3SWang Nan snprintf(errbuf, sizeof(errbuf), 7244edf30e3SWang Nan "Attach events in BPF object failed"); 7254edf30e3SWang Nan goto errout; 7264edf30e3SWang Nan } 7274edf30e3SWang Nan 7284edf30e3SWang Nan return 0; 72984c86ca1SWang Nan errout: 7305d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 7315d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 73284c86ca1SWang Nan return err; 73384c86ca1SWang Nan } 73484c86ca1SWang Nan 735a34f3be7SWang Nan static int 7365d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state, 737a34f3be7SWang Nan struct bpf_object *obj, 738a34f3be7SWang Nan struct list_head *head_config) 739a34f3be7SWang Nan { 740a34f3be7SWang Nan struct parse_events_term *term; 741a34f3be7SWang Nan int error_pos; 742a34f3be7SWang Nan 743a34f3be7SWang Nan if (!head_config || list_empty(head_config)) 744a34f3be7SWang Nan return 0; 745a34f3be7SWang Nan 746a34f3be7SWang Nan list_for_each_entry(term, head_config, list) { 747a34f3be7SWang Nan char errbuf[BUFSIZ]; 748a34f3be7SWang Nan int err; 749a34f3be7SWang Nan 750a34f3be7SWang Nan if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { 751a34f3be7SWang Nan snprintf(errbuf, sizeof(errbuf), 752a34f3be7SWang Nan "Invalid config term for BPF object"); 753a34f3be7SWang Nan errbuf[BUFSIZ - 1] = '\0'; 754a34f3be7SWang Nan 7555d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term; 7565d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 757a34f3be7SWang Nan return -EINVAL; 758a34f3be7SWang Nan } 759a34f3be7SWang Nan 7605d9cdc11SArnaldo Carvalho de Melo err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); 761a34f3be7SWang Nan if (err) { 7625d9cdc11SArnaldo Carvalho de Melo bpf__strerror_config_obj(obj, term, parse_state->evlist, 763a34f3be7SWang Nan &error_pos, err, errbuf, 764a34f3be7SWang Nan sizeof(errbuf)); 7655d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup( 7667630b3e2SWang Nan "Hint:\tValid config terms:\n" 767e571e029SWang Nan " \tmap:[<arraymap>].value<indices>=[value]\n" 768e571e029SWang Nan " \tmap:[<eventmap>].event<indices>=[event]\n" 7697630b3e2SWang Nan "\n" 770e571e029SWang Nan " \twhere <indices> is something like [0,3...5] or [all]\n" 771a34f3be7SWang Nan " \t(add -v to see detail)"); 7725d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 773a34f3be7SWang Nan if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) 7745d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_val; 775a34f3be7SWang Nan else 7765d9cdc11SArnaldo Carvalho de Melo parse_state->error->idx = term->err_term + error_pos; 777a34f3be7SWang Nan return err; 778a34f3be7SWang Nan } 779a34f3be7SWang Nan } 780a34f3be7SWang Nan return 0; 781a34f3be7SWang Nan } 782a34f3be7SWang Nan 78395088a59SWang Nan /* 78495088a59SWang Nan * Split config terms: 78595088a59SWang Nan * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... 78695088a59SWang Nan * 'call-graph=fp' is 'evt config', should be applied to each 78795088a59SWang Nan * events in bpf.c. 78895088a59SWang Nan * 'map:array.value[0]=1' is 'obj config', should be processed 78995088a59SWang Nan * with parse_events_config_bpf. 79095088a59SWang Nan * 79195088a59SWang Nan * Move object config terms from the first list to obj_head_config. 79295088a59SWang Nan */ 79395088a59SWang Nan static void 79495088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config, 79595088a59SWang Nan struct list_head *obj_head_config) 79695088a59SWang Nan { 79795088a59SWang Nan struct parse_events_term *term, *temp; 79895088a59SWang Nan 79995088a59SWang Nan /* 80095088a59SWang Nan * Currectly, all possible user config term 80195088a59SWang Nan * belong to bpf object. parse_events__is_hardcoded_term() 80295088a59SWang Nan * happends to be a good flag. 80395088a59SWang Nan * 80495088a59SWang Nan * See parse_events_config_bpf() and 80595088a59SWang Nan * config_term_tracepoint(). 80695088a59SWang Nan */ 80795088a59SWang Nan list_for_each_entry_safe(term, temp, evt_head_config, list) 80895088a59SWang Nan if (!parse_events__is_hardcoded_term(term)) 80995088a59SWang Nan list_move_tail(&term->list, obj_head_config); 81095088a59SWang Nan } 81195088a59SWang Nan 8125d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state, 81384c86ca1SWang Nan struct list_head *list, 814d509db04SWang Nan char *bpf_file_name, 815a34f3be7SWang Nan bool source, 816a34f3be7SWang Nan struct list_head *head_config) 81784c86ca1SWang Nan { 818a34f3be7SWang Nan int err; 81995088a59SWang Nan struct bpf_object *obj; 82095088a59SWang Nan LIST_HEAD(obj_head_config); 82195088a59SWang Nan 82295088a59SWang Nan if (head_config) 82395088a59SWang Nan split_bpf_config_terms(head_config, &obj_head_config); 82484c86ca1SWang Nan 825d509db04SWang Nan obj = bpf__prepare_load(bpf_file_name, source); 8266371ca3bSWang Nan if (IS_ERR(obj)) { 82784c86ca1SWang Nan char errbuf[BUFSIZ]; 82884c86ca1SWang Nan 8296371ca3bSWang Nan err = PTR_ERR(obj); 83084c86ca1SWang Nan 83184c86ca1SWang Nan if (err == -ENOTSUP) 83284c86ca1SWang Nan snprintf(errbuf, sizeof(errbuf), 83384c86ca1SWang Nan "BPF support is not compiled"); 83484c86ca1SWang Nan else 835d3e0ce39SWang Nan bpf__strerror_prepare_load(bpf_file_name, 836d3e0ce39SWang Nan source, 837d3e0ce39SWang Nan -err, errbuf, 838d3e0ce39SWang Nan sizeof(errbuf)); 83984c86ca1SWang Nan 8405d9cdc11SArnaldo Carvalho de Melo parse_state->error->help = strdup("(add -v to see detail)"); 8415d9cdc11SArnaldo Carvalho de Melo parse_state->error->str = strdup(errbuf); 84284c86ca1SWang Nan return err; 84384c86ca1SWang Nan } 84484c86ca1SWang Nan 8455d9cdc11SArnaldo Carvalho de Melo err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); 846a34f3be7SWang Nan if (err) 847a34f3be7SWang Nan return err; 8485d9cdc11SArnaldo Carvalho de Melo err = parse_events_config_bpf(parse_state, obj, &obj_head_config); 84995088a59SWang Nan 85095088a59SWang Nan /* 85195088a59SWang Nan * Caller doesn't know anything about obj_head_config, 85295088a59SWang Nan * so combine them together again before returnning. 85395088a59SWang Nan */ 85495088a59SWang Nan if (head_config) 85595088a59SWang Nan list_splice_tail(&obj_head_config, head_config); 85695088a59SWang Nan return err; 85784c86ca1SWang Nan } 85884c86ca1SWang Nan 85989812fc8SJiri Olsa static int 86089812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 8611b290d67SFrederic Weisbecker { 8621b290d67SFrederic Weisbecker int i; 8631b290d67SFrederic Weisbecker 8641b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 86589812fc8SJiri Olsa if (!type || !type[i]) 8661b290d67SFrederic Weisbecker break; 8671b290d67SFrederic Weisbecker 8687582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 8697582732fSJiri Olsa do { \ 8707582732fSJiri Olsa if (attr->bp_type & bit) \ 8717582732fSJiri Olsa return -EINVAL; \ 8727582732fSJiri Olsa else \ 8737582732fSJiri Olsa attr->bp_type |= bit; \ 8747582732fSJiri Olsa } while (0) 8757582732fSJiri Olsa 8761b290d67SFrederic Weisbecker switch (type[i]) { 8771b290d67SFrederic Weisbecker case 'r': 8787582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 8791b290d67SFrederic Weisbecker break; 8801b290d67SFrederic Weisbecker case 'w': 8817582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 8821b290d67SFrederic Weisbecker break; 8831b290d67SFrederic Weisbecker case 'x': 8847582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 8851b290d67SFrederic Weisbecker break; 8861b290d67SFrederic Weisbecker default: 88789812fc8SJiri Olsa return -EINVAL; 8881b290d67SFrederic Weisbecker } 8891b290d67SFrederic Weisbecker } 89089812fc8SJiri Olsa 8917582732fSJiri Olsa #undef CHECK_SET_TYPE 8927582732fSJiri Olsa 8931b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 8941b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 8951b290d67SFrederic Weisbecker 89689812fc8SJiri Olsa return 0; 8971b290d67SFrederic Weisbecker } 8981b290d67SFrederic Weisbecker 899c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 9003741eb9fSJacob Shin void *ptr, char *type, u64 len) 9011b290d67SFrederic Weisbecker { 90289812fc8SJiri Olsa struct perf_event_attr attr; 9031b290d67SFrederic Weisbecker 90489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 9059fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 9061b290d67SFrederic Weisbecker 90789812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 90889812fc8SJiri Olsa return -EINVAL; 9091b290d67SFrederic Weisbecker 9103741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 9113741eb9fSJacob Shin if (!len) { 91289812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 9133741eb9fSJacob Shin len = sizeof(long); 914aa59a485SFrederic Weisbecker else 9153741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 9163741eb9fSJacob Shin } 9173741eb9fSJacob Shin 9183741eb9fSJacob Shin attr.bp_len = len; 919aa59a485SFrederic Weisbecker 92089812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 9214a841d65SJovi Zhang attr.sample_period = 1; 9221b290d67SFrederic Weisbecker 923930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 9241b290d67SFrederic Weisbecker } 9251b290d67SFrederic Weisbecker 9263b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 9273b0e371cSJiri Olsa struct parse_events_error *err, 9283b0e371cSJiri Olsa int type) 9293b0e371cSJiri Olsa { 9303b0e371cSJiri Olsa if (type == term->type_val) 9313b0e371cSJiri Olsa return 0; 9323b0e371cSJiri Olsa 9333b0e371cSJiri Olsa if (err) { 9343b0e371cSJiri Olsa err->idx = term->err_val; 9353b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 9363b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 9373b0e371cSJiri Olsa else 9383b0e371cSJiri Olsa err->str = strdup("expected string value"); 9393b0e371cSJiri Olsa } 9403b0e371cSJiri Olsa return -EINVAL; 9413b0e371cSJiri Olsa } 9423b0e371cSJiri Olsa 94317cb5f84SWang Nan /* 94417cb5f84SWang Nan * Update according to parse-events.l 94517cb5f84SWang Nan */ 94617cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 94717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 94817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 94917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 95017cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 95117cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 95217cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 95317cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 95417cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 95517cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 95617cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 95717cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 95817cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 95917cb5f84SWang Nan [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 960792d48b4SArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 9612fda5adaSArnaldo Carvalho de Melo [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 962626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 963626a6b78SWang Nan [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 964dd60fba7SMathieu Poirier [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 965064b4e82SJin Yao [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 9661b992154SAdrian Hunter [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 96717cb5f84SWang Nan }; 96817cb5f84SWang Nan 9691669e509SWang Nan static bool config_term_shrinked; 9701669e509SWang Nan 9711669e509SWang Nan static bool 9721669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err) 9731669e509SWang Nan { 9741669e509SWang Nan if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 9751669e509SWang Nan err->str = strdup("Invalid term_type"); 9761669e509SWang Nan return false; 9771669e509SWang Nan } 9781669e509SWang Nan if (!config_term_shrinked) 9791669e509SWang Nan return true; 9801669e509SWang Nan 9811669e509SWang Nan switch (term_type) { 9821669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG: 9831669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG1: 9841669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_CONFIG2: 9851669e509SWang Nan case PARSE_EVENTS__TERM_TYPE_NAME: 98606835545SSukadev Bhattiprolu case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 987064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 9881669e509SWang Nan return true; 9891669e509SWang Nan default: 9901669e509SWang Nan if (!err) 9911669e509SWang Nan return false; 9921669e509SWang Nan 9931669e509SWang Nan /* term_type is validated so indexing is safe */ 9941669e509SWang Nan if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", 9951669e509SWang Nan config_term_names[term_type]) < 0) 9961669e509SWang Nan err->str = NULL; 9971669e509SWang Nan return false; 9981669e509SWang Nan } 9991669e509SWang Nan } 10001669e509SWang Nan 10011669e509SWang Nan void parse_events__shrink_config_terms(void) 10021669e509SWang Nan { 10031669e509SWang Nan config_term_shrinked = true; 10041669e509SWang Nan } 10051669e509SWang Nan 10060b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 10073b0e371cSJiri Olsa struct parse_events_term *term, 10083b0e371cSJiri Olsa struct parse_events_error *err) 10098f707d84SJiri Olsa { 101016fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 101116fa7e82SJiri Olsa do { \ 10123b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 101316fa7e82SJiri Olsa return -EINVAL; \ 101416fa7e82SJiri Olsa } while (0) 101516fa7e82SJiri Olsa 101616fa7e82SJiri Olsa switch (term->type_term) { 10178f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 101816fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10198f707d84SJiri Olsa attr->config = term->val.num; 10208f707d84SJiri Olsa break; 10218f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 102216fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10238f707d84SJiri Olsa attr->config1 = term->val.num; 10248f707d84SJiri Olsa break; 10258f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 102616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10278f707d84SJiri Olsa attr->config2 = term->val.num; 10288f707d84SJiri Olsa break; 10298f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 103016fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 10318f707d84SJiri Olsa break; 103209af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 103309af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 103409af2a55SNamhyung Kim break; 10358f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1036ac12f676SAndi Kleen CHECK_TYPE_VAL(STR); 1037ac12f676SAndi Kleen if (strcmp(term->val.str, "no") && 1038ac12f676SAndi Kleen parse_branch_str(term->val.str, &attr->branch_sample_type)) { 1039ac12f676SAndi Kleen err->str = strdup("invalid branch sample type"); 1040ac12f676SAndi Kleen err->idx = term->err_val; 1041ac12f676SAndi Kleen return -EINVAL; 1042ac12f676SAndi Kleen } 10438f707d84SJiri Olsa break; 104432067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 104532067712SKan Liang CHECK_TYPE_VAL(NUM); 104632067712SKan Liang if (term->val.num > 1) { 104732067712SKan Liang err->str = strdup("expected 0 or 1"); 104832067712SKan Liang err->idx = term->err_val; 104932067712SKan Liang return -EINVAL; 105032067712SKan Liang } 105132067712SKan Liang break; 1052d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1053d457c963SKan Liang CHECK_TYPE_VAL(STR); 1054d457c963SKan Liang break; 1055d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1056d457c963SKan Liang CHECK_TYPE_VAL(NUM); 1057d457c963SKan Liang break; 1058374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1059374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1060374ce938SWang Nan break; 1061374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1062374ce938SWang Nan CHECK_TYPE_VAL(NUM); 1063374ce938SWang Nan break; 1064626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1065626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1066626a6b78SWang Nan break; 1067626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1068626a6b78SWang Nan CHECK_TYPE_VAL(NUM); 1069626a6b78SWang Nan break; 10706b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 10716b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 10726b5fc39bSJiri Olsa break; 1073792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1074792d48b4SArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 1075792d48b4SArnaldo Carvalho de Melo break; 10762fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 10772fda5adaSArnaldo Carvalho de Melo CHECK_TYPE_VAL(NUM); 10782fda5adaSArnaldo Carvalho de Melo break; 1079064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1080064b4e82SJin Yao CHECK_TYPE_VAL(NUM); 1081064b4e82SJin Yao if ((unsigned int)term->val.num > 1) { 1082064b4e82SJin Yao err->str = strdup("expected 0 or 1"); 1083064b4e82SJin Yao err->idx = term->err_val; 1084064b4e82SJin Yao return -EINVAL; 1085064b4e82SJin Yao } 1086064b4e82SJin Yao break; 10871b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 10881b992154SAdrian Hunter CHECK_TYPE_VAL(NUM); 10891b992154SAdrian Hunter break; 10908f707d84SJiri Olsa default: 1091ffeb883eSHe Kuang err->str = strdup("unknown term"); 1092ffeb883eSHe Kuang err->idx = term->err_term; 1093ffeb883eSHe Kuang err->help = parse_events_formats_error_string(NULL); 10948f707d84SJiri Olsa return -EINVAL; 10958f707d84SJiri Olsa } 109616fa7e82SJiri Olsa 10971669e509SWang Nan /* 10981669e509SWang Nan * Check term availbility after basic checking so 10991669e509SWang Nan * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 11001669e509SWang Nan * 11011669e509SWang Nan * If check availbility at the entry of this function, 11021669e509SWang Nan * user will see "'<sysfs term>' is not usable in 'perf stat'" 11031669e509SWang Nan * if an invalid config term is provided for legacy events 11041669e509SWang Nan * (for example, instructions/badterm/...), which is confusing. 11051669e509SWang Nan */ 11061669e509SWang Nan if (!config_term_avail(term->type_term, err)) 11071669e509SWang Nan return -EINVAL; 11088f707d84SJiri Olsa return 0; 110916fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 11108f707d84SJiri Olsa } 11118f707d84SJiri Olsa 11120b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 11130b8891a8SHe Kuang struct parse_events_term *term, 11140b8891a8SHe Kuang struct parse_events_error *err) 11150b8891a8SHe Kuang { 1116dd60fba7SMathieu Poirier if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 1117dd60fba7SMathieu Poirier term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) 11180b8891a8SHe Kuang /* 11190b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 11200b8891a8SHe Kuang * at this point what type they need to have. 11210b8891a8SHe Kuang */ 11220b8891a8SHe Kuang return 0; 11230b8891a8SHe Kuang else 11240b8891a8SHe Kuang return config_term_common(attr, term, err); 11250b8891a8SHe Kuang } 11260b8891a8SHe Kuang 1127e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr, 1128e637d177SHe Kuang struct parse_events_term *term, 1129e637d177SHe Kuang struct parse_events_error *err) 1130e637d177SHe Kuang { 1131e637d177SHe Kuang switch (term->type_term) { 1132e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1133e637d177SHe Kuang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1134374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1135374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1136792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 11372fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1138626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1139626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 11401b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1141e637d177SHe Kuang return config_term_common(attr, term, err); 1142e637d177SHe Kuang default: 1143e637d177SHe Kuang if (err) { 1144e637d177SHe Kuang err->idx = term->err_term; 1145e637d177SHe Kuang err->str = strdup("unknown term"); 1146e637d177SHe Kuang err->help = strdup("valid terms: call-graph,stack-size\n"); 1147e637d177SHe Kuang } 1148e637d177SHe Kuang return -EINVAL; 1149e637d177SHe Kuang } 1150e637d177SHe Kuang 1151e637d177SHe Kuang return 0; 1152e637d177SHe Kuang } 1153e637d177SHe Kuang 11548f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 11553b0e371cSJiri Olsa struct list_head *head, 11560b8891a8SHe Kuang struct parse_events_error *err, 11570b8891a8SHe Kuang config_term_func_t config_term) 11588f707d84SJiri Olsa { 11596cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 11608f707d84SJiri Olsa 11618f707d84SJiri Olsa list_for_each_entry(term, head, list) 11623b0e371cSJiri Olsa if (config_term(attr, term, err)) 11638f707d84SJiri Olsa return -EINVAL; 11648f707d84SJiri Olsa 11658f707d84SJiri Olsa return 0; 11668f707d84SJiri Olsa } 11678f707d84SJiri Olsa 1168930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 1169930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 1170930a2e29SJiri Olsa { 1171930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 1172930a2e29SJiri Olsa do { \ 1173930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 1174930a2e29SJiri Olsa \ 1175930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 1176930a2e29SJiri Olsa if (!__t) \ 1177930a2e29SJiri Olsa return -ENOMEM; \ 1178930a2e29SJiri Olsa \ 1179930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 1180930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 1181930a2e29SJiri Olsa __t->val.__name = __val; \ 1182c2f1ceadSAndi Kleen __t->weak = term->weak; \ 1183930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 1184930a2e29SJiri Olsa } while (0) 1185930a2e29SJiri Olsa 1186930a2e29SJiri Olsa struct parse_events_term *term; 1187930a2e29SJiri Olsa 1188930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 1189930a2e29SJiri Olsa switch (term->type_term) { 1190ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1191ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 119232067712SKan Liang break; 119309af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 119409af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 119509af2a55SNamhyung Kim break; 119632067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 119732067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 119832067712SKan Liang break; 1199d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1200d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 1201d457c963SKan Liang break; 1202ac12f676SAndi Kleen case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1203ac12f676SAndi Kleen ADD_CONFIG_TERM(BRANCH, branch, term->val.str); 1204ac12f676SAndi Kleen break; 1205d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1206d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 1207d457c963SKan Liang break; 1208374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_INHERIT: 1209374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); 1210374ce938SWang Nan break; 1211374ce938SWang Nan case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1212374ce938SWang Nan ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); 1213374ce938SWang Nan break; 1214792d48b4SArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1215792d48b4SArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num); 1216792d48b4SArnaldo Carvalho de Melo break; 12172fda5adaSArnaldo Carvalho de Melo case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 12182fda5adaSArnaldo Carvalho de Melo ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num); 12192fda5adaSArnaldo Carvalho de Melo break; 1220626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1221626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0); 1222626a6b78SWang Nan break; 1223626a6b78SWang Nan case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1224626a6b78SWang Nan ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1); 1225626a6b78SWang Nan break; 1226dd60fba7SMathieu Poirier case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1227dd60fba7SMathieu Poirier ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str); 1228dd60fba7SMathieu Poirier break; 1229064b4e82SJin Yao case PARSE_EVENTS__TERM_TYPE_PERCORE: 1230064b4e82SJin Yao ADD_CONFIG_TERM(PERCORE, percore, 1231064b4e82SJin Yao term->val.num ? true : false); 1232064b4e82SJin Yao break; 12331b992154SAdrian Hunter case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 12341b992154SAdrian Hunter ADD_CONFIG_TERM(AUX_OUTPUT, aux_output, term->val.num ? 1 : 0); 12351b992154SAdrian Hunter break; 1236930a2e29SJiri Olsa default: 1237930a2e29SJiri Olsa break; 1238930a2e29SJiri Olsa } 1239930a2e29SJiri Olsa } 1240930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 1241930a2e29SJiri Olsa return 0; 1242930a2e29SJiri Olsa } 1243930a2e29SJiri Olsa 1244e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx, 12458c619d6aSWang Nan const char *sys, const char *event, 1246272ed29aSJiri Olsa struct parse_events_error *err, 1247e637d177SHe Kuang struct list_head *head_config) 1248e637d177SHe Kuang { 1249e637d177SHe Kuang if (head_config) { 1250e637d177SHe Kuang struct perf_event_attr attr; 1251e637d177SHe Kuang 1252272ed29aSJiri Olsa if (config_attr(&attr, head_config, err, 1253e637d177SHe Kuang config_term_tracepoint)) 1254e637d177SHe Kuang return -EINVAL; 1255e637d177SHe Kuang } 1256e637d177SHe Kuang 1257e637d177SHe Kuang if (strpbrk(sys, "*?")) 1258e637d177SHe Kuang return add_tracepoint_multi_sys(list, idx, sys, event, 1259272ed29aSJiri Olsa err, head_config); 1260e637d177SHe Kuang else 1261e637d177SHe Kuang return add_tracepoint_event(list, idx, sys, event, 1262272ed29aSJiri Olsa err, head_config); 1263e637d177SHe Kuang } 1264e637d177SHe Kuang 12655d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state, 126687d650beSJiri Olsa struct list_head *list, 1267b527bab5SRobert Richter u32 type, u64 config, 12688f707d84SJiri Olsa struct list_head *head_config) 126974d5b588SJaswinder Singh Rajput { 127089812fc8SJiri Olsa struct perf_event_attr attr; 1271930a2e29SJiri Olsa LIST_HEAD(config_terms); 127274d5b588SJaswinder Singh Rajput 127389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 127489812fc8SJiri Olsa attr.type = type; 127589812fc8SJiri Olsa attr.config = config; 12768f707d84SJiri Olsa 1277930a2e29SJiri Olsa if (head_config) { 12785d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, 12790b8891a8SHe Kuang config_term_common)) 12808f707d84SJiri Olsa return -EINVAL; 12818f707d84SJiri Olsa 1282930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1283930a2e29SJiri Olsa return -ENOMEM; 1284930a2e29SJiri Olsa } 1285930a2e29SJiri Olsa 12865d9cdc11SArnaldo Carvalho de Melo return add_event(list, &parse_state->idx, &attr, 128710bf358aSWang Nan get_config_name(head_config), &config_terms); 1288b908debdSIngo Molnar } 1289b908debdSIngo Molnar 1290f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state, 1291f0fbb114SAndi Kleen struct list_head *list, 1292f0fbb114SAndi Kleen enum perf_tool_event tool_event) 1293f0fbb114SAndi Kleen { 1294f0fbb114SAndi Kleen return add_event_tool(list, &parse_state->idx, tool_event); 1295f0fbb114SAndi Kleen } 1296f0fbb114SAndi Kleen 1297064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms) 1298064b4e82SJin Yao { 1299064b4e82SJin Yao struct perf_evsel_config_term *term; 1300064b4e82SJin Yao 1301064b4e82SJin Yao list_for_each_entry(term, config_terms, list) { 1302064b4e82SJin Yao if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE) 1303064b4e82SJin Yao return term->val.percore; 1304064b4e82SJin Yao } 1305064b4e82SJin Yao 1306064b4e82SJin Yao return false; 1307064b4e82SJin Yao } 1308064b4e82SJin Yao 1309c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state, 131036adec85SJiri Olsa struct list_head *list, char *name, 13113cdc5c2cSKan Liang struct list_head *head_config, 13123cdc5c2cSKan Liang bool auto_merge_stats, 13133cdc5c2cSKan Liang bool use_alias) 13145f537a26SJiri Olsa { 13155f537a26SJiri Olsa struct perf_event_attr attr; 131646441bdcSMatt Fleming struct perf_pmu_info info; 13175f537a26SJiri Olsa struct perf_pmu *pmu; 131832dcd021SJiri Olsa struct evsel *evsel; 1319333b5665SAndi Kleen struct parse_events_error *err = parse_state->error; 13203cdc5c2cSKan Liang bool use_uncore_alias; 1321930a2e29SJiri Olsa LIST_HEAD(config_terms); 13225f537a26SJiri Olsa 13235f537a26SJiri Olsa pmu = perf_pmu__find(name); 1324333b5665SAndi Kleen if (!pmu) { 1325333b5665SAndi Kleen if (asprintf(&err->str, 1326333b5665SAndi Kleen "Cannot find PMU `%s'. Missing kernel support?", 1327333b5665SAndi Kleen name) < 0) 1328333b5665SAndi Kleen err->str = NULL; 13295f537a26SJiri Olsa return -EINVAL; 1330333b5665SAndi Kleen } 13315f537a26SJiri Olsa 1332dc0a6202SAdrian Hunter if (pmu->default_config) { 1333dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 1334dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 1335dc0a6202SAdrian Hunter } else { 13365f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 1337dc0a6202SAdrian Hunter } 13385f537a26SJiri Olsa 13393cdc5c2cSKan Liang use_uncore_alias = (pmu->is_uncore && use_alias); 13403cdc5c2cSKan Liang 1341ad962273SAdrian Hunter if (!head_config) { 1342ad962273SAdrian Hunter attr.type = pmu->type; 1343f0fbb114SAndi Kleen evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, 1344f0fbb114SAndi Kleen auto_merge_stats, NULL); 13458c5421c0SAgustin Vega-Frias if (evsel) { 13468c5421c0SAgustin Vega-Frias evsel->pmu_name = name; 13473cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 13488c5421c0SAgustin Vega-Frias return 0; 13498c5421c0SAgustin Vega-Frias } else { 13508c5421c0SAgustin Vega-Frias return -ENOMEM; 13518c5421c0SAgustin Vega-Frias } 1352ad962273SAdrian Hunter } 1353ad962273SAdrian Hunter 135446441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 1355a6146d50SZheng Yan return -EINVAL; 1356a6146d50SZheng Yan 13575f537a26SJiri Olsa /* 13585f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 13595f537a26SJiri Olsa * return value when called with fail == 0 ;) 13605f537a26SJiri Olsa */ 13615d9cdc11SArnaldo Carvalho de Melo if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1362c056ba6aSJiri Olsa return -EINVAL; 13635f537a26SJiri Olsa 1364930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 1365930a2e29SJiri Olsa return -ENOMEM; 1366930a2e29SJiri Olsa 13675d9cdc11SArnaldo Carvalho de Melo if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) 13685f537a26SJiri Olsa return -EINVAL; 13695f537a26SJiri Olsa 13705d9cdc11SArnaldo Carvalho de Melo evsel = __add_event(list, &parse_state->idx, &attr, 137166ec1191SMark Rutland get_config_name(head_config), pmu, 1372f0fbb114SAndi Kleen &config_terms, auto_merge_stats, NULL); 1373410136f5SStephane Eranian if (evsel) { 137446441bdcSMatt Fleming evsel->unit = info.unit; 137546441bdcSMatt Fleming evsel->scale = info.scale; 1376044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 13771d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 137837932c18SAndi Kleen evsel->metric_expr = info.metric_expr; 137996284814SAndi Kleen evsel->metric_name = info.metric_name; 13808c5421c0SAgustin Vega-Frias evsel->pmu_name = name; 13813cdc5c2cSKan Liang evsel->use_uncore_alias = use_uncore_alias; 1382064b4e82SJin Yao evsel->percore = config_term_percore(&evsel->config_terms); 1383410136f5SStephane Eranian } 1384410136f5SStephane Eranian 1385410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 13865f537a26SJiri Olsa } 13875f537a26SJiri Olsa 13885d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 13892073ad33SAndi Kleen char *str, struct list_head **listp) 13902073ad33SAndi Kleen { 13912073ad33SAndi Kleen struct list_head *head; 13922073ad33SAndi Kleen struct parse_events_term *term; 13932073ad33SAndi Kleen struct list_head *list; 13942073ad33SAndi Kleen struct perf_pmu *pmu = NULL; 13952073ad33SAndi Kleen int ok = 0; 13962073ad33SAndi Kleen 13972073ad33SAndi Kleen *listp = NULL; 13982073ad33SAndi Kleen /* Add it for all PMUs that support the alias */ 13992073ad33SAndi Kleen list = malloc(sizeof(struct list_head)); 14002073ad33SAndi Kleen if (!list) 14012073ad33SAndi Kleen return -1; 14022073ad33SAndi Kleen INIT_LIST_HEAD(list); 14032073ad33SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 14042073ad33SAndi Kleen struct perf_pmu_alias *alias; 14052073ad33SAndi Kleen 14062073ad33SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 14072073ad33SAndi Kleen if (!strcasecmp(alias->name, str)) { 14082073ad33SAndi Kleen head = malloc(sizeof(struct list_head)); 14092073ad33SAndi Kleen if (!head) 14102073ad33SAndi Kleen return -1; 14112073ad33SAndi Kleen INIT_LIST_HEAD(head); 14122073ad33SAndi Kleen if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 14132073ad33SAndi Kleen str, 1, false, &str, NULL) < 0) 14142073ad33SAndi Kleen return -1; 14152073ad33SAndi Kleen list_add_tail(&term->list, head); 14162073ad33SAndi Kleen 1417c199c11dSAgustin Vega-Frias if (!parse_events_add_pmu(parse_state, list, 14183cdc5c2cSKan Liang pmu->name, head, 14193cdc5c2cSKan Liang true, true)) { 14202073ad33SAndi Kleen pr_debug("%s -> %s/%s/\n", str, 14212073ad33SAndi Kleen pmu->name, alias->str); 14222073ad33SAndi Kleen ok++; 14232073ad33SAndi Kleen } 14242073ad33SAndi Kleen 14252073ad33SAndi Kleen parse_events_terms__delete(head); 14262073ad33SAndi Kleen } 14272073ad33SAndi Kleen } 14282073ad33SAndi Kleen } 14292073ad33SAndi Kleen if (!ok) 14302073ad33SAndi Kleen return -1; 14312073ad33SAndi Kleen *listp = list; 14322073ad33SAndi Kleen return 0; 14332073ad33SAndi Kleen } 14342073ad33SAndi Kleen 14356a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 14366a4bb04cSJiri Olsa char *event_mod) 143789efb029SJiri Olsa { 14386a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 14396a4bb04cSJiri Olsa } 14406a4bb04cSJiri Olsa 14413cdc5c2cSKan Liang /* 14423cdc5c2cSKan Liang * Check if the two uncore PMUs are from the same uncore block 14433cdc5c2cSKan Liang * The format of the uncore PMU name is uncore_#blockname_#pmuidx 14443cdc5c2cSKan Liang */ 14453cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b) 14463cdc5c2cSKan Liang { 14473cdc5c2cSKan Liang char *end_a, *end_b; 14483cdc5c2cSKan Liang 14493cdc5c2cSKan Liang end_a = strrchr(pmu_name_a, '_'); 14503cdc5c2cSKan Liang end_b = strrchr(pmu_name_b, '_'); 14513cdc5c2cSKan Liang 14523cdc5c2cSKan Liang if (!end_a || !end_b) 14533cdc5c2cSKan Liang return false; 14543cdc5c2cSKan Liang 14553cdc5c2cSKan Liang if ((end_a - pmu_name_a) != (end_b - pmu_name_b)) 14563cdc5c2cSKan Liang return false; 14573cdc5c2cSKan Liang 14583cdc5c2cSKan Liang return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0); 14593cdc5c2cSKan Liang } 14603cdc5c2cSKan Liang 14613cdc5c2cSKan Liang static int 14623cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list, 14633cdc5c2cSKan Liang struct parse_events_state *parse_state) 14643cdc5c2cSKan Liang { 146532dcd021SJiri Olsa struct evsel *evsel, *leader; 14663cdc5c2cSKan Liang uintptr_t *leaders; 14673cdc5c2cSKan Liang bool is_leader = true; 14683cdc5c2cSKan Liang int i, nr_pmu = 0, total_members, ret = 0; 14693cdc5c2cSKan Liang 1470b27c4eceSJiri Olsa leader = list_first_entry(list, struct evsel, core.node); 1471b27c4eceSJiri Olsa evsel = list_last_entry(list, struct evsel, core.node); 14723cdc5c2cSKan Liang total_members = evsel->idx - leader->idx + 1; 14733cdc5c2cSKan Liang 14743cdc5c2cSKan Liang leaders = calloc(total_members, sizeof(uintptr_t)); 14753cdc5c2cSKan Liang if (WARN_ON(!leaders)) 14763cdc5c2cSKan Liang return 0; 14773cdc5c2cSKan Liang 14783cdc5c2cSKan Liang /* 14793cdc5c2cSKan Liang * Going through the whole group and doing sanity check. 14803cdc5c2cSKan Liang * All members must use alias, and be from the same uncore block. 14813cdc5c2cSKan Liang * Also, storing the leader events in an array. 14823cdc5c2cSKan Liang */ 14833cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 14843cdc5c2cSKan Liang 14853cdc5c2cSKan Liang /* Only split the uncore group which members use alias */ 14863cdc5c2cSKan Liang if (!evsel->use_uncore_alias) 14873cdc5c2cSKan Liang goto out; 14883cdc5c2cSKan Liang 14893cdc5c2cSKan Liang /* The events must be from the same uncore block */ 14903cdc5c2cSKan Liang if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name)) 14913cdc5c2cSKan Liang goto out; 14923cdc5c2cSKan Liang 14933cdc5c2cSKan Liang if (!is_leader) 14943cdc5c2cSKan Liang continue; 14953cdc5c2cSKan Liang /* 14963cdc5c2cSKan Liang * If the event's PMU name starts to repeat, it must be a new 14973cdc5c2cSKan Liang * event. That can be used to distinguish the leader from 14983cdc5c2cSKan Liang * other members, even they have the same event name. 14993cdc5c2cSKan Liang */ 15003cdc5c2cSKan Liang if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) { 15013cdc5c2cSKan Liang is_leader = false; 15023cdc5c2cSKan Liang continue; 15033cdc5c2cSKan Liang } 15043cdc5c2cSKan Liang /* The name is always alias name */ 15053cdc5c2cSKan Liang WARN_ON(strcmp(leader->name, evsel->name)); 15063cdc5c2cSKan Liang 15073cdc5c2cSKan Liang /* Store the leader event for each PMU */ 15083cdc5c2cSKan Liang leaders[nr_pmu++] = (uintptr_t) evsel; 15093cdc5c2cSKan Liang } 15103cdc5c2cSKan Liang 15113cdc5c2cSKan Liang /* only one event alias */ 15123cdc5c2cSKan Liang if (nr_pmu == total_members) { 15133cdc5c2cSKan Liang parse_state->nr_groups--; 15143cdc5c2cSKan Liang goto handled; 15153cdc5c2cSKan Liang } 15163cdc5c2cSKan Liang 15173cdc5c2cSKan Liang /* 15183cdc5c2cSKan Liang * An uncore event alias is a joint name which means the same event 15193cdc5c2cSKan Liang * runs on all PMUs of a block. 15203cdc5c2cSKan Liang * Perf doesn't support mixed events from different PMUs in the same 15213cdc5c2cSKan Liang * group. The big group has to be split into multiple small groups 15223cdc5c2cSKan Liang * which only include the events from the same PMU. 15233cdc5c2cSKan Liang * 15243cdc5c2cSKan Liang * Here the uncore event aliases must be from the same uncore block. 15253cdc5c2cSKan Liang * The number of PMUs must be same for each alias. The number of new 15263cdc5c2cSKan Liang * small groups equals to the number of PMUs. 15273cdc5c2cSKan Liang * Setting the leader event for corresponding members in each group. 15283cdc5c2cSKan Liang */ 15293cdc5c2cSKan Liang i = 0; 15303cdc5c2cSKan Liang __evlist__for_each_entry(list, evsel) { 15313cdc5c2cSKan Liang if (i >= nr_pmu) 15323cdc5c2cSKan Liang i = 0; 153332dcd021SJiri Olsa evsel->leader = (struct evsel *) leaders[i++]; 15343cdc5c2cSKan Liang } 15353cdc5c2cSKan Liang 15363cdc5c2cSKan Liang /* The number of members and group name are same for each group */ 15373cdc5c2cSKan Liang for (i = 0; i < nr_pmu; i++) { 153832dcd021SJiri Olsa evsel = (struct evsel *) leaders[i]; 15395643b1a5SJiri Olsa evsel->core.nr_members = total_members / nr_pmu; 15403cdc5c2cSKan Liang evsel->group_name = name ? strdup(name) : NULL; 15413cdc5c2cSKan Liang } 15423cdc5c2cSKan Liang 15433cdc5c2cSKan Liang /* Take the new small groups into account */ 15443cdc5c2cSKan Liang parse_state->nr_groups += nr_pmu - 1; 15453cdc5c2cSKan Liang 15463cdc5c2cSKan Liang handled: 15473cdc5c2cSKan Liang ret = 1; 15483cdc5c2cSKan Liang out: 15493cdc5c2cSKan Liang free(leaders); 15503cdc5c2cSKan Liang return ret; 15513cdc5c2cSKan Liang } 15523cdc5c2cSKan Liang 15533cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list, 15543cdc5c2cSKan Liang struct parse_events_state *parse_state) 15556a4bb04cSJiri Olsa { 155632dcd021SJiri Olsa struct evsel *leader; 15576a4bb04cSJiri Olsa 1558854f7363SWang Nan if (list_empty(list)) { 1559854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1560854f7363SWang Nan return; 1561854f7363SWang Nan } 1562854f7363SWang Nan 15633cdc5c2cSKan Liang if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state)) 15643cdc5c2cSKan Liang return; 15653cdc5c2cSKan Liang 156663dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 1567b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 15686a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 156989efb029SJiri Olsa } 157089efb029SJiri Olsa 1571c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 15725d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 15735d7be90eSJiri Olsa struct list_head *list_all) 15745d7be90eSJiri Olsa { 15755d7be90eSJiri Olsa /* 15765d7be90eSJiri Olsa * Called for single event definition. Update the 157789efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 15785d7be90eSJiri Olsa * list, for next event definition. 15795d7be90eSJiri Olsa */ 15805d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 1581b847cbdcSJiri Olsa free(list_event); 15825d7be90eSJiri Olsa } 15835d7be90eSJiri Olsa 1584f5b1135bSJiri Olsa struct event_modifier { 1585f5b1135bSJiri Olsa int eu; 1586f5b1135bSJiri Olsa int ek; 1587f5b1135bSJiri Olsa int eh; 1588f5b1135bSJiri Olsa int eH; 1589f5b1135bSJiri Olsa int eG; 1590a1e12da4SJiri Olsa int eI; 1591f5b1135bSJiri Olsa int precise; 15927f94af7aSJiri Olsa int precise_max; 1593f5b1135bSJiri Olsa int exclude_GH; 15943c176311SJiri Olsa int sample_read; 1595e9a7c414SMichael Ellerman int pinned; 15965a5dfe4bSAndi Kleen int weak; 1597f5b1135bSJiri Olsa }; 159861c45981SPaul Mackerras 1599f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 160032dcd021SJiri Olsa struct evsel *evsel) 1601f5b1135bSJiri Olsa { 16021fc632ceSJiri Olsa int eu = evsel ? evsel->core.attr.exclude_user : 0; 16031fc632ceSJiri Olsa int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 16041fc632ceSJiri Olsa int eh = evsel ? evsel->core.attr.exclude_hv : 0; 16051fc632ceSJiri Olsa int eH = evsel ? evsel->core.attr.exclude_host : 0; 16061fc632ceSJiri Olsa int eG = evsel ? evsel->core.attr.exclude_guest : 0; 16071fc632ceSJiri Olsa int eI = evsel ? evsel->core.attr.exclude_idle : 0; 16081fc632ceSJiri Olsa int precise = evsel ? evsel->core.attr.precise_ip : 0; 16097f94af7aSJiri Olsa int precise_max = 0; 16103c176311SJiri Olsa int sample_read = 0; 16111fc632ceSJiri Olsa int pinned = evsel ? evsel->core.attr.pinned : 0; 1612f5b1135bSJiri Olsa 1613f5b1135bSJiri Olsa int exclude = eu | ek | eh; 1614f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 16155a5dfe4bSAndi Kleen int weak = 0; 1616f5b1135bSJiri Olsa 1617f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 1618ceb53fbfSIngo Molnar 161961c45981SPaul Mackerras while (*str) { 1620ab608344SPeter Zijlstra if (*str == 'u') { 1621ab608344SPeter Zijlstra if (!exclude) 1622ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 162361c45981SPaul Mackerras eu = 0; 1624ab608344SPeter Zijlstra } else if (*str == 'k') { 1625ab608344SPeter Zijlstra if (!exclude) 1626ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 162761c45981SPaul Mackerras ek = 0; 1628ab608344SPeter Zijlstra } else if (*str == 'h') { 1629ab608344SPeter Zijlstra if (!exclude) 1630ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 163161c45981SPaul Mackerras eh = 0; 163299320cc8SJoerg Roedel } else if (*str == 'G') { 163399320cc8SJoerg Roedel if (!exclude_GH) 163499320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 163599320cc8SJoerg Roedel eG = 0; 163699320cc8SJoerg Roedel } else if (*str == 'H') { 163799320cc8SJoerg Roedel if (!exclude_GH) 163899320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 163999320cc8SJoerg Roedel eH = 0; 1640a1e12da4SJiri Olsa } else if (*str == 'I') { 1641a1e12da4SJiri Olsa eI = 1; 1642ab608344SPeter Zijlstra } else if (*str == 'p') { 1643ab608344SPeter Zijlstra precise++; 16441342798cSDavid Ahern /* use of precise requires exclude_guest */ 16451342798cSDavid Ahern if (!exclude_GH) 16461342798cSDavid Ahern eG = 1; 16477f94af7aSJiri Olsa } else if (*str == 'P') { 16487f94af7aSJiri Olsa precise_max = 1; 16493c176311SJiri Olsa } else if (*str == 'S') { 16503c176311SJiri Olsa sample_read = 1; 1651e9a7c414SMichael Ellerman } else if (*str == 'D') { 1652e9a7c414SMichael Ellerman pinned = 1; 16535a5dfe4bSAndi Kleen } else if (*str == 'W') { 16545a5dfe4bSAndi Kleen weak = 1; 1655ab608344SPeter Zijlstra } else 165661c45981SPaul Mackerras break; 1657ab608344SPeter Zijlstra 165861c45981SPaul Mackerras ++str; 165961c45981SPaul Mackerras } 166074d5b588SJaswinder Singh Rajput 166186470930SIngo Molnar /* 166289812fc8SJiri Olsa * precise ip: 166389812fc8SJiri Olsa * 166489812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 166589812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 166689812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 166789812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 166889812fc8SJiri Olsa * 166989812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 167086470930SIngo Molnar */ 167189812fc8SJiri Olsa if (precise > 3) 167289812fc8SJiri Olsa return -EINVAL; 167386470930SIngo Molnar 1674f5b1135bSJiri Olsa mod->eu = eu; 1675f5b1135bSJiri Olsa mod->ek = ek; 1676f5b1135bSJiri Olsa mod->eh = eh; 1677f5b1135bSJiri Olsa mod->eH = eH; 1678f5b1135bSJiri Olsa mod->eG = eG; 1679a1e12da4SJiri Olsa mod->eI = eI; 1680f5b1135bSJiri Olsa mod->precise = precise; 16817f94af7aSJiri Olsa mod->precise_max = precise_max; 1682f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 16833c176311SJiri Olsa mod->sample_read = sample_read; 1684e9a7c414SMichael Ellerman mod->pinned = pinned; 16855a5dfe4bSAndi Kleen mod->weak = weak; 1686e9a7c414SMichael Ellerman 1687f5b1135bSJiri Olsa return 0; 1688f5b1135bSJiri Olsa } 1689f5b1135bSJiri Olsa 1690534123f4SJiri Olsa /* 1691534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 1692534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 1693534123f4SJiri Olsa */ 1694534123f4SJiri Olsa static int check_modifier(char *str) 1695534123f4SJiri Olsa { 1696534123f4SJiri Olsa char *p = str; 1697534123f4SJiri Olsa 1698534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 16995a5dfe4bSAndi Kleen if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1)) 1700534123f4SJiri Olsa return -1; 1701534123f4SJiri Olsa 1702534123f4SJiri Olsa while (*p) { 1703534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 1704534123f4SJiri Olsa return -1; 1705534123f4SJiri Olsa p++; 1706534123f4SJiri Olsa } 1707534123f4SJiri Olsa 1708534123f4SJiri Olsa return 0; 1709534123f4SJiri Olsa } 1710534123f4SJiri Olsa 1711f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1712f5b1135bSJiri Olsa { 171332dcd021SJiri Olsa struct evsel *evsel; 1714f5b1135bSJiri Olsa struct event_modifier mod; 1715f5b1135bSJiri Olsa 1716f5b1135bSJiri Olsa if (str == NULL) 1717f5b1135bSJiri Olsa return 0; 1718f5b1135bSJiri Olsa 1719534123f4SJiri Olsa if (check_modifier(str)) 1720534123f4SJiri Olsa return -EINVAL; 1721534123f4SJiri Olsa 1722f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 1723f5b1135bSJiri Olsa return -EINVAL; 1724f5b1135bSJiri Olsa 1725e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1726f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1727f5b1135bSJiri Olsa return -EINVAL; 1728f5b1135bSJiri Olsa 17291fc632ceSJiri Olsa evsel->core.attr.exclude_user = mod.eu; 17301fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = mod.ek; 17311fc632ceSJiri Olsa evsel->core.attr.exclude_hv = mod.eh; 17321fc632ceSJiri Olsa evsel->core.attr.precise_ip = mod.precise; 17331fc632ceSJiri Olsa evsel->core.attr.exclude_host = mod.eH; 17341fc632ceSJiri Olsa evsel->core.attr.exclude_guest = mod.eG; 17351fc632ceSJiri Olsa evsel->core.attr.exclude_idle = mod.eI; 1736f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 17373c176311SJiri Olsa evsel->sample_read = mod.sample_read; 17387f94af7aSJiri Olsa evsel->precise_max = mod.precise_max; 17395a5dfe4bSAndi Kleen evsel->weak_group = mod.weak; 1740e9a7c414SMichael Ellerman 1741e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 17421fc632ceSJiri Olsa evsel->core.attr.pinned = mod.pinned; 1743ceb53fbfSIngo Molnar } 174486470930SIngo Molnar 174589812fc8SJiri Olsa return 0; 174686470930SIngo Molnar } 174786470930SIngo Molnar 1748ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1749ac2ba9f3SRobert Richter { 175032dcd021SJiri Olsa struct evsel *evsel; 1751ac2ba9f3SRobert Richter 1752e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 1753ac2ba9f3SRobert Richter if (!evsel->name) 1754ac2ba9f3SRobert Richter evsel->name = strdup(name); 1755ac2ba9f3SRobert Richter } 1756ac2ba9f3SRobert Richter 1757ac2ba9f3SRobert Richter return 0; 1758ac2ba9f3SRobert Richter } 1759ac2ba9f3SRobert Richter 1760dcb4e102SKan Liang static int 1761dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1762dcb4e102SKan Liang { 1763dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1764dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1765dcb4e102SKan Liang 1766e312bcf1SAndi Kleen return strcasecmp(pmu1->symbol, pmu2->symbol); 1767dcb4e102SKan Liang } 1768dcb4e102SKan Liang 1769dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1770dcb4e102SKan Liang { 1771dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1772dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1773dcb4e102SKan Liang int i; 1774dcb4e102SKan Liang 1775dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1776dcb4e102SKan Liang p = perf_pmu_events_list + i; 1777360e071bSTaeung Song zfree(&p->symbol); 1778dcb4e102SKan Liang } 1779506fde11STaeung Song zfree(&perf_pmu_events_list); 1780dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1781dcb4e102SKan Liang } 1782dcb4e102SKan Liang } 1783dcb4e102SKan Liang 1784dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1785dcb4e102SKan Liang do { \ 1786dcb4e102SKan Liang p->symbol = str; \ 1787dcb4e102SKan Liang if (!p->symbol) \ 1788dcb4e102SKan Liang goto err; \ 1789dcb4e102SKan Liang p->type = stype; \ 1790dcb4e102SKan Liang } while (0) 1791dcb4e102SKan Liang 1792dcb4e102SKan Liang /* 1793dcb4e102SKan Liang * Read the pmu events list from sysfs 1794dcb4e102SKan Liang * Save it into perf_pmu_events_list 1795dcb4e102SKan Liang */ 1796dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1797dcb4e102SKan Liang { 1798dcb4e102SKan Liang 1799dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1800dcb4e102SKan Liang struct perf_pmu_alias *alias; 1801dcb4e102SKan Liang int len = 0; 1802dcb4e102SKan Liang 1803231bb2aaSAndi Kleen pmu = NULL; 1804231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1805dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1806dcb4e102SKan Liang if (strchr(alias->name, '-')) 1807dcb4e102SKan Liang len++; 1808dcb4e102SKan Liang len++; 1809dcb4e102SKan Liang } 1810231bb2aaSAndi Kleen } 1811231bb2aaSAndi Kleen 1812231bb2aaSAndi Kleen if (len == 0) { 1813231bb2aaSAndi Kleen perf_pmu_events_list_num = -1; 1814231bb2aaSAndi Kleen return; 1815231bb2aaSAndi Kleen } 1816dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1817dcb4e102SKan Liang if (!perf_pmu_events_list) 1818dcb4e102SKan Liang return; 1819dcb4e102SKan Liang perf_pmu_events_list_num = len; 1820dcb4e102SKan Liang 1821dcb4e102SKan Liang len = 0; 1822231bb2aaSAndi Kleen pmu = NULL; 1823231bb2aaSAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1824dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1825dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1826dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1827dcb4e102SKan Liang 1828dcb4e102SKan Liang if (tmp != NULL) { 1829dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1830dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1831dcb4e102SKan Liang p++; 1832dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1833dcb4e102SKan Liang len += 2; 1834dcb4e102SKan Liang } else { 1835dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1836dcb4e102SKan Liang len++; 1837dcb4e102SKan Liang } 1838dcb4e102SKan Liang } 1839231bb2aaSAndi Kleen } 1840dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1841dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1842dcb4e102SKan Liang 1843dcb4e102SKan Liang return; 1844dcb4e102SKan Liang err: 1845dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1846dcb4e102SKan Liang } 1847dcb4e102SKan Liang 1848dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1849dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1850dcb4e102SKan Liang { 1851dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1852dcb4e102SKan Liang 1853dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1854dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1855dcb4e102SKan Liang perf_pmu__parse_init(); 1856dcb4e102SKan Liang /* 1857dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1858dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1859dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1860dcb4e102SKan Liang */ 1861dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1862dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1863dcb4e102SKan Liang 1864dcb4e102SKan Liang p.symbol = strdup(name); 1865dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1866dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1867dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1868360e071bSTaeung Song zfree(&p.symbol); 1869dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1870dcb4e102SKan Liang } 1871dcb4e102SKan Liang 18725d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token) 1873ac20de6fSZheng Yan { 1874ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1875ac20de6fSZheng Yan void *scanner; 1876ac20de6fSZheng Yan int ret; 1877ac20de6fSZheng Yan 187890e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1879ac20de6fSZheng Yan if (ret) 1880ac20de6fSZheng Yan return ret; 1881ac20de6fSZheng Yan 1882ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1883ac20de6fSZheng Yan 1884ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1885ac20de6fSZheng Yan parse_events_debug = 1; 1886ac20de6fSZheng Yan #endif 18875d9cdc11SArnaldo Carvalho de Melo ret = parse_events_parse(parse_state, scanner); 1888ac20de6fSZheng Yan 1889ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1890ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1891ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1892ac20de6fSZheng Yan return ret; 1893ac20de6fSZheng Yan } 1894ac20de6fSZheng Yan 189590e2b22dSJiri Olsa /* 189690e2b22dSJiri Olsa * parse event config string, return a list of event terms. 189790e2b22dSJiri Olsa */ 189890e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 189990e2b22dSJiri Olsa { 19005d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 190190e2b22dSJiri Olsa .terms = NULL, 190290e2b22dSJiri Olsa }; 190390e2b22dSJiri Olsa int ret; 190490e2b22dSJiri Olsa 19055d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_TERMS); 190690e2b22dSJiri Olsa if (!ret) { 19075d9cdc11SArnaldo Carvalho de Melo list_splice(parse_state.terms, terms); 19085d9cdc11SArnaldo Carvalho de Melo zfree(&parse_state.terms); 190990e2b22dSJiri Olsa return 0; 191090e2b22dSJiri Olsa } 191190e2b22dSJiri Olsa 19125d9cdc11SArnaldo Carvalho de Melo parse_events_terms__delete(parse_state.terms); 191390e2b22dSJiri Olsa return ret; 191490e2b22dSJiri Olsa } 191590e2b22dSJiri Olsa 191663503dbaSJiri Olsa int parse_events(struct evlist *evlist, const char *str, 1917b39b8393SJiri Olsa struct parse_events_error *err) 191886470930SIngo Molnar { 19195d9cdc11SArnaldo Carvalho de Melo struct parse_events_state parse_state = { 19205d9cdc11SArnaldo Carvalho de Melo .list = LIST_HEAD_INIT(parse_state.list), 19216484d2f9SJiri Olsa .idx = evlist->core.nr_entries, 1922b39b8393SJiri Olsa .error = err, 19237630b3e2SWang Nan .evlist = evlist, 192446010ab2SJiri Olsa }; 192546010ab2SJiri Olsa int ret; 192686470930SIngo Molnar 19275d9cdc11SArnaldo Carvalho de Melo ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS); 1928dcb4e102SKan Liang perf_pmu__parse_cleanup(); 192989812fc8SJiri Olsa if (!ret) { 193032dcd021SJiri Olsa struct evsel *last; 193115bfd2ccSWang Nan 19325d9cdc11SArnaldo Carvalho de Melo if (list_empty(&parse_state.list)) { 1933c23080a6SArnaldo Carvalho de Melo WARN_ONCE(true, "WARNING: event parser found nothing\n"); 1934854f7363SWang Nan return -1; 1935854f7363SWang Nan } 1936854f7363SWang Nan 19375d9cdc11SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &parse_state.list); 19385d9cdc11SArnaldo Carvalho de Melo evlist->nr_groups += parse_state.nr_groups; 193915bfd2ccSWang Nan last = perf_evlist__last(evlist); 194015bfd2ccSWang Nan last->cmdline_group_boundary = true; 194115bfd2ccSWang Nan 194286470930SIngo Molnar return 0; 194386470930SIngo Molnar } 194486470930SIngo Molnar 19455d7be90eSJiri Olsa /* 19465d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 1947c12995a5SJiri Olsa * Both call evlist__delete in case of error, so we dont 19485d7be90eSJiri Olsa * need to bother. 19495d7be90eSJiri Olsa */ 195089812fc8SJiri Olsa return ret; 195189812fc8SJiri Olsa } 195289812fc8SJiri Olsa 1953b39b8393SJiri Olsa #define MAX_WIDTH 1000 1954b39b8393SJiri Olsa static int get_term_width(void) 1955b39b8393SJiri Olsa { 1956b39b8393SJiri Olsa struct winsize ws; 1957b39b8393SJiri Olsa 1958b39b8393SJiri Olsa get_term_dimensions(&ws); 1959b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1960b39b8393SJiri Olsa } 1961b39b8393SJiri Olsa 1962333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err, 1963b39b8393SJiri Olsa const char *event) 1964b39b8393SJiri Olsa { 1965b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1966b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1967b39b8393SJiri Olsa char *buf = (char *) event; 1968b39b8393SJiri Olsa int idx = 0; 1969b39b8393SJiri Olsa 1970b39b8393SJiri Olsa if (err->str) { 1971b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1972b39b8393SJiri Olsa int width = get_term_width() - 2; 1973b39b8393SJiri Olsa int len_event = strlen(event); 1974b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1975b39b8393SJiri Olsa 1976b39b8393SJiri Olsa /* 1977b39b8393SJiri Olsa * Maximum error index indent, we will cut 1978b39b8393SJiri Olsa * the event string if it's bigger. 1979b39b8393SJiri Olsa */ 1980141b2d31SAdrian Hunter int max_err_idx = 13; 1981b39b8393SJiri Olsa 1982b39b8393SJiri Olsa /* 1983b39b8393SJiri Olsa * Let's be specific with the message when 1984b39b8393SJiri Olsa * we have the precise error. 1985b39b8393SJiri Olsa */ 1986b39b8393SJiri Olsa str = "event syntax error: "; 1987b39b8393SJiri Olsa len_str = strlen(str); 1988b39b8393SJiri Olsa max_len = width - len_str; 1989b39b8393SJiri Olsa 1990b39b8393SJiri Olsa buf = _buf; 1991b39b8393SJiri Olsa 1992bd1a0be5SAdam Buchbinder /* We're cutting from the beginning. */ 1993b39b8393SJiri Olsa if (err->idx > max_err_idx) 1994b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1995b39b8393SJiri Olsa 1996b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1997b39b8393SJiri Olsa 1998b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1999b39b8393SJiri Olsa if (cut) 2000b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 2001b39b8393SJiri Olsa 2002b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 2003b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 2004b39b8393SJiri Olsa buf[max_len] = 0; 2005b39b8393SJiri Olsa } 2006b39b8393SJiri Olsa 2007b39b8393SJiri Olsa idx = len_str + err->idx - cut; 2008b39b8393SJiri Olsa } 2009b39b8393SJiri Olsa 2010b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 2011b39b8393SJiri Olsa if (idx) { 2012b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 2013b39b8393SJiri Olsa if (err->help) 2014b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 2015360e071bSTaeung Song zfree(&err->str); 2016360e071bSTaeung Song zfree(&err->help); 2017b39b8393SJiri Olsa } 2018b39b8393SJiri Olsa } 2019b39b8393SJiri Olsa 2020b39b8393SJiri Olsa #undef MAX_WIDTH 2021b39b8393SJiri Olsa 2022f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 20231d037ca1SIrina Tirdea int unset __maybe_unused) 2024f120f9d5SJiri Olsa { 202563503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 2026b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 2027b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 20289175ce1fSAndi Kleen 2029333b5665SAndi Kleen if (ret) { 2030b39b8393SJiri Olsa parse_events_print_error(&err, str); 2031333b5665SAndi Kleen fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2032333b5665SAndi Kleen } 2033b39b8393SJiri Olsa 20349175ce1fSAndi Kleen return ret; 2035f120f9d5SJiri Olsa } 2036f120f9d5SJiri Olsa 20374ba1faa1SWang Nan static int 203863503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist, 203932dcd021SJiri Olsa int (*func)(struct evsel *evsel, 20404ba1faa1SWang Nan const void *arg), 20414ba1faa1SWang Nan const void *arg) 2042c171b552SLi Zefan { 204332dcd021SJiri Olsa struct evsel *last = NULL; 20444ba1faa1SWang Nan int err; 2045c171b552SLi Zefan 2046854f7363SWang Nan /* 2047854f7363SWang Nan * Don't return when list_empty, give func a chance to report 2048854f7363SWang Nan * error when it found last == NULL. 2049854f7363SWang Nan * 2050854f7363SWang Nan * So no need to WARN here, let *func do this. 2051854f7363SWang Nan */ 20526484d2f9SJiri Olsa if (evlist->core.nr_entries > 0) 20530c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 205469aad6f1SArnaldo Carvalho de Melo 205515bfd2ccSWang Nan do { 20564ba1faa1SWang Nan err = (*func)(last, arg); 20574ba1faa1SWang Nan if (err) 2058c171b552SLi Zefan return -1; 20594ba1faa1SWang Nan if (!last) 20604ba1faa1SWang Nan return 0; 2061c171b552SLi Zefan 2062ce9036a6SJiri Olsa if (last->core.node.prev == &evlist->core.entries) 206315bfd2ccSWang Nan return 0; 2064b27c4eceSJiri Olsa last = list_entry(last->core.node.prev, struct evsel, core.node); 206515bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 206615bfd2ccSWang Nan 2067c171b552SLi Zefan return 0; 2068c171b552SLi Zefan } 2069c171b552SLi Zefan 207032dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg) 20714ba1faa1SWang Nan { 20724ba1faa1SWang Nan const char *str = arg; 20731e857484SMathieu Poirier bool found = false; 20741e857484SMathieu Poirier int nr_addr_filters = 0; 20751e857484SMathieu Poirier struct perf_pmu *pmu = NULL; 20764ba1faa1SWang Nan 207749836f78SJack Henschel if (evsel == NULL) { 207849836f78SJack Henschel fprintf(stderr, 207949836f78SJack Henschel "--filter option should follow a -e tracepoint or HW tracer option\n"); 208049836f78SJack Henschel return -1; 208149836f78SJack Henschel } 20824ba1faa1SWang Nan 20831fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 20843541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, str) < 0) { 20854ba1faa1SWang Nan fprintf(stderr, 20864ba1faa1SWang Nan "not enough memory to hold filter string\n"); 20874ba1faa1SWang Nan return -1; 20884ba1faa1SWang Nan } 20894ba1faa1SWang Nan 20904ba1faa1SWang Nan return 0; 20914ba1faa1SWang Nan } 20924ba1faa1SWang Nan 20931e857484SMathieu Poirier while ((pmu = perf_pmu__scan(pmu)) != NULL) 20941fc632ceSJiri Olsa if (pmu->type == evsel->core.attr.type) { 20951e857484SMathieu Poirier found = true; 20961e857484SMathieu Poirier break; 20971e857484SMathieu Poirier } 20981e857484SMathieu Poirier 20991e857484SMathieu Poirier if (found) 21001e857484SMathieu Poirier perf_pmu__scan_file(pmu, "nr_addr_filters", 21011e857484SMathieu Poirier "%d", &nr_addr_filters); 21021e857484SMathieu Poirier 210349836f78SJack Henschel if (!nr_addr_filters) { 210449836f78SJack Henschel fprintf(stderr, 210549836f78SJack Henschel "This CPU does not support address filtering\n"); 210649836f78SJack Henschel return -1; 210749836f78SJack Henschel } 21081e857484SMathieu Poirier 21091e857484SMathieu Poirier if (perf_evsel__append_addr_filter(evsel, str) < 0) { 21101e857484SMathieu Poirier fprintf(stderr, 21111e857484SMathieu Poirier "not enough memory to hold filter string\n"); 21121e857484SMathieu Poirier return -1; 21131e857484SMathieu Poirier } 21141e857484SMathieu Poirier 21151e857484SMathieu Poirier return 0; 21161e857484SMathieu Poirier } 21171e857484SMathieu Poirier 21184ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 21194ba1faa1SWang Nan int unset __maybe_unused) 21204ba1faa1SWang Nan { 212163503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 21224ba1faa1SWang Nan 21234ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 21244ba1faa1SWang Nan (const void *)str); 21254ba1faa1SWang Nan } 21264ba1faa1SWang Nan 212732dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel, 21284ba1faa1SWang Nan const void *arg __maybe_unused) 21294ba1faa1SWang Nan { 21304ba1faa1SWang Nan char new_filter[64]; 21314ba1faa1SWang Nan 21321fc632ceSJiri Olsa if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 21334ba1faa1SWang Nan fprintf(stderr, 21344ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 21354ba1faa1SWang Nan return -1; 21364ba1faa1SWang Nan } 21374ba1faa1SWang Nan 21384ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 21394ba1faa1SWang Nan 21403541c034SMathieu Poirier if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) { 21414ba1faa1SWang Nan fprintf(stderr, 21424ba1faa1SWang Nan "not enough memory to hold filter string\n"); 21434ba1faa1SWang Nan return -1; 21444ba1faa1SWang Nan } 21454ba1faa1SWang Nan 21464ba1faa1SWang Nan return 0; 21474ba1faa1SWang Nan } 21484ba1faa1SWang Nan 21494ba1faa1SWang Nan int exclude_perf(const struct option *opt, 21504ba1faa1SWang Nan const char *arg __maybe_unused, 21514ba1faa1SWang Nan int unset __maybe_unused) 21524ba1faa1SWang Nan { 215363503dbaSJiri Olsa struct evlist *evlist = *(struct evlist **)opt->value; 21544ba1faa1SWang Nan 21554ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 21564ba1faa1SWang Nan NULL); 21574ba1faa1SWang Nan } 21584ba1faa1SWang Nan 215986470930SIngo Molnar static const char * const event_type_descriptors[] = { 216086470930SIngo Molnar "Hardware event", 216186470930SIngo Molnar "Software event", 216286470930SIngo Molnar "Tracepoint event", 216386470930SIngo Molnar "Hardware cache event", 216441bdcb23SLiming Wang "Raw hardware event descriptor", 216541bdcb23SLiming Wang "Hardware breakpoint", 216686470930SIngo Molnar }; 216786470930SIngo Molnar 2168ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 2169ab0e4800SYunlong Song { 2170ab0e4800SYunlong Song const char * const *as = a; 2171ab0e4800SYunlong Song const char * const *bs = b; 2172ab0e4800SYunlong Song 2173ab0e4800SYunlong Song return strcmp(*as, *bs); 2174ab0e4800SYunlong Song } 2175ab0e4800SYunlong Song 217686470930SIngo Molnar /* 2177f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 2178f6bdafefSJason Baron */ 2179f6bdafefSJason Baron 2180a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 2181a3277d2dSFrederic Weisbecker bool name_only) 2182f6bdafefSJason Baron { 2183f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 218422a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 2185f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 218625a7d914SArnaldo Carvalho de Melo char *dir_path; 2187ab0e4800SYunlong Song char **evt_list = NULL; 2188ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 2189ab0e4800SYunlong Song bool evt_num_known = false; 2190f6bdafefSJason Baron 2191ab0e4800SYunlong Song restart: 21927014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 2193f6bdafefSJason Baron if (!sys_dir) 2194725b1368SEric Dumazet return; 2195f6bdafefSJason Baron 2196ab0e4800SYunlong Song if (evt_num_known) { 2197ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2198ab0e4800SYunlong Song if (!evt_list) 2199ab0e4800SYunlong Song goto out_close_sys_dir; 2200ab0e4800SYunlong Song } 2201ab0e4800SYunlong Song 220222a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 2203668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 220422a9f41bSArnaldo Carvalho de Melo !strglobmatch(sys_dirent->d_name, subsys_glob)) 2205668b8788SArnaldo Carvalho de Melo continue; 2206725b1368SEric Dumazet 220725a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 220825a7d914SArnaldo Carvalho de Melo if (!dir_path) 220925a7d914SArnaldo Carvalho de Melo continue; 2210725b1368SEric Dumazet evt_dir = opendir(dir_path); 2211725b1368SEric Dumazet if (!evt_dir) 221225a7d914SArnaldo Carvalho de Melo goto next; 2213725b1368SEric Dumazet 2214c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 2215668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 221622a9f41bSArnaldo Carvalho de Melo !strglobmatch(evt_dirent->d_name, event_glob)) 2217668b8788SArnaldo Carvalho de Melo continue; 2218668b8788SArnaldo Carvalho de Melo 2219ab0e4800SYunlong Song if (!evt_num_known) { 2220ab0e4800SYunlong Song evt_num++; 2221a3277d2dSFrederic Weisbecker continue; 2222a3277d2dSFrederic Weisbecker } 2223a3277d2dSFrederic Weisbecker 2224f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 222522a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 2226ab0e4800SYunlong Song 2227ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 222825a7d914SArnaldo Carvalho de Melo if (evt_list[evt_i] == NULL) { 222925a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2230ab0e4800SYunlong Song goto out_close_evt_dir; 223125a7d914SArnaldo Carvalho de Melo } 2232ab0e4800SYunlong Song evt_i++; 2233f6bdafefSJason Baron } 2234f6bdafefSJason Baron closedir(evt_dir); 223525a7d914SArnaldo Carvalho de Melo next: 223625a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 2237f6bdafefSJason Baron } 2238f6bdafefSJason Baron closedir(sys_dir); 2239ab0e4800SYunlong Song 2240ab0e4800SYunlong Song if (!evt_num_known) { 2241ab0e4800SYunlong Song evt_num_known = true; 2242ab0e4800SYunlong Song goto restart; 2243ab0e4800SYunlong Song } 2244ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2245ab0e4800SYunlong Song evt_i = 0; 2246ab0e4800SYunlong Song while (evt_i < evt_num) { 2247ab0e4800SYunlong Song if (name_only) { 2248ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2249ab0e4800SYunlong Song continue; 2250ab0e4800SYunlong Song } 2251ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2252ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2253ab0e4800SYunlong Song } 2254dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2255ab0e4800SYunlong Song printf("\n"); 2256ab0e4800SYunlong Song 2257ab0e4800SYunlong Song out_free: 2258ab0e4800SYunlong Song evt_num = evt_i; 2259ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2260ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2261ab0e4800SYunlong Song zfree(&evt_list); 2262ab0e4800SYunlong Song return; 2263ab0e4800SYunlong Song 2264ab0e4800SYunlong Song out_close_evt_dir: 2265ab0e4800SYunlong Song closedir(evt_dir); 2266ab0e4800SYunlong Song out_close_sys_dir: 2267ab0e4800SYunlong Song closedir(sys_dir); 2268ab0e4800SYunlong Song 2269ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 2270ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 2271ab0e4800SYunlong Song if (evt_list) 2272ab0e4800SYunlong Song goto out_free; 2273f6bdafefSJason Baron } 2274f6bdafefSJason Baron 2275f6bdafefSJason Baron /* 227620c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 227720c457b8SThomas Renninger */ 227820c457b8SThomas Renninger 227920c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 228020c457b8SThomas Renninger { 228120c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 228222a9f41bSArnaldo Carvalho de Melo struct dirent *sys_dirent, *evt_dirent; 228320c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 228425a7d914SArnaldo Carvalho de Melo char *dir_path; 228520c457b8SThomas Renninger 22867014e0e3SArnaldo Carvalho de Melo sys_dir = tracing_events__opendir(); 228720c457b8SThomas Renninger if (!sys_dir) 228820c457b8SThomas Renninger return 0; 228920c457b8SThomas Renninger 229022a9f41bSArnaldo Carvalho de Melo for_each_subsystem(sys_dir, sys_dirent) { 229125a7d914SArnaldo Carvalho de Melo dir_path = get_events_file(sys_dirent->d_name); 229225a7d914SArnaldo Carvalho de Melo if (!dir_path) 229325a7d914SArnaldo Carvalho de Melo continue; 229420c457b8SThomas Renninger evt_dir = opendir(dir_path); 229520c457b8SThomas Renninger if (!evt_dir) 229625a7d914SArnaldo Carvalho de Melo goto next; 229720c457b8SThomas Renninger 2298c02cab22SArnaldo Carvalho de Melo for_each_event(dir_path, evt_dir, evt_dirent) { 229920c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 230022a9f41bSArnaldo Carvalho de Melo sys_dirent->d_name, evt_dirent->d_name); 230120c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 230220c457b8SThomas Renninger closedir(evt_dir); 230320c457b8SThomas Renninger closedir(sys_dir); 230420c457b8SThomas Renninger return 1; 230520c457b8SThomas Renninger } 230620c457b8SThomas Renninger } 230720c457b8SThomas Renninger closedir(evt_dir); 230825a7d914SArnaldo Carvalho de Melo next: 230925a7d914SArnaldo Carvalho de Melo put_events_file(dir_path); 231020c457b8SThomas Renninger } 231120c457b8SThomas Renninger closedir(sys_dir); 231220c457b8SThomas Renninger return 0; 231320c457b8SThomas Renninger } 231420c457b8SThomas Renninger 2315b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 2316b41f1cecSNamhyung Kim { 2317b41f1cecSNamhyung Kim bool ret = true; 231888fee52eSVince Weaver int open_return; 231932dcd021SJiri Olsa struct evsel *evsel; 2320b41f1cecSNamhyung Kim struct perf_event_attr attr = { 2321b41f1cecSNamhyung Kim .type = type, 2322b41f1cecSNamhyung Kim .config = config, 2323b41f1cecSNamhyung Kim .disabled = 1, 2324b41f1cecSNamhyung Kim }; 23259749b90eSJiri Olsa struct perf_thread_map *tmap = thread_map__new_by_tid(0); 232689896051SArnaldo Carvalho de Melo 232789896051SArnaldo Carvalho de Melo if (tmap == NULL) 232889896051SArnaldo Carvalho de Melo return false; 2329b41f1cecSNamhyung Kim 2330365c3ae7SJiri Olsa evsel = evsel__new(&attr); 2331b41f1cecSNamhyung Kim if (evsel) { 23325972d1e0SJiri Olsa open_return = evsel__open(evsel, NULL, tmap); 233388fee52eSVince Weaver ret = open_return >= 0; 233488fee52eSVince Weaver 233588fee52eSVince Weaver if (open_return == -EACCES) { 233688fee52eSVince Weaver /* 233788fee52eSVince Weaver * This happens if the paranoid value 233888fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 233988fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 234088fee52eSVince Weaver * by default as some ARM machines do not support it. 234188fee52eSVince Weaver * 234288fee52eSVince Weaver */ 23431fc632ceSJiri Olsa evsel->core.attr.exclude_kernel = 1; 23445972d1e0SJiri Olsa ret = evsel__open(evsel, NULL, tmap) >= 0; 234588fee52eSVince Weaver } 23465eb2dd2aSJiri Olsa evsel__delete(evsel); 2347b41f1cecSNamhyung Kim } 2348b41f1cecSNamhyung Kim 23497836e52eSJiri Olsa perf_thread_map__put(tmap); 2350b41f1cecSNamhyung Kim return ret; 2351b41f1cecSNamhyung Kim } 2352b41f1cecSNamhyung Kim 235340218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob, 235440218daeSMasami Hiramatsu bool name_only) 235540218daeSMasami Hiramatsu { 235640218daeSMasami Hiramatsu struct probe_cache *pcache; 235740218daeSMasami Hiramatsu struct probe_cache_entry *ent; 235840218daeSMasami Hiramatsu struct strlist *bidlist, *sdtlist; 235940218daeSMasami Hiramatsu struct strlist_config cfg = {.dont_dupstr = true}; 236040218daeSMasami Hiramatsu struct str_node *nd, *nd2; 236140218daeSMasami Hiramatsu char *buf, *path, *ptr = NULL; 236240218daeSMasami Hiramatsu bool show_detail = false; 236340218daeSMasami Hiramatsu int ret; 236440218daeSMasami Hiramatsu 236540218daeSMasami Hiramatsu sdtlist = strlist__new(NULL, &cfg); 236640218daeSMasami Hiramatsu if (!sdtlist) { 236740218daeSMasami Hiramatsu pr_debug("Failed to allocate new strlist for SDT\n"); 236840218daeSMasami Hiramatsu return; 236940218daeSMasami Hiramatsu } 237040218daeSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 237140218daeSMasami Hiramatsu if (!bidlist) { 237240218daeSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", errno); 237340218daeSMasami Hiramatsu return; 237440218daeSMasami Hiramatsu } 237540218daeSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 2376f045b8c4SKrister Johansen pcache = probe_cache__new(nd->s, NULL); 237740218daeSMasami Hiramatsu if (!pcache) 237840218daeSMasami Hiramatsu continue; 237940218daeSMasami Hiramatsu list_for_each_entry(ent, &pcache->entries, node) { 238040218daeSMasami Hiramatsu if (!ent->sdt) 238140218daeSMasami Hiramatsu continue; 238240218daeSMasami Hiramatsu if (subsys_glob && 238340218daeSMasami Hiramatsu !strglobmatch(ent->pev.group, subsys_glob)) 238440218daeSMasami Hiramatsu continue; 238540218daeSMasami Hiramatsu if (event_glob && 238640218daeSMasami Hiramatsu !strglobmatch(ent->pev.event, event_glob)) 238740218daeSMasami Hiramatsu continue; 238840218daeSMasami Hiramatsu ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 238940218daeSMasami Hiramatsu ent->pev.event, nd->s); 239040218daeSMasami Hiramatsu if (ret > 0) 239140218daeSMasami Hiramatsu strlist__add(sdtlist, buf); 239240218daeSMasami Hiramatsu } 239340218daeSMasami Hiramatsu probe_cache__delete(pcache); 239440218daeSMasami Hiramatsu } 239540218daeSMasami Hiramatsu strlist__delete(bidlist); 239640218daeSMasami Hiramatsu 239740218daeSMasami Hiramatsu strlist__for_each_entry(nd, sdtlist) { 239840218daeSMasami Hiramatsu buf = strchr(nd->s, '@'); 239940218daeSMasami Hiramatsu if (buf) 240040218daeSMasami Hiramatsu *(buf++) = '\0'; 240140218daeSMasami Hiramatsu if (name_only) { 240240218daeSMasami Hiramatsu printf("%s ", nd->s); 240340218daeSMasami Hiramatsu continue; 240440218daeSMasami Hiramatsu } 240540218daeSMasami Hiramatsu nd2 = strlist__next(nd); 240640218daeSMasami Hiramatsu if (nd2) { 240740218daeSMasami Hiramatsu ptr = strchr(nd2->s, '@'); 240840218daeSMasami Hiramatsu if (ptr) 240940218daeSMasami Hiramatsu *ptr = '\0'; 241040218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) == 0) 241140218daeSMasami Hiramatsu show_detail = true; 241240218daeSMasami Hiramatsu } 241340218daeSMasami Hiramatsu if (show_detail) { 241440218daeSMasami Hiramatsu path = build_id_cache__origname(buf); 241540218daeSMasami Hiramatsu ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 241640218daeSMasami Hiramatsu if (ret > 0) { 241740218daeSMasami Hiramatsu printf(" %-50s [%s]\n", buf, "SDT event"); 241840218daeSMasami Hiramatsu free(buf); 241940218daeSMasami Hiramatsu } 24208bde8516SChangbin Du free(path); 242140218daeSMasami Hiramatsu } else 242240218daeSMasami Hiramatsu printf(" %-50s [%s]\n", nd->s, "SDT event"); 242340218daeSMasami Hiramatsu if (nd2) { 242440218daeSMasami Hiramatsu if (strcmp(nd->s, nd2->s) != 0) 242540218daeSMasami Hiramatsu show_detail = false; 242640218daeSMasami Hiramatsu if (ptr) 242740218daeSMasami Hiramatsu *ptr = '@'; 242840218daeSMasami Hiramatsu } 242940218daeSMasami Hiramatsu } 243040218daeSMasami Hiramatsu strlist__delete(sdtlist); 243140218daeSMasami Hiramatsu } 243240218daeSMasami Hiramatsu 2433a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 2434668b8788SArnaldo Carvalho de Melo { 2435ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 24360b668bc9SArnaldo Carvalho de Melo char name[64]; 2437ab0e4800SYunlong Song char **evt_list = NULL; 2438ab0e4800SYunlong Song bool evt_num_known = false; 2439ab0e4800SYunlong Song 2440ab0e4800SYunlong Song restart: 2441ab0e4800SYunlong Song if (evt_num_known) { 2442ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2443ab0e4800SYunlong Song if (!evt_list) 2444ab0e4800SYunlong Song goto out_enomem; 2445ab0e4800SYunlong Song } 2446668b8788SArnaldo Carvalho de Melo 2447668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 2448668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 2449668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 24500b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 2451668b8788SArnaldo Carvalho de Melo continue; 2452668b8788SArnaldo Carvalho de Melo 2453668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 24540b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 24550b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 2456947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 2457668b8788SArnaldo Carvalho de Melo continue; 2458668b8788SArnaldo Carvalho de Melo 2459b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 2460b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 2461b41f1cecSNamhyung Kim continue; 2462b41f1cecSNamhyung Kim 2463ab0e4800SYunlong Song if (!evt_num_known) { 2464ab0e4800SYunlong Song evt_num++; 2465ab0e4800SYunlong Song continue; 2466ab0e4800SYunlong Song } 2467ab0e4800SYunlong Song 2468ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2469ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2470ab0e4800SYunlong Song goto out_enomem; 2471ab0e4800SYunlong Song evt_i++; 2472668b8788SArnaldo Carvalho de Melo } 2473668b8788SArnaldo Carvalho de Melo } 2474668b8788SArnaldo Carvalho de Melo } 2475668b8788SArnaldo Carvalho de Melo 2476ab0e4800SYunlong Song if (!evt_num_known) { 2477ab0e4800SYunlong Song evt_num_known = true; 2478ab0e4800SYunlong Song goto restart; 2479ab0e4800SYunlong Song } 2480ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2481ab0e4800SYunlong Song evt_i = 0; 2482ab0e4800SYunlong Song while (evt_i < evt_num) { 2483ab0e4800SYunlong Song if (name_only) { 2484ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2485ab0e4800SYunlong Song continue; 2486ab0e4800SYunlong Song } 2487ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 2488ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 2489ab0e4800SYunlong Song } 2490dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2491dc098b35SAndi Kleen printf("\n"); 2492ab0e4800SYunlong Song 2493ab0e4800SYunlong Song out_free: 2494ab0e4800SYunlong Song evt_num = evt_i; 2495ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2496ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2497ab0e4800SYunlong Song zfree(&evt_list); 2498ab0e4800SYunlong Song return evt_num; 2499ab0e4800SYunlong Song 2500ab0e4800SYunlong Song out_enomem: 2501ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 2502ab0e4800SYunlong Song if (evt_list) 2503ab0e4800SYunlong Song goto out_free; 2504ab0e4800SYunlong Song return evt_num; 2505668b8788SArnaldo Carvalho de Melo } 2506668b8788SArnaldo Carvalho de Melo 25075e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob, 25085e0861baSAndi Kleen bool name_only) 25095e0861baSAndi Kleen { 25105e0861baSAndi Kleen if (event_glob && !strglobmatch(name, event_glob)) 25115e0861baSAndi Kleen return; 25125e0861baSAndi Kleen if (name_only) 25135e0861baSAndi Kleen printf("%s ", name); 25145e0861baSAndi Kleen else 25155e0861baSAndi Kleen printf(" %-50s [%s]\n", name, "Tool event"); 25165e0861baSAndi Kleen 25175e0861baSAndi Kleen } 25185e0861baSAndi Kleen 25195e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only) 25205e0861baSAndi Kleen { 25215e0861baSAndi Kleen print_tool_event("duration_time", event_glob, name_only); 25225e0861baSAndi Kleen if (pager_in_use()) 25235e0861baSAndi Kleen printf("\n"); 25245e0861baSAndi Kleen } 25255e0861baSAndi Kleen 2526705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 2527a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 2528a3277d2dSFrederic Weisbecker bool name_only) 252986470930SIngo Molnar { 2530ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 2531947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 2532ab0e4800SYunlong Song char **evt_list = NULL; 2533ab0e4800SYunlong Song bool evt_num_known = false; 2534ab0e4800SYunlong Song 2535ab0e4800SYunlong Song restart: 2536ab0e4800SYunlong Song if (evt_num_known) { 2537ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 2538ab0e4800SYunlong Song if (!evt_list) 2539ab0e4800SYunlong Song goto out_enomem; 2540ab0e4800SYunlong Song syms -= max; 2541ab0e4800SYunlong Song } 254286470930SIngo Molnar 25431dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 2544668b8788SArnaldo Carvalho de Melo 2545e37df6c7SArnaldo Carvalho de Melo if (event_glob != NULL && syms->symbol != NULL && 2546668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 2547668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 2548668b8788SArnaldo Carvalho de Melo continue; 254986470930SIngo Molnar 2550b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 2551b41f1cecSNamhyung Kim continue; 2552b41f1cecSNamhyung Kim 2553ab0e4800SYunlong Song if (!evt_num_known) { 2554ab0e4800SYunlong Song evt_num++; 2555a3277d2dSFrederic Weisbecker continue; 2556a3277d2dSFrederic Weisbecker } 2557a3277d2dSFrederic Weisbecker 2558ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 2559947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 256074d5b588SJaswinder Singh Rajput else 2561bd8d57fbSArnaldo Carvalho de Melo strlcpy(name, syms->symbol, MAX_NAME_LEN); 256286470930SIngo Molnar 2563ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 2564ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 2565ab0e4800SYunlong Song goto out_enomem; 2566ab0e4800SYunlong Song evt_i++; 256786470930SIngo Molnar } 256886470930SIngo Molnar 2569ab0e4800SYunlong Song if (!evt_num_known) { 2570ab0e4800SYunlong Song evt_num_known = true; 2571ab0e4800SYunlong Song goto restart; 2572ab0e4800SYunlong Song } 2573ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 2574ab0e4800SYunlong Song evt_i = 0; 2575ab0e4800SYunlong Song while (evt_i < evt_num) { 2576ab0e4800SYunlong Song if (name_only) { 2577ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 2578ab0e4800SYunlong Song continue; 2579ab0e4800SYunlong Song } 2580ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 2581ab0e4800SYunlong Song } 2582dfc431cbSArnaldo Carvalho de Melo if (evt_num && pager_in_use()) 2583689d3018SMarti Raudsepp printf("\n"); 2584ab0e4800SYunlong Song 2585ab0e4800SYunlong Song out_free: 2586ab0e4800SYunlong Song evt_num = evt_i; 2587ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 2588ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 2589ab0e4800SYunlong Song zfree(&evt_list); 2590ab0e4800SYunlong Song return; 2591ab0e4800SYunlong Song 2592ab0e4800SYunlong Song out_enomem: 2593ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 2594ab0e4800SYunlong Song if (evt_list) 2595ab0e4800SYunlong Song goto out_free; 2596668b8788SArnaldo Carvalho de Melo } 25971dc12760SJiri Olsa 25981dc12760SJiri Olsa /* 25991dc12760SJiri Olsa * Print the help text for the event symbols: 26001dc12760SJiri Olsa */ 2601c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag, 2602bf874fcfSAndi Kleen bool long_desc, bool details_flag) 26031dc12760SJiri Olsa { 26041dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 2605a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 26061dc12760SJiri Olsa 26071dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 2608a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 26095e0861baSAndi Kleen print_tool_events(event_glob, name_only); 26101dc12760SJiri Olsa 2611a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 261273c24cb8SJaswinder Singh Rajput 2613bf874fcfSAndi Kleen print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 2614bf874fcfSAndi Kleen details_flag); 2615dc098b35SAndi Kleen 2616668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 2617668b8788SArnaldo Carvalho de Melo return; 261873c24cb8SJaswinder Singh Rajput 2619a3277d2dSFrederic Weisbecker if (!name_only) { 2620947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 26215f537a26SJiri Olsa "rNNN", 26221cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 26235f537a26SJiri Olsa printf(" %-50s [%s]\n", 26245f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 26255f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 2626dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 2627dfc431cbSArnaldo Carvalho de Melo printf(" (see 'man perf-list' on how to encode it)\n\n"); 262886470930SIngo Molnar 2629947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 26303741eb9fSJacob Shin "mem:<addr>[/len][:access]", 263141bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 2632dfc431cbSArnaldo Carvalho de Melo if (pager_in_use()) 26331b290d67SFrederic Weisbecker printf("\n"); 2634a3277d2dSFrederic Weisbecker } 26351b290d67SFrederic Weisbecker 2636a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 263740218daeSMasami Hiramatsu 263840218daeSMasami Hiramatsu print_sdt_events(NULL, NULL, name_only); 263971b0acceSAndi Kleen 264033bbc571SJiri Olsa metricgroup__print(true, true, NULL, name_only, details_flag); 264186470930SIngo Molnar } 26428f707d84SJiri Olsa 26436cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 26448f707d84SJiri Olsa { 264516fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 26468f707d84SJiri Olsa } 26478f707d84SJiri Olsa 264867b49b38SJiri Olsa static int new_term(struct parse_events_term **_term, 264967b49b38SJiri Olsa struct parse_events_term *temp, 265067b49b38SJiri Olsa char *str, u64 num) 26518f707d84SJiri Olsa { 26526cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 26538f707d84SJiri Olsa 265467b49b38SJiri Olsa term = malloc(sizeof(*term)); 26558f707d84SJiri Olsa if (!term) 26568f707d84SJiri Olsa return -ENOMEM; 26578f707d84SJiri Olsa 265867b49b38SJiri Olsa *term = *temp; 26598f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 2660c2f1ceadSAndi Kleen term->weak = false; 26618f707d84SJiri Olsa 266267b49b38SJiri Olsa switch (term->type_val) { 26638f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 26648f707d84SJiri Olsa term->val.num = num; 26658f707d84SJiri Olsa break; 26668f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 26678f707d84SJiri Olsa term->val.str = str; 26688f707d84SJiri Olsa break; 26698f707d84SJiri Olsa default: 26704be8be6bSAdrian Hunter free(term); 26718f707d84SJiri Olsa return -EINVAL; 26728f707d84SJiri Olsa } 26738f707d84SJiri Olsa 26748f707d84SJiri Olsa *_term = term; 26758f707d84SJiri Olsa return 0; 26768f707d84SJiri Olsa } 26778f707d84SJiri Olsa 26786cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 2679cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 268099e7138eSJiri Olsa bool no_value, 2681bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 268216fa7e82SJiri Olsa { 2683bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2684bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2685bb78ce7dSAdrian Hunter 268667b49b38SJiri Olsa struct parse_events_term temp = { 268767b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 268867b49b38SJiri Olsa .type_term = type_term, 268967b49b38SJiri Olsa .config = config, 269099e7138eSJiri Olsa .no_value = no_value, 269167b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 269267b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 269367b49b38SJiri Olsa }; 269467b49b38SJiri Olsa 269567b49b38SJiri Olsa return new_term(term, &temp, NULL, num); 269616fa7e82SJiri Olsa } 269716fa7e82SJiri Olsa 26986cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 2699cecf3a2eSJiri Olsa int type_term, char *config, char *str, 2700bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 270116fa7e82SJiri Olsa { 2702bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 2703bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 2704bb78ce7dSAdrian Hunter 270567b49b38SJiri Olsa struct parse_events_term temp = { 270667b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 270767b49b38SJiri Olsa .type_term = type_term, 270867b49b38SJiri Olsa .config = config, 270967b49b38SJiri Olsa .err_term = loc_term ? loc_term->first_column : 0, 271067b49b38SJiri Olsa .err_val = loc_val ? loc_val->first_column : 0, 271167b49b38SJiri Olsa }; 271267b49b38SJiri Olsa 271367b49b38SJiri Olsa return new_term(term, &temp, str, 0); 271416fa7e82SJiri Olsa } 271516fa7e82SJiri Olsa 27166cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 27171d33d6dcSJiri Olsa char *config, unsigned idx) 27181d33d6dcSJiri Olsa { 27191d33d6dcSJiri Olsa struct event_symbol *sym; 272067b49b38SJiri Olsa struct parse_events_term temp = { 272167b49b38SJiri Olsa .type_val = PARSE_EVENTS__TERM_TYPE_STR, 272267b49b38SJiri Olsa .type_term = PARSE_EVENTS__TERM_TYPE_USER, 272367b49b38SJiri Olsa .config = config ?: (char *) "event", 272467b49b38SJiri Olsa }; 27251d33d6dcSJiri Olsa 27261d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 27271d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 27281d33d6dcSJiri Olsa 272967b49b38SJiri Olsa return new_term(term, &temp, (char *) sym->symbol, 0); 27301d33d6dcSJiri Olsa } 27311d33d6dcSJiri Olsa 27326cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 27336cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 2734a6146d50SZheng Yan { 273567b49b38SJiri Olsa struct parse_events_term temp = { 273667b49b38SJiri Olsa .type_val = term->type_val, 273767b49b38SJiri Olsa .type_term = term->type_term, 273867b49b38SJiri Olsa .config = term->config, 273967b49b38SJiri Olsa .err_term = term->err_term, 274067b49b38SJiri Olsa .err_val = term->err_val, 274167b49b38SJiri Olsa }; 274267b49b38SJiri Olsa 274367b49b38SJiri Olsa return new_term(new, &temp, term->val.str, term->val.num); 2744a6146d50SZheng Yan } 2745a6146d50SZheng Yan 27468255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old, 27478255718fSAndi Kleen struct list_head **new) 27488255718fSAndi Kleen { 27498255718fSAndi Kleen struct parse_events_term *term, *n; 27508255718fSAndi Kleen int ret; 27518255718fSAndi Kleen 27528255718fSAndi Kleen if (!old) { 27538255718fSAndi Kleen *new = NULL; 27548255718fSAndi Kleen return 0; 27558255718fSAndi Kleen } 27568255718fSAndi Kleen 27578255718fSAndi Kleen *new = malloc(sizeof(struct list_head)); 27588255718fSAndi Kleen if (!*new) 27598255718fSAndi Kleen return -ENOMEM; 27608255718fSAndi Kleen INIT_LIST_HEAD(*new); 27618255718fSAndi Kleen 27628255718fSAndi Kleen list_for_each_entry (term, old, list) { 27638255718fSAndi Kleen ret = parse_events_term__clone(&n, term); 27648255718fSAndi Kleen if (ret) 27658255718fSAndi Kleen return ret; 27668255718fSAndi Kleen list_add_tail(&n->list, *new); 27678255718fSAndi Kleen } 27688255718fSAndi Kleen return 0; 27698255718fSAndi Kleen } 27708255718fSAndi Kleen 2771fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms) 27728f707d84SJiri Olsa { 27736cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 27748f707d84SJiri Olsa 2775a8adfcebSWang Nan list_for_each_entry_safe(term, h, terms, list) { 27762d055bf2SWang Nan if (term->array.nr_ranges) 2777360e071bSTaeung Song zfree(&term->array.ranges); 2778a8adfcebSWang Nan list_del_init(&term->list); 27798f707d84SJiri Olsa free(term); 27808f707d84SJiri Olsa } 2781a8adfcebSWang Nan } 2782b39b8393SJiri Olsa 27832146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms) 2784fc0a2c1dSArnaldo Carvalho de Melo { 27852146afc6SArnaldo Carvalho de Melo if (!terms) 27862146afc6SArnaldo Carvalho de Melo return; 2787fc0a2c1dSArnaldo Carvalho de Melo parse_events_terms__purge(terms); 2788d20a5f2bSWang Nan free(terms); 2789fc0a2c1dSArnaldo Carvalho de Melo } 2790fc0a2c1dSArnaldo Carvalho de Melo 27912d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a) 27922d055bf2SWang Nan { 2793360e071bSTaeung Song zfree(&a->ranges); 27942d055bf2SWang Nan } 27952d055bf2SWang Nan 27965d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state, 2797b39b8393SJiri Olsa int idx, const char *str) 2798b39b8393SJiri Olsa { 27995d9cdc11SArnaldo Carvalho de Melo struct parse_events_error *err = parse_state->error; 2800b39b8393SJiri Olsa 2801a6ced2beSAdrian Hunter if (!err) 2802a6ced2beSAdrian Hunter return; 2803b39b8393SJiri Olsa err->idx = idx; 2804b39b8393SJiri Olsa err->str = strdup(str); 2805b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 2806b39b8393SJiri Olsa } 2807ffeb883eSHe Kuang 280817cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz) 280917cb5f84SWang Nan { 281017cb5f84SWang Nan int i; 281117cb5f84SWang Nan bool first = true; 281217cb5f84SWang Nan 281317cb5f84SWang Nan buf[0] = '\0'; 281417cb5f84SWang Nan for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 281517cb5f84SWang Nan const char *name = config_term_names[i]; 281617cb5f84SWang Nan 28171669e509SWang Nan if (!config_term_avail(i, NULL)) 28181669e509SWang Nan continue; 281917cb5f84SWang Nan if (!name) 282017cb5f84SWang Nan continue; 282117cb5f84SWang Nan if (name[0] == '<') 282217cb5f84SWang Nan continue; 282317cb5f84SWang Nan 282417cb5f84SWang Nan if (strlen(buf) + strlen(name) + 2 >= buf_sz) 282517cb5f84SWang Nan return; 282617cb5f84SWang Nan 282717cb5f84SWang Nan if (!first) 282817cb5f84SWang Nan strcat(buf, ","); 282917cb5f84SWang Nan else 283017cb5f84SWang Nan first = false; 283117cb5f84SWang Nan strcat(buf, name); 283217cb5f84SWang Nan } 283317cb5f84SWang Nan } 283417cb5f84SWang Nan 2835ffeb883eSHe Kuang /* 2836ffeb883eSHe Kuang * Return string contains valid config terms of an event. 2837ffeb883eSHe Kuang * @additional_terms: For terms such as PMU sysfs terms. 2838ffeb883eSHe Kuang */ 2839ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms) 2840ffeb883eSHe Kuang { 2841ffeb883eSHe Kuang char *str; 2842626a6b78SWang Nan /* "no-overwrite" is the longest name */ 284317cb5f84SWang Nan char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2844626a6b78SWang Nan (sizeof("no-overwrite") - 1)]; 2845ffeb883eSHe Kuang 284617cb5f84SWang Nan config_terms_list(static_terms, sizeof(static_terms)); 2847ffeb883eSHe Kuang /* valid terms */ 2848ffeb883eSHe Kuang if (additional_terms) { 284926dee028SWang Nan if (asprintf(&str, "valid terms: %s,%s", 285026dee028SWang Nan additional_terms, static_terms) < 0) 2851ffeb883eSHe Kuang goto fail; 2852ffeb883eSHe Kuang } else { 285326dee028SWang Nan if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2854ffeb883eSHe Kuang goto fail; 2855ffeb883eSHe Kuang } 2856ffeb883eSHe Kuang return str; 2857ffeb883eSHe Kuang 2858ffeb883eSHe Kuang fail: 2859ffeb883eSHe Kuang return NULL; 2860ffeb883eSHe Kuang } 2861