1d2709c7cSDavid Howells #include <linux/hw_breakpoint.h> 286470930SIngo Molnar #include "util.h" 36b58e7f1SUlrich Drepper #include "../perf.h" 4361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 569aad6f1SArnaldo Carvalho de Melo #include "evsel.h" 686470930SIngo Molnar #include "parse-options.h" 786470930SIngo Molnar #include "parse-events.h" 886470930SIngo Molnar #include "exec_cmd.h" 942f60c2dSKan Liang #include "string.h" 105aab621bSArnaldo Carvalho de Melo #include "symbol.h" 115beeded1SJason Baron #include "cache.h" 128755a8f2SArjan van de Ven #include "header.h" 136e81c74cSMasami Hiramatsu #include "debug.h" 14553873e1SBorislav Petkov #include <api/fs/debugfs.h> 15ac20de6fSZheng Yan #include "parse-events-bison.h" 1690e2b22dSJiri Olsa #define YY_EXTRA_TYPE int 1789812fc8SJiri Olsa #include "parse-events-flex.h" 185f537a26SJiri Olsa #include "pmu.h" 19b41f1cecSNamhyung Kim #include "thread_map.h" 20f30a79b0SJiri Olsa #include "cpumap.h" 21b39b8393SJiri Olsa #include "asm/bug.h" 2289812fc8SJiri Olsa 2389812fc8SJiri Olsa #define MAX_NAME_LEN 100 2486470930SIngo Molnar 2582ba1f2fSJiri Olsa #ifdef PARSER_DEBUG 2682ba1f2fSJiri Olsa extern int parse_events_debug; 2782ba1f2fSJiri Olsa #endif 28ac20de6fSZheng Yan int parse_events_parse(void *data, void *scanner); 29bcd3279fSFrederic Weisbecker 30dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list; 31dcb4e102SKan Liang /* 32dcb4e102SKan Liang * The variable indicates the number of supported pmu event symbols. 33dcb4e102SKan Liang * 0 means not initialized and ready to init 34dcb4e102SKan Liang * -1 means failed to init, don't try anymore 35dcb4e102SKan Liang * >0 is the number of supported pmu event symbols 36dcb4e102SKan Liang */ 37dcb4e102SKan Liang static int perf_pmu_events_list_num; 38dcb4e102SKan Liang 39705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 401dc12760SJiri Olsa [PERF_COUNT_HW_CPU_CYCLES] = { 411dc12760SJiri Olsa .symbol = "cpu-cycles", 421dc12760SJiri Olsa .alias = "cycles", 431dc12760SJiri Olsa }, 441dc12760SJiri Olsa [PERF_COUNT_HW_INSTRUCTIONS] = { 451dc12760SJiri Olsa .symbol = "instructions", 461dc12760SJiri Olsa .alias = "", 471dc12760SJiri Olsa }, 481dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_REFERENCES] = { 491dc12760SJiri Olsa .symbol = "cache-references", 501dc12760SJiri Olsa .alias = "", 511dc12760SJiri Olsa }, 521dc12760SJiri Olsa [PERF_COUNT_HW_CACHE_MISSES] = { 531dc12760SJiri Olsa .symbol = "cache-misses", 541dc12760SJiri Olsa .alias = "", 551dc12760SJiri Olsa }, 561dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 571dc12760SJiri Olsa .symbol = "branch-instructions", 581dc12760SJiri Olsa .alias = "branches", 591dc12760SJiri Olsa }, 601dc12760SJiri Olsa [PERF_COUNT_HW_BRANCH_MISSES] = { 611dc12760SJiri Olsa .symbol = "branch-misses", 621dc12760SJiri Olsa .alias = "", 631dc12760SJiri Olsa }, 641dc12760SJiri Olsa [PERF_COUNT_HW_BUS_CYCLES] = { 651dc12760SJiri Olsa .symbol = "bus-cycles", 661dc12760SJiri Olsa .alias = "", 671dc12760SJiri Olsa }, 681dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 691dc12760SJiri Olsa .symbol = "stalled-cycles-frontend", 701dc12760SJiri Olsa .alias = "idle-cycles-frontend", 711dc12760SJiri Olsa }, 721dc12760SJiri Olsa [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 731dc12760SJiri Olsa .symbol = "stalled-cycles-backend", 741dc12760SJiri Olsa .alias = "idle-cycles-backend", 751dc12760SJiri Olsa }, 761dc12760SJiri Olsa [PERF_COUNT_HW_REF_CPU_CYCLES] = { 771dc12760SJiri Olsa .symbol = "ref-cycles", 781dc12760SJiri Olsa .alias = "", 791dc12760SJiri Olsa }, 801dc12760SJiri Olsa }; 8186470930SIngo Molnar 82705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 831dc12760SJiri Olsa [PERF_COUNT_SW_CPU_CLOCK] = { 841dc12760SJiri Olsa .symbol = "cpu-clock", 851dc12760SJiri Olsa .alias = "", 861dc12760SJiri Olsa }, 871dc12760SJiri Olsa [PERF_COUNT_SW_TASK_CLOCK] = { 881dc12760SJiri Olsa .symbol = "task-clock", 891dc12760SJiri Olsa .alias = "", 901dc12760SJiri Olsa }, 911dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS] = { 921dc12760SJiri Olsa .symbol = "page-faults", 931dc12760SJiri Olsa .alias = "faults", 941dc12760SJiri Olsa }, 951dc12760SJiri Olsa [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 961dc12760SJiri Olsa .symbol = "context-switches", 971dc12760SJiri Olsa .alias = "cs", 981dc12760SJiri Olsa }, 991dc12760SJiri Olsa [PERF_COUNT_SW_CPU_MIGRATIONS] = { 1001dc12760SJiri Olsa .symbol = "cpu-migrations", 1011dc12760SJiri Olsa .alias = "migrations", 1021dc12760SJiri Olsa }, 1031dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 1041dc12760SJiri Olsa .symbol = "minor-faults", 1051dc12760SJiri Olsa .alias = "", 1061dc12760SJiri Olsa }, 1071dc12760SJiri Olsa [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 1081dc12760SJiri Olsa .symbol = "major-faults", 1091dc12760SJiri Olsa .alias = "", 1101dc12760SJiri Olsa }, 1111dc12760SJiri Olsa [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 1121dc12760SJiri Olsa .symbol = "alignment-faults", 1131dc12760SJiri Olsa .alias = "", 1141dc12760SJiri Olsa }, 1151dc12760SJiri Olsa [PERF_COUNT_SW_EMULATION_FAULTS] = { 1161dc12760SJiri Olsa .symbol = "emulation-faults", 1171dc12760SJiri Olsa .alias = "", 1181dc12760SJiri Olsa }, 119d22d1a2aSAdrian Hunter [PERF_COUNT_SW_DUMMY] = { 120d22d1a2aSAdrian Hunter .symbol = "dummy", 121d22d1a2aSAdrian Hunter .alias = "", 122d22d1a2aSAdrian Hunter }, 12386470930SIngo Molnar }; 12486470930SIngo Molnar 125cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \ 126cdd6c482SIngo Molnar ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 12786470930SIngo Molnar 128cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 129cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 130cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 131cdd6c482SIngo Molnar #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 13286470930SIngo Molnar 1336b58e7f1SUlrich Drepper #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ 134f6bdafefSJason Baron while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ 1356b58e7f1SUlrich Drepper if (sys_dirent.d_type == DT_DIR && \ 136f6bdafefSJason Baron (strcmp(sys_dirent.d_name, ".")) && \ 137f6bdafefSJason Baron (strcmp(sys_dirent.d_name, ".."))) 138f6bdafefSJason Baron 139ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 140ae07b63fSPeter Zijlstra { 141ae07b63fSPeter Zijlstra char evt_path[MAXPATHLEN]; 142ae07b63fSPeter Zijlstra int fd; 143ae07b63fSPeter Zijlstra 144ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 145ae07b63fSPeter Zijlstra sys_dir->d_name, evt_dir->d_name); 146ae07b63fSPeter Zijlstra fd = open(evt_path, O_RDONLY); 147ae07b63fSPeter Zijlstra if (fd < 0) 148ae07b63fSPeter Zijlstra return -EINVAL; 149ae07b63fSPeter Zijlstra close(fd); 150ae07b63fSPeter Zijlstra 151ae07b63fSPeter Zijlstra return 0; 152ae07b63fSPeter Zijlstra } 153ae07b63fSPeter Zijlstra 1546b58e7f1SUlrich Drepper #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ 155f6bdafefSJason Baron while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ 1566b58e7f1SUlrich Drepper if (evt_dirent.d_type == DT_DIR && \ 157f6bdafefSJason Baron (strcmp(evt_dirent.d_name, ".")) && \ 158ae07b63fSPeter Zijlstra (strcmp(evt_dirent.d_name, "..")) && \ 159ae07b63fSPeter Zijlstra (!tp_event_has_id(&sys_dirent, &evt_dirent))) 160f6bdafefSJason Baron 161270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512 162f6bdafefSJason Baron 163f6bdafefSJason Baron 1641ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config) 165f6bdafefSJason Baron { 1661ef2ed10SFrederic Weisbecker struct tracepoint_path *path = NULL; 167f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 168f6bdafefSJason Baron struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1698aa8a7c8SStephane Eranian char id_buf[24]; 170725b1368SEric Dumazet int fd; 171f6bdafefSJason Baron u64 id; 172f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 173725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 174f6bdafefSJason Baron 175ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 176f6bdafefSJason Baron if (!sys_dir) 177725b1368SEric Dumazet return NULL; 178f6bdafefSJason Baron 1796b58e7f1SUlrich Drepper for_each_subsystem(sys_dir, sys_dirent, sys_next) { 180725b1368SEric Dumazet 181ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 182725b1368SEric Dumazet sys_dirent.d_name); 183725b1368SEric Dumazet evt_dir = opendir(dir_path); 184725b1368SEric Dumazet if (!evt_dir) 1856b58e7f1SUlrich Drepper continue; 186725b1368SEric Dumazet 1876b58e7f1SUlrich Drepper for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 188725b1368SEric Dumazet 189725b1368SEric Dumazet snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 190f6bdafefSJason Baron evt_dirent.d_name); 191725b1368SEric Dumazet fd = open(evt_path, O_RDONLY); 192f6bdafefSJason Baron if (fd < 0) 193f6bdafefSJason Baron continue; 194f6bdafefSJason Baron if (read(fd, id_buf, sizeof(id_buf)) < 0) { 195f6bdafefSJason Baron close(fd); 196f6bdafefSJason Baron continue; 197f6bdafefSJason Baron } 198f6bdafefSJason Baron close(fd); 199f6bdafefSJason Baron id = atoll(id_buf); 200f6bdafefSJason Baron if (id == config) { 201f6bdafefSJason Baron closedir(evt_dir); 202f6bdafefSJason Baron closedir(sys_dir); 20359b4caebSJulia Lawall path = zalloc(sizeof(*path)); 2041ef2ed10SFrederic Weisbecker path->system = malloc(MAX_EVENT_LENGTH); 2051ef2ed10SFrederic Weisbecker if (!path->system) { 2061ef2ed10SFrederic Weisbecker free(path); 2071ef2ed10SFrederic Weisbecker return NULL; 2081ef2ed10SFrederic Weisbecker } 2091ef2ed10SFrederic Weisbecker path->name = malloc(MAX_EVENT_LENGTH); 2101ef2ed10SFrederic Weisbecker if (!path->name) { 21174cf249dSArnaldo Carvalho de Melo zfree(&path->system); 2121ef2ed10SFrederic Weisbecker free(path); 2131ef2ed10SFrederic Weisbecker return NULL; 2141ef2ed10SFrederic Weisbecker } 2151ef2ed10SFrederic Weisbecker strncpy(path->system, sys_dirent.d_name, 2161ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2171ef2ed10SFrederic Weisbecker strncpy(path->name, evt_dirent.d_name, 2181ef2ed10SFrederic Weisbecker MAX_EVENT_LENGTH); 2191ef2ed10SFrederic Weisbecker return path; 220f6bdafefSJason Baron } 221f6bdafefSJason Baron } 222f6bdafefSJason Baron closedir(evt_dir); 223f6bdafefSJason Baron } 224f6bdafefSJason Baron 225f6bdafefSJason Baron closedir(sys_dir); 2261ef2ed10SFrederic Weisbecker return NULL; 2271ef2ed10SFrederic Weisbecker } 2281ef2ed10SFrederic Weisbecker 229e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name) 230e7c93f09SNamhyung Kim { 231e7c93f09SNamhyung Kim struct tracepoint_path *path = zalloc(sizeof(*path)); 232e7c93f09SNamhyung Kim char *str = strchr(name, ':'); 233e7c93f09SNamhyung Kim 234e7c93f09SNamhyung Kim if (path == NULL || str == NULL) { 235e7c93f09SNamhyung Kim free(path); 236e7c93f09SNamhyung Kim return NULL; 237e7c93f09SNamhyung Kim } 238e7c93f09SNamhyung Kim 239e7c93f09SNamhyung Kim path->system = strndup(name, str - name); 240e7c93f09SNamhyung Kim path->name = strdup(str+1); 241e7c93f09SNamhyung Kim 242e7c93f09SNamhyung Kim if (path->system == NULL || path->name == NULL) { 24374cf249dSArnaldo Carvalho de Melo zfree(&path->system); 24474cf249dSArnaldo Carvalho de Melo zfree(&path->name); 245e7c93f09SNamhyung Kim free(path); 246e7c93f09SNamhyung Kim path = NULL; 247e7c93f09SNamhyung Kim } 248e7c93f09SNamhyung Kim 249e7c93f09SNamhyung Kim return path; 250e7c93f09SNamhyung Kim } 251e7c93f09SNamhyung Kim 2521424dc96SDavid Ahern const char *event_type(int type) 2531424dc96SDavid Ahern { 2541424dc96SDavid Ahern switch (type) { 2551424dc96SDavid Ahern case PERF_TYPE_HARDWARE: 2561424dc96SDavid Ahern return "hardware"; 2571424dc96SDavid Ahern 2581424dc96SDavid Ahern case PERF_TYPE_SOFTWARE: 2591424dc96SDavid Ahern return "software"; 2601424dc96SDavid Ahern 2611424dc96SDavid Ahern case PERF_TYPE_TRACEPOINT: 2621424dc96SDavid Ahern return "tracepoint"; 2631424dc96SDavid Ahern 2641424dc96SDavid Ahern case PERF_TYPE_HW_CACHE: 2651424dc96SDavid Ahern return "hardware-cache"; 2661424dc96SDavid Ahern 2671424dc96SDavid Ahern default: 2681424dc96SDavid Ahern break; 2691424dc96SDavid Ahern } 2701424dc96SDavid Ahern 2711424dc96SDavid Ahern return "unknown"; 2721424dc96SDavid Ahern } 2731424dc96SDavid Ahern 2747ae92e74SYan, Zheng 2757ae92e74SYan, Zheng 276410136f5SStephane Eranian static struct perf_evsel * 277410136f5SStephane Eranian __add_event(struct list_head *list, int *idx, 2787ae92e74SYan, Zheng struct perf_event_attr *attr, 279930a2e29SJiri Olsa char *name, struct cpu_map *cpus, 280930a2e29SJiri Olsa struct list_head *config_terms) 28189812fc8SJiri Olsa { 28289812fc8SJiri Olsa struct perf_evsel *evsel; 28389812fc8SJiri Olsa 28489812fc8SJiri Olsa event_attr_init(attr); 28589812fc8SJiri Olsa 286ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attr, (*idx)++); 287c5cd8ac0SDavid Ahern if (!evsel) 288410136f5SStephane Eranian return NULL; 28989812fc8SJiri Olsa 290f30a79b0SJiri Olsa if (cpus) 291f30a79b0SJiri Olsa evsel->cpus = cpu_map__get(cpus); 292f30a79b0SJiri Olsa 2939db1763cSArnaldo Carvalho de Melo if (name) 29489812fc8SJiri Olsa evsel->name = strdup(name); 295930a2e29SJiri Olsa 296930a2e29SJiri Olsa if (config_terms) 297930a2e29SJiri Olsa list_splice(config_terms, &evsel->config_terms); 298930a2e29SJiri Olsa 299b847cbdcSJiri Olsa list_add_tail(&evsel->node, list); 300410136f5SStephane Eranian return evsel; 30189812fc8SJiri Olsa } 30289812fc8SJiri Olsa 303c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx, 304930a2e29SJiri Olsa struct perf_event_attr *attr, char *name, 305930a2e29SJiri Olsa struct list_head *config_terms) 3067ae92e74SYan, Zheng { 307930a2e29SJiri Olsa return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; 3087ae92e74SYan, Zheng } 3097ae92e74SYan, Zheng 3100b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 31186470930SIngo Molnar { 31286470930SIngo Molnar int i, j; 31361c45981SPaul Mackerras int n, longest = -1; 31486470930SIngo Molnar 31586470930SIngo Molnar for (i = 0; i < size; i++) { 3160b668bc9SArnaldo Carvalho de Melo for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 31761c45981SPaul Mackerras n = strlen(names[i][j]); 31889812fc8SJiri Olsa if (n > longest && !strncasecmp(str, names[i][j], n)) 31961c45981SPaul Mackerras longest = n; 32061c45981SPaul Mackerras } 32189812fc8SJiri Olsa if (longest > 0) 32286470930SIngo Molnar return i; 32386470930SIngo Molnar } 32486470930SIngo Molnar 3258953645fSIngo Molnar return -1; 32686470930SIngo Molnar } 32786470930SIngo Molnar 328c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx, 32989812fc8SJiri Olsa char *type, char *op_result1, char *op_result2) 33086470930SIngo Molnar { 33189812fc8SJiri Olsa struct perf_event_attr attr; 33289812fc8SJiri Olsa char name[MAX_NAME_LEN]; 33361c45981SPaul Mackerras int cache_type = -1, cache_op = -1, cache_result = -1; 33489812fc8SJiri Olsa char *op_result[2] = { op_result1, op_result2 }; 33589812fc8SJiri Olsa int i, n; 33686470930SIngo Molnar 33786470930SIngo Molnar /* 33886470930SIngo Molnar * No fallback - if we cannot get a clear cache type 33986470930SIngo Molnar * then bail out: 34086470930SIngo Molnar */ 3410b668bc9SArnaldo Carvalho de Melo cache_type = parse_aliases(type, perf_evsel__hw_cache, 34289812fc8SJiri Olsa PERF_COUNT_HW_CACHE_MAX); 34386470930SIngo Molnar if (cache_type == -1) 34489812fc8SJiri Olsa return -EINVAL; 34586470930SIngo Molnar 34689812fc8SJiri Olsa n = snprintf(name, MAX_NAME_LEN, "%s", type); 34789812fc8SJiri Olsa 34889812fc8SJiri Olsa for (i = 0; (i < 2) && (op_result[i]); i++) { 34989812fc8SJiri Olsa char *str = op_result[i]; 35089812fc8SJiri Olsa 351275ef387SJiri Olsa n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 35261c45981SPaul Mackerras 35361c45981SPaul Mackerras if (cache_op == -1) { 3540b668bc9SArnaldo Carvalho de Melo cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 35561c45981SPaul Mackerras PERF_COUNT_HW_CACHE_OP_MAX); 35661c45981SPaul Mackerras if (cache_op >= 0) { 3570b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 35889812fc8SJiri Olsa return -EINVAL; 35961c45981SPaul Mackerras continue; 36061c45981SPaul Mackerras } 36161c45981SPaul Mackerras } 36261c45981SPaul Mackerras 36361c45981SPaul Mackerras if (cache_result == -1) { 3640b668bc9SArnaldo Carvalho de Melo cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 36561c45981SPaul Mackerras PERF_COUNT_HW_CACHE_RESULT_MAX); 36661c45981SPaul Mackerras if (cache_result >= 0) 36761c45981SPaul Mackerras continue; 36861c45981SPaul Mackerras } 36961c45981SPaul Mackerras } 37061c45981SPaul Mackerras 37186470930SIngo Molnar /* 37286470930SIngo Molnar * Fall back to reads: 37386470930SIngo Molnar */ 3748953645fSIngo Molnar if (cache_op == -1) 3758953645fSIngo Molnar cache_op = PERF_COUNT_HW_CACHE_OP_READ; 37686470930SIngo Molnar 37786470930SIngo Molnar /* 37886470930SIngo Molnar * Fall back to accesses: 37986470930SIngo Molnar */ 38086470930SIngo Molnar if (cache_result == -1) 38186470930SIngo Molnar cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 38286470930SIngo Molnar 38389812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 38489812fc8SJiri Olsa attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 38589812fc8SJiri Olsa attr.type = PERF_TYPE_HW_CACHE; 386930a2e29SJiri Olsa return add_event(list, idx, &attr, name, NULL); 38786470930SIngo Molnar } 38886470930SIngo Molnar 389c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx, 39089812fc8SJiri Olsa char *sys_name, char *evt_name) 391bcd3279fSFrederic Weisbecker { 39282fe1c29SArnaldo Carvalho de Melo struct perf_evsel *evsel; 393bcd3279fSFrederic Weisbecker 394ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 395c5cd8ac0SDavid Ahern if (!evsel) 39682fe1c29SArnaldo Carvalho de Melo return -ENOMEM; 397bcd3279fSFrederic Weisbecker 39882fe1c29SArnaldo Carvalho de Melo list_add_tail(&evsel->node, list); 399c5cd8ac0SDavid Ahern 40082fe1c29SArnaldo Carvalho de Melo return 0; 401bcd3279fSFrederic Weisbecker } 402bcd3279fSFrederic Weisbecker 403c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx, 40489812fc8SJiri Olsa char *sys_name, char *evt_name) 405bcd3279fSFrederic Weisbecker { 406bcd3279fSFrederic Weisbecker char evt_path[MAXPATHLEN]; 407bcd3279fSFrederic Weisbecker struct dirent *evt_ent; 408bcd3279fSFrederic Weisbecker DIR *evt_dir; 40989812fc8SJiri Olsa int ret = 0; 410bcd3279fSFrederic Weisbecker 411ebf294bfSArnaldo Carvalho de Melo snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 412bcd3279fSFrederic Weisbecker evt_dir = opendir(evt_path); 413bcd3279fSFrederic Weisbecker if (!evt_dir) { 414bcd3279fSFrederic Weisbecker perror("Can't open event dir"); 41589812fc8SJiri Olsa return -1; 416bcd3279fSFrederic Weisbecker } 417bcd3279fSFrederic Weisbecker 41889812fc8SJiri Olsa while (!ret && (evt_ent = readdir(evt_dir))) { 419bcd3279fSFrederic Weisbecker if (!strcmp(evt_ent->d_name, ".") 420bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "..") 421bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "enable") 422bcd3279fSFrederic Weisbecker || !strcmp(evt_ent->d_name, "filter")) 423bcd3279fSFrederic Weisbecker continue; 424bcd3279fSFrederic Weisbecker 42589812fc8SJiri Olsa if (!strglobmatch(evt_ent->d_name, evt_name)) 426fb1d2edfSMasami Hiramatsu continue; 427fb1d2edfSMasami Hiramatsu 42889812fc8SJiri Olsa ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name); 429bcd3279fSFrederic Weisbecker } 430bcd3279fSFrederic Weisbecker 4310bd3f084SJiri Olsa closedir(evt_dir); 43289812fc8SJiri Olsa return ret; 433bcd3279fSFrederic Weisbecker } 434bcd3279fSFrederic Weisbecker 435c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx, 436f35488f9SJiri Olsa char *sys_name, char *evt_name) 437f35488f9SJiri Olsa { 438f35488f9SJiri Olsa return strpbrk(evt_name, "*?") ? 439f35488f9SJiri Olsa add_tracepoint_multi_event(list, idx, sys_name, evt_name) : 440f35488f9SJiri Olsa add_tracepoint(list, idx, sys_name, evt_name); 441f35488f9SJiri Olsa } 442f35488f9SJiri Olsa 443c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 444f35488f9SJiri Olsa char *sys_name, char *evt_name) 445f35488f9SJiri Olsa { 446f35488f9SJiri Olsa struct dirent *events_ent; 447f35488f9SJiri Olsa DIR *events_dir; 448f35488f9SJiri Olsa int ret = 0; 449f35488f9SJiri Olsa 450f35488f9SJiri Olsa events_dir = opendir(tracing_events_path); 451f35488f9SJiri Olsa if (!events_dir) { 452f35488f9SJiri Olsa perror("Can't open event dir"); 453f35488f9SJiri Olsa return -1; 454f35488f9SJiri Olsa } 455f35488f9SJiri Olsa 456f35488f9SJiri Olsa while (!ret && (events_ent = readdir(events_dir))) { 457f35488f9SJiri Olsa if (!strcmp(events_ent->d_name, ".") 458f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "..") 459f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "enable") 460f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_event") 461f35488f9SJiri Olsa || !strcmp(events_ent->d_name, "header_page")) 462f35488f9SJiri Olsa continue; 463f35488f9SJiri Olsa 464f35488f9SJiri Olsa if (!strglobmatch(events_ent->d_name, sys_name)) 465f35488f9SJiri Olsa continue; 466f35488f9SJiri Olsa 467f35488f9SJiri Olsa ret = add_tracepoint_event(list, idx, events_ent->d_name, 468f35488f9SJiri Olsa evt_name); 469f35488f9SJiri Olsa } 470f35488f9SJiri Olsa 471f35488f9SJiri Olsa closedir(events_dir); 472f35488f9SJiri Olsa return ret; 473f35488f9SJiri Olsa } 474f35488f9SJiri Olsa 475c5cd8ac0SDavid Ahern int parse_events_add_tracepoint(struct list_head *list, int *idx, 47689812fc8SJiri Olsa char *sys, char *event) 477f6bdafefSJason Baron { 478f35488f9SJiri Olsa if (strpbrk(sys, "*?")) 479f35488f9SJiri Olsa return add_tracepoint_multi_sys(list, idx, sys, event); 480f35488f9SJiri Olsa else 481f35488f9SJiri Olsa return add_tracepoint_event(list, idx, sys, event); 4823a9f131fSFrederic Weisbecker } 4833a9f131fSFrederic Weisbecker 48489812fc8SJiri Olsa static int 48589812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 4861b290d67SFrederic Weisbecker { 4871b290d67SFrederic Weisbecker int i; 4881b290d67SFrederic Weisbecker 4891b290d67SFrederic Weisbecker for (i = 0; i < 3; i++) { 49089812fc8SJiri Olsa if (!type || !type[i]) 4911b290d67SFrederic Weisbecker break; 4921b290d67SFrederic Weisbecker 4937582732fSJiri Olsa #define CHECK_SET_TYPE(bit) \ 4947582732fSJiri Olsa do { \ 4957582732fSJiri Olsa if (attr->bp_type & bit) \ 4967582732fSJiri Olsa return -EINVAL; \ 4977582732fSJiri Olsa else \ 4987582732fSJiri Olsa attr->bp_type |= bit; \ 4997582732fSJiri Olsa } while (0) 5007582732fSJiri Olsa 5011b290d67SFrederic Weisbecker switch (type[i]) { 5021b290d67SFrederic Weisbecker case 'r': 5037582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_R); 5041b290d67SFrederic Weisbecker break; 5051b290d67SFrederic Weisbecker case 'w': 5067582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_W); 5071b290d67SFrederic Weisbecker break; 5081b290d67SFrederic Weisbecker case 'x': 5097582732fSJiri Olsa CHECK_SET_TYPE(HW_BREAKPOINT_X); 5101b290d67SFrederic Weisbecker break; 5111b290d67SFrederic Weisbecker default: 51289812fc8SJiri Olsa return -EINVAL; 5131b290d67SFrederic Weisbecker } 5141b290d67SFrederic Weisbecker } 51589812fc8SJiri Olsa 5167582732fSJiri Olsa #undef CHECK_SET_TYPE 5177582732fSJiri Olsa 5181b290d67SFrederic Weisbecker if (!attr->bp_type) /* Default */ 5191b290d67SFrederic Weisbecker attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 5201b290d67SFrederic Weisbecker 52189812fc8SJiri Olsa return 0; 5221b290d67SFrederic Weisbecker } 5231b290d67SFrederic Weisbecker 524c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx, 5253741eb9fSJacob Shin void *ptr, char *type, u64 len) 5261b290d67SFrederic Weisbecker { 52789812fc8SJiri Olsa struct perf_event_attr attr; 5281b290d67SFrederic Weisbecker 52989812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 5309fafd98fSJiri Olsa attr.bp_addr = (unsigned long) ptr; 5311b290d67SFrederic Weisbecker 53289812fc8SJiri Olsa if (parse_breakpoint_type(type, &attr)) 53389812fc8SJiri Olsa return -EINVAL; 5341b290d67SFrederic Weisbecker 5353741eb9fSJacob Shin /* Provide some defaults if len is not specified */ 5363741eb9fSJacob Shin if (!len) { 53789812fc8SJiri Olsa if (attr.bp_type == HW_BREAKPOINT_X) 5383741eb9fSJacob Shin len = sizeof(long); 539aa59a485SFrederic Weisbecker else 5403741eb9fSJacob Shin len = HW_BREAKPOINT_LEN_4; 5413741eb9fSJacob Shin } 5423741eb9fSJacob Shin 5433741eb9fSJacob Shin attr.bp_len = len; 544aa59a485SFrederic Weisbecker 54589812fc8SJiri Olsa attr.type = PERF_TYPE_BREAKPOINT; 5464a841d65SJovi Zhang attr.sample_period = 1; 5471b290d67SFrederic Weisbecker 548930a2e29SJiri Olsa return add_event(list, idx, &attr, NULL, NULL); 5491b290d67SFrederic Weisbecker } 5501b290d67SFrederic Weisbecker 5513b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term, 5523b0e371cSJiri Olsa struct parse_events_error *err, 5533b0e371cSJiri Olsa int type) 5543b0e371cSJiri Olsa { 5553b0e371cSJiri Olsa if (type == term->type_val) 5563b0e371cSJiri Olsa return 0; 5573b0e371cSJiri Olsa 5583b0e371cSJiri Olsa if (err) { 5593b0e371cSJiri Olsa err->idx = term->err_val; 5603b0e371cSJiri Olsa if (type == PARSE_EVENTS__TERM_TYPE_NUM) 5613b0e371cSJiri Olsa err->str = strdup("expected numeric value"); 5623b0e371cSJiri Olsa else 5633b0e371cSJiri Olsa err->str = strdup("expected string value"); 5643b0e371cSJiri Olsa } 5653b0e371cSJiri Olsa return -EINVAL; 5663b0e371cSJiri Olsa } 5673b0e371cSJiri Olsa 5688f707d84SJiri Olsa static int config_term(struct perf_event_attr *attr, 5693b0e371cSJiri Olsa struct parse_events_term *term, 5703b0e371cSJiri Olsa struct parse_events_error *err) 5718f707d84SJiri Olsa { 57216fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type) \ 57316fa7e82SJiri Olsa do { \ 5743b0e371cSJiri Olsa if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 57516fa7e82SJiri Olsa return -EINVAL; \ 57616fa7e82SJiri Olsa } while (0) 57716fa7e82SJiri Olsa 57816fa7e82SJiri Olsa switch (term->type_term) { 579c056ba6aSJiri Olsa case PARSE_EVENTS__TERM_TYPE_USER: 580c056ba6aSJiri Olsa /* 581c056ba6aSJiri Olsa * Always succeed for sysfs terms, as we dont know 582c056ba6aSJiri Olsa * at this point what type they need to have. 583c056ba6aSJiri Olsa */ 584c056ba6aSJiri Olsa return 0; 5858f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG: 58616fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 5878f707d84SJiri Olsa attr->config = term->val.num; 5888f707d84SJiri Olsa break; 5898f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG1: 59016fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 5918f707d84SJiri Olsa attr->config1 = term->val.num; 5928f707d84SJiri Olsa break; 5938f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_CONFIG2: 59416fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 5958f707d84SJiri Olsa attr->config2 = term->val.num; 5968f707d84SJiri Olsa break; 5978f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 59816fa7e82SJiri Olsa CHECK_TYPE_VAL(NUM); 5998f707d84SJiri Olsa break; 6008f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 6018f707d84SJiri Olsa /* 6028f707d84SJiri Olsa * TODO uncomment when the field is available 6038f707d84SJiri Olsa * attr->branch_sample_type = term->val.num; 6048f707d84SJiri Olsa */ 6058f707d84SJiri Olsa break; 6066b5fc39bSJiri Olsa case PARSE_EVENTS__TERM_TYPE_NAME: 6076b5fc39bSJiri Olsa CHECK_TYPE_VAL(STR); 6086b5fc39bSJiri Olsa break; 6098f707d84SJiri Olsa default: 6108f707d84SJiri Olsa return -EINVAL; 6118f707d84SJiri Olsa } 61216fa7e82SJiri Olsa 6138f707d84SJiri Olsa return 0; 61416fa7e82SJiri Olsa #undef CHECK_TYPE_VAL 6158f707d84SJiri Olsa } 6168f707d84SJiri Olsa 6178f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr, 6183b0e371cSJiri Olsa struct list_head *head, 6193b0e371cSJiri Olsa struct parse_events_error *err) 6208f707d84SJiri Olsa { 6216cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 6228f707d84SJiri Olsa 6238f707d84SJiri Olsa list_for_each_entry(term, head, list) 6243b0e371cSJiri Olsa if (config_term(attr, term, err)) 6258f707d84SJiri Olsa return -EINVAL; 6268f707d84SJiri Olsa 6278f707d84SJiri Olsa return 0; 6288f707d84SJiri Olsa } 6298f707d84SJiri Olsa 630930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config, 631930a2e29SJiri Olsa struct list_head *head_terms __maybe_unused) 632930a2e29SJiri Olsa { 633930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val) \ 634930a2e29SJiri Olsa do { \ 635930a2e29SJiri Olsa struct perf_evsel_config_term *__t; \ 636930a2e29SJiri Olsa \ 637930a2e29SJiri Olsa __t = zalloc(sizeof(*__t)); \ 638930a2e29SJiri Olsa if (!__t) \ 639930a2e29SJiri Olsa return -ENOMEM; \ 640930a2e29SJiri Olsa \ 641930a2e29SJiri Olsa INIT_LIST_HEAD(&__t->list); \ 642930a2e29SJiri Olsa __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 643930a2e29SJiri Olsa __t->val.__name = __val; \ 644930a2e29SJiri Olsa list_add_tail(&__t->list, head_terms); \ 645930a2e29SJiri Olsa } while (0) 646930a2e29SJiri Olsa 647930a2e29SJiri Olsa struct parse_events_term *term; 648930a2e29SJiri Olsa 649930a2e29SJiri Olsa list_for_each_entry(term, head_config, list) { 650930a2e29SJiri Olsa switch (term->type_term) { 651*ee4c7588SJiri Olsa case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 652*ee4c7588SJiri Olsa ADD_CONFIG_TERM(PERIOD, period, term->val.num); 653930a2e29SJiri Olsa default: 654930a2e29SJiri Olsa break; 655930a2e29SJiri Olsa } 656930a2e29SJiri Olsa } 657930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG 658930a2e29SJiri Olsa return 0; 659930a2e29SJiri Olsa } 660930a2e29SJiri Olsa 66187d650beSJiri Olsa int parse_events_add_numeric(struct parse_events_evlist *data, 66287d650beSJiri Olsa struct list_head *list, 663b527bab5SRobert Richter u32 type, u64 config, 6648f707d84SJiri Olsa struct list_head *head_config) 66574d5b588SJaswinder Singh Rajput { 66689812fc8SJiri Olsa struct perf_event_attr attr; 667930a2e29SJiri Olsa LIST_HEAD(config_terms); 66874d5b588SJaswinder Singh Rajput 66989812fc8SJiri Olsa memset(&attr, 0, sizeof(attr)); 67089812fc8SJiri Olsa attr.type = type; 67189812fc8SJiri Olsa attr.config = config; 6728f707d84SJiri Olsa 673930a2e29SJiri Olsa if (head_config) { 674930a2e29SJiri Olsa if (config_attr(&attr, head_config, data->error)) 6758f707d84SJiri Olsa return -EINVAL; 6768f707d84SJiri Olsa 677930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 678930a2e29SJiri Olsa return -ENOMEM; 679930a2e29SJiri Olsa } 680930a2e29SJiri Olsa 681930a2e29SJiri Olsa return add_event(list, &data->idx, &attr, NULL, &config_terms); 682b908debdSIngo Molnar } 683b908debdSIngo Molnar 6846cee6cd3SArnaldo Carvalho de Melo static int parse_events__is_name_term(struct parse_events_term *term) 6856b5fc39bSJiri Olsa { 6866b5fc39bSJiri Olsa return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 6876b5fc39bSJiri Olsa } 6886b5fc39bSJiri Olsa 6899db1763cSArnaldo Carvalho de Melo static char *pmu_event_name(struct list_head *head_terms) 6906b5fc39bSJiri Olsa { 6916cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 6926b5fc39bSJiri Olsa 6936b5fc39bSJiri Olsa list_for_each_entry(term, head_terms, list) 6946b5fc39bSJiri Olsa if (parse_events__is_name_term(term)) 6956b5fc39bSJiri Olsa return term->val.str; 6966b5fc39bSJiri Olsa 6979db1763cSArnaldo Carvalho de Melo return NULL; 6986b5fc39bSJiri Olsa } 6996b5fc39bSJiri Olsa 70036adec85SJiri Olsa int parse_events_add_pmu(struct parse_events_evlist *data, 70136adec85SJiri Olsa struct list_head *list, char *name, 70236adec85SJiri Olsa struct list_head *head_config) 7035f537a26SJiri Olsa { 7045f537a26SJiri Olsa struct perf_event_attr attr; 70546441bdcSMatt Fleming struct perf_pmu_info info; 7065f537a26SJiri Olsa struct perf_pmu *pmu; 707410136f5SStephane Eranian struct perf_evsel *evsel; 708930a2e29SJiri Olsa LIST_HEAD(config_terms); 7095f537a26SJiri Olsa 7105f537a26SJiri Olsa pmu = perf_pmu__find(name); 7115f537a26SJiri Olsa if (!pmu) 7125f537a26SJiri Olsa return -EINVAL; 7135f537a26SJiri Olsa 714dc0a6202SAdrian Hunter if (pmu->default_config) { 715dc0a6202SAdrian Hunter memcpy(&attr, pmu->default_config, 716dc0a6202SAdrian Hunter sizeof(struct perf_event_attr)); 717dc0a6202SAdrian Hunter } else { 7185f537a26SJiri Olsa memset(&attr, 0, sizeof(attr)); 719dc0a6202SAdrian Hunter } 7205f537a26SJiri Olsa 721ad962273SAdrian Hunter if (!head_config) { 722ad962273SAdrian Hunter attr.type = pmu->type; 723930a2e29SJiri Olsa evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); 724ad962273SAdrian Hunter return evsel ? 0 : -ENOMEM; 725ad962273SAdrian Hunter } 726ad962273SAdrian Hunter 72746441bdcSMatt Fleming if (perf_pmu__check_alias(pmu, head_config, &info)) 728a6146d50SZheng Yan return -EINVAL; 729a6146d50SZheng Yan 7305f537a26SJiri Olsa /* 7315f537a26SJiri Olsa * Configure hardcoded terms first, no need to check 7325f537a26SJiri Olsa * return value when called with fail == 0 ;) 7335f537a26SJiri Olsa */ 7343b0e371cSJiri Olsa if (config_attr(&attr, head_config, data->error)) 735c056ba6aSJiri Olsa return -EINVAL; 7365f537a26SJiri Olsa 737930a2e29SJiri Olsa if (get_config_terms(head_config, &config_terms)) 738930a2e29SJiri Olsa return -ENOMEM; 739930a2e29SJiri Olsa 740e64b020bSJiri Olsa if (perf_pmu__config(pmu, &attr, head_config, data->error)) 7415f537a26SJiri Olsa return -EINVAL; 7425f537a26SJiri Olsa 74336adec85SJiri Olsa evsel = __add_event(list, &data->idx, &attr, 744930a2e29SJiri Olsa pmu_event_name(head_config), pmu->cpus, 745930a2e29SJiri Olsa &config_terms); 746410136f5SStephane Eranian if (evsel) { 74746441bdcSMatt Fleming evsel->unit = info.unit; 74846441bdcSMatt Fleming evsel->scale = info.scale; 749044330c1SMatt Fleming evsel->per_pkg = info.per_pkg; 7501d9e446bSJiri Olsa evsel->snapshot = info.snapshot; 751410136f5SStephane Eranian } 752410136f5SStephane Eranian 753410136f5SStephane Eranian return evsel ? 0 : -ENOMEM; 7545f537a26SJiri Olsa } 7555f537a26SJiri Olsa 7566a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list, 7576a4bb04cSJiri Olsa char *event_mod) 75889efb029SJiri Olsa { 7596a4bb04cSJiri Olsa return parse_events__modifier_event(list, event_mod, true); 7606a4bb04cSJiri Olsa } 7616a4bb04cSJiri Olsa 76263dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list) 7636a4bb04cSJiri Olsa { 7646a4bb04cSJiri Olsa struct perf_evsel *leader; 7656a4bb04cSJiri Olsa 76663dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(list); 76763dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 7686a4bb04cSJiri Olsa leader->group_name = name ? strdup(name) : NULL; 76989efb029SJiri Olsa } 77089efb029SJiri Olsa 771c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */ 7725d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event, 7735d7be90eSJiri Olsa struct list_head *list_all) 7745d7be90eSJiri Olsa { 7755d7be90eSJiri Olsa /* 7765d7be90eSJiri Olsa * Called for single event definition. Update the 77789efb029SJiri Olsa * 'all event' list, and reinit the 'single event' 7785d7be90eSJiri Olsa * list, for next event definition. 7795d7be90eSJiri Olsa */ 7805d7be90eSJiri Olsa list_splice_tail(list_event, list_all); 781b847cbdcSJiri Olsa free(list_event); 7825d7be90eSJiri Olsa } 7835d7be90eSJiri Olsa 784f5b1135bSJiri Olsa struct event_modifier { 785f5b1135bSJiri Olsa int eu; 786f5b1135bSJiri Olsa int ek; 787f5b1135bSJiri Olsa int eh; 788f5b1135bSJiri Olsa int eH; 789f5b1135bSJiri Olsa int eG; 790a1e12da4SJiri Olsa int eI; 791f5b1135bSJiri Olsa int precise; 792f5b1135bSJiri Olsa int exclude_GH; 7933c176311SJiri Olsa int sample_read; 794e9a7c414SMichael Ellerman int pinned; 795f5b1135bSJiri Olsa }; 79661c45981SPaul Mackerras 797f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str, 798f5b1135bSJiri Olsa struct perf_evsel *evsel) 799f5b1135bSJiri Olsa { 800f5b1135bSJiri Olsa int eu = evsel ? evsel->attr.exclude_user : 0; 801f5b1135bSJiri Olsa int ek = evsel ? evsel->attr.exclude_kernel : 0; 802f5b1135bSJiri Olsa int eh = evsel ? evsel->attr.exclude_hv : 0; 803f5b1135bSJiri Olsa int eH = evsel ? evsel->attr.exclude_host : 0; 804f5b1135bSJiri Olsa int eG = evsel ? evsel->attr.exclude_guest : 0; 805a1e12da4SJiri Olsa int eI = evsel ? evsel->attr.exclude_idle : 0; 806f5b1135bSJiri Olsa int precise = evsel ? evsel->attr.precise_ip : 0; 8073c176311SJiri Olsa int sample_read = 0; 808e9a7c414SMichael Ellerman int pinned = evsel ? evsel->attr.pinned : 0; 809f5b1135bSJiri Olsa 810f5b1135bSJiri Olsa int exclude = eu | ek | eh; 811f5b1135bSJiri Olsa int exclude_GH = evsel ? evsel->exclude_GH : 0; 812f5b1135bSJiri Olsa 813f5b1135bSJiri Olsa memset(mod, 0, sizeof(*mod)); 814ceb53fbfSIngo Molnar 81561c45981SPaul Mackerras while (*str) { 816ab608344SPeter Zijlstra if (*str == 'u') { 817ab608344SPeter Zijlstra if (!exclude) 818ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 81961c45981SPaul Mackerras eu = 0; 820ab608344SPeter Zijlstra } else if (*str == 'k') { 821ab608344SPeter Zijlstra if (!exclude) 822ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 82361c45981SPaul Mackerras ek = 0; 824ab608344SPeter Zijlstra } else if (*str == 'h') { 825ab608344SPeter Zijlstra if (!exclude) 826ab608344SPeter Zijlstra exclude = eu = ek = eh = 1; 82761c45981SPaul Mackerras eh = 0; 82899320cc8SJoerg Roedel } else if (*str == 'G') { 82999320cc8SJoerg Roedel if (!exclude_GH) 83099320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 83199320cc8SJoerg Roedel eG = 0; 83299320cc8SJoerg Roedel } else if (*str == 'H') { 83399320cc8SJoerg Roedel if (!exclude_GH) 83499320cc8SJoerg Roedel exclude_GH = eG = eH = 1; 83599320cc8SJoerg Roedel eH = 0; 836a1e12da4SJiri Olsa } else if (*str == 'I') { 837a1e12da4SJiri Olsa eI = 1; 838ab608344SPeter Zijlstra } else if (*str == 'p') { 839ab608344SPeter Zijlstra precise++; 8401342798cSDavid Ahern /* use of precise requires exclude_guest */ 8411342798cSDavid Ahern if (!exclude_GH) 8421342798cSDavid Ahern eG = 1; 8433c176311SJiri Olsa } else if (*str == 'S') { 8443c176311SJiri Olsa sample_read = 1; 845e9a7c414SMichael Ellerman } else if (*str == 'D') { 846e9a7c414SMichael Ellerman pinned = 1; 847ab608344SPeter Zijlstra } else 84861c45981SPaul Mackerras break; 849ab608344SPeter Zijlstra 85061c45981SPaul Mackerras ++str; 85161c45981SPaul Mackerras } 85274d5b588SJaswinder Singh Rajput 85386470930SIngo Molnar /* 85489812fc8SJiri Olsa * precise ip: 85589812fc8SJiri Olsa * 85689812fc8SJiri Olsa * 0 - SAMPLE_IP can have arbitrary skid 85789812fc8SJiri Olsa * 1 - SAMPLE_IP must have constant skid 85889812fc8SJiri Olsa * 2 - SAMPLE_IP requested to have 0 skid 85989812fc8SJiri Olsa * 3 - SAMPLE_IP must have 0 skid 86089812fc8SJiri Olsa * 86189812fc8SJiri Olsa * See also PERF_RECORD_MISC_EXACT_IP 86286470930SIngo Molnar */ 86389812fc8SJiri Olsa if (precise > 3) 86489812fc8SJiri Olsa return -EINVAL; 86586470930SIngo Molnar 866f5b1135bSJiri Olsa mod->eu = eu; 867f5b1135bSJiri Olsa mod->ek = ek; 868f5b1135bSJiri Olsa mod->eh = eh; 869f5b1135bSJiri Olsa mod->eH = eH; 870f5b1135bSJiri Olsa mod->eG = eG; 871a1e12da4SJiri Olsa mod->eI = eI; 872f5b1135bSJiri Olsa mod->precise = precise; 873f5b1135bSJiri Olsa mod->exclude_GH = exclude_GH; 8743c176311SJiri Olsa mod->sample_read = sample_read; 875e9a7c414SMichael Ellerman mod->pinned = pinned; 876e9a7c414SMichael Ellerman 877f5b1135bSJiri Olsa return 0; 878f5b1135bSJiri Olsa } 879f5b1135bSJiri Olsa 880534123f4SJiri Olsa /* 881534123f4SJiri Olsa * Basic modifier sanity check to validate it contains only one 882534123f4SJiri Olsa * instance of any modifier (apart from 'p') present. 883534123f4SJiri Olsa */ 884534123f4SJiri Olsa static int check_modifier(char *str) 885534123f4SJiri Olsa { 886534123f4SJiri Olsa char *p = str; 887534123f4SJiri Olsa 888534123f4SJiri Olsa /* The sizeof includes 0 byte as well. */ 889a1e12da4SJiri Olsa if (strlen(str) > (sizeof("ukhGHpppSDI") - 1)) 890534123f4SJiri Olsa return -1; 891534123f4SJiri Olsa 892534123f4SJiri Olsa while (*p) { 893534123f4SJiri Olsa if (*p != 'p' && strchr(p + 1, *p)) 894534123f4SJiri Olsa return -1; 895534123f4SJiri Olsa p++; 896534123f4SJiri Olsa } 897534123f4SJiri Olsa 898534123f4SJiri Olsa return 0; 899534123f4SJiri Olsa } 900534123f4SJiri Olsa 901f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add) 902f5b1135bSJiri Olsa { 903f5b1135bSJiri Olsa struct perf_evsel *evsel; 904f5b1135bSJiri Olsa struct event_modifier mod; 905f5b1135bSJiri Olsa 906f5b1135bSJiri Olsa if (str == NULL) 907f5b1135bSJiri Olsa return 0; 908f5b1135bSJiri Olsa 909534123f4SJiri Olsa if (check_modifier(str)) 910534123f4SJiri Olsa return -EINVAL; 911534123f4SJiri Olsa 912f5b1135bSJiri Olsa if (!add && get_event_modifier(&mod, str, NULL)) 913f5b1135bSJiri Olsa return -EINVAL; 914f5b1135bSJiri Olsa 9150050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 916f5b1135bSJiri Olsa if (add && get_event_modifier(&mod, str, evsel)) 917f5b1135bSJiri Olsa return -EINVAL; 918f5b1135bSJiri Olsa 919f5b1135bSJiri Olsa evsel->attr.exclude_user = mod.eu; 920f5b1135bSJiri Olsa evsel->attr.exclude_kernel = mod.ek; 921f5b1135bSJiri Olsa evsel->attr.exclude_hv = mod.eh; 922f5b1135bSJiri Olsa evsel->attr.precise_ip = mod.precise; 923f5b1135bSJiri Olsa evsel->attr.exclude_host = mod.eH; 924f5b1135bSJiri Olsa evsel->attr.exclude_guest = mod.eG; 925a1e12da4SJiri Olsa evsel->attr.exclude_idle = mod.eI; 926f5b1135bSJiri Olsa evsel->exclude_GH = mod.exclude_GH; 9273c176311SJiri Olsa evsel->sample_read = mod.sample_read; 928e9a7c414SMichael Ellerman 929e9a7c414SMichael Ellerman if (perf_evsel__is_group_leader(evsel)) 930e9a7c414SMichael Ellerman evsel->attr.pinned = mod.pinned; 931ceb53fbfSIngo Molnar } 93286470930SIngo Molnar 93389812fc8SJiri Olsa return 0; 93486470930SIngo Molnar } 93586470930SIngo Molnar 936ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name) 937ac2ba9f3SRobert Richter { 938ac2ba9f3SRobert Richter struct perf_evsel *evsel; 939ac2ba9f3SRobert Richter 9400050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 941ac2ba9f3SRobert Richter if (!evsel->name) 942ac2ba9f3SRobert Richter evsel->name = strdup(name); 943ac2ba9f3SRobert Richter } 944ac2ba9f3SRobert Richter 945ac2ba9f3SRobert Richter return 0; 946ac2ba9f3SRobert Richter } 947ac2ba9f3SRobert Richter 948dcb4e102SKan Liang static int 949dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2) 950dcb4e102SKan Liang { 951dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 952dcb4e102SKan Liang struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 953dcb4e102SKan Liang 954dcb4e102SKan Liang return strcmp(pmu1->symbol, pmu2->symbol); 955dcb4e102SKan Liang } 956dcb4e102SKan Liang 957dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void) 958dcb4e102SKan Liang { 959dcb4e102SKan Liang if (perf_pmu_events_list_num > 0) { 960dcb4e102SKan Liang struct perf_pmu_event_symbol *p; 961dcb4e102SKan Liang int i; 962dcb4e102SKan Liang 963dcb4e102SKan Liang for (i = 0; i < perf_pmu_events_list_num; i++) { 964dcb4e102SKan Liang p = perf_pmu_events_list + i; 965dcb4e102SKan Liang free(p->symbol); 966dcb4e102SKan Liang } 967dcb4e102SKan Liang free(perf_pmu_events_list); 968dcb4e102SKan Liang perf_pmu_events_list = NULL; 969dcb4e102SKan Liang perf_pmu_events_list_num = 0; 970dcb4e102SKan Liang } 971dcb4e102SKan Liang } 972dcb4e102SKan Liang 973dcb4e102SKan Liang #define SET_SYMBOL(str, stype) \ 974dcb4e102SKan Liang do { \ 975dcb4e102SKan Liang p->symbol = str; \ 976dcb4e102SKan Liang if (!p->symbol) \ 977dcb4e102SKan Liang goto err; \ 978dcb4e102SKan Liang p->type = stype; \ 979dcb4e102SKan Liang } while (0) 980dcb4e102SKan Liang 981dcb4e102SKan Liang /* 982dcb4e102SKan Liang * Read the pmu events list from sysfs 983dcb4e102SKan Liang * Save it into perf_pmu_events_list 984dcb4e102SKan Liang */ 985dcb4e102SKan Liang static void perf_pmu__parse_init(void) 986dcb4e102SKan Liang { 987dcb4e102SKan Liang 988dcb4e102SKan Liang struct perf_pmu *pmu = NULL; 989dcb4e102SKan Liang struct perf_pmu_alias *alias; 990dcb4e102SKan Liang int len = 0; 991dcb4e102SKan Liang 992dcb4e102SKan Liang pmu = perf_pmu__find("cpu"); 993dcb4e102SKan Liang if ((pmu == NULL) || list_empty(&pmu->aliases)) { 994dcb4e102SKan Liang perf_pmu_events_list_num = -1; 995dcb4e102SKan Liang return; 996dcb4e102SKan Liang } 997dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 998dcb4e102SKan Liang if (strchr(alias->name, '-')) 999dcb4e102SKan Liang len++; 1000dcb4e102SKan Liang len++; 1001dcb4e102SKan Liang } 1002dcb4e102SKan Liang perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1003dcb4e102SKan Liang if (!perf_pmu_events_list) 1004dcb4e102SKan Liang return; 1005dcb4e102SKan Liang perf_pmu_events_list_num = len; 1006dcb4e102SKan Liang 1007dcb4e102SKan Liang len = 0; 1008dcb4e102SKan Liang list_for_each_entry(alias, &pmu->aliases, list) { 1009dcb4e102SKan Liang struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1010dcb4e102SKan Liang char *tmp = strchr(alias->name, '-'); 1011dcb4e102SKan Liang 1012dcb4e102SKan Liang if (tmp != NULL) { 1013dcb4e102SKan Liang SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1014dcb4e102SKan Liang PMU_EVENT_SYMBOL_PREFIX); 1015dcb4e102SKan Liang p++; 1016dcb4e102SKan Liang SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1017dcb4e102SKan Liang len += 2; 1018dcb4e102SKan Liang } else { 1019dcb4e102SKan Liang SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1020dcb4e102SKan Liang len++; 1021dcb4e102SKan Liang } 1022dcb4e102SKan Liang } 1023dcb4e102SKan Liang qsort(perf_pmu_events_list, len, 1024dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1025dcb4e102SKan Liang 1026dcb4e102SKan Liang return; 1027dcb4e102SKan Liang err: 1028dcb4e102SKan Liang perf_pmu__parse_cleanup(); 1029dcb4e102SKan Liang } 1030dcb4e102SKan Liang 1031dcb4e102SKan Liang enum perf_pmu_event_symbol_type 1032dcb4e102SKan Liang perf_pmu__parse_check(const char *name) 1033dcb4e102SKan Liang { 1034dcb4e102SKan Liang struct perf_pmu_event_symbol p, *r; 1035dcb4e102SKan Liang 1036dcb4e102SKan Liang /* scan kernel pmu events from sysfs if needed */ 1037dcb4e102SKan Liang if (perf_pmu_events_list_num == 0) 1038dcb4e102SKan Liang perf_pmu__parse_init(); 1039dcb4e102SKan Liang /* 1040dcb4e102SKan Liang * name "cpu" could be prefix of cpu-cycles or cpu// events. 1041dcb4e102SKan Liang * cpu-cycles has been handled by hardcode. 1042dcb4e102SKan Liang * So it must be cpu// events, not kernel pmu event. 1043dcb4e102SKan Liang */ 1044dcb4e102SKan Liang if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1045dcb4e102SKan Liang return PMU_EVENT_SYMBOL_ERR; 1046dcb4e102SKan Liang 1047dcb4e102SKan Liang p.symbol = strdup(name); 1048dcb4e102SKan Liang r = bsearch(&p, perf_pmu_events_list, 1049dcb4e102SKan Liang (size_t) perf_pmu_events_list_num, 1050dcb4e102SKan Liang sizeof(struct perf_pmu_event_symbol), comp_pmu); 1051dcb4e102SKan Liang free(p.symbol); 1052dcb4e102SKan Liang return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1053dcb4e102SKan Liang } 1054dcb4e102SKan Liang 105590e2b22dSJiri Olsa static int parse_events__scanner(const char *str, void *data, int start_token) 1056ac20de6fSZheng Yan { 1057ac20de6fSZheng Yan YY_BUFFER_STATE buffer; 1058ac20de6fSZheng Yan void *scanner; 1059ac20de6fSZheng Yan int ret; 1060ac20de6fSZheng Yan 106190e2b22dSJiri Olsa ret = parse_events_lex_init_extra(start_token, &scanner); 1062ac20de6fSZheng Yan if (ret) 1063ac20de6fSZheng Yan return ret; 1064ac20de6fSZheng Yan 1065ac20de6fSZheng Yan buffer = parse_events__scan_string(str, scanner); 1066ac20de6fSZheng Yan 1067ac20de6fSZheng Yan #ifdef PARSER_DEBUG 1068ac20de6fSZheng Yan parse_events_debug = 1; 1069ac20de6fSZheng Yan #endif 1070ac20de6fSZheng Yan ret = parse_events_parse(data, scanner); 1071ac20de6fSZheng Yan 1072ac20de6fSZheng Yan parse_events__flush_buffer(buffer, scanner); 1073ac20de6fSZheng Yan parse_events__delete_buffer(buffer, scanner); 1074ac20de6fSZheng Yan parse_events_lex_destroy(scanner); 1075ac20de6fSZheng Yan return ret; 1076ac20de6fSZheng Yan } 1077ac20de6fSZheng Yan 107890e2b22dSJiri Olsa /* 107990e2b22dSJiri Olsa * parse event config string, return a list of event terms. 108090e2b22dSJiri Olsa */ 108190e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str) 108290e2b22dSJiri Olsa { 108323b6339bSArnaldo Carvalho de Melo struct parse_events_terms data = { 108490e2b22dSJiri Olsa .terms = NULL, 108590e2b22dSJiri Olsa }; 108690e2b22dSJiri Olsa int ret; 108790e2b22dSJiri Olsa 108890e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_TERMS); 108990e2b22dSJiri Olsa if (!ret) { 109090e2b22dSJiri Olsa list_splice(data.terms, terms); 109174cf249dSArnaldo Carvalho de Melo zfree(&data.terms); 109290e2b22dSJiri Olsa return 0; 109390e2b22dSJiri Olsa } 109490e2b22dSJiri Olsa 1095b2c34fdeSAdrian Hunter if (data.terms) 109690e2b22dSJiri Olsa parse_events__free_terms(data.terms); 109790e2b22dSJiri Olsa return ret; 109890e2b22dSJiri Olsa } 109990e2b22dSJiri Olsa 1100b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str, 1101b39b8393SJiri Olsa struct parse_events_error *err) 110286470930SIngo Molnar { 110323b6339bSArnaldo Carvalho de Melo struct parse_events_evlist data = { 110446010ab2SJiri Olsa .list = LIST_HEAD_INIT(data.list), 110546010ab2SJiri Olsa .idx = evlist->nr_entries, 1106b39b8393SJiri Olsa .error = err, 110746010ab2SJiri Olsa }; 110846010ab2SJiri Olsa int ret; 110986470930SIngo Molnar 111090e2b22dSJiri Olsa ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1111dcb4e102SKan Liang perf_pmu__parse_cleanup(); 111289812fc8SJiri Olsa if (!ret) { 111346010ab2SJiri Olsa int entries = data.idx - evlist->nr_entries; 111415bfd2ccSWang Nan struct perf_evsel *last; 111515bfd2ccSWang Nan 111646010ab2SJiri Olsa perf_evlist__splice_list_tail(evlist, &data.list, entries); 111797f63e4aSNamhyung Kim evlist->nr_groups += data.nr_groups; 111815bfd2ccSWang Nan last = perf_evlist__last(evlist); 111915bfd2ccSWang Nan last->cmdline_group_boundary = true; 112015bfd2ccSWang Nan 112186470930SIngo Molnar return 0; 112286470930SIngo Molnar } 112386470930SIngo Molnar 11245d7be90eSJiri Olsa /* 11255d7be90eSJiri Olsa * There are 2 users - builtin-record and builtin-test objects. 11265d7be90eSJiri Olsa * Both call perf_evlist__delete in case of error, so we dont 11275d7be90eSJiri Olsa * need to bother. 11285d7be90eSJiri Olsa */ 112989812fc8SJiri Olsa return ret; 113089812fc8SJiri Olsa } 113189812fc8SJiri Olsa 1132b39b8393SJiri Olsa #define MAX_WIDTH 1000 1133b39b8393SJiri Olsa static int get_term_width(void) 1134b39b8393SJiri Olsa { 1135b39b8393SJiri Olsa struct winsize ws; 1136b39b8393SJiri Olsa 1137b39b8393SJiri Olsa get_term_dimensions(&ws); 1138b39b8393SJiri Olsa return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1139b39b8393SJiri Olsa } 1140b39b8393SJiri Olsa 1141b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err, 1142b39b8393SJiri Olsa const char *event) 1143b39b8393SJiri Olsa { 1144b39b8393SJiri Olsa const char *str = "invalid or unsupported event: "; 1145b39b8393SJiri Olsa char _buf[MAX_WIDTH]; 1146b39b8393SJiri Olsa char *buf = (char *) event; 1147b39b8393SJiri Olsa int idx = 0; 1148b39b8393SJiri Olsa 1149b39b8393SJiri Olsa if (err->str) { 1150b39b8393SJiri Olsa /* -2 for extra '' in the final fprintf */ 1151b39b8393SJiri Olsa int width = get_term_width() - 2; 1152b39b8393SJiri Olsa int len_event = strlen(event); 1153b39b8393SJiri Olsa int len_str, max_len, cut = 0; 1154b39b8393SJiri Olsa 1155b39b8393SJiri Olsa /* 1156b39b8393SJiri Olsa * Maximum error index indent, we will cut 1157b39b8393SJiri Olsa * the event string if it's bigger. 1158b39b8393SJiri Olsa */ 1159b39b8393SJiri Olsa int max_err_idx = 10; 1160b39b8393SJiri Olsa 1161b39b8393SJiri Olsa /* 1162b39b8393SJiri Olsa * Let's be specific with the message when 1163b39b8393SJiri Olsa * we have the precise error. 1164b39b8393SJiri Olsa */ 1165b39b8393SJiri Olsa str = "event syntax error: "; 1166b39b8393SJiri Olsa len_str = strlen(str); 1167b39b8393SJiri Olsa max_len = width - len_str; 1168b39b8393SJiri Olsa 1169b39b8393SJiri Olsa buf = _buf; 1170b39b8393SJiri Olsa 1171b39b8393SJiri Olsa /* We're cutting from the beggining. */ 1172b39b8393SJiri Olsa if (err->idx > max_err_idx) 1173b39b8393SJiri Olsa cut = err->idx - max_err_idx; 1174b39b8393SJiri Olsa 1175b39b8393SJiri Olsa strncpy(buf, event + cut, max_len); 1176b39b8393SJiri Olsa 1177b39b8393SJiri Olsa /* Mark cut parts with '..' on both sides. */ 1178b39b8393SJiri Olsa if (cut) 1179b39b8393SJiri Olsa buf[0] = buf[1] = '.'; 1180b39b8393SJiri Olsa 1181b39b8393SJiri Olsa if ((len_event - cut) > max_len) { 1182b39b8393SJiri Olsa buf[max_len - 1] = buf[max_len - 2] = '.'; 1183b39b8393SJiri Olsa buf[max_len] = 0; 1184b39b8393SJiri Olsa } 1185b39b8393SJiri Olsa 1186b39b8393SJiri Olsa idx = len_str + err->idx - cut; 1187b39b8393SJiri Olsa } 1188b39b8393SJiri Olsa 1189b39b8393SJiri Olsa fprintf(stderr, "%s'%s'\n", str, buf); 1190b39b8393SJiri Olsa if (idx) { 1191b39b8393SJiri Olsa fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1192b39b8393SJiri Olsa if (err->help) 1193b39b8393SJiri Olsa fprintf(stderr, "\n%s\n", err->help); 1194b39b8393SJiri Olsa free(err->str); 1195b39b8393SJiri Olsa free(err->help); 1196b39b8393SJiri Olsa } 1197b39b8393SJiri Olsa 1198b39b8393SJiri Olsa fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1199b39b8393SJiri Olsa } 1200b39b8393SJiri Olsa 1201b39b8393SJiri Olsa #undef MAX_WIDTH 1202b39b8393SJiri Olsa 1203f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str, 12041d037ca1SIrina Tirdea int unset __maybe_unused) 1205f120f9d5SJiri Olsa { 1206f120f9d5SJiri Olsa struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1207b39b8393SJiri Olsa struct parse_events_error err = { .idx = 0, }; 1208b39b8393SJiri Olsa int ret = parse_events(evlist, str, &err); 12099175ce1fSAndi Kleen 1210b39b8393SJiri Olsa if (ret) 1211b39b8393SJiri Olsa parse_events_print_error(&err, str); 1212b39b8393SJiri Olsa 12139175ce1fSAndi Kleen return ret; 1214f120f9d5SJiri Olsa } 1215f120f9d5SJiri Olsa 12164ba1faa1SWang Nan static int 12174ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist, 12184ba1faa1SWang Nan int (*func)(struct perf_evsel *evsel, 12194ba1faa1SWang Nan const void *arg), 12204ba1faa1SWang Nan const void *arg) 1221c171b552SLi Zefan { 122269aad6f1SArnaldo Carvalho de Melo struct perf_evsel *last = NULL; 12234ba1faa1SWang Nan int err; 1224c171b552SLi Zefan 1225361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 0) 12260c21f736SArnaldo Carvalho de Melo last = perf_evlist__last(evlist); 122769aad6f1SArnaldo Carvalho de Melo 122815bfd2ccSWang Nan do { 12294ba1faa1SWang Nan err = (*func)(last, arg); 12304ba1faa1SWang Nan if (err) 1231c171b552SLi Zefan return -1; 12324ba1faa1SWang Nan if (!last) 12334ba1faa1SWang Nan return 0; 1234c171b552SLi Zefan 123515bfd2ccSWang Nan if (last->node.prev == &evlist->entries) 123615bfd2ccSWang Nan return 0; 123715bfd2ccSWang Nan last = list_entry(last->node.prev, struct perf_evsel, node); 123815bfd2ccSWang Nan } while (!last->cmdline_group_boundary); 123915bfd2ccSWang Nan 1240c171b552SLi Zefan return 0; 1241c171b552SLi Zefan } 1242c171b552SLi Zefan 12434ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg) 12444ba1faa1SWang Nan { 12454ba1faa1SWang Nan const char *str = arg; 12464ba1faa1SWang Nan 12474ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 12484ba1faa1SWang Nan fprintf(stderr, 12494ba1faa1SWang Nan "--filter option should follow a -e tracepoint option\n"); 12504ba1faa1SWang Nan return -1; 12514ba1faa1SWang Nan } 12524ba1faa1SWang Nan 12534ba1faa1SWang Nan if (perf_evsel__append_filter(evsel, "&&", str) < 0) { 12544ba1faa1SWang Nan fprintf(stderr, 12554ba1faa1SWang Nan "not enough memory to hold filter string\n"); 12564ba1faa1SWang Nan return -1; 12574ba1faa1SWang Nan } 12584ba1faa1SWang Nan 12594ba1faa1SWang Nan return 0; 12604ba1faa1SWang Nan } 12614ba1faa1SWang Nan 12624ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str, 12634ba1faa1SWang Nan int unset __maybe_unused) 12644ba1faa1SWang Nan { 12654ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 12664ba1faa1SWang Nan 12674ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, set_filter, 12684ba1faa1SWang Nan (const void *)str); 12694ba1faa1SWang Nan } 12704ba1faa1SWang Nan 12714ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel, 12724ba1faa1SWang Nan const void *arg __maybe_unused) 12734ba1faa1SWang Nan { 12744ba1faa1SWang Nan char new_filter[64]; 12754ba1faa1SWang Nan 12764ba1faa1SWang Nan if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 12774ba1faa1SWang Nan fprintf(stderr, 12784ba1faa1SWang Nan "--exclude-perf option should follow a -e tracepoint option\n"); 12794ba1faa1SWang Nan return -1; 12804ba1faa1SWang Nan } 12814ba1faa1SWang Nan 12824ba1faa1SWang Nan snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 12834ba1faa1SWang Nan 12844ba1faa1SWang Nan if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) { 12854ba1faa1SWang Nan fprintf(stderr, 12864ba1faa1SWang Nan "not enough memory to hold filter string\n"); 12874ba1faa1SWang Nan return -1; 12884ba1faa1SWang Nan } 12894ba1faa1SWang Nan 12904ba1faa1SWang Nan return 0; 12914ba1faa1SWang Nan } 12924ba1faa1SWang Nan 12934ba1faa1SWang Nan int exclude_perf(const struct option *opt, 12944ba1faa1SWang Nan const char *arg __maybe_unused, 12954ba1faa1SWang Nan int unset __maybe_unused) 12964ba1faa1SWang Nan { 12974ba1faa1SWang Nan struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 12984ba1faa1SWang Nan 12994ba1faa1SWang Nan return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 13004ba1faa1SWang Nan NULL); 13014ba1faa1SWang Nan } 13024ba1faa1SWang Nan 130386470930SIngo Molnar static const char * const event_type_descriptors[] = { 130486470930SIngo Molnar "Hardware event", 130586470930SIngo Molnar "Software event", 130686470930SIngo Molnar "Tracepoint event", 130786470930SIngo Molnar "Hardware cache event", 130841bdcb23SLiming Wang "Raw hardware event descriptor", 130941bdcb23SLiming Wang "Hardware breakpoint", 131086470930SIngo Molnar }; 131186470930SIngo Molnar 1312ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b) 1313ab0e4800SYunlong Song { 1314ab0e4800SYunlong Song const char * const *as = a; 1315ab0e4800SYunlong Song const char * const *bs = b; 1316ab0e4800SYunlong Song 1317ab0e4800SYunlong Song return strcmp(*as, *bs); 1318ab0e4800SYunlong Song } 1319ab0e4800SYunlong Song 132086470930SIngo Molnar /* 1321f6bdafefSJason Baron * Print the events from <debugfs_mount_point>/tracing/events 1322f6bdafefSJason Baron */ 1323f6bdafefSJason Baron 1324a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1325a3277d2dSFrederic Weisbecker bool name_only) 1326f6bdafefSJason Baron { 1327f6bdafefSJason Baron DIR *sys_dir, *evt_dir; 1328f6bdafefSJason Baron struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1329f6bdafefSJason Baron char evt_path[MAXPATHLEN]; 1330725b1368SEric Dumazet char dir_path[MAXPATHLEN]; 1331ab0e4800SYunlong Song char **evt_list = NULL; 1332ab0e4800SYunlong Song unsigned int evt_i = 0, evt_num = 0; 1333ab0e4800SYunlong Song bool evt_num_known = false; 1334f6bdafefSJason Baron 1335ab0e4800SYunlong Song restart: 1336ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 1337f6bdafefSJason Baron if (!sys_dir) 1338725b1368SEric Dumazet return; 1339f6bdafefSJason Baron 1340ab0e4800SYunlong Song if (evt_num_known) { 1341ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1342ab0e4800SYunlong Song if (!evt_list) 1343ab0e4800SYunlong Song goto out_close_sys_dir; 1344ab0e4800SYunlong Song } 1345ab0e4800SYunlong Song 13466b58e7f1SUlrich Drepper for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1347668b8788SArnaldo Carvalho de Melo if (subsys_glob != NULL && 1348668b8788SArnaldo Carvalho de Melo !strglobmatch(sys_dirent.d_name, subsys_glob)) 1349668b8788SArnaldo Carvalho de Melo continue; 1350725b1368SEric Dumazet 1351ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1352725b1368SEric Dumazet sys_dirent.d_name); 1353725b1368SEric Dumazet evt_dir = opendir(dir_path); 1354725b1368SEric Dumazet if (!evt_dir) 13556b58e7f1SUlrich Drepper continue; 1356725b1368SEric Dumazet 13576b58e7f1SUlrich Drepper for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1358668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 1359668b8788SArnaldo Carvalho de Melo !strglobmatch(evt_dirent.d_name, event_glob)) 1360668b8788SArnaldo Carvalho de Melo continue; 1361668b8788SArnaldo Carvalho de Melo 1362ab0e4800SYunlong Song if (!evt_num_known) { 1363ab0e4800SYunlong Song evt_num++; 1364a3277d2dSFrederic Weisbecker continue; 1365a3277d2dSFrederic Weisbecker } 1366a3277d2dSFrederic Weisbecker 1367f6bdafefSJason Baron snprintf(evt_path, MAXPATHLEN, "%s:%s", 1368f6bdafefSJason Baron sys_dirent.d_name, evt_dirent.d_name); 1369ab0e4800SYunlong Song 1370ab0e4800SYunlong Song evt_list[evt_i] = strdup(evt_path); 1371ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1372ab0e4800SYunlong Song goto out_close_evt_dir; 1373ab0e4800SYunlong Song evt_i++; 1374f6bdafefSJason Baron } 1375f6bdafefSJason Baron closedir(evt_dir); 1376f6bdafefSJason Baron } 1377f6bdafefSJason Baron closedir(sys_dir); 1378ab0e4800SYunlong Song 1379ab0e4800SYunlong Song if (!evt_num_known) { 1380ab0e4800SYunlong Song evt_num_known = true; 1381ab0e4800SYunlong Song goto restart; 1382ab0e4800SYunlong Song } 1383ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1384ab0e4800SYunlong Song evt_i = 0; 1385ab0e4800SYunlong Song while (evt_i < evt_num) { 1386ab0e4800SYunlong Song if (name_only) { 1387ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1388ab0e4800SYunlong Song continue; 1389ab0e4800SYunlong Song } 1390ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 1391ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1392ab0e4800SYunlong Song } 1393ab0e4800SYunlong Song if (evt_num) 1394ab0e4800SYunlong Song printf("\n"); 1395ab0e4800SYunlong Song 1396ab0e4800SYunlong Song out_free: 1397ab0e4800SYunlong Song evt_num = evt_i; 1398ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1399ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1400ab0e4800SYunlong Song zfree(&evt_list); 1401ab0e4800SYunlong Song return; 1402ab0e4800SYunlong Song 1403ab0e4800SYunlong Song out_close_evt_dir: 1404ab0e4800SYunlong Song closedir(evt_dir); 1405ab0e4800SYunlong Song out_close_sys_dir: 1406ab0e4800SYunlong Song closedir(sys_dir); 1407ab0e4800SYunlong Song 1408ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", 1409ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1410ab0e4800SYunlong Song if (evt_list) 1411ab0e4800SYunlong Song goto out_free; 1412f6bdafefSJason Baron } 1413f6bdafefSJason Baron 1414f6bdafefSJason Baron /* 141520c457b8SThomas Renninger * Check whether event is in <debugfs_mount_point>/tracing/events 141620c457b8SThomas Renninger */ 141720c457b8SThomas Renninger 141820c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string) 141920c457b8SThomas Renninger { 142020c457b8SThomas Renninger DIR *sys_dir, *evt_dir; 142120c457b8SThomas Renninger struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 142220c457b8SThomas Renninger char evt_path[MAXPATHLEN]; 142320c457b8SThomas Renninger char dir_path[MAXPATHLEN]; 142420c457b8SThomas Renninger 1425ebf294bfSArnaldo Carvalho de Melo sys_dir = opendir(tracing_events_path); 142620c457b8SThomas Renninger if (!sys_dir) 142720c457b8SThomas Renninger return 0; 142820c457b8SThomas Renninger 142920c457b8SThomas Renninger for_each_subsystem(sys_dir, sys_dirent, sys_next) { 143020c457b8SThomas Renninger 1431ebf294bfSArnaldo Carvalho de Melo snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 143220c457b8SThomas Renninger sys_dirent.d_name); 143320c457b8SThomas Renninger evt_dir = opendir(dir_path); 143420c457b8SThomas Renninger if (!evt_dir) 143520c457b8SThomas Renninger continue; 143620c457b8SThomas Renninger 143720c457b8SThomas Renninger for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 143820c457b8SThomas Renninger snprintf(evt_path, MAXPATHLEN, "%s:%s", 143920c457b8SThomas Renninger sys_dirent.d_name, evt_dirent.d_name); 144020c457b8SThomas Renninger if (!strcmp(evt_path, event_string)) { 144120c457b8SThomas Renninger closedir(evt_dir); 144220c457b8SThomas Renninger closedir(sys_dir); 144320c457b8SThomas Renninger return 1; 144420c457b8SThomas Renninger } 144520c457b8SThomas Renninger } 144620c457b8SThomas Renninger closedir(evt_dir); 144720c457b8SThomas Renninger } 144820c457b8SThomas Renninger closedir(sys_dir); 144920c457b8SThomas Renninger return 0; 145020c457b8SThomas Renninger } 145120c457b8SThomas Renninger 1452b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config) 1453b41f1cecSNamhyung Kim { 1454b41f1cecSNamhyung Kim bool ret = true; 145588fee52eSVince Weaver int open_return; 1456b41f1cecSNamhyung Kim struct perf_evsel *evsel; 1457b41f1cecSNamhyung Kim struct perf_event_attr attr = { 1458b41f1cecSNamhyung Kim .type = type, 1459b41f1cecSNamhyung Kim .config = config, 1460b41f1cecSNamhyung Kim .disabled = 1, 1461b41f1cecSNamhyung Kim }; 1462b41f1cecSNamhyung Kim struct { 1463b41f1cecSNamhyung Kim struct thread_map map; 1464b41f1cecSNamhyung Kim int threads[1]; 1465b41f1cecSNamhyung Kim } tmap = { 1466b41f1cecSNamhyung Kim .map.nr = 1, 1467b41f1cecSNamhyung Kim .threads = { 0 }, 1468b41f1cecSNamhyung Kim }; 1469b41f1cecSNamhyung Kim 1470ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 1471b41f1cecSNamhyung Kim if (evsel) { 147288fee52eSVince Weaver open_return = perf_evsel__open(evsel, NULL, &tmap.map); 147388fee52eSVince Weaver ret = open_return >= 0; 147488fee52eSVince Weaver 147588fee52eSVince Weaver if (open_return == -EACCES) { 147688fee52eSVince Weaver /* 147788fee52eSVince Weaver * This happens if the paranoid value 147888fee52eSVince Weaver * /proc/sys/kernel/perf_event_paranoid is set to 2 147988fee52eSVince Weaver * Re-run with exclude_kernel set; we don't do that 148088fee52eSVince Weaver * by default as some ARM machines do not support it. 148188fee52eSVince Weaver * 148288fee52eSVince Weaver */ 148388fee52eSVince Weaver evsel->attr.exclude_kernel = 1; 1484b41f1cecSNamhyung Kim ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; 148588fee52eSVince Weaver } 1486b41f1cecSNamhyung Kim perf_evsel__delete(evsel); 1487b41f1cecSNamhyung Kim } 1488b41f1cecSNamhyung Kim 1489b41f1cecSNamhyung Kim return ret; 1490b41f1cecSNamhyung Kim } 1491b41f1cecSNamhyung Kim 1492a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only) 1493668b8788SArnaldo Carvalho de Melo { 1494ab0e4800SYunlong Song unsigned int type, op, i, evt_i = 0, evt_num = 0; 14950b668bc9SArnaldo Carvalho de Melo char name[64]; 1496ab0e4800SYunlong Song char **evt_list = NULL; 1497ab0e4800SYunlong Song bool evt_num_known = false; 1498ab0e4800SYunlong Song 1499ab0e4800SYunlong Song restart: 1500ab0e4800SYunlong Song if (evt_num_known) { 1501ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1502ab0e4800SYunlong Song if (!evt_list) 1503ab0e4800SYunlong Song goto out_enomem; 1504ab0e4800SYunlong Song } 1505668b8788SArnaldo Carvalho de Melo 1506668b8788SArnaldo Carvalho de Melo for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 1507668b8788SArnaldo Carvalho de Melo for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 1508668b8788SArnaldo Carvalho de Melo /* skip invalid cache type */ 15090b668bc9SArnaldo Carvalho de Melo if (!perf_evsel__is_cache_op_valid(type, op)) 1510668b8788SArnaldo Carvalho de Melo continue; 1511668b8788SArnaldo Carvalho de Melo 1512668b8788SArnaldo Carvalho de Melo for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 15130b668bc9SArnaldo Carvalho de Melo __perf_evsel__hw_cache_type_op_res_name(type, op, i, 15140b668bc9SArnaldo Carvalho de Melo name, sizeof(name)); 1515947b4ad1SIngo Molnar if (event_glob != NULL && !strglobmatch(name, event_glob)) 1516668b8788SArnaldo Carvalho de Melo continue; 1517668b8788SArnaldo Carvalho de Melo 1518b41f1cecSNamhyung Kim if (!is_event_supported(PERF_TYPE_HW_CACHE, 1519b41f1cecSNamhyung Kim type | (op << 8) | (i << 16))) 1520b41f1cecSNamhyung Kim continue; 1521b41f1cecSNamhyung Kim 1522ab0e4800SYunlong Song if (!evt_num_known) { 1523ab0e4800SYunlong Song evt_num++; 1524ab0e4800SYunlong Song continue; 1525ab0e4800SYunlong Song } 1526ab0e4800SYunlong Song 1527ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 1528ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1529ab0e4800SYunlong Song goto out_enomem; 1530ab0e4800SYunlong Song evt_i++; 1531668b8788SArnaldo Carvalho de Melo } 1532668b8788SArnaldo Carvalho de Melo } 1533668b8788SArnaldo Carvalho de Melo } 1534668b8788SArnaldo Carvalho de Melo 1535ab0e4800SYunlong Song if (!evt_num_known) { 1536ab0e4800SYunlong Song evt_num_known = true; 1537ab0e4800SYunlong Song goto restart; 1538ab0e4800SYunlong Song } 1539ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1540ab0e4800SYunlong Song evt_i = 0; 1541ab0e4800SYunlong Song while (evt_i < evt_num) { 1542ab0e4800SYunlong Song if (name_only) { 1543ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1544ab0e4800SYunlong Song continue; 1545ab0e4800SYunlong Song } 1546ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], 1547ab0e4800SYunlong Song event_type_descriptors[PERF_TYPE_HW_CACHE]); 1548ab0e4800SYunlong Song } 1549ab0e4800SYunlong Song if (evt_num) 1550dc098b35SAndi Kleen printf("\n"); 1551ab0e4800SYunlong Song 1552ab0e4800SYunlong Song out_free: 1553ab0e4800SYunlong Song evt_num = evt_i; 1554ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1555ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1556ab0e4800SYunlong Song zfree(&evt_list); 1557ab0e4800SYunlong Song return evt_num; 1558ab0e4800SYunlong Song 1559ab0e4800SYunlong Song out_enomem: 1560ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 1561ab0e4800SYunlong Song if (evt_list) 1562ab0e4800SYunlong Song goto out_free; 1563ab0e4800SYunlong Song return evt_num; 1564668b8788SArnaldo Carvalho de Melo } 1565668b8788SArnaldo Carvalho de Melo 1566705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type, 1567a3277d2dSFrederic Weisbecker struct event_symbol *syms, unsigned max, 1568a3277d2dSFrederic Weisbecker bool name_only) 156986470930SIngo Molnar { 1570ab0e4800SYunlong Song unsigned int i, evt_i = 0, evt_num = 0; 1571947b4ad1SIngo Molnar char name[MAX_NAME_LEN]; 1572ab0e4800SYunlong Song char **evt_list = NULL; 1573ab0e4800SYunlong Song bool evt_num_known = false; 1574ab0e4800SYunlong Song 1575ab0e4800SYunlong Song restart: 1576ab0e4800SYunlong Song if (evt_num_known) { 1577ab0e4800SYunlong Song evt_list = zalloc(sizeof(char *) * evt_num); 1578ab0e4800SYunlong Song if (!evt_list) 1579ab0e4800SYunlong Song goto out_enomem; 1580ab0e4800SYunlong Song syms -= max; 1581ab0e4800SYunlong Song } 158286470930SIngo Molnar 15831dc12760SJiri Olsa for (i = 0; i < max; i++, syms++) { 1584668b8788SArnaldo Carvalho de Melo 1585668b8788SArnaldo Carvalho de Melo if (event_glob != NULL && 1586668b8788SArnaldo Carvalho de Melo !(strglobmatch(syms->symbol, event_glob) || 1587668b8788SArnaldo Carvalho de Melo (syms->alias && strglobmatch(syms->alias, event_glob)))) 1588668b8788SArnaldo Carvalho de Melo continue; 158986470930SIngo Molnar 1590b41f1cecSNamhyung Kim if (!is_event_supported(type, i)) 1591b41f1cecSNamhyung Kim continue; 1592b41f1cecSNamhyung Kim 1593ab0e4800SYunlong Song if (!evt_num_known) { 1594ab0e4800SYunlong Song evt_num++; 1595a3277d2dSFrederic Weisbecker continue; 1596a3277d2dSFrederic Weisbecker } 1597a3277d2dSFrederic Weisbecker 1598ab0e4800SYunlong Song if (!name_only && strlen(syms->alias)) 1599947b4ad1SIngo Molnar snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 160074d5b588SJaswinder Singh Rajput else 1601947b4ad1SIngo Molnar strncpy(name, syms->symbol, MAX_NAME_LEN); 160286470930SIngo Molnar 1603ab0e4800SYunlong Song evt_list[evt_i] = strdup(name); 1604ab0e4800SYunlong Song if (evt_list[evt_i] == NULL) 1605ab0e4800SYunlong Song goto out_enomem; 1606ab0e4800SYunlong Song evt_i++; 160786470930SIngo Molnar } 160886470930SIngo Molnar 1609ab0e4800SYunlong Song if (!evt_num_known) { 1610ab0e4800SYunlong Song evt_num_known = true; 1611ab0e4800SYunlong Song goto restart; 1612ab0e4800SYunlong Song } 1613ab0e4800SYunlong Song qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1614ab0e4800SYunlong Song evt_i = 0; 1615ab0e4800SYunlong Song while (evt_i < evt_num) { 1616ab0e4800SYunlong Song if (name_only) { 1617ab0e4800SYunlong Song printf("%s ", evt_list[evt_i++]); 1618ab0e4800SYunlong Song continue; 1619ab0e4800SYunlong Song } 1620ab0e4800SYunlong Song printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 1621ab0e4800SYunlong Song } 1622ab0e4800SYunlong Song if (evt_num) 1623689d3018SMarti Raudsepp printf("\n"); 1624ab0e4800SYunlong Song 1625ab0e4800SYunlong Song out_free: 1626ab0e4800SYunlong Song evt_num = evt_i; 1627ab0e4800SYunlong Song for (evt_i = 0; evt_i < evt_num; evt_i++) 1628ab0e4800SYunlong Song zfree(&evt_list[evt_i]); 1629ab0e4800SYunlong Song zfree(&evt_list); 1630ab0e4800SYunlong Song return; 1631ab0e4800SYunlong Song 1632ab0e4800SYunlong Song out_enomem: 1633ab0e4800SYunlong Song printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 1634ab0e4800SYunlong Song if (evt_list) 1635ab0e4800SYunlong Song goto out_free; 1636668b8788SArnaldo Carvalho de Melo } 16371dc12760SJiri Olsa 16381dc12760SJiri Olsa /* 16391dc12760SJiri Olsa * Print the help text for the event symbols: 16401dc12760SJiri Olsa */ 1641a3277d2dSFrederic Weisbecker void print_events(const char *event_glob, bool name_only) 16421dc12760SJiri Olsa { 16431dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 1644a3277d2dSFrederic Weisbecker event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 16451dc12760SJiri Olsa 16461dc12760SJiri Olsa print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 1647a3277d2dSFrederic Weisbecker event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 16481dc12760SJiri Olsa 1649a3277d2dSFrederic Weisbecker print_hwcache_events(event_glob, name_only); 165073c24cb8SJaswinder Singh Rajput 1651dc098b35SAndi Kleen print_pmu_events(event_glob, name_only); 1652dc098b35SAndi Kleen 1653668b8788SArnaldo Carvalho de Melo if (event_glob != NULL) 1654668b8788SArnaldo Carvalho de Melo return; 165573c24cb8SJaswinder Singh Rajput 1656a3277d2dSFrederic Weisbecker if (!name_only) { 1657947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 16585f537a26SJiri Olsa "rNNN", 16591cf4a063SArnaldo Carvalho de Melo event_type_descriptors[PERF_TYPE_RAW]); 16605f537a26SJiri Olsa printf(" %-50s [%s]\n", 16615f537a26SJiri Olsa "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 16625f537a26SJiri Olsa event_type_descriptors[PERF_TYPE_RAW]); 1663af3df2cfSBorislav Petkov printf(" (see 'man perf-list' on how to encode it)\n"); 1664689d3018SMarti Raudsepp printf("\n"); 166586470930SIngo Molnar 1666947b4ad1SIngo Molnar printf(" %-50s [%s]\n", 16673741eb9fSJacob Shin "mem:<addr>[/len][:access]", 166841bdcb23SLiming Wang event_type_descriptors[PERF_TYPE_BREAKPOINT]); 16691b290d67SFrederic Weisbecker printf("\n"); 1670a3277d2dSFrederic Weisbecker } 16711b290d67SFrederic Weisbecker 1672a3277d2dSFrederic Weisbecker print_tracepoint_events(NULL, NULL, name_only); 167386470930SIngo Molnar } 16748f707d84SJiri Olsa 16756cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term) 16768f707d84SJiri Olsa { 167716fa7e82SJiri Olsa return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 16788f707d84SJiri Olsa } 16798f707d84SJiri Olsa 16806cee6cd3SArnaldo Carvalho de Melo static int new_term(struct parse_events_term **_term, int type_val, 168116fa7e82SJiri Olsa int type_term, char *config, 1682cecf3a2eSJiri Olsa char *str, u64 num, int err_term, int err_val) 16838f707d84SJiri Olsa { 16846cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 16858f707d84SJiri Olsa 16868f707d84SJiri Olsa term = zalloc(sizeof(*term)); 16878f707d84SJiri Olsa if (!term) 16888f707d84SJiri Olsa return -ENOMEM; 16898f707d84SJiri Olsa 16908f707d84SJiri Olsa INIT_LIST_HEAD(&term->list); 169116fa7e82SJiri Olsa term->type_val = type_val; 169216fa7e82SJiri Olsa term->type_term = type_term; 16938f707d84SJiri Olsa term->config = config; 1694cecf3a2eSJiri Olsa term->err_term = err_term; 1695cecf3a2eSJiri Olsa term->err_val = err_val; 16968f707d84SJiri Olsa 169716fa7e82SJiri Olsa switch (type_val) { 16988f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_NUM: 16998f707d84SJiri Olsa term->val.num = num; 17008f707d84SJiri Olsa break; 17018f707d84SJiri Olsa case PARSE_EVENTS__TERM_TYPE_STR: 17028f707d84SJiri Olsa term->val.str = str; 17038f707d84SJiri Olsa break; 17048f707d84SJiri Olsa default: 17054be8be6bSAdrian Hunter free(term); 17068f707d84SJiri Olsa return -EINVAL; 17078f707d84SJiri Olsa } 17088f707d84SJiri Olsa 17098f707d84SJiri Olsa *_term = term; 17108f707d84SJiri Olsa return 0; 17118f707d84SJiri Olsa } 17128f707d84SJiri Olsa 17136cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term, 1714cecf3a2eSJiri Olsa int type_term, char *config, u64 num, 1715bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 171616fa7e82SJiri Olsa { 1717bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 1718bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 1719bb78ce7dSAdrian Hunter 172016fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, 1721cecf3a2eSJiri Olsa config, NULL, num, 1722cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 1723cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 172416fa7e82SJiri Olsa } 172516fa7e82SJiri Olsa 17266cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term, 1727cecf3a2eSJiri Olsa int type_term, char *config, char *str, 1728bb78ce7dSAdrian Hunter void *loc_term_, void *loc_val_) 172916fa7e82SJiri Olsa { 1730bb78ce7dSAdrian Hunter YYLTYPE *loc_term = loc_term_; 1731bb78ce7dSAdrian Hunter YYLTYPE *loc_val = loc_val_; 1732bb78ce7dSAdrian Hunter 173316fa7e82SJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, 1734cecf3a2eSJiri Olsa config, str, 0, 1735cecf3a2eSJiri Olsa loc_term ? loc_term->first_column : 0, 1736cecf3a2eSJiri Olsa loc_val ? loc_val->first_column : 0); 173716fa7e82SJiri Olsa } 173816fa7e82SJiri Olsa 17396cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term, 17401d33d6dcSJiri Olsa char *config, unsigned idx) 17411d33d6dcSJiri Olsa { 17421d33d6dcSJiri Olsa struct event_symbol *sym; 17431d33d6dcSJiri Olsa 17441d33d6dcSJiri Olsa BUG_ON(idx >= PERF_COUNT_HW_MAX); 17451d33d6dcSJiri Olsa sym = &event_symbols_hw[idx]; 17461d33d6dcSJiri Olsa 17471d33d6dcSJiri Olsa if (config) 17481d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 17491d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, config, 1750cecf3a2eSJiri Olsa (char *) sym->symbol, 0, 0, 0); 17511d33d6dcSJiri Olsa else 17521d33d6dcSJiri Olsa return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 17531d33d6dcSJiri Olsa PARSE_EVENTS__TERM_TYPE_USER, 1754cecf3a2eSJiri Olsa (char *) "event", (char *) sym->symbol, 1755cecf3a2eSJiri Olsa 0, 0, 0); 17561d33d6dcSJiri Olsa } 17571d33d6dcSJiri Olsa 17586cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new, 17596cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1760a6146d50SZheng Yan { 1761a6146d50SZheng Yan return new_term(new, term->type_val, term->type_term, term->config, 1762cecf3a2eSJiri Olsa term->val.str, term->val.num, 1763cecf3a2eSJiri Olsa term->err_term, term->err_val); 1764a6146d50SZheng Yan } 1765a6146d50SZheng Yan 17668f707d84SJiri Olsa void parse_events__free_terms(struct list_head *terms) 17678f707d84SJiri Olsa { 17686cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 17698f707d84SJiri Olsa 17708f707d84SJiri Olsa list_for_each_entry_safe(term, h, terms, list) 17718f707d84SJiri Olsa free(term); 17728f707d84SJiri Olsa } 1773b39b8393SJiri Olsa 1774b39b8393SJiri Olsa void parse_events_evlist_error(struct parse_events_evlist *data, 1775b39b8393SJiri Olsa int idx, const char *str) 1776b39b8393SJiri Olsa { 1777b39b8393SJiri Olsa struct parse_events_error *err = data->error; 1778b39b8393SJiri Olsa 1779a6ced2beSAdrian Hunter if (!err) 1780a6ced2beSAdrian Hunter return; 1781b39b8393SJiri Olsa err->idx = idx; 1782b39b8393SJiri Olsa err->str = strdup(str); 1783b39b8393SJiri Olsa WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 1784b39b8393SJiri Olsa } 1785