1d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 28dd2a131SJiri Olsa #include <linux/err.h> 386470930SIngo Molnar #include "util.h" 46b58e7f1SUlrich Drepper #include "../perf.h" 5361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 669aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 786470930SIngo Molnar #include "parse-options.h" 886470930SIngo Molnar #include "parse-events.h" 986470930SIngo Molnar #include "exec_cmd.h" 1042f60c2dSKan Liang #include "string.h" 115aab621bSArnaldo Carvalho de Melo #include "symbol.h" 125beeded1SJason Baron #include "cache.h" 138755a8f2SArjan van de Ven #include "header.h" 146e81c74cSMasami Hiramatsu #include "debug.h" 15592d5a6bSJiri Olsa #include <api/fs/tracing_path.h> 16ac20de6fSZheng Yan #include "parse-events-bison.h" 1790e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 1889812fc8SJiri Olsa #include "parse-events-flex.h" 195f537a26SJiri Olsa #include "pmu.h" 20b41f1cecSNamhyung Kim #include "thread_map.h" 21f30a79b0SJiri Olsa #include "cpumap.h" 22b39b8393SJiri Olsa #include "asm/bug.h" 2389812fc8SJiri Olsa 2489812fc8SJiri Olsa #define MAX_NAME_LEN 100 2586470930SIngo Molnar 2682ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 2782ba1f2fSJiri Olsa extern int parse_events_debug; 2882ba1f2fSJiri Olsa #endif 29ac20de6fSZheng Yan int parse_events_parse(void *data, void *scanner); 30bcd3279fSFrederic Weisbecker 31dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 32dcb4e102SKan Liang /* 33dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 34dcb4e102SKan Liang * 0 means not initialized and ready to init 35dcb4e102SKan Liang * -1 means failed to init, don't try anymore 36dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 37dcb4e102SKan Liang */ 38dcb4e102SKan Liang static int perf_pmu_events_list_num; 39dcb4e102SKan Liang 40705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 411dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 421dc12760SJiri Olsa .symbol = "cpu-cycles", 431dc12760SJiri Olsa .alias = "cycles", 441dc12760SJiri Olsa }, 451dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 461dc12760SJiri Olsa .symbol = "instructions", 471dc12760SJiri Olsa .alias = "", 481dc12760SJiri Olsa }, 491dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 501dc12760SJiri Olsa .symbol = "cache-references", 511dc12760SJiri Olsa .alias = "", 521dc12760SJiri Olsa }, 531dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 541dc12760SJiri Olsa .symbol = "cache-misses", 551dc12760SJiri Olsa .alias = "", 561dc12760SJiri Olsa }, 571dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 581dc12760SJiri Olsa .symbol = "branch-instructions", 591dc12760SJiri Olsa .alias = "branches", 601dc12760SJiri Olsa }, 611dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 621dc12760SJiri Olsa .symbol = "branch-misses", 631dc12760SJiri Olsa .alias = "", 641dc12760SJiri Olsa }, 651dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 661dc12760SJiri Olsa .symbol = "bus-cycles", 671dc12760SJiri Olsa .alias = "", 681dc12760SJiri Olsa }, 691dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 701dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 711dc12760SJiri Olsa .alias = "idle-cycles-frontend", 721dc12760SJiri Olsa }, 731dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 741dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 751dc12760SJiri Olsa .alias = "idle-cycles-backend", 761dc12760SJiri Olsa }, 771dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 781dc12760SJiri Olsa .symbol = "ref-cycles", 791dc12760SJiri Olsa .alias = "", 801dc12760SJiri Olsa }, 811dc12760SJiri Olsa }; 8286470930SIngo Molnar 83705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 841dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 851dc12760SJiri Olsa .symbol = "cpu-clock", 861dc12760SJiri Olsa .alias = "", 871dc12760SJiri Olsa }, 881dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 891dc12760SJiri Olsa .symbol = "task-clock", 901dc12760SJiri Olsa .alias = "", 911dc12760SJiri Olsa }, 921dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 931dc12760SJiri Olsa .symbol = "page-faults", 941dc12760SJiri Olsa .alias = "faults", 951dc12760SJiri Olsa }, 961dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 971dc12760SJiri Olsa .symbol = "context-switches", 981dc12760SJiri Olsa .alias = "cs", 991dc12760SJiri Olsa }, 1001dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1011dc12760SJiri Olsa .symbol = "cpu-migrations", 1021dc12760SJiri Olsa .alias = "migrations", 1031dc12760SJiri Olsa }, 1041dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1051dc12760SJiri Olsa .symbol = "minor-faults", 1061dc12760SJiri Olsa .alias = "", 1071dc12760SJiri Olsa }, 1081dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1091dc12760SJiri Olsa .symbol = "major-faults", 1101dc12760SJiri Olsa .alias = "", 1111dc12760SJiri Olsa }, 1121dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1131dc12760SJiri Olsa .symbol = "alignment-faults", 1141dc12760SJiri Olsa .alias = "", 1151dc12760SJiri Olsa }, 1161dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1171dc12760SJiri Olsa .symbol = "emulation-faults", 1181dc12760SJiri Olsa .alias = "", 1191dc12760SJiri Olsa }, 120d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 121d22d1a2aSAdrian Hunter .symbol = "dummy", 122d22d1a2aSAdrian Hunter .alias = "", 123d22d1a2aSAdrian Hunter }, 12486470930SIngo Molnar }; 12586470930SIngo Molnar 126cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 127cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 12886470930SIngo Molnar 129cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 130cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 131cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 132cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 13386470930SIngo Molnar 1346b58e7f1SUlrich Drepper #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ 135f6bdafefSJason Baron while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ 1366b58e7f1SUlrich Drepper if (sys_dirent.d_type == DT_DIR && \ 137f6bdafefSJason Baron (strcmp(sys_dirent.d_name, ".")) && \ 138f6bdafefSJason Baron (strcmp(sys_dirent.d_name, ".."))) 139f6bdafefSJason Baron 140ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 141ae07b63fSPeter Zijlstra { 142ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 143ae07b63fSPeter Zijlstra int fd; 144ae07b63fSPeter Zijlstra 145ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 146ae07b63fSPeter Zijlstra sys_dir->d_name, evt_dir->d_name); 147ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 148ae07b63fSPeter Zijlstra if (fd < 0) 149ae07b63fSPeter Zijlstra return -EINVAL; 150ae07b63fSPeter Zijlstra close(fd); 151ae07b63fSPeter Zijlstra 152ae07b63fSPeter Zijlstra return 0; 153ae07b63fSPeter Zijlstra } 154ae07b63fSPeter Zijlstra 1556b58e7f1SUlrich Drepper #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ 156f6bdafefSJason Baron while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ 1576b58e7f1SUlrich Drepper if (evt_dirent.d_type == DT_DIR && \ 158f6bdafefSJason Baron (strcmp(evt_dirent.d_name, ".")) && \ 159ae07b63fSPeter Zijlstra (strcmp(evt_dirent.d_name, "..")) && \ 160ae07b63fSPeter Zijlstra (!tp_event_has_id(&sys_dirent, &evt_dirent))) 161f6bdafefSJason Baron 162270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 163f6bdafefSJason Baron 164f6bdafefSJason Baron 1651ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 166f6bdafefSJason Baron { 1671ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 168f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 169f6bdafefSJason Baron struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1708aa8a7c8SStephane Eranian char id_buf[24]; 171725b1368SEric Dumazet int fd; 172f6bdafefSJason Baron u64 id; 173f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 174725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 175f6bdafefSJason Baron 176ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 177f6bdafefSJason Baron if (!sys_dir) 178725b1368SEric Dumazet return NULL; 179f6bdafefSJason Baron 1806b58e7f1SUlrich Drepper for_each_subsystem(sys_dir, sys_dirent, sys_next) { 181725b1368SEric Dumazet 182ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 183725b1368SEric Dumazet sys_dirent.d_name); 184725b1368SEric Dumazet evt_dir = opendir(dir_path); 185725b1368SEric Dumazet if (!evt_dir) 1866b58e7f1SUlrich Drepper continue; 187725b1368SEric Dumazet 1886b58e7f1SUlrich Drepper for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 189725b1368SEric Dumazet 190725b1368SEric Dumazet snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 191f6bdafefSJason Baron evt_dirent.d_name); 192725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 193f6bdafefSJason Baron if (fd < 0) 194f6bdafefSJason Baron continue; 195f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 196f6bdafefSJason Baron close(fd); 197f6bdafefSJason Baron continue; 198f6bdafefSJason Baron } 199f6bdafefSJason Baron close(fd); 200f6bdafefSJason Baron id = atoll(id_buf); 201f6bdafefSJason Baron if (id == config) { 202f6bdafefSJason Baron closedir(evt_dir); 203f6bdafefSJason Baron closedir(sys_dir); 20459b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2051ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2061ef2ed10SFrederic Weisbecker if (!path->system) { 2071ef2ed10SFrederic Weisbecker free(path); 2081ef2ed10SFrederic Weisbecker return NULL; 2091ef2ed10SFrederic Weisbecker } 2101ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2111ef2ed10SFrederic Weisbecker if (!path->name) { 21274cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2131ef2ed10SFrederic Weisbecker free(path); 2141ef2ed10SFrederic Weisbecker return NULL; 2151ef2ed10SFrederic Weisbecker } 2161ef2ed10SFrederic Weisbecker strncpy(path->system, sys_dirent.d_name, 2171ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2181ef2ed10SFrederic Weisbecker strncpy(path->name, evt_dirent.d_name, 2191ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2201ef2ed10SFrederic Weisbecker return path; 221f6bdafefSJason Baron } 222f6bdafefSJason Baron } 223f6bdafefSJason Baron closedir(evt_dir); 224f6bdafefSJason Baron } 225f6bdafefSJason Baron 226f6bdafefSJason Baron closedir(sys_dir); 2271ef2ed10SFrederic Weisbecker return NULL; 2281ef2ed10SFrederic Weisbecker } 2291ef2ed10SFrederic Weisbecker 230e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 231e7c93f09SNamhyung Kim { 232e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 233e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 234e7c93f09SNamhyung Kim 235e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 236e7c93f09SNamhyung Kim free(path); 237e7c93f09SNamhyung Kim return NULL; 238e7c93f09SNamhyung Kim } 239e7c93f09SNamhyung Kim 240e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 241e7c93f09SNamhyung Kim path->name = strdup(str+1); 242e7c93f09SNamhyung Kim 243e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 24474cf249dSArnaldo Carvalho de Melo zfree(&path->system); 24574cf249dSArnaldo Carvalho de Melo zfree(&path->name); 246e7c93f09SNamhyung Kim free(path); 247e7c93f09SNamhyung Kim path = NULL; 248e7c93f09SNamhyung Kim } 249e7c93f09SNamhyung Kim 250e7c93f09SNamhyung Kim return path; 251e7c93f09SNamhyung Kim } 252e7c93f09SNamhyung Kim 2531424dc96SDavid Ahern const char *event_type(int type) 2541424dc96SDavid Ahern { 2551424dc96SDavid Ahern switch (type) { 2561424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2571424dc96SDavid Ahern return "hardware"; 2581424dc96SDavid Ahern 2591424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2601424dc96SDavid Ahern return "software"; 2611424dc96SDavid Ahern 2621424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2631424dc96SDavid Ahern return "tracepoint"; 2641424dc96SDavid Ahern 2651424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2661424dc96SDavid Ahern return "hardware-cache"; 2671424dc96SDavid Ahern 2681424dc96SDavid Ahern default: 2691424dc96SDavid Ahern break; 2701424dc96SDavid Ahern } 2711424dc96SDavid Ahern 2721424dc96SDavid Ahern return "unknown"; 2731424dc96SDavid Ahern } 2741424dc96SDavid Ahern 2757ae92e74SYan, Zheng 2767ae92e74SYan, Zheng 277410136f5SStephane Eranian static struct perf_evsel * 278410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 2797ae92e74SYan, Zheng struct perf_event_attr *attr, 280930a2e29SJiri Olsa char *name, struct cpu_map *cpus, 281930a2e29SJiri Olsa struct list_head *config_terms) 28289812fc8SJiri Olsa { 28389812fc8SJiri Olsa struct perf_evsel *evsel; 28489812fc8SJiri Olsa 28589812fc8SJiri Olsa event_attr_init(attr); 28689812fc8SJiri Olsa 287ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attr, (*idx)++); 288c5cd8ac0SDavid Ahern if (!evsel) 289410136f5SStephane Eranian return NULL; 29089812fc8SJiri Olsa 291f30a79b0SJiri Olsa evsel->cpus = cpu_map__get(cpus); 292fce4d296SAdrian Hunter evsel->own_cpus = cpu_map__get(cpus); 293f30a79b0SJiri Olsa 2949db1763cSArnaldo Carvalho de Melo if (name) 29589812fc8SJiri Olsa evsel->name = strdup(name); 296930a2e29SJiri Olsa 297930a2e29SJiri Olsa if (config_terms) 298930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 299930a2e29SJiri Olsa 300b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 301410136f5SStephane Eranian return evsel; 30289812fc8SJiri Olsa } 30389812fc8SJiri Olsa 304c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 305930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 306930a2e29SJiri Olsa struct list_head *config_terms) 3077ae92e74SYan, Zheng { 308930a2e29SJiri Olsa return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; 3097ae92e74SYan, Zheng } 3107ae92e74SYan, Zheng 3110b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 31286470930SIngo Molnar { 31386470930SIngo Molnar int i, j; 31461c45981SPaul Mackerras int n, longest = -1; 31586470930SIngo Molnar 31686470930SIngo Molnar for (i = 0; i < size; i++) { 3170b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 31861c45981SPaul Mackerras n = strlen(names[i][j]); 31989812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 32061c45981SPaul Mackerras longest = n; 32161c45981SPaul Mackerras } 32289812fc8SJiri Olsa if (longest > 0) 32386470930SIngo Molnar return i; 32486470930SIngo Molnar } 32586470930SIngo Molnar 3268953645fSIngo Molnar return -1; 32786470930SIngo Molnar } 32886470930SIngo Molnar 329c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 33089812fc8SJiri Olsa char *type, char *op_result1, char *op_result2) 33186470930SIngo Molnar { 33289812fc8SJiri Olsa struct perf_event_attr attr; 33389812fc8SJiri Olsa char name[MAX_NAME_LEN]; 33461c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 33589812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 33689812fc8SJiri Olsa int i, n; 33786470930SIngo Molnar 33886470930SIngo Molnar /* 33986470930SIngo Molnar * No fallback - if we cannot get a clear cache type 34086470930SIngo Molnar * then bail out: 34186470930SIngo Molnar */ 3420b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 34389812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 34486470930SIngo Molnar if (cache_type == -1) 34589812fc8SJiri Olsa return -EINVAL; 34686470930SIngo Molnar 34789812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 34889812fc8SJiri Olsa 34989812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 35089812fc8SJiri Olsa char *str = op_result[i]; 35189812fc8SJiri Olsa 352275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 35361c45981SPaul Mackerras 35461c45981SPaul Mackerras if (cache_op == -1) { 3550b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 35661c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 35761c45981SPaul Mackerras if (cache_op >= 0) { 3580b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 35989812fc8SJiri Olsa return -EINVAL; 36061c45981SPaul Mackerras continue; 36161c45981SPaul Mackerras } 36261c45981SPaul Mackerras } 36361c45981SPaul Mackerras 36461c45981SPaul Mackerras if (cache_result == -1) { 3650b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 36661c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 36761c45981SPaul Mackerras if (cache_result >= 0) 36861c45981SPaul Mackerras continue; 36961c45981SPaul Mackerras } 37061c45981SPaul Mackerras } 37161c45981SPaul Mackerras 37286470930SIngo Molnar /* 37386470930SIngo Molnar * Fall back to reads: 37486470930SIngo Molnar */ 3758953645fSIngo Molnar if (cache_op == -1) 3768953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 37786470930SIngo Molnar 37886470930SIngo Molnar /* 37986470930SIngo Molnar * Fall back to accesses: 38086470930SIngo Molnar */ 38186470930SIngo Molnar if (cache_result == -1) 38286470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 38386470930SIngo Molnar 38489812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 38589812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 38689812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 387930a2e29SJiri Olsa return add_event(list, idx, &attr, name, NULL); 38886470930SIngo Molnar } 38986470930SIngo Molnar 39019658171SJiri Olsa static void tracepoint_error(struct parse_events_error *error, int err, 39119658171SJiri Olsa char *sys, char *name) 39219658171SJiri Olsa { 39319658171SJiri Olsa char help[BUFSIZ]; 39419658171SJiri Olsa 39519658171SJiri Olsa /* 39619658171SJiri Olsa * We get error directly from syscall errno ( > 0), 39719658171SJiri Olsa * or from encoded pointer's error ( < 0). 39819658171SJiri Olsa */ 39919658171SJiri Olsa err = abs(err); 40019658171SJiri Olsa 40119658171SJiri Olsa switch (err) { 40219658171SJiri Olsa case EACCES: 40319658171SJiri Olsa error->str = strdup("can't access trace events"); 40419658171SJiri Olsa break; 40519658171SJiri Olsa case ENOENT: 40619658171SJiri Olsa error->str = strdup("unknown tracepoint"); 40719658171SJiri Olsa break; 40819658171SJiri Olsa default: 40919658171SJiri Olsa error->str = strdup("failed to add tracepoint"); 41019658171SJiri Olsa break; 41119658171SJiri Olsa } 41219658171SJiri Olsa 41319658171SJiri Olsa tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 41419658171SJiri Olsa error->help = strdup(help); 41519658171SJiri Olsa } 41619658171SJiri Olsa 417c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 418e2f9f8eaSJiri Olsa char *sys_name, char *evt_name, 419e2f9f8eaSJiri Olsa struct parse_events_error *error __maybe_unused) 420bcd3279fSFrederic Weisbecker { 42182fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 422bcd3279fSFrederic Weisbecker 423ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 42419658171SJiri Olsa if (IS_ERR(evsel)) { 42519658171SJiri Olsa tracepoint_error(error, PTR_ERR(evsel), sys_name, evt_name); 4268dd2a131SJiri Olsa return PTR_ERR(evsel); 42719658171SJiri Olsa } 428bcd3279fSFrederic Weisbecker 42982fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 43082fe1c29SArnaldo Carvalho de Melo return 0; 431bcd3279fSFrederic Weisbecker } 432bcd3279fSFrederic Weisbecker 433c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 434e2f9f8eaSJiri Olsa char *sys_name, char *evt_name, 435e2f9f8eaSJiri Olsa struct parse_events_error *error) 436bcd3279fSFrederic Weisbecker { 437bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 438bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 439bcd3279fSFrederic Weisbecker DIR *evt_dir; 44089812fc8SJiri Olsa int ret = 0; 441bcd3279fSFrederic Weisbecker 442ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 443bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 444bcd3279fSFrederic Weisbecker if (!evt_dir) { 44519658171SJiri Olsa tracepoint_error(error, errno, sys_name, evt_name); 44689812fc8SJiri Olsa return -1; 447bcd3279fSFrederic Weisbecker } 448bcd3279fSFrederic Weisbecker 44989812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 450bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 451bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 452bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 453bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 454bcd3279fSFrederic Weisbecker continue; 455bcd3279fSFrederic Weisbecker 45689812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 457fb1d2edfSMasami Hiramatsu continue; 458fb1d2edfSMasami Hiramatsu 459e2f9f8eaSJiri Olsa ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, error); 460bcd3279fSFrederic Weisbecker } 461bcd3279fSFrederic Weisbecker 4620bd3f084SJiri Olsa closedir(evt_dir); 46389812fc8SJiri Olsa return ret; 464bcd3279fSFrederic Weisbecker } 465bcd3279fSFrederic Weisbecker 466c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 467e2f9f8eaSJiri Olsa char *sys_name, char *evt_name, 468e2f9f8eaSJiri Olsa struct parse_events_error *error) 469f35488f9SJiri Olsa { 470f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 471e2f9f8eaSJiri Olsa add_tracepoint_multi_event(list, idx, sys_name, evt_name, error) : 472e2f9f8eaSJiri Olsa add_tracepoint(list, idx, sys_name, evt_name, error); 473f35488f9SJiri Olsa } 474f35488f9SJiri Olsa 475c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 476e2f9f8eaSJiri Olsa char *sys_name, char *evt_name, 477e2f9f8eaSJiri Olsa struct parse_events_error *error) 478f35488f9SJiri Olsa { 479f35488f9SJiri Olsa struct dirent *events_ent; 480f35488f9SJiri Olsa DIR *events_dir; 481f35488f9SJiri Olsa int ret = 0; 482f35488f9SJiri Olsa 483f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 484f35488f9SJiri Olsa if (!events_dir) { 48519658171SJiri Olsa tracepoint_error(error, errno, sys_name, evt_name); 486f35488f9SJiri Olsa return -1; 487f35488f9SJiri Olsa } 488f35488f9SJiri Olsa 489f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 490f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 491f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 492f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 493f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 494f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 495f35488f9SJiri Olsa continue; 496f35488f9SJiri Olsa 497f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 498f35488f9SJiri Olsa continue; 499f35488f9SJiri Olsa 500f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 501e2f9f8eaSJiri Olsa evt_name, error); 502f35488f9SJiri Olsa } 503f35488f9SJiri Olsa 504f35488f9SJiri Olsa closedir(events_dir); 505f35488f9SJiri Olsa return ret; 506f35488f9SJiri Olsa } 507f35488f9SJiri Olsa 508c5cd8ac0SDavid Ahern int parse_events_add_tracepoint(struct list_head *list, int *idx, 509e2f9f8eaSJiri Olsa char *sys, char *event, 510e2f9f8eaSJiri Olsa struct parse_events_error *error) 511f6bdafefSJason Baron { 512f35488f9SJiri Olsa if (strpbrk(sys, "*?")) 513e2f9f8eaSJiri Olsa return add_tracepoint_multi_sys(list, idx, sys, event, error); 514f35488f9SJiri Olsa else 515e2f9f8eaSJiri Olsa return add_tracepoint_event(list, idx, sys, event, error); 5163a9f131fSFrederic Weisbecker } 5173a9f131fSFrederic Weisbecker 51889812fc8SJiri Olsa static int 51989812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 5201b290d67SFrederic Weisbecker { 5211b290d67SFrederic Weisbecker int i; 5221b290d67SFrederic Weisbecker 5231b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 52489812fc8SJiri Olsa if (!type || !type[i]) 5251b290d67SFrederic Weisbecker break; 5261b290d67SFrederic Weisbecker 5277582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 5287582732fSJiri Olsa do { \ 5297582732fSJiri Olsa if (attr->bp_type & bit) \ 5307582732fSJiri Olsa return -EINVAL; \ 5317582732fSJiri Olsa else \ 5327582732fSJiri Olsa attr->bp_type |= bit; \ 5337582732fSJiri Olsa } while (0) 5347582732fSJiri Olsa 5351b290d67SFrederic Weisbecker switch (type[i]) { 5361b290d67SFrederic Weisbecker case 'r': 5377582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 5381b290d67SFrederic Weisbecker break; 5391b290d67SFrederic Weisbecker case 'w': 5407582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 5411b290d67SFrederic Weisbecker break; 5421b290d67SFrederic Weisbecker case 'x': 5437582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 5441b290d67SFrederic Weisbecker break; 5451b290d67SFrederic Weisbecker default: 54689812fc8SJiri Olsa return -EINVAL; 5471b290d67SFrederic Weisbecker } 5481b290d67SFrederic Weisbecker } 54989812fc8SJiri Olsa 5507582732fSJiri Olsa #undef CHECK_SET_TYPE 5517582732fSJiri Olsa 5521b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 5531b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 5541b290d67SFrederic Weisbecker 55589812fc8SJiri Olsa return 0; 5561b290d67SFrederic Weisbecker } 5571b290d67SFrederic Weisbecker 558c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 5593741eb9fSJacob Shin void *ptr, char *type, u64 len) 5601b290d67SFrederic Weisbecker { 56189812fc8SJiri Olsa struct perf_event_attr attr; 5621b290d67SFrederic Weisbecker 56389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 5649fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 5651b290d67SFrederic Weisbecker 56689812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 56789812fc8SJiri Olsa return -EINVAL; 5681b290d67SFrederic Weisbecker 5693741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 5703741eb9fSJacob Shin if (!len) { 57189812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 5723741eb9fSJacob Shin len = sizeof(long); 573aa59a485SFrederic Weisbecker else 5743741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 5753741eb9fSJacob Shin } 5763741eb9fSJacob Shin 5773741eb9fSJacob Shin attr.bp_len = len; 578aa59a485SFrederic Weisbecker 57989812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 5804a841d65SJovi Zhang attr.sample_period = 1; 5811b290d67SFrederic Weisbecker 582930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 5831b290d67SFrederic Weisbecker } 5841b290d67SFrederic Weisbecker 5853b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 5863b0e371cSJiri Olsa struct parse_events_error *err, 5873b0e371cSJiri Olsa int type) 5883b0e371cSJiri Olsa { 5893b0e371cSJiri Olsa if (type == term->type_val) 5903b0e371cSJiri Olsa return 0; 5913b0e371cSJiri Olsa 5923b0e371cSJiri Olsa if (err) { 5933b0e371cSJiri Olsa err->idx = term->err_val; 5943b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 5953b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 5963b0e371cSJiri Olsa else 5973b0e371cSJiri Olsa err->str = strdup("expected string value"); 5983b0e371cSJiri Olsa } 5993b0e371cSJiri Olsa return -EINVAL; 6003b0e371cSJiri Olsa } 6013b0e371cSJiri Olsa 602*0b8891a8SHe Kuang typedef int config_term_func_t(struct perf_event_attr *attr, 603*0b8891a8SHe Kuang struct parse_events_term *term, 604*0b8891a8SHe Kuang struct parse_events_error *err); 605*0b8891a8SHe Kuang 606*0b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr, 6073b0e371cSJiri Olsa struct parse_events_term *term, 6083b0e371cSJiri Olsa struct parse_events_error *err) 6098f707d84SJiri Olsa { 61016fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 61116fa7e82SJiri Olsa do { \ 6123b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 61316fa7e82SJiri Olsa return -EINVAL; \ 61416fa7e82SJiri Olsa } while (0) 61516fa7e82SJiri Olsa 61616fa7e82SJiri Olsa switch (term->type_term) { 6178f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 61816fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 6198f707d84SJiri Olsa attr->config = term->val.num; 6208f707d84SJiri Olsa break; 6218f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 62216fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 6238f707d84SJiri Olsa attr->config1 = term->val.num; 6248f707d84SJiri Olsa break; 6258f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 62616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 6278f707d84SJiri Olsa attr->config2 = term->val.num; 6288f707d84SJiri Olsa break; 6298f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 63016fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 6318f707d84SJiri Olsa break; 63209af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 63309af2a55SNamhyung Kim CHECK_TYPE_VAL(NUM); 63409af2a55SNamhyung Kim break; 6358f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 6368f707d84SJiri Olsa /* 6378f707d84SJiri Olsa * TODO uncomment when the field is available 6388f707d84SJiri Olsa * attr->branch_sample_type = term->val.num; 6398f707d84SJiri Olsa */ 6408f707d84SJiri Olsa break; 64132067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 64232067712SKan Liang CHECK_TYPE_VAL(NUM); 64332067712SKan Liang if (term->val.num > 1) { 64432067712SKan Liang err->str = strdup("expected 0 or 1"); 64532067712SKan Liang err->idx = term->err_val; 64632067712SKan Liang return -EINVAL; 64732067712SKan Liang } 64832067712SKan Liang break; 649d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 650d457c963SKan Liang CHECK_TYPE_VAL(STR); 651d457c963SKan Liang break; 652d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 653d457c963SKan Liang CHECK_TYPE_VAL(NUM); 654d457c963SKan Liang break; 6556b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 6566b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 6576b5fc39bSJiri Olsa break; 6588f707d84SJiri Olsa default: 6598f707d84SJiri Olsa return -EINVAL; 6608f707d84SJiri Olsa } 66116fa7e82SJiri Olsa 6628f707d84SJiri Olsa return 0; 66316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 6648f707d84SJiri Olsa } 6658f707d84SJiri Olsa 666*0b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr, 667*0b8891a8SHe Kuang struct parse_events_term *term, 668*0b8891a8SHe Kuang struct parse_events_error *err) 669*0b8891a8SHe Kuang { 670*0b8891a8SHe Kuang if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) 671*0b8891a8SHe Kuang /* 672*0b8891a8SHe Kuang * Always succeed for sysfs terms, as we dont know 673*0b8891a8SHe Kuang * at this point what type they need to have. 674*0b8891a8SHe Kuang */ 675*0b8891a8SHe Kuang return 0; 676*0b8891a8SHe Kuang else 677*0b8891a8SHe Kuang return config_term_common(attr, term, err); 678*0b8891a8SHe Kuang } 679*0b8891a8SHe Kuang 6808f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 6813b0e371cSJiri Olsa struct list_head *head, 682*0b8891a8SHe Kuang struct parse_events_error *err, 683*0b8891a8SHe Kuang config_term_func_t config_term) 6848f707d84SJiri Olsa { 6856cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 6868f707d84SJiri Olsa 6878f707d84SJiri Olsa list_for_each_entry(term, head, list) 6883b0e371cSJiri Olsa if (config_term(attr, term, err)) 6898f707d84SJiri Olsa return -EINVAL; 6908f707d84SJiri Olsa 6918f707d84SJiri Olsa return 0; 6928f707d84SJiri Olsa } 6938f707d84SJiri Olsa 694930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 695930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 696930a2e29SJiri Olsa { 697930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 698930a2e29SJiri Olsa do { \ 699930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 700930a2e29SJiri Olsa \ 701930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 702930a2e29SJiri Olsa if (!__t) \ 703930a2e29SJiri Olsa return -ENOMEM; \ 704930a2e29SJiri Olsa \ 705930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 706930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 707930a2e29SJiri Olsa __t->val.__name = __val; \ 708930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 709930a2e29SJiri Olsa } while (0) 710930a2e29SJiri Olsa 711930a2e29SJiri Olsa struct parse_events_term *term; 712930a2e29SJiri Olsa 713930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 714930a2e29SJiri Olsa switch (term->type_term) { 715ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 716ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 71732067712SKan Liang break; 71809af2a55SNamhyung Kim case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 71909af2a55SNamhyung Kim ADD_CONFIG_TERM(FREQ, freq, term->val.num); 72009af2a55SNamhyung Kim break; 72132067712SKan Liang case PARSE_EVENTS__TERM_TYPE_TIME: 72232067712SKan Liang ADD_CONFIG_TERM(TIME, time, term->val.num); 72332067712SKan Liang break; 724d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 725d457c963SKan Liang ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 726d457c963SKan Liang break; 727d457c963SKan Liang case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 728d457c963SKan Liang ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 729d457c963SKan Liang break; 730930a2e29SJiri Olsa default: 731930a2e29SJiri Olsa break; 732930a2e29SJiri Olsa } 733930a2e29SJiri Olsa } 734930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 735930a2e29SJiri Olsa return 0; 736930a2e29SJiri Olsa } 737930a2e29SJiri Olsa 73887d650beSJiri Olsa int parse_events_add_numeric(struct parse_events_evlist *data, 73987d650beSJiri Olsa struct list_head *list, 740b527bab5SRobert Richter u32 type, u64 config, 7418f707d84SJiri Olsa struct list_head *head_config) 74274d5b588SJaswinder Singh Rajput { 74389812fc8SJiri Olsa struct perf_event_attr attr; 744930a2e29SJiri Olsa LIST_HEAD(config_terms); 74574d5b588SJaswinder Singh Rajput 74689812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 74789812fc8SJiri Olsa attr.type = type; 74889812fc8SJiri Olsa attr.config = config; 7498f707d84SJiri Olsa 750930a2e29SJiri Olsa if (head_config) { 751*0b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, 752*0b8891a8SHe Kuang config_term_common)) 7538f707d84SJiri Olsa return -EINVAL; 7548f707d84SJiri Olsa 755930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 756930a2e29SJiri Olsa return -ENOMEM; 757930a2e29SJiri Olsa } 758930a2e29SJiri Olsa 759930a2e29SJiri Olsa return add_event(list, &data->idx, &attr, NULL, &config_terms); 760b908debdSIngo Molnar } 761b908debdSIngo Molnar 7626cee6cd3SArnaldo Carvalho de Melo static int parse_events__is_name_term(struct parse_events_term *term) 7636b5fc39bSJiri Olsa { 7646b5fc39bSJiri Olsa return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 7656b5fc39bSJiri Olsa } 7666b5fc39bSJiri Olsa 7679db1763cSArnaldo Carvalho de Melo static char *pmu_event_name(struct list_head *head_terms) 7686b5fc39bSJiri Olsa { 7696cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 7706b5fc39bSJiri Olsa 7716b5fc39bSJiri Olsa list_for_each_entry(term, head_terms, list) 7726b5fc39bSJiri Olsa if (parse_events__is_name_term(term)) 7736b5fc39bSJiri Olsa return term->val.str; 7746b5fc39bSJiri Olsa 7759db1763cSArnaldo Carvalho de Melo return NULL; 7766b5fc39bSJiri Olsa } 7776b5fc39bSJiri Olsa 77836adec85SJiri Olsa int parse_events_add_pmu(struct parse_events_evlist *data, 77936adec85SJiri Olsa struct list_head *list, char *name, 78036adec85SJiri Olsa struct list_head *head_config) 7815f537a26SJiri Olsa { 7825f537a26SJiri Olsa struct perf_event_attr attr; 78346441bdcSMatt Fleming struct perf_pmu_info info; 7845f537a26SJiri Olsa struct perf_pmu *pmu; 785410136f5SStephane Eranian struct perf_evsel *evsel; 786930a2e29SJiri Olsa LIST_HEAD(config_terms); 7875f537a26SJiri Olsa 7885f537a26SJiri Olsa pmu = perf_pmu__find(name); 7895f537a26SJiri Olsa if (!pmu) 7905f537a26SJiri Olsa return -EINVAL; 7915f537a26SJiri Olsa 792dc0a6202SAdrian Hunter if (pmu->default_config) { 793dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 794dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 795dc0a6202SAdrian Hunter } else { 7965f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 797dc0a6202SAdrian Hunter } 7985f537a26SJiri Olsa 799ad962273SAdrian Hunter if (!head_config) { 800ad962273SAdrian Hunter attr.type = pmu->type; 801930a2e29SJiri Olsa evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); 802ad962273SAdrian Hunter return evsel ? 0 : -ENOMEM; 803ad962273SAdrian Hunter } 804ad962273SAdrian Hunter 80546441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 806a6146d50SZheng Yan return -EINVAL; 807a6146d50SZheng Yan 8085f537a26SJiri Olsa /* 8095f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 8105f537a26SJiri Olsa * return value when called with fail == 0 ;) 8115f537a26SJiri Olsa */ 812*0b8891a8SHe Kuang if (config_attr(&attr, head_config, data->error, config_term_pmu)) 813c056ba6aSJiri Olsa return -EINVAL; 8145f537a26SJiri Olsa 815930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 816930a2e29SJiri Olsa return -ENOMEM; 817930a2e29SJiri Olsa 818e64b020bSJiri Olsa if (perf_pmu__config(pmu, &attr, head_config, data->error)) 8195f537a26SJiri Olsa return -EINVAL; 8205f537a26SJiri Olsa 82136adec85SJiri Olsa evsel = __add_event(list, &data->idx, &attr, 822930a2e29SJiri Olsa pmu_event_name(head_config), pmu->cpus, 823930a2e29SJiri Olsa &config_terms); 824410136f5SStephane Eranian if (evsel) { 82546441bdcSMatt Fleming evsel->unit = info.unit; 82646441bdcSMatt Fleming evsel->scale = info.scale; 827044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 8281d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 829410136f5SStephane Eranian } 830410136f5SStephane Eranian 831410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 8325f537a26SJiri Olsa } 8335f537a26SJiri Olsa 8346a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 8356a4bb04cSJiri Olsa char *event_mod) 83689efb029SJiri Olsa { 8376a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 8386a4bb04cSJiri Olsa } 8396a4bb04cSJiri Olsa 84063dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 8416a4bb04cSJiri Olsa { 8426a4bb04cSJiri Olsa struct perf_evsel *leader; 8436a4bb04cSJiri Olsa 844854f7363SWang Nan if (list_empty(list)) { 845854f7363SWang Nan WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 846854f7363SWang Nan return; 847854f7363SWang Nan } 848854f7363SWang Nan 84963dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 85063dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 8516a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 85289efb029SJiri Olsa } 85389efb029SJiri Olsa 854c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 8555d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 8565d7be90eSJiri Olsa struct list_head *list_all) 8575d7be90eSJiri Olsa { 8585d7be90eSJiri Olsa /* 8595d7be90eSJiri Olsa * Called for single event definition. Update the 86089efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 8615d7be90eSJiri Olsa * list, for next event definition. 8625d7be90eSJiri Olsa */ 8635d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 864b847cbdcSJiri Olsa free(list_event); 8655d7be90eSJiri Olsa } 8665d7be90eSJiri Olsa 867f5b1135bSJiri Olsa struct event_modifier { 868f5b1135bSJiri Olsa int eu; 869f5b1135bSJiri Olsa int ek; 870f5b1135bSJiri Olsa int eh; 871f5b1135bSJiri Olsa int eH; 872f5b1135bSJiri Olsa int eG; 873a1e12da4SJiri Olsa int eI; 874f5b1135bSJiri Olsa int precise; 875f5b1135bSJiri Olsa int exclude_GH; 8763c176311SJiri Olsa int sample_read; 877e9a7c414SMichael Ellerman int pinned; 878f5b1135bSJiri Olsa }; 87961c45981SPaul Mackerras 880f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 881f5b1135bSJiri Olsa struct perf_evsel *evsel) 882f5b1135bSJiri Olsa { 883f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 884f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 885f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 886f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 887f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 888a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 889f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 8903c176311SJiri Olsa int sample_read = 0; 891e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 892f5b1135bSJiri Olsa 893f5b1135bSJiri Olsa int exclude = eu | ek | eh; 894f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 895f5b1135bSJiri Olsa 896f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 897ceb53fbfSIngo Molnar 89861c45981SPaul Mackerras while (*str) { 899ab608344SPeter Zijlstra if (*str == 'u') { 900ab608344SPeter Zijlstra if (!exclude) 901ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 90261c45981SPaul Mackerras eu = 0; 903ab608344SPeter Zijlstra } else if (*str == 'k') { 904ab608344SPeter Zijlstra if (!exclude) 905ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 90661c45981SPaul Mackerras ek = 0; 907ab608344SPeter Zijlstra } else if (*str == 'h') { 908ab608344SPeter Zijlstra if (!exclude) 909ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 91061c45981SPaul Mackerras eh = 0; 91199320cc8SJoerg Roedel } else if (*str == 'G') { 91299320cc8SJoerg Roedel if (!exclude_GH) 91399320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 91499320cc8SJoerg Roedel eG = 0; 91599320cc8SJoerg Roedel } else if (*str == 'H') { 91699320cc8SJoerg Roedel if (!exclude_GH) 91799320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 91899320cc8SJoerg Roedel eH = 0; 919a1e12da4SJiri Olsa } else if (*str == 'I') { 920a1e12da4SJiri Olsa eI = 1; 921ab608344SPeter Zijlstra } else if (*str == 'p') { 922ab608344SPeter Zijlstra precise++; 9231342798cSDavid Ahern /* use of precise requires exclude_guest */ 9241342798cSDavid Ahern if (!exclude_GH) 9251342798cSDavid Ahern eG = 1; 9263c176311SJiri Olsa } else if (*str == 'S') { 9273c176311SJiri Olsa sample_read = 1; 928e9a7c414SMichael Ellerman } else if (*str == 'D') { 929e9a7c414SMichael Ellerman pinned = 1; 930ab608344SPeter Zijlstra } else 93161c45981SPaul Mackerras break; 932ab608344SPeter Zijlstra 93361c45981SPaul Mackerras ++str; 93461c45981SPaul Mackerras } 93574d5b588SJaswinder Singh Rajput 93686470930SIngo Molnar /* 93789812fc8SJiri Olsa * precise ip: 93889812fc8SJiri Olsa * 93989812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 94089812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 94189812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 94289812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 94389812fc8SJiri Olsa * 94489812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 94586470930SIngo Molnar */ 94689812fc8SJiri Olsa if (precise > 3) 94789812fc8SJiri Olsa return -EINVAL; 94886470930SIngo Molnar 949f5b1135bSJiri Olsa mod->eu = eu; 950f5b1135bSJiri Olsa mod->ek = ek; 951f5b1135bSJiri Olsa mod->eh = eh; 952f5b1135bSJiri Olsa mod->eH = eH; 953f5b1135bSJiri Olsa mod->eG = eG; 954a1e12da4SJiri Olsa mod->eI = eI; 955f5b1135bSJiri Olsa mod->precise = precise; 956f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 9573c176311SJiri Olsa mod->sample_read = sample_read; 958e9a7c414SMichael Ellerman mod->pinned = pinned; 959e9a7c414SMichael Ellerman 960f5b1135bSJiri Olsa return 0; 961f5b1135bSJiri Olsa } 962f5b1135bSJiri Olsa 963534123f4SJiri Olsa /* 964534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 965534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 966534123f4SJiri Olsa */ 967534123f4SJiri Olsa static int check_modifier(char *str) 968534123f4SJiri Olsa { 969534123f4SJiri Olsa char *p = str; 970534123f4SJiri Olsa 971534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 972a1e12da4SJiri Olsa if (strlen(str) > (sizeof("ukhGHpppSDI") - 1)) 973534123f4SJiri Olsa return -1; 974534123f4SJiri Olsa 975534123f4SJiri Olsa while (*p) { 976534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 977534123f4SJiri Olsa return -1; 978534123f4SJiri Olsa p++; 979534123f4SJiri Olsa } 980534123f4SJiri Olsa 981534123f4SJiri Olsa return 0; 982534123f4SJiri Olsa } 983534123f4SJiri Olsa 984f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 985f5b1135bSJiri Olsa { 986f5b1135bSJiri Olsa struct perf_evsel *evsel; 987f5b1135bSJiri Olsa struct event_modifier mod; 988f5b1135bSJiri Olsa 989f5b1135bSJiri Olsa if (str == NULL) 990f5b1135bSJiri Olsa return 0; 991f5b1135bSJiri Olsa 992534123f4SJiri Olsa if (check_modifier(str)) 993534123f4SJiri Olsa return -EINVAL; 994534123f4SJiri Olsa 995f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 996f5b1135bSJiri Olsa return -EINVAL; 997f5b1135bSJiri Olsa 9980050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 999f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 1000f5b1135bSJiri Olsa return -EINVAL; 1001f5b1135bSJiri Olsa 1002f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 1003f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 1004f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 1005f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 1006f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 1007f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 1008a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 1009f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 10103c176311SJiri Olsa evsel->sample_read = mod.sample_read; 1011e9a7c414SMichael Ellerman 1012e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 1013e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 1014ceb53fbfSIngo Molnar } 101586470930SIngo Molnar 101689812fc8SJiri Olsa return 0; 101786470930SIngo Molnar } 101886470930SIngo Molnar 1019ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 1020ac2ba9f3SRobert Richter { 1021ac2ba9f3SRobert Richter struct perf_evsel *evsel; 1022ac2ba9f3SRobert Richter 10230050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 1024ac2ba9f3SRobert Richter if (!evsel->name) 1025ac2ba9f3SRobert Richter evsel->name = strdup(name); 1026ac2ba9f3SRobert Richter } 1027ac2ba9f3SRobert Richter 1028ac2ba9f3SRobert Richter return 0; 1029ac2ba9f3SRobert Richter } 1030ac2ba9f3SRobert Richter 1031dcb4e102SKan Liang static int 1032dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 1033dcb4e102SKan Liang { 1034dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1035dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1036dcb4e102SKan Liang 1037dcb4e102SKan Liang return strcmp(pmu1->symbol, pmu2->symbol); 1038dcb4e102SKan Liang } 1039dcb4e102SKan Liang 1040dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 1041dcb4e102SKan Liang { 1042dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 1043dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 1044dcb4e102SKan Liang int i; 1045dcb4e102SKan Liang 1046dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 1047dcb4e102SKan Liang p = perf_pmu_events_list + i; 1048dcb4e102SKan Liang free(p->symbol); 1049dcb4e102SKan Liang } 1050dcb4e102SKan Liang free(perf_pmu_events_list); 1051dcb4e102SKan Liang perf_pmu_events_list = NULL; 1052dcb4e102SKan Liang perf_pmu_events_list_num = 0; 1053dcb4e102SKan Liang } 1054dcb4e102SKan Liang } 1055dcb4e102SKan Liang 1056dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 1057dcb4e102SKan Liang do { \ 1058dcb4e102SKan Liang p->symbol = str; \ 1059dcb4e102SKan Liang if (!p->symbol) \ 1060dcb4e102SKan Liang goto err; \ 1061dcb4e102SKan Liang p->type = stype; \ 1062dcb4e102SKan Liang } while (0) 1063dcb4e102SKan Liang 1064dcb4e102SKan Liang /* 1065dcb4e102SKan Liang * Read the pmu events list from sysfs 1066dcb4e102SKan Liang * Save it into perf_pmu_events_list 1067dcb4e102SKan Liang */ 1068dcb4e102SKan Liang static void perf_pmu__parse_init(void) 1069dcb4e102SKan Liang { 1070dcb4e102SKan Liang 1071dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 1072dcb4e102SKan Liang struct perf_pmu_alias *alias; 1073dcb4e102SKan Liang int len = 0; 1074dcb4e102SKan Liang 1075dcb4e102SKan Liang pmu = perf_pmu__find("cpu"); 1076dcb4e102SKan Liang if ((pmu == NULL) || list_empty(&pmu->aliases)) { 1077dcb4e102SKan Liang perf_pmu_events_list_num = -1; 1078dcb4e102SKan Liang return; 1079dcb4e102SKan Liang } 1080dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1081dcb4e102SKan Liang if (strchr(alias->name, '-')) 1082dcb4e102SKan Liang len++; 1083dcb4e102SKan Liang len++; 1084dcb4e102SKan Liang } 1085dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1086dcb4e102SKan Liang if (!perf_pmu_events_list) 1087dcb4e102SKan Liang return; 1088dcb4e102SKan Liang perf_pmu_events_list_num = len; 1089dcb4e102SKan Liang 1090dcb4e102SKan Liang len = 0; 1091dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1092dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1093dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1094dcb4e102SKan Liang 1095dcb4e102SKan Liang if (tmp != NULL) { 1096dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1097dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1098dcb4e102SKan Liang p++; 1099dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1100dcb4e102SKan Liang len += 2; 1101dcb4e102SKan Liang } else { 1102dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1103dcb4e102SKan Liang len++; 1104dcb4e102SKan Liang } 1105dcb4e102SKan Liang } 1106dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1107dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1108dcb4e102SKan Liang 1109dcb4e102SKan Liang return; 1110dcb4e102SKan Liang err: 1111dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1112dcb4e102SKan Liang } 1113dcb4e102SKan Liang 1114dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1115dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1116dcb4e102SKan Liang { 1117dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1118dcb4e102SKan Liang 1119dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1120dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1121dcb4e102SKan Liang perf_pmu__parse_init(); 1122dcb4e102SKan Liang /* 1123dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1124dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1125dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1126dcb4e102SKan Liang */ 1127dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1128dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1129dcb4e102SKan Liang 1130dcb4e102SKan Liang p.symbol = strdup(name); 1131dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1132dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1133dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1134dcb4e102SKan Liang free(p.symbol); 1135dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1136dcb4e102SKan Liang } 1137dcb4e102SKan Liang 113890e2b22dSJiri Olsa static int parse_events__scanner(const char *str, void *data, int start_token) 1139ac20de6fSZheng Yan { 1140ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1141ac20de6fSZheng Yan void *scanner; 1142ac20de6fSZheng Yan int ret; 1143ac20de6fSZheng Yan 114490e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1145ac20de6fSZheng Yan if (ret) 1146ac20de6fSZheng Yan return ret; 1147ac20de6fSZheng Yan 1148ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1149ac20de6fSZheng Yan 1150ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1151ac20de6fSZheng Yan parse_events_debug = 1; 1152ac20de6fSZheng Yan #endif 1153ac20de6fSZheng Yan ret = parse_events_parse(data, scanner); 1154ac20de6fSZheng Yan 1155ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1156ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1157ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1158ac20de6fSZheng Yan return ret; 1159ac20de6fSZheng Yan } 1160ac20de6fSZheng Yan 116190e2b22dSJiri Olsa /* 116290e2b22dSJiri Olsa * parse event config string, return a list of event terms. 116390e2b22dSJiri Olsa */ 116490e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 116590e2b22dSJiri Olsa { 116623b6339bSArnaldo Carvalho de Melo struct parse_events_terms data = { 116790e2b22dSJiri Olsa .terms = NULL, 116890e2b22dSJiri Olsa }; 116990e2b22dSJiri Olsa int ret; 117090e2b22dSJiri Olsa 117190e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_TERMS); 117290e2b22dSJiri Olsa if (!ret) { 117390e2b22dSJiri Olsa list_splice(data.terms, terms); 117474cf249dSArnaldo Carvalho de Melo zfree(&data.terms); 117590e2b22dSJiri Olsa return 0; 117690e2b22dSJiri Olsa } 117790e2b22dSJiri Olsa 1178b2c34fdeSAdrian Hunter if (data.terms) 117990e2b22dSJiri Olsa parse_events__free_terms(data.terms); 118090e2b22dSJiri Olsa return ret; 118190e2b22dSJiri Olsa } 118290e2b22dSJiri Olsa 1183b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1184b39b8393SJiri Olsa struct parse_events_error *err) 118586470930SIngo Molnar { 118623b6339bSArnaldo Carvalho de Melo struct parse_events_evlist data = { 118746010ab2SJiri Olsa .list = LIST_HEAD_INIT(data.list), 118846010ab2SJiri Olsa .idx = evlist->nr_entries, 1189b39b8393SJiri Olsa .error = err, 119046010ab2SJiri Olsa }; 119146010ab2SJiri Olsa int ret; 119286470930SIngo Molnar 119390e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1194dcb4e102SKan Liang perf_pmu__parse_cleanup(); 119589812fc8SJiri Olsa if (!ret) { 119615bfd2ccSWang Nan struct perf_evsel *last; 119715bfd2ccSWang Nan 1198854f7363SWang Nan if (list_empty(&data.list)) { 1199854f7363SWang Nan WARN_ONCE(true, "WARNING: event parser found nothing"); 1200854f7363SWang Nan return -1; 1201854f7363SWang Nan } 1202854f7363SWang Nan 1203f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &data.list); 120497f63e4aSNamhyung Kim evlist->nr_groups += data.nr_groups; 120515bfd2ccSWang Nan last = perf_evlist__last(evlist); 120615bfd2ccSWang Nan last->cmdline_group_boundary = true; 120715bfd2ccSWang Nan 120886470930SIngo Molnar return 0; 120986470930SIngo Molnar } 121086470930SIngo Molnar 12115d7be90eSJiri Olsa /* 12125d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 12135d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 12145d7be90eSJiri Olsa * need to bother. 12155d7be90eSJiri Olsa */ 121689812fc8SJiri Olsa return ret; 121789812fc8SJiri Olsa } 121889812fc8SJiri Olsa 1219b39b8393SJiri Olsa #define MAX_WIDTH 1000 1220b39b8393SJiri Olsa static int get_term_width(void) 1221b39b8393SJiri Olsa { 1222b39b8393SJiri Olsa struct winsize ws; 1223b39b8393SJiri Olsa 1224b39b8393SJiri Olsa get_term_dimensions(&ws); 1225b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1226b39b8393SJiri Olsa } 1227b39b8393SJiri Olsa 1228b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err, 1229b39b8393SJiri Olsa const char *event) 1230b39b8393SJiri Olsa { 1231b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1232b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1233b39b8393SJiri Olsa char *buf = (char *) event; 1234b39b8393SJiri Olsa int idx = 0; 1235b39b8393SJiri Olsa 1236b39b8393SJiri Olsa if (err->str) { 1237b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1238b39b8393SJiri Olsa int width = get_term_width() - 2; 1239b39b8393SJiri Olsa int len_event = strlen(event); 1240b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1241b39b8393SJiri Olsa 1242b39b8393SJiri Olsa /* 1243b39b8393SJiri Olsa * Maximum error index indent, we will cut 1244b39b8393SJiri Olsa * the event string if it's bigger. 1245b39b8393SJiri Olsa */ 1246141b2d31SAdrian Hunter int max_err_idx = 13; 1247b39b8393SJiri Olsa 1248b39b8393SJiri Olsa /* 1249b39b8393SJiri Olsa * Let's be specific with the message when 1250b39b8393SJiri Olsa * we have the precise error. 1251b39b8393SJiri Olsa */ 1252b39b8393SJiri Olsa str = "event syntax error: "; 1253b39b8393SJiri Olsa len_str = strlen(str); 1254b39b8393SJiri Olsa max_len = width - len_str; 1255b39b8393SJiri Olsa 1256b39b8393SJiri Olsa buf = _buf; 1257b39b8393SJiri Olsa 1258b39b8393SJiri Olsa /* We're cutting from the beggining. */ 1259b39b8393SJiri Olsa if (err->idx > max_err_idx) 1260b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1261b39b8393SJiri Olsa 1262b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1263b39b8393SJiri Olsa 1264b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1265b39b8393SJiri Olsa if (cut) 1266b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1267b39b8393SJiri Olsa 1268b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1269b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1270b39b8393SJiri Olsa buf[max_len] = 0; 1271b39b8393SJiri Olsa } 1272b39b8393SJiri Olsa 1273b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1274b39b8393SJiri Olsa } 1275b39b8393SJiri Olsa 1276b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1277b39b8393SJiri Olsa if (idx) { 1278b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1279b39b8393SJiri Olsa if (err->help) 1280b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1281b39b8393SJiri Olsa free(err->str); 1282b39b8393SJiri Olsa free(err->help); 1283b39b8393SJiri Olsa } 1284b39b8393SJiri Olsa 1285b39b8393SJiri Olsa fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1286b39b8393SJiri Olsa } 1287b39b8393SJiri Olsa 1288b39b8393SJiri Olsa #undef MAX_WIDTH 1289b39b8393SJiri Olsa 1290f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 12911d037ca1SIrina Tirdea int unset __maybe_unused) 1292f120f9d5SJiri Olsa { 1293f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1294b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1295b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 12969175ce1fSAndi Kleen 1297b39b8393SJiri Olsa if (ret) 1298b39b8393SJiri Olsa parse_events_print_error(&err, str); 1299b39b8393SJiri Olsa 13009175ce1fSAndi Kleen return ret; 1301f120f9d5SJiri Olsa } 1302f120f9d5SJiri Olsa 13034ba1faa1SWang Nan static int 13044ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 13054ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 13064ba1faa1SWang Nan const void *arg), 13074ba1faa1SWang Nan const void *arg) 1308c171b552SLi Zefan { 130969aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 13104ba1faa1SWang Nan int err; 1311c171b552SLi Zefan 1312854f7363SWang Nan /* 1313854f7363SWang Nan * Don't return when list_empty, give func a chance to report 1314854f7363SWang Nan * error when it found last == NULL. 1315854f7363SWang Nan * 1316854f7363SWang Nan * So no need to WARN here, let *func do this. 1317854f7363SWang Nan */ 1318361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 13190c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 132069aad6f1SArnaldo Carvalho de Melo 132115bfd2ccSWang Nan do { 13224ba1faa1SWang Nan err = (*func)(last, arg); 13234ba1faa1SWang Nan if (err) 1324c171b552SLi Zefan return -1; 13254ba1faa1SWang Nan if (!last) 13264ba1faa1SWang Nan return 0; 1327c171b552SLi Zefan 132815bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 132915bfd2ccSWang Nan return 0; 133015bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 133115bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 133215bfd2ccSWang Nan 1333c171b552SLi Zefan return 0; 1334c171b552SLi Zefan } 1335c171b552SLi Zefan 13364ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 13374ba1faa1SWang Nan { 13384ba1faa1SWang Nan const char *str = arg; 13394ba1faa1SWang Nan 13404ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 13414ba1faa1SWang Nan fprintf(stderr, 13424ba1faa1SWang Nan "--filter option should follow a -e tracepoint option\n"); 13434ba1faa1SWang Nan return -1; 13444ba1faa1SWang Nan } 13454ba1faa1SWang Nan 13464ba1faa1SWang Nan if (perf_evsel__append_filter(evsel, "&&", str) < 0) { 13474ba1faa1SWang Nan fprintf(stderr, 13484ba1faa1SWang Nan "not enough memory to hold filter string\n"); 13494ba1faa1SWang Nan return -1; 13504ba1faa1SWang Nan } 13514ba1faa1SWang Nan 13524ba1faa1SWang Nan return 0; 13534ba1faa1SWang Nan } 13544ba1faa1SWang Nan 13554ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 13564ba1faa1SWang Nan int unset __maybe_unused) 13574ba1faa1SWang Nan { 13584ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 13594ba1faa1SWang Nan 13604ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 13614ba1faa1SWang Nan (const void *)str); 13624ba1faa1SWang Nan } 13634ba1faa1SWang Nan 13644ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 13654ba1faa1SWang Nan const void *arg __maybe_unused) 13664ba1faa1SWang Nan { 13674ba1faa1SWang Nan char new_filter[64]; 13684ba1faa1SWang Nan 13694ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 13704ba1faa1SWang Nan fprintf(stderr, 13714ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 13724ba1faa1SWang Nan return -1; 13734ba1faa1SWang Nan } 13744ba1faa1SWang Nan 13754ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 13764ba1faa1SWang Nan 13774ba1faa1SWang Nan if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) { 13784ba1faa1SWang Nan fprintf(stderr, 13794ba1faa1SWang Nan "not enough memory to hold filter string\n"); 13804ba1faa1SWang Nan return -1; 13814ba1faa1SWang Nan } 13824ba1faa1SWang Nan 13834ba1faa1SWang Nan return 0; 13844ba1faa1SWang Nan } 13854ba1faa1SWang Nan 13864ba1faa1SWang Nan int exclude_perf(const struct option *opt, 13874ba1faa1SWang Nan const char *arg __maybe_unused, 13884ba1faa1SWang Nan int unset __maybe_unused) 13894ba1faa1SWang Nan { 13904ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 13914ba1faa1SWang Nan 13924ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 13934ba1faa1SWang Nan NULL); 13944ba1faa1SWang Nan } 13954ba1faa1SWang Nan 139686470930SIngo Molnar static const char * const event_type_descriptors[] = { 139786470930SIngo Molnar "Hardware event", 139886470930SIngo Molnar "Software event", 139986470930SIngo Molnar "Tracepoint event", 140086470930SIngo Molnar "Hardware cache event", 140141bdcb23SLiming Wang "Raw hardware event descriptor", 140241bdcb23SLiming Wang "Hardware breakpoint", 140386470930SIngo Molnar }; 140486470930SIngo Molnar 1405ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1406ab0e4800SYunlong Song { 1407ab0e4800SYunlong Song const char * const *as = a; 1408ab0e4800SYunlong Song const char * const *bs = b; 1409ab0e4800SYunlong Song 1410ab0e4800SYunlong Song return strcmp(*as, *bs); 1411ab0e4800SYunlong Song } 1412ab0e4800SYunlong Song 141386470930SIngo Molnar /* 1414f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1415f6bdafefSJason Baron */ 1416f6bdafefSJason Baron 1417a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1418a3277d2dSFrederic Weisbecker bool name_only) 1419f6bdafefSJason Baron { 1420f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 1421f6bdafefSJason Baron struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1422f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1423725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1424ab0e4800SYunlong Song char **evt_list = NULL; 1425ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1426ab0e4800SYunlong Song bool evt_num_known = false; 1427f6bdafefSJason Baron 1428ab0e4800SYunlong Song restart: 1429ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1430f6bdafefSJason Baron if (!sys_dir) 1431725b1368SEric Dumazet return; 1432f6bdafefSJason Baron 1433ab0e4800SYunlong Song if (evt_num_known) { 1434ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1435ab0e4800SYunlong Song if (!evt_list) 1436ab0e4800SYunlong Song goto out_close_sys_dir; 1437ab0e4800SYunlong Song } 1438ab0e4800SYunlong Song 14396b58e7f1SUlrich Drepper for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1440668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 1441668b8788SArnaldo Carvalho de Melo !strglobmatch(sys_dirent.d_name, subsys_glob)) 1442668b8788SArnaldo Carvalho de Melo continue; 1443725b1368SEric Dumazet 1444ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1445725b1368SEric Dumazet sys_dirent.d_name); 1446725b1368SEric Dumazet evt_dir = opendir(dir_path); 1447725b1368SEric Dumazet if (!evt_dir) 14486b58e7f1SUlrich Drepper continue; 1449725b1368SEric Dumazet 14506b58e7f1SUlrich Drepper for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1451668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 1452668b8788SArnaldo Carvalho de Melo !strglobmatch(evt_dirent.d_name, event_glob)) 1453668b8788SArnaldo Carvalho de Melo continue; 1454668b8788SArnaldo Carvalho de Melo 1455ab0e4800SYunlong Song if (!evt_num_known) { 1456ab0e4800SYunlong Song evt_num++; 1457a3277d2dSFrederic Weisbecker continue; 1458a3277d2dSFrederic Weisbecker } 1459a3277d2dSFrederic Weisbecker 1460f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 1461f6bdafefSJason Baron sys_dirent.d_name, evt_dirent.d_name); 1462ab0e4800SYunlong Song 1463ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 1464ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1465ab0e4800SYunlong Song goto out_close_evt_dir; 1466ab0e4800SYunlong Song evt_i++; 1467f6bdafefSJason Baron } 1468f6bdafefSJason Baron closedir(evt_dir); 1469f6bdafefSJason Baron } 1470f6bdafefSJason Baron closedir(sys_dir); 1471ab0e4800SYunlong Song 1472ab0e4800SYunlong Song if (!evt_num_known) { 1473ab0e4800SYunlong Song evt_num_known = true; 1474ab0e4800SYunlong Song goto restart; 1475ab0e4800SYunlong Song } 1476ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1477ab0e4800SYunlong Song evt_i = 0; 1478ab0e4800SYunlong Song while (evt_i < evt_num) { 1479ab0e4800SYunlong Song if (name_only) { 1480ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1481ab0e4800SYunlong Song continue; 1482ab0e4800SYunlong Song } 1483ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 1484ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1485ab0e4800SYunlong Song } 1486ab0e4800SYunlong Song if (evt_num) 1487ab0e4800SYunlong Song printf("\n"); 1488ab0e4800SYunlong Song 1489ab0e4800SYunlong Song out_free: 1490ab0e4800SYunlong Song evt_num = evt_i; 1491ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1492ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1493ab0e4800SYunlong Song zfree(&evt_list); 1494ab0e4800SYunlong Song return; 1495ab0e4800SYunlong Song 1496ab0e4800SYunlong Song out_close_evt_dir: 1497ab0e4800SYunlong Song closedir(evt_dir); 1498ab0e4800SYunlong Song out_close_sys_dir: 1499ab0e4800SYunlong Song closedir(sys_dir); 1500ab0e4800SYunlong Song 1501ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 1502ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1503ab0e4800SYunlong Song if (evt_list) 1504ab0e4800SYunlong Song goto out_free; 1505f6bdafefSJason Baron } 1506f6bdafefSJason Baron 1507f6bdafefSJason Baron /* 150820c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 150920c457b8SThomas Renninger */ 151020c457b8SThomas Renninger 151120c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 151220c457b8SThomas Renninger { 151320c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 151420c457b8SThomas Renninger struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 151520c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 151620c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 151720c457b8SThomas Renninger 1518ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 151920c457b8SThomas Renninger if (!sys_dir) 152020c457b8SThomas Renninger return 0; 152120c457b8SThomas Renninger 152220c457b8SThomas Renninger for_each_subsystem(sys_dir, sys_dirent, sys_next) { 152320c457b8SThomas Renninger 1524ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 152520c457b8SThomas Renninger sys_dirent.d_name); 152620c457b8SThomas Renninger evt_dir = opendir(dir_path); 152720c457b8SThomas Renninger if (!evt_dir) 152820c457b8SThomas Renninger continue; 152920c457b8SThomas Renninger 153020c457b8SThomas Renninger for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 153120c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 153220c457b8SThomas Renninger sys_dirent.d_name, evt_dirent.d_name); 153320c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 153420c457b8SThomas Renninger closedir(evt_dir); 153520c457b8SThomas Renninger closedir(sys_dir); 153620c457b8SThomas Renninger return 1; 153720c457b8SThomas Renninger } 153820c457b8SThomas Renninger } 153920c457b8SThomas Renninger closedir(evt_dir); 154020c457b8SThomas Renninger } 154120c457b8SThomas Renninger closedir(sys_dir); 154220c457b8SThomas Renninger return 0; 154320c457b8SThomas Renninger } 154420c457b8SThomas Renninger 1545b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 1546b41f1cecSNamhyung Kim { 1547b41f1cecSNamhyung Kim bool ret = true; 154888fee52eSVince Weaver int open_return; 1549b41f1cecSNamhyung Kim struct perf_evsel *evsel; 1550b41f1cecSNamhyung Kim struct perf_event_attr attr = { 1551b41f1cecSNamhyung Kim .type = type, 1552b41f1cecSNamhyung Kim .config = config, 1553b41f1cecSNamhyung Kim .disabled = 1, 1554b41f1cecSNamhyung Kim }; 1555b41f1cecSNamhyung Kim struct { 1556b41f1cecSNamhyung Kim struct thread_map map; 1557b41f1cecSNamhyung Kim int threads[1]; 1558b41f1cecSNamhyung Kim } tmap = { 1559b41f1cecSNamhyung Kim .map.nr = 1, 1560b41f1cecSNamhyung Kim .threads = { 0 }, 1561b41f1cecSNamhyung Kim }; 1562b41f1cecSNamhyung Kim 1563ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 1564b41f1cecSNamhyung Kim if (evsel) { 156588fee52eSVince Weaver open_return = perf_evsel__open(evsel, NULL, &tmap.map); 156688fee52eSVince Weaver ret = open_return >= 0; 156788fee52eSVince Weaver 156888fee52eSVince Weaver if (open_return == -EACCES) { 156988fee52eSVince Weaver /* 157088fee52eSVince Weaver * This happens if the paranoid value 157188fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 157288fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 157388fee52eSVince Weaver * by default as some ARM machines do not support it. 157488fee52eSVince Weaver * 157588fee52eSVince Weaver */ 157688fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 1577b41f1cecSNamhyung Kim ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; 157888fee52eSVince Weaver } 1579b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 1580b41f1cecSNamhyung Kim } 1581b41f1cecSNamhyung Kim 1582b41f1cecSNamhyung Kim return ret; 1583b41f1cecSNamhyung Kim } 1584b41f1cecSNamhyung Kim 1585a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 1586668b8788SArnaldo Carvalho de Melo { 1587ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 15880b668bc9SArnaldo Carvalho de Melo char name[64]; 1589ab0e4800SYunlong Song char **evt_list = NULL; 1590ab0e4800SYunlong Song bool evt_num_known = false; 1591ab0e4800SYunlong Song 1592ab0e4800SYunlong Song restart: 1593ab0e4800SYunlong Song if (evt_num_known) { 1594ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1595ab0e4800SYunlong Song if (!evt_list) 1596ab0e4800SYunlong Song goto out_enomem; 1597ab0e4800SYunlong Song } 1598668b8788SArnaldo Carvalho de Melo 1599668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 1600668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 1601668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 16020b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 1603668b8788SArnaldo Carvalho de Melo continue; 1604668b8788SArnaldo Carvalho de Melo 1605668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 16060b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 16070b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 1608947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 1609668b8788SArnaldo Carvalho de Melo continue; 1610668b8788SArnaldo Carvalho de Melo 1611b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 1612b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 1613b41f1cecSNamhyung Kim continue; 1614b41f1cecSNamhyung Kim 1615ab0e4800SYunlong Song if (!evt_num_known) { 1616ab0e4800SYunlong Song evt_num++; 1617ab0e4800SYunlong Song continue; 1618ab0e4800SYunlong Song } 1619ab0e4800SYunlong Song 1620ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 1621ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1622ab0e4800SYunlong Song goto out_enomem; 1623ab0e4800SYunlong Song evt_i++; 1624668b8788SArnaldo Carvalho de Melo } 1625668b8788SArnaldo Carvalho de Melo } 1626668b8788SArnaldo Carvalho de Melo } 1627668b8788SArnaldo Carvalho de Melo 1628ab0e4800SYunlong Song if (!evt_num_known) { 1629ab0e4800SYunlong Song evt_num_known = true; 1630ab0e4800SYunlong Song goto restart; 1631ab0e4800SYunlong Song } 1632ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1633ab0e4800SYunlong Song evt_i = 0; 1634ab0e4800SYunlong Song while (evt_i < evt_num) { 1635ab0e4800SYunlong Song if (name_only) { 1636ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1637ab0e4800SYunlong Song continue; 1638ab0e4800SYunlong Song } 1639ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 1640ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 1641ab0e4800SYunlong Song } 1642ab0e4800SYunlong Song if (evt_num) 1643dc098b35SAndi Kleen printf("\n"); 1644ab0e4800SYunlong Song 1645ab0e4800SYunlong Song out_free: 1646ab0e4800SYunlong Song evt_num = evt_i; 1647ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1648ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1649ab0e4800SYunlong Song zfree(&evt_list); 1650ab0e4800SYunlong Song return evt_num; 1651ab0e4800SYunlong Song 1652ab0e4800SYunlong Song out_enomem: 1653ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 1654ab0e4800SYunlong Song if (evt_list) 1655ab0e4800SYunlong Song goto out_free; 1656ab0e4800SYunlong Song return evt_num; 1657668b8788SArnaldo Carvalho de Melo } 1658668b8788SArnaldo Carvalho de Melo 1659705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 1660a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 1661a3277d2dSFrederic Weisbecker bool name_only) 166286470930SIngo Molnar { 1663ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 1664947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 1665ab0e4800SYunlong Song char **evt_list = NULL; 1666ab0e4800SYunlong Song bool evt_num_known = false; 1667ab0e4800SYunlong Song 1668ab0e4800SYunlong Song restart: 1669ab0e4800SYunlong Song if (evt_num_known) { 1670ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1671ab0e4800SYunlong Song if (!evt_list) 1672ab0e4800SYunlong Song goto out_enomem; 1673ab0e4800SYunlong Song syms -= max; 1674ab0e4800SYunlong Song } 167586470930SIngo Molnar 16761dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 1677668b8788SArnaldo Carvalho de Melo 1678668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 1679668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 1680668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 1681668b8788SArnaldo Carvalho de Melo continue; 168286470930SIngo Molnar 1683b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 1684b41f1cecSNamhyung Kim continue; 1685b41f1cecSNamhyung Kim 1686ab0e4800SYunlong Song if (!evt_num_known) { 1687ab0e4800SYunlong Song evt_num++; 1688a3277d2dSFrederic Weisbecker continue; 1689a3277d2dSFrederic Weisbecker } 1690a3277d2dSFrederic Weisbecker 1691ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 1692947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 169374d5b588SJaswinder Singh Rajput else 1694947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 169586470930SIngo Molnar 1696ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 1697ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1698ab0e4800SYunlong Song goto out_enomem; 1699ab0e4800SYunlong Song evt_i++; 170086470930SIngo Molnar } 170186470930SIngo Molnar 1702ab0e4800SYunlong Song if (!evt_num_known) { 1703ab0e4800SYunlong Song evt_num_known = true; 1704ab0e4800SYunlong Song goto restart; 1705ab0e4800SYunlong Song } 1706ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1707ab0e4800SYunlong Song evt_i = 0; 1708ab0e4800SYunlong Song while (evt_i < evt_num) { 1709ab0e4800SYunlong Song if (name_only) { 1710ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1711ab0e4800SYunlong Song continue; 1712ab0e4800SYunlong Song } 1713ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 1714ab0e4800SYunlong Song } 1715ab0e4800SYunlong Song if (evt_num) 1716689d3018SMarti Raudsepp printf("\n"); 1717ab0e4800SYunlong Song 1718ab0e4800SYunlong Song out_free: 1719ab0e4800SYunlong Song evt_num = evt_i; 1720ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1721ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1722ab0e4800SYunlong Song zfree(&evt_list); 1723ab0e4800SYunlong Song return; 1724ab0e4800SYunlong Song 1725ab0e4800SYunlong Song out_enomem: 1726ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 1727ab0e4800SYunlong Song if (evt_list) 1728ab0e4800SYunlong Song goto out_free; 1729668b8788SArnaldo Carvalho de Melo } 17301dc12760SJiri Olsa 17311dc12760SJiri Olsa /* 17321dc12760SJiri Olsa * Print the help text for the event symbols: 17331dc12760SJiri Olsa */ 1734a3277d2dSFrederic Weisbecker void print_events(const char *event_glob, bool name_only) 17351dc12760SJiri Olsa { 17361dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 1737a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 17381dc12760SJiri Olsa 17391dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 1740a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 17411dc12760SJiri Olsa 1742a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 174373c24cb8SJaswinder Singh Rajput 1744dc098b35SAndi Kleen print_pmu_events(event_glob, name_only); 1745dc098b35SAndi Kleen 1746668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 1747668b8788SArnaldo Carvalho de Melo return; 174873c24cb8SJaswinder Singh Rajput 1749a3277d2dSFrederic Weisbecker if (!name_only) { 1750947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 17515f537a26SJiri Olsa "rNNN", 17521cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 17535f537a26SJiri Olsa printf(" %-50s [%s]\n", 17545f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 17555f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 1756af3df2cfSBorislav Petkov printf(" (see 'man perf-list' on how to encode it)\n"); 1757689d3018SMarti Raudsepp printf("\n"); 175886470930SIngo Molnar 1759947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 17603741eb9fSJacob Shin "mem:<addr>[/len][:access]", 176141bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 17621b290d67SFrederic Weisbecker printf("\n"); 1763a3277d2dSFrederic Weisbecker } 17641b290d67SFrederic Weisbecker 1765a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 176686470930SIngo Molnar } 17678f707d84SJiri Olsa 17686cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 17698f707d84SJiri Olsa { 177016fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 17718f707d84SJiri Olsa } 17728f707d84SJiri Olsa 17736cee6cd3SArnaldo Carvalho de Melo static int new_term(struct parse_events_term **_term, int type_val, 177416fa7e82SJiri Olsa int type_term, char *config, 1775cecf3a2eSJiri Olsa char *str, u64 num, int err_term, int err_val) 17768f707d84SJiri Olsa { 17776cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 17788f707d84SJiri Olsa 17798f707d84SJiri Olsa term = zalloc(sizeof(*term)); 17808f707d84SJiri Olsa if (!term) 17818f707d84SJiri Olsa return -ENOMEM; 17828f707d84SJiri Olsa 17838f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 178416fa7e82SJiri Olsa term->type_val = type_val; 178516fa7e82SJiri Olsa term->type_term = type_term; 17868f707d84SJiri Olsa term->config = config; 1787cecf3a2eSJiri Olsa term->err_term = err_term; 1788cecf3a2eSJiri Olsa term->err_val = err_val; 17898f707d84SJiri Olsa 179016fa7e82SJiri Olsa switch (type_val) { 17918f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 17928f707d84SJiri Olsa term->val.num = num; 17938f707d84SJiri Olsa break; 17948f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 17958f707d84SJiri Olsa term->val.str = str; 17968f707d84SJiri Olsa break; 17978f707d84SJiri Olsa default: 17984be8be6bSAdrian Hunter free(term); 17998f707d84SJiri Olsa return -EINVAL; 18008f707d84SJiri Olsa } 18018f707d84SJiri Olsa 18028f707d84SJiri Olsa *_term = term; 18038f707d84SJiri Olsa return 0; 18048f707d84SJiri Olsa } 18058f707d84SJiri Olsa 18066cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 1807cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 1808bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 180916fa7e82SJiri Olsa { 1810bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 1811bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 1812bb78ce7dSAdrian Hunter 181316fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, 1814cecf3a2eSJiri Olsa config, NULL, num, 1815cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 1816cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 181716fa7e82SJiri Olsa } 181816fa7e82SJiri Olsa 18196cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 1820cecf3a2eSJiri Olsa int type_term, char *config, char *str, 1821bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 182216fa7e82SJiri Olsa { 1823bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 1824bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 1825bb78ce7dSAdrian Hunter 182616fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, 1827cecf3a2eSJiri Olsa config, str, 0, 1828cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 1829cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 183016fa7e82SJiri Olsa } 183116fa7e82SJiri Olsa 18326cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 18331d33d6dcSJiri Olsa char *config, unsigned idx) 18341d33d6dcSJiri Olsa { 18351d33d6dcSJiri Olsa struct event_symbol *sym; 18361d33d6dcSJiri Olsa 18371d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 18381d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 18391d33d6dcSJiri Olsa 18401d33d6dcSJiri Olsa if (config) 18411d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 18421d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, config, 1843cecf3a2eSJiri Olsa (char *) sym->symbol, 0, 0, 0); 18441d33d6dcSJiri Olsa else 18451d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 18461d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, 1847cecf3a2eSJiri Olsa (char *) "event", (char *) sym->symbol, 1848cecf3a2eSJiri Olsa 0, 0, 0); 18491d33d6dcSJiri Olsa } 18501d33d6dcSJiri Olsa 18516cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 18526cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1853a6146d50SZheng Yan { 1854a6146d50SZheng Yan return new_term(new, term->type_val, term->type_term, term->config, 1855cecf3a2eSJiri Olsa term->val.str, term->val.num, 1856cecf3a2eSJiri Olsa term->err_term, term->err_val); 1857a6146d50SZheng Yan } 1858a6146d50SZheng Yan 18598f707d84SJiri Olsa void parse_events__free_terms(struct list_head *terms) 18608f707d84SJiri Olsa { 18616cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 18628f707d84SJiri Olsa 18638f707d84SJiri Olsa list_for_each_entry_safe(term, h, terms, list) 18648f707d84SJiri Olsa free(term); 18658f707d84SJiri Olsa } 1866b39b8393SJiri Olsa 1867b39b8393SJiri Olsa void parse_events_evlist_error(struct parse_events_evlist *data, 1868b39b8393SJiri Olsa int idx, const char *str) 1869b39b8393SJiri Olsa { 1870b39b8393SJiri Olsa struct parse_events_error *err = data->error; 1871b39b8393SJiri Olsa 1872a6ced2beSAdrian Hunter if (!err) 1873a6ced2beSAdrian Hunter return; 1874b39b8393SJiri Olsa err->idx = idx; 1875b39b8393SJiri Olsa err->str = strdup(str); 1876b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 1877b39b8393SJiri Olsa } 1878