David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 1 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 2 | #include <linux/err.h> |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3 | #include "util.h" |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 4 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 5 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 6 | #include "evsel.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 7 | #include <subcmd/parse-options.h> |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 8 | #include "parse-events.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 9 | #include <subcmd/exec-cmd.h> |
Kan Liang | 42f60c2 | 2014-10-07 11:08:49 -0400 | [diff] [blame] | 10 | #include "string.h" |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 11 | #include "symbol.h" |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 12 | #include "cache.h" |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 13 | #include "header.h" |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 14 | #include "bpf-loader.h" |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 15 | #include "debug.h" |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 16 | #include <api/fs/tracing_path.h> |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 17 | #include "parse-events-bison.h" |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 18 | #define YY_EXTRA_TYPE int |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 19 | #include "parse-events-flex.h" |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 20 | #include "pmu.h" |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 21 | #include "thread_map.h" |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 22 | #include "cpumap.h" |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 23 | #include "asm/bug.h" |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 24 | |
| 25 | #define MAX_NAME_LEN 100 |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 26 | |
Jiri Olsa | 82ba1f2 | 2012-05-21 09:12:50 +0200 | [diff] [blame] | 27 | #ifdef PARSER_DEBUG |
| 28 | extern int parse_events_debug; |
| 29 | #endif |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 30 | int parse_events_parse(void *data, void *scanner); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 31 | static int get_config_terms(struct list_head *head_config, |
| 32 | struct list_head *head_terms __maybe_unused); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 33 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 34 | static struct perf_pmu_event_symbol *perf_pmu_events_list; |
| 35 | /* |
| 36 | * The variable indicates the number of supported pmu event symbols. |
| 37 | * 0 means not initialized and ready to init |
| 38 | * -1 means failed to init, don't try anymore |
| 39 | * >0 is the number of supported pmu event symbols |
| 40 | */ |
| 41 | static int perf_pmu_events_list_num; |
| 42 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 43 | struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 44 | [PERF_COUNT_HW_CPU_CYCLES] = { |
| 45 | .symbol = "cpu-cycles", |
| 46 | .alias = "cycles", |
| 47 | }, |
| 48 | [PERF_COUNT_HW_INSTRUCTIONS] = { |
| 49 | .symbol = "instructions", |
| 50 | .alias = "", |
| 51 | }, |
| 52 | [PERF_COUNT_HW_CACHE_REFERENCES] = { |
| 53 | .symbol = "cache-references", |
| 54 | .alias = "", |
| 55 | }, |
| 56 | [PERF_COUNT_HW_CACHE_MISSES] = { |
| 57 | .symbol = "cache-misses", |
| 58 | .alias = "", |
| 59 | }, |
| 60 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { |
| 61 | .symbol = "branch-instructions", |
| 62 | .alias = "branches", |
| 63 | }, |
| 64 | [PERF_COUNT_HW_BRANCH_MISSES] = { |
| 65 | .symbol = "branch-misses", |
| 66 | .alias = "", |
| 67 | }, |
| 68 | [PERF_COUNT_HW_BUS_CYCLES] = { |
| 69 | .symbol = "bus-cycles", |
| 70 | .alias = "", |
| 71 | }, |
| 72 | [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { |
| 73 | .symbol = "stalled-cycles-frontend", |
| 74 | .alias = "idle-cycles-frontend", |
| 75 | }, |
| 76 | [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { |
| 77 | .symbol = "stalled-cycles-backend", |
| 78 | .alias = "idle-cycles-backend", |
| 79 | }, |
| 80 | [PERF_COUNT_HW_REF_CPU_CYCLES] = { |
| 81 | .symbol = "ref-cycles", |
| 82 | .alias = "", |
| 83 | }, |
| 84 | }; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 85 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 86 | struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 87 | [PERF_COUNT_SW_CPU_CLOCK] = { |
| 88 | .symbol = "cpu-clock", |
| 89 | .alias = "", |
| 90 | }, |
| 91 | [PERF_COUNT_SW_TASK_CLOCK] = { |
| 92 | .symbol = "task-clock", |
| 93 | .alias = "", |
| 94 | }, |
| 95 | [PERF_COUNT_SW_PAGE_FAULTS] = { |
| 96 | .symbol = "page-faults", |
| 97 | .alias = "faults", |
| 98 | }, |
| 99 | [PERF_COUNT_SW_CONTEXT_SWITCHES] = { |
| 100 | .symbol = "context-switches", |
| 101 | .alias = "cs", |
| 102 | }, |
| 103 | [PERF_COUNT_SW_CPU_MIGRATIONS] = { |
| 104 | .symbol = "cpu-migrations", |
| 105 | .alias = "migrations", |
| 106 | }, |
| 107 | [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { |
| 108 | .symbol = "minor-faults", |
| 109 | .alias = "", |
| 110 | }, |
| 111 | [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { |
| 112 | .symbol = "major-faults", |
| 113 | .alias = "", |
| 114 | }, |
| 115 | [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { |
| 116 | .symbol = "alignment-faults", |
| 117 | .alias = "", |
| 118 | }, |
| 119 | [PERF_COUNT_SW_EMULATION_FAULTS] = { |
| 120 | .symbol = "emulation-faults", |
| 121 | .alias = "", |
| 122 | }, |
Adrian Hunter | d22d1a2 | 2013-08-31 21:50:52 +0300 | [diff] [blame] | 123 | [PERF_COUNT_SW_DUMMY] = { |
| 124 | .symbol = "dummy", |
| 125 | .alias = "", |
| 126 | }, |
Arnaldo Carvalho de Melo | bae9cc4 | 2015-11-27 15:54:33 -0300 | [diff] [blame] | 127 | [PERF_COUNT_SW_BPF_OUTPUT] = { |
| 128 | .symbol = "bpf-output", |
| 129 | .alias = "", |
| 130 | }, |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 133 | #define __PERF_EVENT_FIELD(config, name) \ |
| 134 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 135 | |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 136 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 137 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 138 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 139 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 140 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 141 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 142 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 143 | if (sys_dirent.d_type == DT_DIR && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 144 | (strcmp(sys_dirent.d_name, ".")) && \ |
| 145 | (strcmp(sys_dirent.d_name, ".."))) |
| 146 | |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 147 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
| 148 | { |
| 149 | char evt_path[MAXPATHLEN]; |
| 150 | int fd; |
| 151 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 152 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 153 | sys_dir->d_name, evt_dir->d_name); |
| 154 | fd = open(evt_path, O_RDONLY); |
| 155 | if (fd < 0) |
| 156 | return -EINVAL; |
| 157 | close(fd); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 162 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 163 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 164 | if (evt_dirent.d_type == DT_DIR && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 165 | (strcmp(evt_dirent.d_name, ".")) && \ |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 166 | (strcmp(evt_dirent.d_name, "..")) && \ |
| 167 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 168 | |
Li Zefan | 270bbbe | 2009-09-17 16:34:51 +0800 | [diff] [blame] | 169 | #define MAX_EVENT_LENGTH 512 |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 170 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 171 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 172 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 173 | { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 174 | struct tracepoint_path *path = NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 175 | DIR *sys_dir, *evt_dir; |
| 176 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
Stephane Eranian | 8aa8a7c | 2012-03-13 16:51:02 +0100 | [diff] [blame] | 177 | char id_buf[24]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 178 | int fd; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 179 | u64 id; |
| 180 | char evt_path[MAXPATHLEN]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 181 | char dir_path[MAXPATHLEN]; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 182 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 183 | sys_dir = opendir(tracing_events_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 184 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 185 | return NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 186 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 187 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 188 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 189 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 190 | sys_dirent.d_name); |
| 191 | evt_dir = opendir(dir_path); |
| 192 | if (!evt_dir) |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 193 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 194 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 195 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 196 | |
| 197 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 198 | evt_dirent.d_name); |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 199 | fd = open(evt_path, O_RDONLY); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 200 | if (fd < 0) |
| 201 | continue; |
| 202 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| 203 | close(fd); |
| 204 | continue; |
| 205 | } |
| 206 | close(fd); |
| 207 | id = atoll(id_buf); |
| 208 | if (id == config) { |
| 209 | closedir(evt_dir); |
| 210 | closedir(sys_dir); |
Julia Lawall | 59b4cae | 2009-12-06 10:16:30 +0100 | [diff] [blame] | 211 | path = zalloc(sizeof(*path)); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 212 | path->system = malloc(MAX_EVENT_LENGTH); |
| 213 | if (!path->system) { |
| 214 | free(path); |
| 215 | return NULL; |
| 216 | } |
| 217 | path->name = malloc(MAX_EVENT_LENGTH); |
| 218 | if (!path->name) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 219 | zfree(&path->system); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 220 | free(path); |
| 221 | return NULL; |
| 222 | } |
| 223 | strncpy(path->system, sys_dirent.d_name, |
| 224 | MAX_EVENT_LENGTH); |
| 225 | strncpy(path->name, evt_dirent.d_name, |
| 226 | MAX_EVENT_LENGTH); |
| 227 | return path; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | closedir(evt_dir); |
| 231 | } |
| 232 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 233 | closedir(sys_dir); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 234 | return NULL; |
| 235 | } |
| 236 | |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 237 | struct tracepoint_path *tracepoint_name_to_path(const char *name) |
| 238 | { |
| 239 | struct tracepoint_path *path = zalloc(sizeof(*path)); |
| 240 | char *str = strchr(name, ':'); |
| 241 | |
| 242 | if (path == NULL || str == NULL) { |
| 243 | free(path); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | path->system = strndup(name, str - name); |
| 248 | path->name = strdup(str+1); |
| 249 | |
| 250 | if (path->system == NULL || path->name == NULL) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 251 | zfree(&path->system); |
| 252 | zfree(&path->name); |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 253 | free(path); |
| 254 | path = NULL; |
| 255 | } |
| 256 | |
| 257 | return path; |
| 258 | } |
| 259 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 260 | const char *event_type(int type) |
| 261 | { |
| 262 | switch (type) { |
| 263 | case PERF_TYPE_HARDWARE: |
| 264 | return "hardware"; |
| 265 | |
| 266 | case PERF_TYPE_SOFTWARE: |
| 267 | return "software"; |
| 268 | |
| 269 | case PERF_TYPE_TRACEPOINT: |
| 270 | return "tracepoint"; |
| 271 | |
| 272 | case PERF_TYPE_HW_CACHE: |
| 273 | return "hardware-cache"; |
| 274 | |
| 275 | default: |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | return "unknown"; |
| 280 | } |
| 281 | |
Wang Nan | e814fdd | 2016-02-19 11:43:59 +0000 | [diff] [blame] | 282 | static int parse_events__is_name_term(struct parse_events_term *term) |
| 283 | { |
| 284 | return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; |
| 285 | } |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 286 | |
Wang Nan | e814fdd | 2016-02-19 11:43:59 +0000 | [diff] [blame] | 287 | static char *get_config_name(struct list_head *head_terms) |
| 288 | { |
| 289 | struct parse_events_term *term; |
| 290 | |
| 291 | if (!head_terms) |
| 292 | return NULL; |
| 293 | |
| 294 | list_for_each_entry(term, head_terms, list) |
| 295 | if (parse_events__is_name_term(term)) |
| 296 | return term->val.str; |
| 297 | |
| 298 | return NULL; |
| 299 | } |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 300 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 301 | static struct perf_evsel * |
| 302 | __add_event(struct list_head *list, int *idx, |
| 303 | struct perf_event_attr *attr, |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 304 | char *name, struct cpu_map *cpus, |
| 305 | struct list_head *config_terms) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 306 | { |
| 307 | struct perf_evsel *evsel; |
| 308 | |
| 309 | event_attr_init(attr); |
| 310 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 311 | evsel = perf_evsel__new_idx(attr, (*idx)++); |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 312 | if (!evsel) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 313 | return NULL; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 314 | |
Adrian Hunter | fce4d29 | 2015-09-08 10:58:55 +0300 | [diff] [blame] | 315 | evsel->cpus = cpu_map__get(cpus); |
| 316 | evsel->own_cpus = cpu_map__get(cpus); |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 317 | |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 318 | if (name) |
| 319 | evsel->name = strdup(name); |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 320 | |
| 321 | if (config_terms) |
| 322 | list_splice(config_terms, &evsel->config_terms); |
| 323 | |
Jiri Olsa | b847cbd | 2012-05-21 09:12:51 +0200 | [diff] [blame] | 324 | list_add_tail(&evsel->node, list); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 325 | return evsel; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 326 | } |
| 327 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 328 | static int add_event(struct list_head *list, int *idx, |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 329 | struct perf_event_attr *attr, char *name, |
| 330 | struct list_head *config_terms) |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 331 | { |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 332 | return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 335 | static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 336 | { |
| 337 | int i, j; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 338 | int n, longest = -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 339 | |
| 340 | for (i = 0; i < size; i++) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 341 | for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 342 | n = strlen(names[i][j]); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 343 | if (n > longest && !strncasecmp(str, names[i][j], n)) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 344 | longest = n; |
| 345 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 346 | if (longest > 0) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 347 | return i; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 350 | return -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 353 | typedef int config_term_func_t(struct perf_event_attr *attr, |
| 354 | struct parse_events_term *term, |
| 355 | struct parse_events_error *err); |
| 356 | static int config_term_common(struct perf_event_attr *attr, |
| 357 | struct parse_events_term *term, |
| 358 | struct parse_events_error *err); |
| 359 | static int config_attr(struct perf_event_attr *attr, |
| 360 | struct list_head *head, |
| 361 | struct parse_events_error *err, |
| 362 | config_term_func_t config_term); |
| 363 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 364 | int parse_events_add_cache(struct list_head *list, int *idx, |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 365 | char *type, char *op_result1, char *op_result2, |
Arnaldo Carvalho de Melo | 2c97b0d | 2016-02-19 19:47:04 -0300 | [diff] [blame] | 366 | struct parse_events_error *err, |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 367 | struct list_head *head_config) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 368 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 369 | struct perf_event_attr attr; |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 370 | LIST_HEAD(config_terms); |
| 371 | char name[MAX_NAME_LEN], *config_name; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 372 | int cache_type = -1, cache_op = -1, cache_result = -1; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 373 | char *op_result[2] = { op_result1, op_result2 }; |
| 374 | int i, n; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 375 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 376 | /* |
| 377 | * No fallback - if we cannot get a clear cache type |
| 378 | * then bail out: |
| 379 | */ |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 380 | cache_type = parse_aliases(type, perf_evsel__hw_cache, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 381 | PERF_COUNT_HW_CACHE_MAX); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 382 | if (cache_type == -1) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 383 | return -EINVAL; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 384 | |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 385 | config_name = get_config_name(head_config); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 386 | n = snprintf(name, MAX_NAME_LEN, "%s", type); |
| 387 | |
| 388 | for (i = 0; (i < 2) && (op_result[i]); i++) { |
| 389 | char *str = op_result[i]; |
| 390 | |
Jiri Olsa | 275ef38 | 2012-09-05 19:51:33 +0200 | [diff] [blame] | 391 | n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 392 | |
| 393 | if (cache_op == -1) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 394 | cache_op = parse_aliases(str, perf_evsel__hw_cache_op, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 395 | PERF_COUNT_HW_CACHE_OP_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 396 | if (cache_op >= 0) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 397 | if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 398 | return -EINVAL; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (cache_result == -1) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 404 | cache_result = parse_aliases(str, perf_evsel__hw_cache_result, |
| 405 | PERF_COUNT_HW_CACHE_RESULT_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 406 | if (cache_result >= 0) |
| 407 | continue; |
| 408 | } |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 409 | } |
| 410 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 411 | /* |
| 412 | * Fall back to reads: |
| 413 | */ |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 414 | if (cache_op == -1) |
| 415 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 416 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 417 | /* |
| 418 | * Fall back to accesses: |
| 419 | */ |
| 420 | if (cache_result == -1) |
| 421 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; |
| 422 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 423 | memset(&attr, 0, sizeof(attr)); |
| 424 | attr.config = cache_type | (cache_op << 8) | (cache_result << 16); |
| 425 | attr.type = PERF_TYPE_HW_CACHE; |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 426 | |
| 427 | if (head_config) { |
Arnaldo Carvalho de Melo | 2c97b0d | 2016-02-19 19:47:04 -0300 | [diff] [blame] | 428 | if (config_attr(&attr, head_config, err, |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 429 | config_term_common)) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | if (get_config_terms(head_config, &config_terms)) |
| 433 | return -ENOMEM; |
| 434 | } |
| 435 | return add_event(list, idx, &attr, config_name ? : name, &config_terms); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 438 | static void tracepoint_error(struct parse_events_error *e, int err, |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 439 | char *sys, char *name) |
| 440 | { |
| 441 | char help[BUFSIZ]; |
| 442 | |
Adrian Hunter | ec183d2 | 2016-01-26 14:05:20 +0200 | [diff] [blame] | 443 | if (!e) |
| 444 | return; |
| 445 | |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 446 | /* |
| 447 | * We get error directly from syscall errno ( > 0), |
| 448 | * or from encoded pointer's error ( < 0). |
| 449 | */ |
| 450 | err = abs(err); |
| 451 | |
| 452 | switch (err) { |
| 453 | case EACCES: |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 454 | e->str = strdup("can't access trace events"); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 455 | break; |
| 456 | case ENOENT: |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 457 | e->str = strdup("unknown tracepoint"); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 458 | break; |
| 459 | default: |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 460 | e->str = strdup("failed to add tracepoint"); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 461 | break; |
| 462 | } |
| 463 | |
| 464 | tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 465 | e->help = strdup(help); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 466 | } |
| 467 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 468 | static int add_tracepoint(struct list_head *list, int *idx, |
Jiri Olsa | e2f9f8e | 2015-09-07 10:38:05 +0200 | [diff] [blame] | 469 | char *sys_name, char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 470 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 471 | struct list_head *head_config) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 472 | { |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 473 | struct perf_evsel *evsel; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 474 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 475 | evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 476 | if (IS_ERR(evsel)) { |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 477 | tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 478 | return PTR_ERR(evsel); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 479 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 480 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 481 | if (head_config) { |
| 482 | LIST_HEAD(config_terms); |
| 483 | |
| 484 | if (get_config_terms(head_config, &config_terms)) |
| 485 | return -ENOMEM; |
| 486 | list_splice(&config_terms, &evsel->config_terms); |
| 487 | } |
| 488 | |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 489 | list_add_tail(&evsel->node, list); |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 490 | return 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 491 | } |
| 492 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 493 | static int add_tracepoint_multi_event(struct list_head *list, int *idx, |
Jiri Olsa | e2f9f8e | 2015-09-07 10:38:05 +0200 | [diff] [blame] | 494 | char *sys_name, char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 495 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 496 | struct list_head *head_config) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 497 | { |
| 498 | char evt_path[MAXPATHLEN]; |
| 499 | struct dirent *evt_ent; |
| 500 | DIR *evt_dir; |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 501 | int ret = 0, found = 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 502 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 503 | snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 504 | evt_dir = opendir(evt_path); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 505 | if (!evt_dir) { |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 506 | tracepoint_error(err, errno, sys_name, evt_name); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 507 | return -1; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 510 | while (!ret && (evt_ent = readdir(evt_dir))) { |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 511 | if (!strcmp(evt_ent->d_name, ".") |
| 512 | || !strcmp(evt_ent->d_name, "..") |
| 513 | || !strcmp(evt_ent->d_name, "enable") |
| 514 | || !strcmp(evt_ent->d_name, "filter")) |
| 515 | continue; |
| 516 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 517 | if (!strglobmatch(evt_ent->d_name, evt_name)) |
Masami Hiramatsu | fb1d2ed | 2010-01-05 17:47:17 -0500 | [diff] [blame] | 518 | continue; |
| 519 | |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 520 | found++; |
| 521 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 522 | ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 523 | err, head_config); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 526 | if (!found) { |
| 527 | tracepoint_error(err, ENOENT, sys_name, evt_name); |
| 528 | ret = -1; |
| 529 | } |
| 530 | |
Jiri Olsa | 0bd3f08 | 2012-12-17 14:08:36 +0100 | [diff] [blame] | 531 | closedir(evt_dir); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 532 | return ret; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 533 | } |
| 534 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 535 | static int add_tracepoint_event(struct list_head *list, int *idx, |
Jiri Olsa | e2f9f8e | 2015-09-07 10:38:05 +0200 | [diff] [blame] | 536 | char *sys_name, char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 537 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 538 | struct list_head *head_config) |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 539 | { |
| 540 | return strpbrk(evt_name, "*?") ? |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 541 | add_tracepoint_multi_event(list, idx, sys_name, evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 542 | err, head_config) : |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 543 | add_tracepoint(list, idx, sys_name, evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 544 | err, head_config); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 545 | } |
| 546 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 547 | static int add_tracepoint_multi_sys(struct list_head *list, int *idx, |
Jiri Olsa | e2f9f8e | 2015-09-07 10:38:05 +0200 | [diff] [blame] | 548 | char *sys_name, char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 549 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 550 | struct list_head *head_config) |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 551 | { |
| 552 | struct dirent *events_ent; |
| 553 | DIR *events_dir; |
| 554 | int ret = 0; |
| 555 | |
| 556 | events_dir = opendir(tracing_events_path); |
| 557 | if (!events_dir) { |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 558 | tracepoint_error(err, errno, sys_name, evt_name); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | while (!ret && (events_ent = readdir(events_dir))) { |
| 563 | if (!strcmp(events_ent->d_name, ".") |
| 564 | || !strcmp(events_ent->d_name, "..") |
| 565 | || !strcmp(events_ent->d_name, "enable") |
| 566 | || !strcmp(events_ent->d_name, "header_event") |
| 567 | || !strcmp(events_ent->d_name, "header_page")) |
| 568 | continue; |
| 569 | |
| 570 | if (!strglobmatch(events_ent->d_name, sys_name)) |
| 571 | continue; |
| 572 | |
| 573 | ret = add_tracepoint_event(list, idx, events_ent->d_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 574 | evt_name, err, head_config); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | closedir(events_dir); |
| 578 | return ret; |
| 579 | } |
| 580 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 581 | struct __add_bpf_event_param { |
| 582 | struct parse_events_evlist *data; |
| 583 | struct list_head *list; |
| 584 | }; |
| 585 | |
| 586 | static int add_bpf_event(struct probe_trace_event *tev, int fd, |
| 587 | void *_param) |
| 588 | { |
| 589 | LIST_HEAD(new_evsels); |
| 590 | struct __add_bpf_event_param *param = _param; |
| 591 | struct parse_events_evlist *evlist = param->data; |
| 592 | struct list_head *list = param->list; |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 593 | struct perf_evsel *pos; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 594 | int err; |
| 595 | |
| 596 | pr_debug("add bpf event %s:%s and attach bpf program %d\n", |
| 597 | tev->group, tev->event, fd); |
| 598 | |
| 599 | err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, tev->group, |
| 600 | tev->event, evlist->error, NULL); |
| 601 | if (err) { |
| 602 | struct perf_evsel *evsel, *tmp; |
| 603 | |
| 604 | pr_debug("Failed to add BPF event %s:%s\n", |
| 605 | tev->group, tev->event); |
| 606 | list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { |
| 607 | list_del(&evsel->node); |
| 608 | perf_evsel__delete(evsel); |
| 609 | } |
| 610 | return err; |
| 611 | } |
| 612 | pr_debug("adding %s:%s\n", tev->group, tev->event); |
| 613 | |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 614 | list_for_each_entry(pos, &new_evsels, node) { |
| 615 | pr_debug("adding %s:%s to %p\n", |
| 616 | tev->group, tev->event, pos); |
| 617 | pos->bpf_fd = fd; |
| 618 | } |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 619 | list_splice(&new_evsels, list); |
| 620 | return 0; |
| 621 | } |
| 622 | |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 623 | int parse_events_load_bpf_obj(struct parse_events_evlist *data, |
| 624 | struct list_head *list, |
| 625 | struct bpf_object *obj) |
| 626 | { |
| 627 | int err; |
| 628 | char errbuf[BUFSIZ]; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 629 | struct __add_bpf_event_param param = {data, list}; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 630 | static bool registered_unprobe_atexit = false; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 631 | |
| 632 | if (IS_ERR(obj) || !obj) { |
| 633 | snprintf(errbuf, sizeof(errbuf), |
| 634 | "Internal error: load bpf obj with NULL"); |
| 635 | err = -EINVAL; |
| 636 | goto errout; |
| 637 | } |
| 638 | |
| 639 | /* |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 640 | * Register atexit handler before calling bpf__probe() so |
| 641 | * bpf__probe() don't need to unprobe probe points its already |
| 642 | * created when failure. |
| 643 | */ |
| 644 | if (!registered_unprobe_atexit) { |
| 645 | atexit(bpf__clear); |
| 646 | registered_unprobe_atexit = true; |
| 647 | } |
| 648 | |
| 649 | err = bpf__probe(obj); |
| 650 | if (err) { |
| 651 | bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); |
| 652 | goto errout; |
| 653 | } |
| 654 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 655 | err = bpf__load(obj); |
| 656 | if (err) { |
| 657 | bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); |
| 658 | goto errout; |
| 659 | } |
| 660 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 661 | err = bpf__foreach_tev(obj, add_bpf_event, ¶m); |
| 662 | if (err) { |
| 663 | snprintf(errbuf, sizeof(errbuf), |
| 664 | "Attach events in BPF object failed"); |
| 665 | goto errout; |
| 666 | } |
| 667 | |
| 668 | return 0; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 669 | errout: |
| 670 | data->error->help = strdup("(add -v to see detail)"); |
| 671 | data->error->str = strdup(errbuf); |
| 672 | return err; |
| 673 | } |
| 674 | |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame^] | 675 | static int |
| 676 | parse_events_config_bpf(struct parse_events_evlist *data, |
| 677 | struct bpf_object *obj, |
| 678 | struct list_head *head_config) |
| 679 | { |
| 680 | struct parse_events_term *term; |
| 681 | int error_pos; |
| 682 | |
| 683 | if (!head_config || list_empty(head_config)) |
| 684 | return 0; |
| 685 | |
| 686 | list_for_each_entry(term, head_config, list) { |
| 687 | char errbuf[BUFSIZ]; |
| 688 | int err; |
| 689 | |
| 690 | if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { |
| 691 | snprintf(errbuf, sizeof(errbuf), |
| 692 | "Invalid config term for BPF object"); |
| 693 | errbuf[BUFSIZ - 1] = '\0'; |
| 694 | |
| 695 | data->error->idx = term->err_term; |
| 696 | data->error->str = strdup(errbuf); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | err = bpf__config_obj(obj, term, NULL, &error_pos); |
| 701 | if (err) { |
| 702 | bpf__strerror_config_obj(obj, term, NULL, |
| 703 | &error_pos, err, errbuf, |
| 704 | sizeof(errbuf)); |
| 705 | data->error->help = strdup( |
| 706 | "Hint:\tValid config term:\n" |
| 707 | " \tmap:[<arraymap>].value=[value]\n" |
| 708 | " \t(add -v to see detail)"); |
| 709 | data->error->str = strdup(errbuf); |
| 710 | if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) |
| 711 | data->error->idx = term->err_val; |
| 712 | else |
| 713 | data->error->idx = term->err_term + error_pos; |
| 714 | return err; |
| 715 | } |
| 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 720 | int parse_events_load_bpf(struct parse_events_evlist *data, |
| 721 | struct list_head *list, |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 722 | char *bpf_file_name, |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame^] | 723 | bool source, |
| 724 | struct list_head *head_config) |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 725 | { |
| 726 | struct bpf_object *obj; |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame^] | 727 | int err; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 728 | |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 729 | obj = bpf__prepare_load(bpf_file_name, source); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 730 | if (IS_ERR(obj)) { |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 731 | char errbuf[BUFSIZ]; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 732 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 733 | err = PTR_ERR(obj); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 734 | |
| 735 | if (err == -ENOTSUP) |
| 736 | snprintf(errbuf, sizeof(errbuf), |
| 737 | "BPF support is not compiled"); |
| 738 | else |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 739 | bpf__strerror_prepare_load(bpf_file_name, |
| 740 | source, |
| 741 | -err, errbuf, |
| 742 | sizeof(errbuf)); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 743 | |
| 744 | data->error->help = strdup("(add -v to see detail)"); |
| 745 | data->error->str = strdup(errbuf); |
| 746 | return err; |
| 747 | } |
| 748 | |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame^] | 749 | err = parse_events_load_bpf_obj(data, list, obj); |
| 750 | if (err) |
| 751 | return err; |
| 752 | return parse_events_config_bpf(data, obj, head_config); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 755 | static int |
| 756 | parse_breakpoint_type(const char *type, struct perf_event_attr *attr) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 757 | { |
| 758 | int i; |
| 759 | |
| 760 | for (i = 0; i < 3; i++) { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 761 | if (!type || !type[i]) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 762 | break; |
| 763 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 764 | #define CHECK_SET_TYPE(bit) \ |
| 765 | do { \ |
| 766 | if (attr->bp_type & bit) \ |
| 767 | return -EINVAL; \ |
| 768 | else \ |
| 769 | attr->bp_type |= bit; \ |
| 770 | } while (0) |
| 771 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 772 | switch (type[i]) { |
| 773 | case 'r': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 774 | CHECK_SET_TYPE(HW_BREAKPOINT_R); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 775 | break; |
| 776 | case 'w': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 777 | CHECK_SET_TYPE(HW_BREAKPOINT_W); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 778 | break; |
| 779 | case 'x': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 780 | CHECK_SET_TYPE(HW_BREAKPOINT_X); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 781 | break; |
| 782 | default: |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 783 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 784 | } |
| 785 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 786 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 787 | #undef CHECK_SET_TYPE |
| 788 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 789 | if (!attr->bp_type) /* Default */ |
| 790 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; |
| 791 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 792 | return 0; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 793 | } |
| 794 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 795 | int parse_events_add_breakpoint(struct list_head *list, int *idx, |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 796 | void *ptr, char *type, u64 len) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 797 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 798 | struct perf_event_attr attr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 799 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 800 | memset(&attr, 0, sizeof(attr)); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 801 | attr.bp_addr = (unsigned long) ptr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 802 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 803 | if (parse_breakpoint_type(type, &attr)) |
| 804 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 805 | |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 806 | /* Provide some defaults if len is not specified */ |
| 807 | if (!len) { |
| 808 | if (attr.bp_type == HW_BREAKPOINT_X) |
| 809 | len = sizeof(long); |
| 810 | else |
| 811 | len = HW_BREAKPOINT_LEN_4; |
| 812 | } |
| 813 | |
| 814 | attr.bp_len = len; |
Frederic Weisbecker | aa59a48 | 2010-06-24 21:36:19 +0200 | [diff] [blame] | 815 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 816 | attr.type = PERF_TYPE_BREAKPOINT; |
Jovi Zhang | 4a841d6 | 2012-07-15 03:03:10 +0800 | [diff] [blame] | 817 | attr.sample_period = 1; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 818 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 819 | return add_event(list, idx, &attr, NULL, NULL); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 822 | static int check_type_val(struct parse_events_term *term, |
| 823 | struct parse_events_error *err, |
| 824 | int type) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 825 | { |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 826 | if (type == term->type_val) |
| 827 | return 0; |
| 828 | |
| 829 | if (err) { |
| 830 | err->idx = term->err_val; |
| 831 | if (type == PARSE_EVENTS__TERM_TYPE_NUM) |
| 832 | err->str = strdup("expected numeric value"); |
| 833 | else |
| 834 | err->str = strdup("expected string value"); |
| 835 | } |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 839 | /* |
| 840 | * Update according to parse-events.l |
| 841 | */ |
| 842 | static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { |
| 843 | [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", |
| 844 | [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", |
| 845 | [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", |
| 846 | [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", |
| 847 | [PARSE_EVENTS__TERM_TYPE_NAME] = "name", |
| 848 | [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", |
| 849 | [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", |
| 850 | [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", |
| 851 | [PARSE_EVENTS__TERM_TYPE_TIME] = "time", |
| 852 | [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", |
| 853 | [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", |
| 854 | [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", |
| 855 | [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", |
| 856 | }; |
| 857 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 858 | static bool config_term_shrinked; |
| 859 | |
| 860 | static bool |
| 861 | config_term_avail(int term_type, struct parse_events_error *err) |
| 862 | { |
| 863 | if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { |
| 864 | err->str = strdup("Invalid term_type"); |
| 865 | return false; |
| 866 | } |
| 867 | if (!config_term_shrinked) |
| 868 | return true; |
| 869 | |
| 870 | switch (term_type) { |
| 871 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
| 872 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
| 873 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
| 874 | case PARSE_EVENTS__TERM_TYPE_NAME: |
| 875 | return true; |
| 876 | default: |
| 877 | if (!err) |
| 878 | return false; |
| 879 | |
| 880 | /* term_type is validated so indexing is safe */ |
| 881 | if (asprintf(&err->str, "'%s' is not usable in 'perf stat'", |
| 882 | config_term_names[term_type]) < 0) |
| 883 | err->str = NULL; |
| 884 | return false; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | void parse_events__shrink_config_terms(void) |
| 889 | { |
| 890 | config_term_shrinked = true; |
| 891 | } |
| 892 | |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 893 | static int config_term_common(struct perf_event_attr *attr, |
| 894 | struct parse_events_term *term, |
| 895 | struct parse_events_error *err) |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 896 | { |
| 897 | #define CHECK_TYPE_VAL(type) \ |
| 898 | do { \ |
| 899 | if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ |
| 900 | return -EINVAL; \ |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 901 | } while (0) |
| 902 | |
| 903 | switch (term->type_term) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 904 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 905 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 906 | attr->config = term->val.num; |
| 907 | break; |
| 908 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 909 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 910 | attr->config1 = term->val.num; |
| 911 | break; |
| 912 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 913 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 914 | attr->config2 = term->val.num; |
| 915 | break; |
| 916 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 917 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 918 | break; |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 919 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
| 920 | CHECK_TYPE_VAL(NUM); |
| 921 | break; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 922 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: |
| 923 | /* |
| 924 | * TODO uncomment when the field is available |
| 925 | * attr->branch_sample_type = term->val.num; |
| 926 | */ |
| 927 | break; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 928 | case PARSE_EVENTS__TERM_TYPE_TIME: |
| 929 | CHECK_TYPE_VAL(NUM); |
| 930 | if (term->val.num > 1) { |
| 931 | err->str = strdup("expected 0 or 1"); |
| 932 | err->idx = term->err_val; |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 936 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
| 937 | CHECK_TYPE_VAL(STR); |
| 938 | break; |
| 939 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
| 940 | CHECK_TYPE_VAL(NUM); |
| 941 | break; |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 942 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
| 943 | CHECK_TYPE_VAL(NUM); |
| 944 | break; |
| 945 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
| 946 | CHECK_TYPE_VAL(NUM); |
| 947 | break; |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 948 | case PARSE_EVENTS__TERM_TYPE_NAME: |
| 949 | CHECK_TYPE_VAL(STR); |
| 950 | break; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 951 | default: |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 952 | err->str = strdup("unknown term"); |
| 953 | err->idx = term->err_term; |
| 954 | err->help = parse_events_formats_error_string(NULL); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 955 | return -EINVAL; |
| 956 | } |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 957 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 958 | /* |
| 959 | * Check term availbility after basic checking so |
| 960 | * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. |
| 961 | * |
| 962 | * If check availbility at the entry of this function, |
| 963 | * user will see "'<sysfs term>' is not usable in 'perf stat'" |
| 964 | * if an invalid config term is provided for legacy events |
| 965 | * (for example, instructions/badterm/...), which is confusing. |
| 966 | */ |
| 967 | if (!config_term_avail(term->type_term, err)) |
| 968 | return -EINVAL; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 969 | return 0; |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 970 | #undef CHECK_TYPE_VAL |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 971 | } |
| 972 | |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 973 | static int config_term_pmu(struct perf_event_attr *attr, |
| 974 | struct parse_events_term *term, |
| 975 | struct parse_events_error *err) |
| 976 | { |
| 977 | if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) |
| 978 | /* |
| 979 | * Always succeed for sysfs terms, as we dont know |
| 980 | * at this point what type they need to have. |
| 981 | */ |
| 982 | return 0; |
| 983 | else |
| 984 | return config_term_common(attr, term, err); |
| 985 | } |
| 986 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 987 | static int config_term_tracepoint(struct perf_event_attr *attr, |
| 988 | struct parse_events_term *term, |
| 989 | struct parse_events_error *err) |
| 990 | { |
| 991 | switch (term->type_term) { |
| 992 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
| 993 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 994 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
| 995 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 996 | return config_term_common(attr, term, err); |
| 997 | default: |
| 998 | if (err) { |
| 999 | err->idx = term->err_term; |
| 1000 | err->str = strdup("unknown term"); |
| 1001 | err->help = strdup("valid terms: call-graph,stack-size\n"); |
| 1002 | } |
| 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1009 | static int config_attr(struct perf_event_attr *attr, |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1010 | struct list_head *head, |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1011 | struct parse_events_error *err, |
| 1012 | config_term_func_t config_term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1013 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1014 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1015 | |
| 1016 | list_for_each_entry(term, head, list) |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1017 | if (config_term(attr, term, err)) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1018 | return -EINVAL; |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1023 | static int get_config_terms(struct list_head *head_config, |
| 1024 | struct list_head *head_terms __maybe_unused) |
| 1025 | { |
| 1026 | #define ADD_CONFIG_TERM(__type, __name, __val) \ |
| 1027 | do { \ |
| 1028 | struct perf_evsel_config_term *__t; \ |
| 1029 | \ |
| 1030 | __t = zalloc(sizeof(*__t)); \ |
| 1031 | if (!__t) \ |
| 1032 | return -ENOMEM; \ |
| 1033 | \ |
| 1034 | INIT_LIST_HEAD(&__t->list); \ |
| 1035 | __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ |
| 1036 | __t->val.__name = __val; \ |
| 1037 | list_add_tail(&__t->list, head_terms); \ |
| 1038 | } while (0) |
| 1039 | |
| 1040 | struct parse_events_term *term; |
| 1041 | |
| 1042 | list_for_each_entry(term, head_config, list) { |
| 1043 | switch (term->type_term) { |
Jiri Olsa | ee4c758 | 2015-07-29 05:42:11 -0400 | [diff] [blame] | 1044 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
| 1045 | ADD_CONFIG_TERM(PERIOD, period, term->val.num); |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1046 | break; |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 1047 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
| 1048 | ADD_CONFIG_TERM(FREQ, freq, term->val.num); |
| 1049 | break; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1050 | case PARSE_EVENTS__TERM_TYPE_TIME: |
| 1051 | ADD_CONFIG_TERM(TIME, time, term->val.num); |
| 1052 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1053 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
| 1054 | ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); |
| 1055 | break; |
| 1056 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
| 1057 | ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); |
| 1058 | break; |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1059 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
| 1060 | ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0); |
| 1061 | break; |
| 1062 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
| 1063 | ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1); |
| 1064 | break; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1065 | default: |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | #undef ADD_EVSEL_CONFIG |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1073 | int parse_events_add_tracepoint(struct list_head *list, int *idx, |
| 1074 | char *sys, char *event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1075 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1076 | struct list_head *head_config) |
| 1077 | { |
| 1078 | if (head_config) { |
| 1079 | struct perf_event_attr attr; |
| 1080 | |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1081 | if (config_attr(&attr, head_config, err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1082 | config_term_tracepoint)) |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
| 1086 | if (strpbrk(sys, "*?")) |
| 1087 | return add_tracepoint_multi_sys(list, idx, sys, event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1088 | err, head_config); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1089 | else |
| 1090 | return add_tracepoint_event(list, idx, sys, event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1091 | err, head_config); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Jiri Olsa | 87d650b | 2015-04-22 21:10:24 +0200 | [diff] [blame] | 1094 | int parse_events_add_numeric(struct parse_events_evlist *data, |
| 1095 | struct list_head *list, |
Robert Richter | b527bab | 2012-08-07 19:43:13 +0200 | [diff] [blame] | 1096 | u32 type, u64 config, |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1097 | struct list_head *head_config) |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1098 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1099 | struct perf_event_attr attr; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1100 | LIST_HEAD(config_terms); |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1101 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1102 | memset(&attr, 0, sizeof(attr)); |
| 1103 | attr.type = type; |
| 1104 | attr.config = config; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1105 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1106 | if (head_config) { |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1107 | if (config_attr(&attr, head_config, data->error, |
| 1108 | config_term_common)) |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1109 | return -EINVAL; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1110 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1111 | if (get_config_terms(head_config, &config_terms)) |
| 1112 | return -ENOMEM; |
| 1113 | } |
| 1114 | |
Wang Nan | 10bf358 | 2016-02-19 11:44:00 +0000 | [diff] [blame] | 1115 | return add_event(list, &data->idx, &attr, |
| 1116 | get_config_name(head_config), &config_terms); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1117 | } |
| 1118 | |
Jiri Olsa | 36adec8 | 2015-04-22 21:10:19 +0200 | [diff] [blame] | 1119 | int parse_events_add_pmu(struct parse_events_evlist *data, |
| 1120 | struct list_head *list, char *name, |
| 1121 | struct list_head *head_config) |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1122 | { |
| 1123 | struct perf_event_attr attr; |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1124 | struct perf_pmu_info info; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1125 | struct perf_pmu *pmu; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1126 | struct perf_evsel *evsel; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1127 | LIST_HEAD(config_terms); |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1128 | |
| 1129 | pmu = perf_pmu__find(name); |
| 1130 | if (!pmu) |
| 1131 | return -EINVAL; |
| 1132 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 1133 | if (pmu->default_config) { |
| 1134 | memcpy(&attr, pmu->default_config, |
| 1135 | sizeof(struct perf_event_attr)); |
| 1136 | } else { |
| 1137 | memset(&attr, 0, sizeof(attr)); |
| 1138 | } |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1139 | |
Adrian Hunter | ad96227 | 2014-08-15 22:08:40 +0300 | [diff] [blame] | 1140 | if (!head_config) { |
| 1141 | attr.type = pmu->type; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1142 | evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); |
Adrian Hunter | ad96227 | 2014-08-15 22:08:40 +0300 | [diff] [blame] | 1143 | return evsel ? 0 : -ENOMEM; |
| 1144 | } |
| 1145 | |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1146 | if (perf_pmu__check_alias(pmu, head_config, &info)) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1147 | return -EINVAL; |
| 1148 | |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1149 | /* |
| 1150 | * Configure hardcoded terms first, no need to check |
| 1151 | * return value when called with fail == 0 ;) |
| 1152 | */ |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1153 | if (config_attr(&attr, head_config, data->error, config_term_pmu)) |
Jiri Olsa | c056ba6a | 2015-04-22 21:10:18 +0200 | [diff] [blame] | 1154 | return -EINVAL; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1155 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1156 | if (get_config_terms(head_config, &config_terms)) |
| 1157 | return -ENOMEM; |
| 1158 | |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1159 | if (perf_pmu__config(pmu, &attr, head_config, data->error)) |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1160 | return -EINVAL; |
| 1161 | |
Jiri Olsa | 36adec8 | 2015-04-22 21:10:19 +0200 | [diff] [blame] | 1162 | evsel = __add_event(list, &data->idx, &attr, |
Wang Nan | e814fdd | 2016-02-19 11:43:59 +0000 | [diff] [blame] | 1163 | get_config_name(head_config), pmu->cpus, |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1164 | &config_terms); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1165 | if (evsel) { |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1166 | evsel->unit = info.unit; |
| 1167 | evsel->scale = info.scale; |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 1168 | evsel->per_pkg = info.per_pkg; |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1169 | evsel->snapshot = info.snapshot; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | return evsel ? 0 : -ENOMEM; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1175 | int parse_events__modifier_group(struct list_head *list, |
| 1176 | char *event_mod) |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1177 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1178 | return parse_events__modifier_event(list, event_mod, true); |
| 1179 | } |
| 1180 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 1181 | void parse_events__set_leader(char *name, struct list_head *list) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1182 | { |
| 1183 | struct perf_evsel *leader; |
| 1184 | |
Wang Nan | 854f736 | 2015-09-06 07:13:17 +0000 | [diff] [blame] | 1185 | if (list_empty(list)) { |
| 1186 | WARN_ONCE(true, "WARNING: failed to set leader: empty list"); |
| 1187 | return; |
| 1188 | } |
| 1189 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 1190 | __perf_evlist__set_leader(list); |
| 1191 | leader = list_entry(list->next, struct perf_evsel, node); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1192 | leader->group_name = name ? strdup(name) : NULL; |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 1195 | /* list_event is assumed to point to malloc'ed memory */ |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1196 | void parse_events_update_lists(struct list_head *list_event, |
| 1197 | struct list_head *list_all) |
| 1198 | { |
| 1199 | /* |
| 1200 | * Called for single event definition. Update the |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1201 | * 'all event' list, and reinit the 'single event' |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1202 | * list, for next event definition. |
| 1203 | */ |
| 1204 | list_splice_tail(list_event, list_all); |
Jiri Olsa | b847cbd | 2012-05-21 09:12:51 +0200 | [diff] [blame] | 1205 | free(list_event); |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1208 | struct event_modifier { |
| 1209 | int eu; |
| 1210 | int ek; |
| 1211 | int eh; |
| 1212 | int eH; |
| 1213 | int eG; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1214 | int eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1215 | int precise; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1216 | int precise_max; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1217 | int exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1218 | int sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1219 | int pinned; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1220 | }; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1221 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1222 | static int get_event_modifier(struct event_modifier *mod, char *str, |
| 1223 | struct perf_evsel *evsel) |
| 1224 | { |
| 1225 | int eu = evsel ? evsel->attr.exclude_user : 0; |
| 1226 | int ek = evsel ? evsel->attr.exclude_kernel : 0; |
| 1227 | int eh = evsel ? evsel->attr.exclude_hv : 0; |
| 1228 | int eH = evsel ? evsel->attr.exclude_host : 0; |
| 1229 | int eG = evsel ? evsel->attr.exclude_guest : 0; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1230 | int eI = evsel ? evsel->attr.exclude_idle : 0; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1231 | int precise = evsel ? evsel->attr.precise_ip : 0; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1232 | int precise_max = 0; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1233 | int sample_read = 0; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1234 | int pinned = evsel ? evsel->attr.pinned : 0; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1235 | |
| 1236 | int exclude = eu | ek | eh; |
| 1237 | int exclude_GH = evsel ? evsel->exclude_GH : 0; |
| 1238 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1239 | memset(mod, 0, sizeof(*mod)); |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1240 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1241 | while (*str) { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1242 | if (*str == 'u') { |
| 1243 | if (!exclude) |
| 1244 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1245 | eu = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1246 | } else if (*str == 'k') { |
| 1247 | if (!exclude) |
| 1248 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1249 | ek = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1250 | } else if (*str == 'h') { |
| 1251 | if (!exclude) |
| 1252 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1253 | eh = 0; |
Joerg Roedel | 99320cc | 2012-01-04 17:54:19 +0100 | [diff] [blame] | 1254 | } else if (*str == 'G') { |
| 1255 | if (!exclude_GH) |
| 1256 | exclude_GH = eG = eH = 1; |
| 1257 | eG = 0; |
| 1258 | } else if (*str == 'H') { |
| 1259 | if (!exclude_GH) |
| 1260 | exclude_GH = eG = eH = 1; |
| 1261 | eH = 0; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1262 | } else if (*str == 'I') { |
| 1263 | eI = 1; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1264 | } else if (*str == 'p') { |
| 1265 | precise++; |
David Ahern | 1342798 | 2012-09-13 14:59:13 -0600 | [diff] [blame] | 1266 | /* use of precise requires exclude_guest */ |
| 1267 | if (!exclude_GH) |
| 1268 | eG = 1; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1269 | } else if (*str == 'P') { |
| 1270 | precise_max = 1; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1271 | } else if (*str == 'S') { |
| 1272 | sample_read = 1; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1273 | } else if (*str == 'D') { |
| 1274 | pinned = 1; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1275 | } else |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1276 | break; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1277 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1278 | ++str; |
| 1279 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1280 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1281 | /* |
| 1282 | * precise ip: |
| 1283 | * |
| 1284 | * 0 - SAMPLE_IP can have arbitrary skid |
| 1285 | * 1 - SAMPLE_IP must have constant skid |
| 1286 | * 2 - SAMPLE_IP requested to have 0 skid |
| 1287 | * 3 - SAMPLE_IP must have 0 skid |
| 1288 | * |
| 1289 | * See also PERF_RECORD_MISC_EXACT_IP |
| 1290 | */ |
| 1291 | if (precise > 3) |
| 1292 | return -EINVAL; |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1293 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1294 | mod->eu = eu; |
| 1295 | mod->ek = ek; |
| 1296 | mod->eh = eh; |
| 1297 | mod->eH = eH; |
| 1298 | mod->eG = eG; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1299 | mod->eI = eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1300 | mod->precise = precise; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1301 | mod->precise_max = precise_max; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1302 | mod->exclude_GH = exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1303 | mod->sample_read = sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1304 | mod->pinned = pinned; |
| 1305 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1306 | return 0; |
| 1307 | } |
| 1308 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 1309 | /* |
| 1310 | * Basic modifier sanity check to validate it contains only one |
| 1311 | * instance of any modifier (apart from 'p') present. |
| 1312 | */ |
| 1313 | static int check_modifier(char *str) |
| 1314 | { |
| 1315 | char *p = str; |
| 1316 | |
| 1317 | /* The sizeof includes 0 byte as well. */ |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1318 | if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1)) |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 1319 | return -1; |
| 1320 | |
| 1321 | while (*p) { |
| 1322 | if (*p != 'p' && strchr(p + 1, *p)) |
| 1323 | return -1; |
| 1324 | p++; |
| 1325 | } |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1330 | int parse_events__modifier_event(struct list_head *list, char *str, bool add) |
| 1331 | { |
| 1332 | struct perf_evsel *evsel; |
| 1333 | struct event_modifier mod; |
| 1334 | |
| 1335 | if (str == NULL) |
| 1336 | return 0; |
| 1337 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 1338 | if (check_modifier(str)) |
| 1339 | return -EINVAL; |
| 1340 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1341 | if (!add && get_event_modifier(&mod, str, NULL)) |
| 1342 | return -EINVAL; |
| 1343 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1344 | __evlist__for_each(list, evsel) { |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1345 | if (add && get_event_modifier(&mod, str, evsel)) |
| 1346 | return -EINVAL; |
| 1347 | |
| 1348 | evsel->attr.exclude_user = mod.eu; |
| 1349 | evsel->attr.exclude_kernel = mod.ek; |
| 1350 | evsel->attr.exclude_hv = mod.eh; |
| 1351 | evsel->attr.precise_ip = mod.precise; |
| 1352 | evsel->attr.exclude_host = mod.eH; |
| 1353 | evsel->attr.exclude_guest = mod.eG; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1354 | evsel->attr.exclude_idle = mod.eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1355 | evsel->exclude_GH = mod.exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1356 | evsel->sample_read = mod.sample_read; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1357 | evsel->precise_max = mod.precise_max; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1358 | |
| 1359 | if (perf_evsel__is_group_leader(evsel)) |
| 1360 | evsel->attr.pinned = mod.pinned; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1361 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1362 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
| 1365 | |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 1366 | int parse_events_name(struct list_head *list, char *name) |
| 1367 | { |
| 1368 | struct perf_evsel *evsel; |
| 1369 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1370 | __evlist__for_each(list, evsel) { |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 1371 | if (!evsel->name) |
| 1372 | evsel->name = strdup(name); |
| 1373 | } |
| 1374 | |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 1378 | static int |
| 1379 | comp_pmu(const void *p1, const void *p2) |
| 1380 | { |
| 1381 | struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; |
| 1382 | struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; |
| 1383 | |
| 1384 | return strcmp(pmu1->symbol, pmu2->symbol); |
| 1385 | } |
| 1386 | |
| 1387 | static void perf_pmu__parse_cleanup(void) |
| 1388 | { |
| 1389 | if (perf_pmu_events_list_num > 0) { |
| 1390 | struct perf_pmu_event_symbol *p; |
| 1391 | int i; |
| 1392 | |
| 1393 | for (i = 0; i < perf_pmu_events_list_num; i++) { |
| 1394 | p = perf_pmu_events_list + i; |
| 1395 | free(p->symbol); |
| 1396 | } |
| 1397 | free(perf_pmu_events_list); |
| 1398 | perf_pmu_events_list = NULL; |
| 1399 | perf_pmu_events_list_num = 0; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | #define SET_SYMBOL(str, stype) \ |
| 1404 | do { \ |
| 1405 | p->symbol = str; \ |
| 1406 | if (!p->symbol) \ |
| 1407 | goto err; \ |
| 1408 | p->type = stype; \ |
| 1409 | } while (0) |
| 1410 | |
| 1411 | /* |
| 1412 | * Read the pmu events list from sysfs |
| 1413 | * Save it into perf_pmu_events_list |
| 1414 | */ |
| 1415 | static void perf_pmu__parse_init(void) |
| 1416 | { |
| 1417 | |
| 1418 | struct perf_pmu *pmu = NULL; |
| 1419 | struct perf_pmu_alias *alias; |
| 1420 | int len = 0; |
| 1421 | |
| 1422 | pmu = perf_pmu__find("cpu"); |
| 1423 | if ((pmu == NULL) || list_empty(&pmu->aliases)) { |
| 1424 | perf_pmu_events_list_num = -1; |
| 1425 | return; |
| 1426 | } |
| 1427 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 1428 | if (strchr(alias->name, '-')) |
| 1429 | len++; |
| 1430 | len++; |
| 1431 | } |
| 1432 | perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); |
| 1433 | if (!perf_pmu_events_list) |
| 1434 | return; |
| 1435 | perf_pmu_events_list_num = len; |
| 1436 | |
| 1437 | len = 0; |
| 1438 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 1439 | struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; |
| 1440 | char *tmp = strchr(alias->name, '-'); |
| 1441 | |
| 1442 | if (tmp != NULL) { |
| 1443 | SET_SYMBOL(strndup(alias->name, tmp - alias->name), |
| 1444 | PMU_EVENT_SYMBOL_PREFIX); |
| 1445 | p++; |
| 1446 | SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); |
| 1447 | len += 2; |
| 1448 | } else { |
| 1449 | SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); |
| 1450 | len++; |
| 1451 | } |
| 1452 | } |
| 1453 | qsort(perf_pmu_events_list, len, |
| 1454 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
| 1455 | |
| 1456 | return; |
| 1457 | err: |
| 1458 | perf_pmu__parse_cleanup(); |
| 1459 | } |
| 1460 | |
| 1461 | enum perf_pmu_event_symbol_type |
| 1462 | perf_pmu__parse_check(const char *name) |
| 1463 | { |
| 1464 | struct perf_pmu_event_symbol p, *r; |
| 1465 | |
| 1466 | /* scan kernel pmu events from sysfs if needed */ |
| 1467 | if (perf_pmu_events_list_num == 0) |
| 1468 | perf_pmu__parse_init(); |
| 1469 | /* |
| 1470 | * name "cpu" could be prefix of cpu-cycles or cpu// events. |
| 1471 | * cpu-cycles has been handled by hardcode. |
| 1472 | * So it must be cpu// events, not kernel pmu event. |
| 1473 | */ |
| 1474 | if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) |
| 1475 | return PMU_EVENT_SYMBOL_ERR; |
| 1476 | |
| 1477 | p.symbol = strdup(name); |
| 1478 | r = bsearch(&p, perf_pmu_events_list, |
| 1479 | (size_t) perf_pmu_events_list_num, |
| 1480 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
| 1481 | free(p.symbol); |
| 1482 | return r ? r->type : PMU_EVENT_SYMBOL_ERR; |
| 1483 | } |
| 1484 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1485 | static int parse_events__scanner(const char *str, void *data, int start_token) |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 1486 | { |
| 1487 | YY_BUFFER_STATE buffer; |
| 1488 | void *scanner; |
| 1489 | int ret; |
| 1490 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1491 | ret = parse_events_lex_init_extra(start_token, &scanner); |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 1492 | if (ret) |
| 1493 | return ret; |
| 1494 | |
| 1495 | buffer = parse_events__scan_string(str, scanner); |
| 1496 | |
| 1497 | #ifdef PARSER_DEBUG |
| 1498 | parse_events_debug = 1; |
| 1499 | #endif |
| 1500 | ret = parse_events_parse(data, scanner); |
| 1501 | |
| 1502 | parse_events__flush_buffer(buffer, scanner); |
| 1503 | parse_events__delete_buffer(buffer, scanner); |
| 1504 | parse_events_lex_destroy(scanner); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1508 | /* |
| 1509 | * parse event config string, return a list of event terms. |
| 1510 | */ |
| 1511 | int parse_events_terms(struct list_head *terms, const char *str) |
| 1512 | { |
Arnaldo Carvalho de Melo | 23b6339 | 2013-01-18 16:56:57 -0300 | [diff] [blame] | 1513 | struct parse_events_terms data = { |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1514 | .terms = NULL, |
| 1515 | }; |
| 1516 | int ret; |
| 1517 | |
| 1518 | ret = parse_events__scanner(str, &data, PE_START_TERMS); |
| 1519 | if (!ret) { |
| 1520 | list_splice(data.terms, terms); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1521 | zfree(&data.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
Arnaldo Carvalho de Melo | 2146afc | 2016-02-12 17:09:17 -0300 | [diff] [blame] | 1525 | parse_events_terms__delete(data.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1526 | return ret; |
| 1527 | } |
| 1528 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1529 | int parse_events(struct perf_evlist *evlist, const char *str, |
| 1530 | struct parse_events_error *err) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1531 | { |
Arnaldo Carvalho de Melo | 23b6339 | 2013-01-18 16:56:57 -0300 | [diff] [blame] | 1532 | struct parse_events_evlist data = { |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1533 | .list = LIST_HEAD_INIT(data.list), |
| 1534 | .idx = evlist->nr_entries, |
| 1535 | .error = err, |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 1536 | }; |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 1537 | int ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1538 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1539 | ret = parse_events__scanner(str, &data, PE_START_EVENTS); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 1540 | perf_pmu__parse_cleanup(); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1541 | if (!ret) { |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 1542 | struct perf_evsel *last; |
| 1543 | |
Wang Nan | 854f736 | 2015-09-06 07:13:17 +0000 | [diff] [blame] | 1544 | if (list_empty(&data.list)) { |
| 1545 | WARN_ONCE(true, "WARNING: event parser found nothing"); |
| 1546 | return -1; |
| 1547 | } |
| 1548 | |
Adrian Hunter | f114d6e | 2015-09-08 10:58:53 +0300 | [diff] [blame] | 1549 | perf_evlist__splice_list_tail(evlist, &data.list); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 1550 | evlist->nr_groups += data.nr_groups; |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 1551 | last = perf_evlist__last(evlist); |
| 1552 | last->cmdline_group_boundary = true; |
| 1553 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 1556 | |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1557 | /* |
| 1558 | * There are 2 users - builtin-record and builtin-test objects. |
| 1559 | * Both call perf_evlist__delete in case of error, so we dont |
| 1560 | * need to bother. |
| 1561 | */ |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 1562 | return ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1565 | #define MAX_WIDTH 1000 |
| 1566 | static int get_term_width(void) |
| 1567 | { |
| 1568 | struct winsize ws; |
| 1569 | |
| 1570 | get_term_dimensions(&ws); |
| 1571 | return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; |
| 1572 | } |
| 1573 | |
| 1574 | static void parse_events_print_error(struct parse_events_error *err, |
| 1575 | const char *event) |
| 1576 | { |
| 1577 | const char *str = "invalid or unsupported event: "; |
| 1578 | char _buf[MAX_WIDTH]; |
| 1579 | char *buf = (char *) event; |
| 1580 | int idx = 0; |
| 1581 | |
| 1582 | if (err->str) { |
| 1583 | /* -2 for extra '' in the final fprintf */ |
| 1584 | int width = get_term_width() - 2; |
| 1585 | int len_event = strlen(event); |
| 1586 | int len_str, max_len, cut = 0; |
| 1587 | |
| 1588 | /* |
| 1589 | * Maximum error index indent, we will cut |
| 1590 | * the event string if it's bigger. |
| 1591 | */ |
Adrian Hunter | 141b2d3 | 2015-07-17 19:33:51 +0300 | [diff] [blame] | 1592 | int max_err_idx = 13; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1593 | |
| 1594 | /* |
| 1595 | * Let's be specific with the message when |
| 1596 | * we have the precise error. |
| 1597 | */ |
| 1598 | str = "event syntax error: "; |
| 1599 | len_str = strlen(str); |
| 1600 | max_len = width - len_str; |
| 1601 | |
| 1602 | buf = _buf; |
| 1603 | |
| 1604 | /* We're cutting from the beggining. */ |
| 1605 | if (err->idx > max_err_idx) |
| 1606 | cut = err->idx - max_err_idx; |
| 1607 | |
| 1608 | strncpy(buf, event + cut, max_len); |
| 1609 | |
| 1610 | /* Mark cut parts with '..' on both sides. */ |
| 1611 | if (cut) |
| 1612 | buf[0] = buf[1] = '.'; |
| 1613 | |
| 1614 | if ((len_event - cut) > max_len) { |
| 1615 | buf[max_len - 1] = buf[max_len - 2] = '.'; |
| 1616 | buf[max_len] = 0; |
| 1617 | } |
| 1618 | |
| 1619 | idx = len_str + err->idx - cut; |
| 1620 | } |
| 1621 | |
| 1622 | fprintf(stderr, "%s'%s'\n", str, buf); |
| 1623 | if (idx) { |
| 1624 | fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); |
| 1625 | if (err->help) |
| 1626 | fprintf(stderr, "\n%s\n", err->help); |
| 1627 | free(err->str); |
| 1628 | free(err->help); |
| 1629 | } |
| 1630 | |
| 1631 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); |
| 1632 | } |
| 1633 | |
| 1634 | #undef MAX_WIDTH |
| 1635 | |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1636 | int parse_events_option(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1637 | int unset __maybe_unused) |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1638 | { |
| 1639 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1640 | struct parse_events_error err = { .idx = 0, }; |
| 1641 | int ret = parse_events(evlist, str, &err); |
Andi Kleen | 9175ce1 | 2012-10-26 13:30:06 -0700 | [diff] [blame] | 1642 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 1643 | if (ret) |
| 1644 | parse_events_print_error(&err, str); |
| 1645 | |
Andi Kleen | 9175ce1 | 2012-10-26 13:30:06 -0700 | [diff] [blame] | 1646 | return ret; |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 1649 | static int |
| 1650 | foreach_evsel_in_last_glob(struct perf_evlist *evlist, |
| 1651 | int (*func)(struct perf_evsel *evsel, |
| 1652 | const void *arg), |
| 1653 | const void *arg) |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1654 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1655 | struct perf_evsel *last = NULL; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 1656 | int err; |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1657 | |
Wang Nan | 854f736 | 2015-09-06 07:13:17 +0000 | [diff] [blame] | 1658 | /* |
| 1659 | * Don't return when list_empty, give func a chance to report |
| 1660 | * error when it found last == NULL. |
| 1661 | * |
| 1662 | * So no need to WARN here, let *func do this. |
| 1663 | */ |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1664 | if (evlist->nr_entries > 0) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1665 | last = perf_evlist__last(evlist); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1666 | |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 1667 | do { |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 1668 | err = (*func)(last, arg); |
| 1669 | if (err) |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 1670 | return -1; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 1671 | if (!last) |
| 1672 | return 0; |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 1673 | |
| 1674 | if (last->node.prev == &evlist->entries) |
| 1675 | return 0; |
| 1676 | last = list_entry(last->node.prev, struct perf_evsel, node); |
| 1677 | } while (!last->cmdline_group_boundary); |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 1682 | static int set_filter(struct perf_evsel *evsel, const void *arg) |
| 1683 | { |
| 1684 | const char *str = arg; |
| 1685 | |
| 1686 | if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { |
| 1687 | fprintf(stderr, |
| 1688 | "--filter option should follow a -e tracepoint option\n"); |
| 1689 | return -1; |
| 1690 | } |
| 1691 | |
| 1692 | if (perf_evsel__append_filter(evsel, "&&", str) < 0) { |
| 1693 | fprintf(stderr, |
| 1694 | "not enough memory to hold filter string\n"); |
| 1695 | return -1; |
| 1696 | } |
| 1697 | |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | int parse_filter(const struct option *opt, const char *str, |
| 1702 | int unset __maybe_unused) |
| 1703 | { |
| 1704 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
| 1705 | |
| 1706 | return foreach_evsel_in_last_glob(evlist, set_filter, |
| 1707 | (const void *)str); |
| 1708 | } |
| 1709 | |
| 1710 | static int add_exclude_perf_filter(struct perf_evsel *evsel, |
| 1711 | const void *arg __maybe_unused) |
| 1712 | { |
| 1713 | char new_filter[64]; |
| 1714 | |
| 1715 | if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { |
| 1716 | fprintf(stderr, |
| 1717 | "--exclude-perf option should follow a -e tracepoint option\n"); |
| 1718 | return -1; |
| 1719 | } |
| 1720 | |
| 1721 | snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); |
| 1722 | |
| 1723 | if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) { |
| 1724 | fprintf(stderr, |
| 1725 | "not enough memory to hold filter string\n"); |
| 1726 | return -1; |
| 1727 | } |
| 1728 | |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | int exclude_perf(const struct option *opt, |
| 1733 | const char *arg __maybe_unused, |
| 1734 | int unset __maybe_unused) |
| 1735 | { |
| 1736 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
| 1737 | |
| 1738 | return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, |
| 1739 | NULL); |
| 1740 | } |
| 1741 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1742 | static const char * const event_type_descriptors[] = { |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1743 | "Hardware event", |
| 1744 | "Software event", |
| 1745 | "Tracepoint event", |
| 1746 | "Hardware cache event", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 1747 | "Raw hardware event descriptor", |
| 1748 | "Hardware breakpoint", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1749 | }; |
| 1750 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1751 | static int cmp_string(const void *a, const void *b) |
| 1752 | { |
| 1753 | const char * const *as = a; |
| 1754 | const char * const *bs = b; |
| 1755 | |
| 1756 | return strcmp(*as, *bs); |
| 1757 | } |
| 1758 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1759 | /* |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1760 | * Print the events from <debugfs_mount_point>/tracing/events |
| 1761 | */ |
| 1762 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1763 | void print_tracepoint_events(const char *subsys_glob, const char *event_glob, |
| 1764 | bool name_only) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1765 | { |
| 1766 | DIR *sys_dir, *evt_dir; |
| 1767 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1768 | char evt_path[MAXPATHLEN]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1769 | char dir_path[MAXPATHLEN]; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1770 | char **evt_list = NULL; |
| 1771 | unsigned int evt_i = 0, evt_num = 0; |
| 1772 | bool evt_num_known = false; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1773 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1774 | restart: |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1775 | sys_dir = opendir(tracing_events_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1776 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1777 | return; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1778 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1779 | if (evt_num_known) { |
| 1780 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 1781 | if (!evt_list) |
| 1782 | goto out_close_sys_dir; |
| 1783 | } |
| 1784 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1785 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 1786 | if (subsys_glob != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1787 | !strglobmatch(sys_dirent.d_name, subsys_glob)) |
| 1788 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1789 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1790 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1791 | sys_dirent.d_name); |
| 1792 | evt_dir = opendir(dir_path); |
| 1793 | if (!evt_dir) |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1794 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1795 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1796 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 1797 | if (event_glob != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1798 | !strglobmatch(evt_dirent.d_name, event_glob)) |
| 1799 | continue; |
| 1800 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1801 | if (!evt_num_known) { |
| 1802 | evt_num++; |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1803 | continue; |
| 1804 | } |
| 1805 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1806 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 1807 | sys_dirent.d_name, evt_dirent.d_name); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1808 | |
| 1809 | evt_list[evt_i] = strdup(evt_path); |
| 1810 | if (evt_list[evt_i] == NULL) |
| 1811 | goto out_close_evt_dir; |
| 1812 | evt_i++; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1813 | } |
| 1814 | closedir(evt_dir); |
| 1815 | } |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1816 | closedir(sys_dir); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1817 | |
| 1818 | if (!evt_num_known) { |
| 1819 | evt_num_known = true; |
| 1820 | goto restart; |
| 1821 | } |
| 1822 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 1823 | evt_i = 0; |
| 1824 | while (evt_i < evt_num) { |
| 1825 | if (name_only) { |
| 1826 | printf("%s ", evt_list[evt_i++]); |
| 1827 | continue; |
| 1828 | } |
| 1829 | printf(" %-50s [%s]\n", evt_list[evt_i++], |
| 1830 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
| 1831 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 1832 | if (evt_num && pager_in_use()) |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1833 | printf("\n"); |
| 1834 | |
| 1835 | out_free: |
| 1836 | evt_num = evt_i; |
| 1837 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 1838 | zfree(&evt_list[evt_i]); |
| 1839 | zfree(&evt_list); |
| 1840 | return; |
| 1841 | |
| 1842 | out_close_evt_dir: |
| 1843 | closedir(evt_dir); |
| 1844 | out_close_sys_dir: |
| 1845 | closedir(sys_dir); |
| 1846 | |
| 1847 | printf("FATAL: not enough memory to print %s\n", |
| 1848 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
| 1849 | if (evt_list) |
| 1850 | goto out_free; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1854 | * Check whether event is in <debugfs_mount_point>/tracing/events |
| 1855 | */ |
| 1856 | |
| 1857 | int is_valid_tracepoint(const char *event_string) |
| 1858 | { |
| 1859 | DIR *sys_dir, *evt_dir; |
| 1860 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 1861 | char evt_path[MAXPATHLEN]; |
| 1862 | char dir_path[MAXPATHLEN]; |
| 1863 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1864 | sys_dir = opendir(tracing_events_path); |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1865 | if (!sys_dir) |
| 1866 | return 0; |
| 1867 | |
| 1868 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
| 1869 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1870 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1871 | sys_dirent.d_name); |
| 1872 | evt_dir = opendir(dir_path); |
| 1873 | if (!evt_dir) |
| 1874 | continue; |
| 1875 | |
| 1876 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
| 1877 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 1878 | sys_dirent.d_name, evt_dirent.d_name); |
| 1879 | if (!strcmp(evt_path, event_string)) { |
| 1880 | closedir(evt_dir); |
| 1881 | closedir(sys_dir); |
| 1882 | return 1; |
| 1883 | } |
| 1884 | } |
| 1885 | closedir(evt_dir); |
| 1886 | } |
| 1887 | closedir(sys_dir); |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1891 | static bool is_event_supported(u8 type, unsigned config) |
| 1892 | { |
| 1893 | bool ret = true; |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 1894 | int open_return; |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1895 | struct perf_evsel *evsel; |
| 1896 | struct perf_event_attr attr = { |
| 1897 | .type = type, |
| 1898 | .config = config, |
| 1899 | .disabled = 1, |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1900 | }; |
| 1901 | struct { |
| 1902 | struct thread_map map; |
| 1903 | int threads[1]; |
| 1904 | } tmap = { |
| 1905 | .map.nr = 1, |
| 1906 | .threads = { 0 }, |
| 1907 | }; |
| 1908 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 1909 | evsel = perf_evsel__new(&attr); |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1910 | if (evsel) { |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 1911 | open_return = perf_evsel__open(evsel, NULL, &tmap.map); |
| 1912 | ret = open_return >= 0; |
| 1913 | |
| 1914 | if (open_return == -EACCES) { |
| 1915 | /* |
| 1916 | * This happens if the paranoid value |
| 1917 | * /proc/sys/kernel/perf_event_paranoid is set to 2 |
| 1918 | * Re-run with exclude_kernel set; we don't do that |
| 1919 | * by default as some ARM machines do not support it. |
| 1920 | * |
| 1921 | */ |
| 1922 | evsel->attr.exclude_kernel = 1; |
| 1923 | ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; |
| 1924 | } |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1925 | perf_evsel__delete(evsel); |
| 1926 | } |
| 1927 | |
| 1928 | return ret; |
| 1929 | } |
| 1930 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1931 | int print_hwcache_events(const char *event_glob, bool name_only) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1932 | { |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1933 | unsigned int type, op, i, evt_i = 0, evt_num = 0; |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1934 | char name[64]; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1935 | char **evt_list = NULL; |
| 1936 | bool evt_num_known = false; |
| 1937 | |
| 1938 | restart: |
| 1939 | if (evt_num_known) { |
| 1940 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 1941 | if (!evt_list) |
| 1942 | goto out_enomem; |
| 1943 | } |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1944 | |
| 1945 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 1946 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 1947 | /* skip invalid cache type */ |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1948 | if (!perf_evsel__is_cache_op_valid(type, op)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1949 | continue; |
| 1950 | |
| 1951 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1952 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
| 1953 | name, sizeof(name)); |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1954 | if (event_glob != NULL && !strglobmatch(name, event_glob)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1955 | continue; |
| 1956 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1957 | if (!is_event_supported(PERF_TYPE_HW_CACHE, |
| 1958 | type | (op << 8) | (i << 16))) |
| 1959 | continue; |
| 1960 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1961 | if (!evt_num_known) { |
| 1962 | evt_num++; |
| 1963 | continue; |
| 1964 | } |
| 1965 | |
| 1966 | evt_list[evt_i] = strdup(name); |
| 1967 | if (evt_list[evt_i] == NULL) |
| 1968 | goto out_enomem; |
| 1969 | evt_i++; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | } |
| 1973 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1974 | if (!evt_num_known) { |
| 1975 | evt_num_known = true; |
| 1976 | goto restart; |
| 1977 | } |
| 1978 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 1979 | evt_i = 0; |
| 1980 | while (evt_i < evt_num) { |
| 1981 | if (name_only) { |
| 1982 | printf("%s ", evt_list[evt_i++]); |
| 1983 | continue; |
| 1984 | } |
| 1985 | printf(" %-50s [%s]\n", evt_list[evt_i++], |
| 1986 | event_type_descriptors[PERF_TYPE_HW_CACHE]); |
| 1987 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 1988 | if (evt_num && pager_in_use()) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1989 | printf("\n"); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 1990 | |
| 1991 | out_free: |
| 1992 | evt_num = evt_i; |
| 1993 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 1994 | zfree(&evt_list[evt_i]); |
| 1995 | zfree(&evt_list); |
| 1996 | return evt_num; |
| 1997 | |
| 1998 | out_enomem: |
| 1999 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); |
| 2000 | if (evt_list) |
| 2001 | goto out_free; |
| 2002 | return evt_num; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 2005 | void print_symbol_events(const char *event_glob, unsigned type, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2006 | struct event_symbol *syms, unsigned max, |
| 2007 | bool name_only) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2008 | { |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2009 | unsigned int i, evt_i = 0, evt_num = 0; |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 2010 | char name[MAX_NAME_LEN]; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2011 | char **evt_list = NULL; |
| 2012 | bool evt_num_known = false; |
| 2013 | |
| 2014 | restart: |
| 2015 | if (evt_num_known) { |
| 2016 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 2017 | if (!evt_list) |
| 2018 | goto out_enomem; |
| 2019 | syms -= max; |
| 2020 | } |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2021 | |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2022 | for (i = 0; i < max; i++, syms++) { |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2023 | |
Arnaldo Carvalho de Melo | e37df6c | 2015-11-27 16:04:58 -0300 | [diff] [blame] | 2024 | if (event_glob != NULL && syms->symbol != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2025 | !(strglobmatch(syms->symbol, event_glob) || |
| 2026 | (syms->alias && strglobmatch(syms->alias, event_glob)))) |
| 2027 | continue; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2028 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2029 | if (!is_event_supported(type, i)) |
| 2030 | continue; |
| 2031 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2032 | if (!evt_num_known) { |
| 2033 | evt_num++; |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2034 | continue; |
| 2035 | } |
| 2036 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2037 | if (!name_only && strlen(syms->alias)) |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 2038 | snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 2039 | else |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 2040 | strncpy(name, syms->symbol, MAX_NAME_LEN); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2041 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2042 | evt_list[evt_i] = strdup(name); |
| 2043 | if (evt_list[evt_i] == NULL) |
| 2044 | goto out_enomem; |
| 2045 | evt_i++; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2048 | if (!evt_num_known) { |
| 2049 | evt_num_known = true; |
| 2050 | goto restart; |
| 2051 | } |
| 2052 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 2053 | evt_i = 0; |
| 2054 | while (evt_i < evt_num) { |
| 2055 | if (name_only) { |
| 2056 | printf("%s ", evt_list[evt_i++]); |
| 2057 | continue; |
| 2058 | } |
| 2059 | printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); |
| 2060 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 2061 | if (evt_num && pager_in_use()) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2062 | printf("\n"); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2063 | |
| 2064 | out_free: |
| 2065 | evt_num = evt_i; |
| 2066 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 2067 | zfree(&evt_list[evt_i]); |
| 2068 | zfree(&evt_list); |
| 2069 | return; |
| 2070 | |
| 2071 | out_enomem: |
| 2072 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); |
| 2073 | if (evt_list) |
| 2074 | goto out_free; |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * Print the help text for the event symbols: |
| 2079 | */ |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2080 | void print_events(const char *event_glob, bool name_only) |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2081 | { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2082 | print_symbol_events(event_glob, PERF_TYPE_HARDWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2083 | event_symbols_hw, PERF_COUNT_HW_MAX, name_only); |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2084 | |
| 2085 | print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2086 | event_symbols_sw, PERF_COUNT_SW_MAX, name_only); |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 2087 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2088 | print_hwcache_events(event_glob, name_only); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2089 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 2090 | print_pmu_events(event_glob, name_only); |
| 2091 | |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2092 | if (event_glob != NULL) |
| 2093 | return; |
Jaswinder Singh Rajput | 73c24cb | 2009-07-01 18:36:18 +0530 | [diff] [blame] | 2094 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2095 | if (!name_only) { |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2096 | printf(" %-50s [%s]\n", |
| 2097 | "rNNN", |
| 2098 | event_type_descriptors[PERF_TYPE_RAW]); |
| 2099 | printf(" %-50s [%s]\n", |
| 2100 | "cpu/t1=v1[,t2=v2,t3 ...]/modifier", |
| 2101 | event_type_descriptors[PERF_TYPE_RAW]); |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 2102 | if (pager_in_use()) |
| 2103 | printf(" (see 'man perf-list' on how to encode it)\n\n"); |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 2104 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2105 | printf(" %-50s [%s]\n", |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 2106 | "mem:<addr>[/len][:access]", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 2107 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 2108 | if (pager_in_use()) |
| 2109 | printf("\n"); |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2110 | } |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 2111 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2112 | print_tracepoint_events(NULL, NULL, name_only); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2113 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2114 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2115 | int parse_events__is_hardcoded_term(struct parse_events_term *term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2116 | { |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2117 | return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2120 | static int new_term(struct parse_events_term **_term, int type_val, |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2121 | int type_term, char *config, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2122 | char *str, u64 num, int err_term, int err_val) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2123 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2124 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2125 | |
| 2126 | term = zalloc(sizeof(*term)); |
| 2127 | if (!term) |
| 2128 | return -ENOMEM; |
| 2129 | |
| 2130 | INIT_LIST_HEAD(&term->list); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2131 | term->type_val = type_val; |
| 2132 | term->type_term = type_term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2133 | term->config = config; |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2134 | term->err_term = err_term; |
| 2135 | term->err_val = err_val; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2136 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2137 | switch (type_val) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2138 | case PARSE_EVENTS__TERM_TYPE_NUM: |
| 2139 | term->val.num = num; |
| 2140 | break; |
| 2141 | case PARSE_EVENTS__TERM_TYPE_STR: |
| 2142 | term->val.str = str; |
| 2143 | break; |
| 2144 | default: |
Adrian Hunter | 4be8be6 | 2013-07-04 16:20:24 +0300 | [diff] [blame] | 2145 | free(term); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2146 | return -EINVAL; |
| 2147 | } |
| 2148 | |
| 2149 | *_term = term; |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2153 | int parse_events_term__num(struct parse_events_term **term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2154 | int type_term, char *config, u64 num, |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 2155 | void *loc_term_, void *loc_val_) |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2156 | { |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 2157 | YYLTYPE *loc_term = loc_term_; |
| 2158 | YYLTYPE *loc_val = loc_val_; |
| 2159 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2160 | return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2161 | config, NULL, num, |
| 2162 | loc_term ? loc_term->first_column : 0, |
| 2163 | loc_val ? loc_val->first_column : 0); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2166 | int parse_events_term__str(struct parse_events_term **term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2167 | int type_term, char *config, char *str, |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 2168 | void *loc_term_, void *loc_val_) |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2169 | { |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 2170 | YYLTYPE *loc_term = loc_term_; |
| 2171 | YYLTYPE *loc_val = loc_val_; |
| 2172 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2173 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2174 | config, str, 0, |
| 2175 | loc_term ? loc_term->first_column : 0, |
| 2176 | loc_val ? loc_val->first_column : 0); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2179 | int parse_events_term__sym_hw(struct parse_events_term **term, |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 2180 | char *config, unsigned idx) |
| 2181 | { |
| 2182 | struct event_symbol *sym; |
| 2183 | |
| 2184 | BUG_ON(idx >= PERF_COUNT_HW_MAX); |
| 2185 | sym = &event_symbols_hw[idx]; |
| 2186 | |
| 2187 | if (config) |
| 2188 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, |
| 2189 | PARSE_EVENTS__TERM_TYPE_USER, config, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2190 | (char *) sym->symbol, 0, 0, 0); |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 2191 | else |
| 2192 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, |
| 2193 | PARSE_EVENTS__TERM_TYPE_USER, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2194 | (char *) "event", (char *) sym->symbol, |
| 2195 | 0, 0, 0); |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 2196 | } |
| 2197 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2198 | int parse_events_term__clone(struct parse_events_term **new, |
| 2199 | struct parse_events_term *term) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 2200 | { |
| 2201 | return new_term(new, term->type_val, term->type_term, term->config, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 2202 | term->val.str, term->val.num, |
| 2203 | term->err_term, term->err_val); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 2204 | } |
| 2205 | |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 2206 | void parse_events_terms__purge(struct list_head *terms) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2207 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 2208 | struct parse_events_term *term, *h; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2209 | |
Wang Nan | a8adfce | 2016-02-12 16:31:23 -0300 | [diff] [blame] | 2210 | list_for_each_entry_safe(term, h, terms, list) { |
| 2211 | list_del_init(&term->list); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2212 | free(term); |
Wang Nan | a8adfce | 2016-02-12 16:31:23 -0300 | [diff] [blame] | 2213 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 2214 | } |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2215 | |
Arnaldo Carvalho de Melo | 2146afc | 2016-02-12 17:09:17 -0300 | [diff] [blame] | 2216 | void parse_events_terms__delete(struct list_head *terms) |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 2217 | { |
Arnaldo Carvalho de Melo | 2146afc | 2016-02-12 17:09:17 -0300 | [diff] [blame] | 2218 | if (!terms) |
| 2219 | return; |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 2220 | parse_events_terms__purge(terms); |
Wang Nan | d20a5f2 | 2016-02-12 17:01:17 -0300 | [diff] [blame] | 2221 | free(terms); |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 2222 | } |
| 2223 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2224 | void parse_events_evlist_error(struct parse_events_evlist *data, |
| 2225 | int idx, const char *str) |
| 2226 | { |
| 2227 | struct parse_events_error *err = data->error; |
| 2228 | |
Adrian Hunter | a6ced2b | 2015-05-19 16:05:44 +0300 | [diff] [blame] | 2229 | if (!err) |
| 2230 | return; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2231 | err->idx = idx; |
| 2232 | err->str = strdup(str); |
| 2233 | WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); |
| 2234 | } |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2235 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 2236 | static void config_terms_list(char *buf, size_t buf_sz) |
| 2237 | { |
| 2238 | int i; |
| 2239 | bool first = true; |
| 2240 | |
| 2241 | buf[0] = '\0'; |
| 2242 | for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { |
| 2243 | const char *name = config_term_names[i]; |
| 2244 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 2245 | if (!config_term_avail(i, NULL)) |
| 2246 | continue; |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 2247 | if (!name) |
| 2248 | continue; |
| 2249 | if (name[0] == '<') |
| 2250 | continue; |
| 2251 | |
| 2252 | if (strlen(buf) + strlen(name) + 2 >= buf_sz) |
| 2253 | return; |
| 2254 | |
| 2255 | if (!first) |
| 2256 | strcat(buf, ","); |
| 2257 | else |
| 2258 | first = false; |
| 2259 | strcat(buf, name); |
| 2260 | } |
| 2261 | } |
| 2262 | |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2263 | /* |
| 2264 | * Return string contains valid config terms of an event. |
| 2265 | * @additional_terms: For terms such as PMU sysfs terms. |
| 2266 | */ |
| 2267 | char *parse_events_formats_error_string(char *additional_terms) |
| 2268 | { |
| 2269 | char *str; |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 2270 | /* "branch_type" is the longest name */ |
| 2271 | char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * |
| 2272 | (sizeof("branch_type") - 1)]; |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2273 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 2274 | config_terms_list(static_terms, sizeof(static_terms)); |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2275 | /* valid terms */ |
| 2276 | if (additional_terms) { |
Wang Nan | 26dee02 | 2016-02-19 11:43:52 +0000 | [diff] [blame] | 2277 | if (asprintf(&str, "valid terms: %s,%s", |
| 2278 | additional_terms, static_terms) < 0) |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2279 | goto fail; |
| 2280 | } else { |
Wang Nan | 26dee02 | 2016-02-19 11:43:52 +0000 | [diff] [blame] | 2281 | if (asprintf(&str, "valid terms: %s", static_terms) < 0) |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 2282 | goto fail; |
| 2283 | } |
| 2284 | return str; |
| 2285 | |
| 2286 | fail: |
| 2287 | return NULL; |
| 2288 | } |