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