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