Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 2 | /* |
| 3 | * builtin-probe.c |
| 4 | * |
| 5 | * Builtin probe command: Set up probe events by C expression |
| 6 | * |
| 7 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 8 | */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 9 | #include <sys/utsname.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 19 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 40f3b2d | 2019-01-22 11:24:34 -0200 | [diff] [blame] | 20 | #include "namespaces.h" |
Arnaldo Carvalho de Melo | b1d1b09 | 2019-08-30 10:26:37 -0300 | [diff] [blame] | 21 | #include "util/build-id.h" |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 22 | #include "util/strlist.h" |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 23 | #include "util/strfilter.h" |
James Clark | 7cc7255 | 2021-10-18 14:48:42 +0100 | [diff] [blame] | 24 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | afce8c4 | 2019-08-30 11:56:47 -0300 | [diff] [blame] | 25 | #include "util/symbol_conf.h" |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 26 | #include "util/debug.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 27 | #include <subcmd/parse-options.h> |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 28 | #include "util/probe-finder.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 29 | #include "util/probe-event.h" |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 30 | #include "util/probe-file.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 31 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 32 | #include <linux/zalloc.h> |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 33 | |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 34 | #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*" |
Masami Hiramatsu | d5882a9 | 2021-07-04 00:35:34 +0900 | [diff] [blame] | 35 | #define DEFAULT_FUNC_FILTER "!_* & !*@plt" |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 36 | #define DEFAULT_LIST_FILTER "*" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 37 | |
| 38 | /* Session management structure */ |
| 39 | static struct { |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 40 | int command; /* Command short_name */ |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 41 | bool list_events; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 42 | bool uprobes; |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 43 | bool quiet; |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 44 | bool target_used; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 45 | int nevents; |
| 46 | struct perf_probe_event events[MAX_PROBES]; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 47 | struct line_range line_range; |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 48 | char *target; |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 49 | struct strfilter *filter; |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 50 | struct nsinfo *nsi; |
Masami Hiramatsu | 12a1fad | 2010-03-16 18:05:44 -0400 | [diff] [blame] | 51 | } params; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 52 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 53 | /* Parse an event definition. Note that any error must die. */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 54 | static int parse_probe_event(const char *str) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 55 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 56 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 57 | int ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 58 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 59 | pr_debug("probe-definition(%d): %s\n", params.nevents, str); |
Arnaldo Carvalho de Melo | 8a7ddad | 2010-05-18 22:57:27 -0300 | [diff] [blame] | 60 | if (++params.nevents == MAX_PROBES) { |
| 61 | pr_err("Too many probes (> %d) were specified.", MAX_PROBES); |
| 62 | return -1; |
| 63 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 64 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 65 | pev->uprobes = params.uprobes; |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 66 | if (params.target) { |
| 67 | pev->target = strdup(params.target); |
| 68 | if (!pev->target) |
| 69 | return -ENOMEM; |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 70 | params.target_used = true; |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 71 | } |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 72 | |
Arnaldo Carvalho de Melo | 362379a | 2018-05-24 11:20:39 -0300 | [diff] [blame] | 73 | pev->nsi = nsinfo__get(params.nsi); |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 74 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 75 | /* Parse a perf-probe command into event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 76 | ret = parse_perf_probe_command(str, pev); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 77 | pr_debug("%d arguments\n", pev->nargs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 78 | |
| 79 | return ret; |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 82 | static int params_add_filter(const char *str) |
| 83 | { |
| 84 | const char *err = NULL; |
| 85 | int ret = 0; |
| 86 | |
| 87 | pr_debug2("Add filter: %s\n", str); |
| 88 | if (!params.filter) { |
| 89 | params.filter = strfilter__new(str, &err); |
| 90 | if (!params.filter) |
| 91 | ret = err ? -EINVAL : -ENOMEM; |
| 92 | } else |
| 93 | ret = strfilter__or(params.filter, str, &err); |
| 94 | |
| 95 | if (ret == -EINVAL) { |
| 96 | pr_err("Filter parse error at %td.\n", err - str + 1); |
| 97 | pr_err("Source: \"%s\"\n", str); |
| 98 | pr_err(" %*c\n", (int)(err - str + 1), '^'); |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 104 | static int set_target(const char *ptr) |
| 105 | { |
| 106 | int found = 0; |
| 107 | const char *buf; |
| 108 | |
| 109 | /* |
| 110 | * The first argument after options can be an absolute path |
| 111 | * to an executable / library or kernel module. |
| 112 | * |
| 113 | * TODO: Support relative path, and $PATH, $LD_LIBRARY_PATH, |
| 114 | * short module name. |
| 115 | */ |
| 116 | if (!params.target && ptr && *ptr == '/') { |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 117 | params.target = strdup(ptr); |
| 118 | if (!params.target) |
| 119 | return -ENOMEM; |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 120 | params.target_used = false; |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 121 | |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 122 | found = 1; |
| 123 | buf = ptr + (strlen(ptr) - 3); |
| 124 | |
| 125 | if (strcmp(buf, ".ko")) |
| 126 | params.uprobes = true; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | return found; |
| 131 | } |
| 132 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 133 | static int parse_probe_event_argv(int argc, const char **argv) |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 134 | { |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 135 | int i, len, ret, found_target; |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 136 | char *buf; |
| 137 | |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 138 | found_target = set_target(argv[0]); |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 139 | if (found_target < 0) |
| 140 | return found_target; |
| 141 | |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 142 | if (found_target && argc == 1) |
| 143 | return 0; |
| 144 | |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 145 | /* Bind up rest arguments */ |
| 146 | len = 0; |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 147 | for (i = 0; i < argc; i++) { |
| 148 | if (i == 0 && found_target) |
| 149 | continue; |
| 150 | |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 151 | len += strlen(argv[i]) + 1; |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 152 | } |
Arnaldo Carvalho de Melo | 8a7ddad | 2010-05-18 22:57:27 -0300 | [diff] [blame] | 153 | buf = zalloc(len + 1); |
| 154 | if (buf == NULL) |
| 155 | return -ENOMEM; |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 156 | len = 0; |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 157 | for (i = 0; i < argc; i++) { |
| 158 | if (i == 0 && found_target) |
| 159 | continue; |
| 160 | |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 161 | len += sprintf(&buf[len], "%s ", argv[i]); |
Srikar Dronamraju | 73eff9f | 2012-04-16 17:39:25 +0530 | [diff] [blame] | 162 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 163 | ret = parse_probe_event(buf); |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 164 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 165 | return ret; |
Masami Hiramatsu | d1bde3f | 2009-12-08 17:02:54 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 168 | static int opt_set_target(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 169 | int unset __maybe_unused) |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 170 | { |
| 171 | int ret = -ENOENT; |
Masami Hiramatsu | 8a613d4 | 2013-12-26 05:41:50 +0000 | [diff] [blame] | 172 | char *tmp; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 173 | |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 174 | if (str) { |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 175 | if (!strcmp(opt->long_name, "exec")) |
| 176 | params.uprobes = true; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 177 | else if (!strcmp(opt->long_name, "module")) |
| 178 | params.uprobes = false; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 179 | else |
| 180 | return ret; |
| 181 | |
Masami Hiramatsu | 8a613d4 | 2013-12-26 05:41:50 +0000 | [diff] [blame] | 182 | /* Expand given path to absolute path, except for modulename */ |
| 183 | if (params.uprobes || strchr(str, '/')) { |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 184 | tmp = nsinfo__realpath(str, params.nsi); |
Masami Hiramatsu | 8a613d4 | 2013-12-26 05:41:50 +0000 | [diff] [blame] | 185 | if (!tmp) { |
| 186 | pr_warning("Failed to get the absolute path of %s: %m\n", str); |
| 187 | return ret; |
| 188 | } |
| 189 | } else { |
| 190 | tmp = strdup(str); |
| 191 | if (!tmp) |
| 192 | return -ENOMEM; |
| 193 | } |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 194 | free(params.target); |
Masami Hiramatsu | 8a613d4 | 2013-12-26 05:41:50 +0000 | [diff] [blame] | 195 | params.target = tmp; |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 196 | params.target_used = false; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 197 | ret = 0; |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 203 | static int opt_set_target_ns(const struct option *opt __maybe_unused, |
| 204 | const char *str, int unset __maybe_unused) |
| 205 | { |
| 206 | int ret = -ENOENT; |
| 207 | pid_t ns_pid; |
| 208 | struct nsinfo *nsip; |
| 209 | |
| 210 | if (str) { |
| 211 | errno = 0; |
| 212 | ns_pid = (pid_t)strtol(str, NULL, 10); |
| 213 | if (errno != 0) { |
| 214 | ret = -errno; |
| 215 | pr_warning("Failed to parse %s as a pid: %s\n", str, |
| 216 | strerror(errno)); |
| 217 | return ret; |
| 218 | } |
| 219 | nsip = nsinfo__new(ns_pid); |
| 220 | if (nsip && nsip->need_setns) |
| 221 | params.nsi = nsinfo__get(nsip); |
| 222 | nsinfo__put(nsip); |
| 223 | |
| 224 | ret = 0; |
| 225 | } |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 231 | /* Command option callbacks */ |
| 232 | |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 233 | #ifdef HAVE_DWARF_SUPPORT |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 234 | static int opt_show_lines(const struct option *opt, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 235 | const char *str, int unset __maybe_unused) |
Hitoshi Mitake | 0eda738 | 2010-01-16 21:31:16 +0900 | [diff] [blame] | 236 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 237 | int ret = 0; |
| 238 | |
Masami Hiramatsu | 13e27d7 | 2011-08-11 20:02:53 +0900 | [diff] [blame] | 239 | if (!str) |
| 240 | return 0; |
| 241 | |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 242 | if (params.command == 'L') { |
Masami Hiramatsu | 13e27d7 | 2011-08-11 20:02:53 +0900 | [diff] [blame] | 243 | pr_warning("Warning: more than one --line options are" |
| 244 | " detected. Only the first one is valid.\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 248 | params.command = opt->short_name; |
Masami Hiramatsu | 13e27d7 | 2011-08-11 20:02:53 +0900 | [diff] [blame] | 249 | ret = parse_line_range_desc(str, ¶ms.line_range); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 250 | |
| 251 | return ret; |
Hitoshi Mitake | 0eda738 | 2010-01-16 21:31:16 +0900 | [diff] [blame] | 252 | } |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 253 | |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 254 | static int opt_show_vars(const struct option *opt, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 255 | const char *str, int unset __maybe_unused) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 256 | { |
| 257 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; |
| 258 | int ret; |
| 259 | |
| 260 | if (!str) |
| 261 | return 0; |
| 262 | |
| 263 | ret = parse_probe_event(str); |
| 264 | if (!ret && pev->nargs != 0) { |
| 265 | pr_err(" Error: '--vars' doesn't accept arguments.\n"); |
| 266 | return -EINVAL; |
| 267 | } |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 268 | params.command = opt->short_name; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 269 | |
| 270 | return ret; |
| 271 | } |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 272 | #else |
| 273 | # define opt_show_lines NULL |
| 274 | # define opt_show_vars NULL |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 275 | #endif |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 276 | static int opt_add_probe_event(const struct option *opt, |
| 277 | const char *str, int unset __maybe_unused) |
| 278 | { |
| 279 | if (str) { |
| 280 | params.command = opt->short_name; |
| 281 | return parse_probe_event(str); |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int opt_set_filter_with_command(const struct option *opt, |
| 288 | const char *str, int unset) |
Masami Hiramatsu | 9f7811d | 2015-05-05 11:29:50 +0900 | [diff] [blame] | 289 | { |
| 290 | if (!unset) |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 291 | params.command = opt->short_name; |
Masami Hiramatsu | 9f7811d | 2015-05-05 11:29:50 +0900 | [diff] [blame] | 292 | |
| 293 | if (str) |
| 294 | return params_add_filter(str); |
| 295 | |
| 296 | return 0; |
| 297 | } |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 298 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 299 | static int opt_set_filter(const struct option *opt __maybe_unused, |
| 300 | const char *str, int unset __maybe_unused) |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 301 | { |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 302 | if (str) |
| 303 | return params_add_filter(str); |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 304 | |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 305 | return 0; |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 306 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 307 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 308 | static int init_params(void) |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 309 | { |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 310 | return line_range__init(¶ms.line_range); |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void cleanup_params(void) |
| 314 | { |
| 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < params.nevents; i++) |
| 318 | clear_perf_probe_event(params.events + i); |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 319 | line_range__clear(¶ms.line_range); |
| 320 | free(params.target); |
Markus Elfring | 200802a | 2015-07-04 07:44:22 +0200 | [diff] [blame] | 321 | strfilter__delete(params.filter); |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 322 | nsinfo__put(params.nsi); |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 323 | memset(¶ms, 0, sizeof(params)); |
| 324 | } |
| 325 | |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 326 | static void pr_err_with_code(const char *msg, int err) |
| 327 | { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 328 | char sbuf[STRERR_BUFSIZE]; |
| 329 | |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 330 | pr_err("%s", msg); |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 331 | pr_debug(" Reason: %s (Code: %d)", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 332 | str_error_r(-err, sbuf, sizeof(sbuf)), err); |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 333 | pr_err("\n"); |
| 334 | } |
| 335 | |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 336 | static int perf_add_probe_events(struct perf_probe_event *pevs, int npevs) |
| 337 | { |
| 338 | int ret; |
| 339 | int i, k; |
| 340 | const char *event = NULL, *group = NULL; |
| 341 | |
Namhyung Kim | 9bae1e8 | 2015-09-10 11:27:05 +0900 | [diff] [blame] | 342 | ret = init_probe_symbol_maps(pevs->uprobes); |
| 343 | if (ret < 0) |
| 344 | return ret; |
| 345 | |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 346 | ret = convert_perf_probe_events(pevs, npevs); |
| 347 | if (ret < 0) |
| 348 | goto out_cleanup; |
| 349 | |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 350 | if (params.command == 'D') { /* it shows definition */ |
Masami Hiramatsu | 45237f9 | 2021-06-05 01:28:43 +0900 | [diff] [blame] | 351 | if (probe_conf.bootconfig) |
| 352 | ret = show_bootconfig_events(pevs, npevs); |
| 353 | else |
| 354 | ret = show_probe_trace_events(pevs, npevs); |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 355 | goto out_cleanup; |
| 356 | } |
| 357 | |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 358 | ret = apply_perf_probe_events(pevs, npevs); |
| 359 | if (ret < 0) |
| 360 | goto out_cleanup; |
| 361 | |
| 362 | for (i = k = 0; i < npevs; i++) |
| 363 | k += pevs[i].ntevs; |
| 364 | |
| 365 | pr_info("Added new event%s\n", (k > 1) ? "s:" : ":"); |
| 366 | for (i = 0; i < npevs; i++) { |
| 367 | struct perf_probe_event *pev = &pevs[i]; |
| 368 | |
| 369 | for (k = 0; k < pev->ntevs; k++) { |
| 370 | struct probe_trace_event *tev = &pev->tevs[k]; |
Masami Hiramatsu | f41ebe9 | 2020-04-23 20:01:22 +0900 | [diff] [blame] | 371 | /* Skipped events have no event name */ |
| 372 | if (!tev->event) |
| 373 | continue; |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 374 | |
| 375 | /* We use tev's name for showing new events */ |
| 376 | show_perf_probe_event(tev->group, tev->event, pev, |
| 377 | tev->point.module, false); |
| 378 | |
| 379 | /* Save the last valid name */ |
| 380 | event = tev->event; |
| 381 | group = tev->group; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* Note that it is possible to skip all events because of blacklist */ |
| 386 | if (event) { |
| 387 | /* Show how to use the event. */ |
| 388 | pr_info("\nYou can now use it in all perf tools, such as:\n\n"); |
| 389 | pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event); |
| 390 | } |
| 391 | |
| 392 | out_cleanup: |
| 393 | cleanup_perf_probe_events(pevs, npevs); |
Namhyung Kim | 9bae1e8 | 2015-09-10 11:27:05 +0900 | [diff] [blame] | 394 | exit_probe_symbol_maps(); |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 395 | return ret; |
| 396 | } |
| 397 | |
Masami Hiramatsu | 4a0f65c | 2016-07-01 17:03:36 +0900 | [diff] [blame] | 398 | static int del_perf_probe_caches(struct strfilter *filter) |
| 399 | { |
| 400 | struct probe_cache *cache; |
| 401 | struct strlist *bidlist; |
| 402 | struct str_node *nd; |
| 403 | int ret; |
| 404 | |
Masami Hiramatsu | c3492a3 | 2016-07-12 19:04:54 +0900 | [diff] [blame] | 405 | bidlist = build_id_cache__list_all(false); |
Masami Hiramatsu | 4a0f65c | 2016-07-01 17:03:36 +0900 | [diff] [blame] | 406 | if (!bidlist) { |
| 407 | ret = -errno; |
| 408 | pr_debug("Failed to get buildids: %d\n", ret); |
| 409 | return ret ?: -ENOMEM; |
| 410 | } |
| 411 | |
| 412 | strlist__for_each_entry(nd, bidlist) { |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 413 | cache = probe_cache__new(nd->s, NULL); |
Masami Hiramatsu | 4a0f65c | 2016-07-01 17:03:36 +0900 | [diff] [blame] | 414 | if (!cache) |
| 415 | continue; |
| 416 | if (probe_cache__filter_purge(cache, filter) < 0 || |
| 417 | probe_cache__commit(cache) < 0) |
| 418 | pr_warning("Failed to remove entries for %s\n", nd->s); |
| 419 | probe_cache__delete(cache); |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 424 | static int perf_del_probe_events(struct strfilter *filter) |
| 425 | { |
| 426 | int ret, ret2, ufd = -1, kfd = -1; |
| 427 | char *str = strfilter__string(filter); |
| 428 | struct strlist *klist = NULL, *ulist = NULL; |
| 429 | struct str_node *ent; |
| 430 | |
| 431 | if (!str) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | pr_debug("Delete filter: \'%s\'\n", str); |
| 435 | |
Masami Hiramatsu | 4a0f65c | 2016-07-01 17:03:36 +0900 | [diff] [blame] | 436 | if (probe_conf.cache) |
| 437 | return del_perf_probe_caches(filter); |
| 438 | |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 439 | /* Get current event names */ |
| 440 | ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); |
| 441 | if (ret < 0) |
| 442 | goto out; |
| 443 | |
| 444 | klist = strlist__new(NULL, NULL); |
Masami Hiramatsu | 0fb1855 | 2015-09-16 21:52:42 +0900 | [diff] [blame] | 445 | ulist = strlist__new(NULL, NULL); |
| 446 | if (!klist || !ulist) { |
| 447 | ret = -ENOMEM; |
| 448 | goto out; |
| 449 | } |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 450 | |
| 451 | ret = probe_file__get_events(kfd, filter, klist); |
| 452 | if (ret == 0) { |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 453 | strlist__for_each_entry(ent, klist) |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 454 | pr_info("Removed event: %s\n", ent->s); |
| 455 | |
| 456 | ret = probe_file__del_strlist(kfd, klist); |
| 457 | if (ret < 0) |
| 458 | goto error; |
He Zhe | bd862b1 | 2020-02-26 22:30:04 +0800 | [diff] [blame] | 459 | } else if (ret == -ENOMEM) |
| 460 | goto error; |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 461 | |
| 462 | ret2 = probe_file__get_events(ufd, filter, ulist); |
| 463 | if (ret2 == 0) { |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 464 | strlist__for_each_entry(ent, ulist) |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 465 | pr_info("Removed event: %s\n", ent->s); |
| 466 | |
| 467 | ret2 = probe_file__del_strlist(ufd, ulist); |
| 468 | if (ret2 < 0) |
| 469 | goto error; |
He Zhe | bd862b1 | 2020-02-26 22:30:04 +0800 | [diff] [blame] | 470 | } else if (ret2 == -ENOMEM) |
| 471 | goto error; |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 472 | |
| 473 | if (ret == -ENOENT && ret2 == -ENOENT) |
Kefeng Wang | 7094672 | 2017-03-17 16:16:32 +0800 | [diff] [blame] | 474 | pr_warning("\"%s\" does not hit any event.\n", str); |
| 475 | else |
| 476 | ret = 0; |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 477 | |
| 478 | error: |
| 479 | if (kfd >= 0) |
| 480 | close(kfd); |
| 481 | if (ufd >= 0) |
| 482 | close(ufd); |
| 483 | out: |
| 484 | strlist__delete(klist); |
| 485 | strlist__delete(ulist); |
| 486 | free(str); |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 491 | #ifdef HAVE_DWARF_SUPPORT |
| 492 | #define PROBEDEF_STR \ |
| 493 | "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT [[NAME=]ARG ...]" |
| 494 | #else |
| 495 | #define PROBEDEF_STR "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]" |
| 496 | #endif |
| 497 | |
| 498 | |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 499 | static int |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 500 | __cmd_probe(int argc, const char **argv) |
Arnaldo Carvalho de Melo | 11c4e4a | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 501 | { |
| 502 | const char * const probe_usage[] = { |
| 503 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
| 504 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", |
| 505 | "perf probe [<options>] --del '[GROUP:]EVENT' ...", |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 506 | "perf probe --list [GROUP:]EVENT ...", |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 507 | #ifdef HAVE_DWARF_SUPPORT |
Arnaldo Carvalho de Melo | 11c4e4a | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 508 | "perf probe [<options>] --line 'LINEDESC'", |
| 509 | "perf probe [<options>] --vars 'PROBEPOINT'", |
Masami Hiramatsu | f3ab481 | 2010-02-25 08:35:12 -0500 | [diff] [blame] | 510 | #endif |
Masami Hiramatsu | b3ac032 | 2015-04-23 22:46:12 +0900 | [diff] [blame] | 511 | "perf probe [<options>] --funcs", |
Arnaldo Carvalho de Melo | 11c4e4a | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 512 | NULL |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 513 | }; |
Namhyung Kim | 13dcbbc | 2014-10-23 00:15:49 +0900 | [diff] [blame] | 514 | struct option options[] = { |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 515 | OPT_INCR('v', "verbose", &verbose, |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 516 | "be more verbose (show parsed arguments, etc)"), |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 517 | OPT_BOOLEAN('q', "quiet", ¶ms.quiet, |
Masanari Iida | 0a95160 | 2016-11-23 22:44:47 +0900 | [diff] [blame] | 518 | "be quiet (do not show any messages)"), |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 519 | OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT", |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 520 | "list up probe events", |
| 521 | opt_set_filter_with_command, DEFAULT_LIST_FILTER), |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 522 | OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.", |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 523 | opt_set_filter_with_command), |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 524 | OPT_CALLBACK('a', "add", NULL, PROBEDEF_STR, |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 525 | "probe point definition, where\n" |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 526 | "\t\tGROUP:\tGroup name (optional)\n" |
| 527 | "\t\tEVENT:\tEvent name\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 528 | "\t\tFUNC:\tFunction name\n" |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 529 | "\t\tOFF:\tOffset from function entry (in byte)\n" |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 530 | "\t\t%return:\tPut the probe at function return\n" |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 531 | #ifdef HAVE_DWARF_SUPPORT |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 532 | "\t\tSRC:\tSource code path\n" |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 533 | "\t\tRL:\tRelative line number from function entry.\n" |
| 534 | "\t\tAL:\tAbsolute line number in file.\n" |
| 535 | "\t\tPT:\tLazy expression of line code.\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 536 | "\t\tARG:\tProbe argument (local variable name or\n" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 537 | "\t\t\tkprobe-tracer argument format.)\n", |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 538 | #else |
| 539 | "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n", |
| 540 | #endif |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 541 | opt_add_probe_event), |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 542 | OPT_CALLBACK('D', "definition", NULL, PROBEDEF_STR, |
| 543 | "Show trace event definition of given traceevent for k/uprobe_events.", |
| 544 | opt_add_probe_event), |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 545 | OPT_BOOLEAN('f', "force", &probe_conf.force_add, "forcibly add events" |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 546 | " with existing name"), |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 547 | OPT_CALLBACK('L', "line", NULL, |
Masami Hiramatsu | 085ea73 | 2010-04-02 12:50:39 -0400 | [diff] [blame] | 548 | "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]", |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 549 | "Show source code lines.", opt_show_lines), |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 550 | OPT_CALLBACK('V', "vars", NULL, |
| 551 | "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT", |
| 552 | "Show accessible variables on PROBEDEF", opt_show_vars), |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 553 | OPT_BOOLEAN('\0', "externs", &probe_conf.show_ext_vars, |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 554 | "Show external variables too (with --vars only)"), |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 555 | OPT_BOOLEAN('\0', "range", &probe_conf.show_location_range, |
| 556 | "Show variables location range in scope (with --vars only)"), |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 557 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 558 | "file", "vmlinux pathname"), |
Chase Douglas | 9ed7e1b | 2010-06-14 15:26:30 -0400 | [diff] [blame] | 559 | OPT_STRING('s', "source", &symbol_conf.source_prefix, |
| 560 | "directory", "path to kernel source"), |
Masami Hiramatsu | 6cfd1f6 | 2015-05-08 10:03:33 +0900 | [diff] [blame] | 561 | OPT_BOOLEAN('\0', "no-inlines", &probe_conf.no_inlines, |
| 562 | "Don't search inlined functions"), |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 563 | OPT__DRY_RUN(&probe_event_dry_run), |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 564 | OPT_INTEGER('\0', "max-probes", &probe_conf.max_probes, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 565 | "Set how many probe points can be found for a probe."), |
Masami Hiramatsu | 9f7811d | 2015-05-05 11:29:50 +0900 | [diff] [blame] | 566 | OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]", |
| 567 | "Show potential probe-able functions.", |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 568 | opt_set_filter_with_command, DEFAULT_FUNC_FILTER), |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 569 | OPT_CALLBACK('\0', "filter", NULL, |
| 570 | "[!]FILTER", "Set a filter (with --vars/funcs only)\n" |
| 571 | "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n" |
| 572 | "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)", |
| 573 | opt_set_filter), |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 574 | OPT_CALLBACK('x', "exec", NULL, "executable|path", |
| 575 | "target executable name or path", opt_set_target), |
Masami Hiramatsu | 1a8ac29 | 2015-10-02 21:58:32 +0900 | [diff] [blame] | 576 | OPT_CALLBACK('m', "module", NULL, "modname|path", |
| 577 | "target module name (for online) or path (for offline)", |
| 578 | opt_set_target), |
Azat Khuzhin | 35e17b2 | 2013-10-28 12:04:24 +0400 | [diff] [blame] | 579 | OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, |
Masami Hiramatsu | 4cdcc33 | 2014-10-27 16:31:24 -0400 | [diff] [blame] | 580 | "Enable symbol demangling"), |
Avi Kivity | 763122a | 2014-09-13 07:15:05 +0300 | [diff] [blame] | 581 | OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, |
| 582 | "Enable kernel symbol demangling"), |
Masami Hiramatsu | 2fd457a | 2016-06-15 12:28:40 +0900 | [diff] [blame] | 583 | OPT_BOOLEAN(0, "cache", &probe_conf.cache, "Manipulate probe cache"), |
Uwe Kleine-König | d5c3a93 | 2016-07-21 11:48:32 +0200 | [diff] [blame] | 584 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
| 585 | "Look for files with symbols relative to this directory"), |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 586 | OPT_CALLBACK(0, "target-ns", NULL, "pid", |
| 587 | "target pid for namespace contexts", opt_set_target_ns), |
Masami Hiramatsu | 45237f9 | 2021-06-05 01:28:43 +0900 | [diff] [blame] | 588 | OPT_BOOLEAN(0, "bootconfig", &probe_conf.bootconfig, |
| 589 | "Output probe definition with bootconfig format"), |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 590 | OPT_END() |
Arnaldo Carvalho de Melo | 11c4e4a | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 591 | }; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 592 | int ret; |
| 593 | |
Namhyung Kim | 13dcbbc | 2014-10-23 00:15:49 +0900 | [diff] [blame] | 594 | set_option_flag(options, 'a', "add", PARSE_OPT_EXCLUSIVE); |
| 595 | set_option_flag(options, 'd', "del", PARSE_OPT_EXCLUSIVE); |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 596 | set_option_flag(options, 'D', "definition", PARSE_OPT_EXCLUSIVE); |
Namhyung Kim | 13dcbbc | 2014-10-23 00:15:49 +0900 | [diff] [blame] | 597 | set_option_flag(options, 'l', "list", PARSE_OPT_EXCLUSIVE); |
| 598 | #ifdef HAVE_DWARF_SUPPORT |
| 599 | set_option_flag(options, 'L', "line", PARSE_OPT_EXCLUSIVE); |
| 600 | set_option_flag(options, 'V', "vars", PARSE_OPT_EXCLUSIVE); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 601 | #else |
| 602 | # define set_nobuild(s, l, c) set_option_nobuild(options, s, l, "NO_DWARF=1", c) |
| 603 | set_nobuild('L', "line", false); |
| 604 | set_nobuild('V', "vars", false); |
| 605 | set_nobuild('\0', "externs", false); |
| 606 | set_nobuild('\0', "range", false); |
| 607 | set_nobuild('k', "vmlinux", true); |
| 608 | set_nobuild('s', "source", true); |
| 609 | set_nobuild('\0', "no-inlines", true); |
| 610 | # undef set_nobuild |
Namhyung Kim | 13dcbbc | 2014-10-23 00:15:49 +0900 | [diff] [blame] | 611 | #endif |
Masami Hiramatsu | b3ac032 | 2015-04-23 22:46:12 +0900 | [diff] [blame] | 612 | set_option_flag(options, 'F', "funcs", PARSE_OPT_EXCLUSIVE); |
Namhyung Kim | 13dcbbc | 2014-10-23 00:15:49 +0900 | [diff] [blame] | 613 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 614 | argc = parse_options(argc, argv, options, probe_usage, |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 615 | PARSE_OPT_STOP_AT_NON_OPTION); |
Masami Hiramatsu | ce11a60 | 2009-12-15 10:31:28 -0500 | [diff] [blame] | 616 | if (argc > 0) { |
| 617 | if (strcmp(argv[0], "-") == 0) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 618 | usage_with_options_msg(probe_usage, options, |
| 619 | "'-' is not supported.\n"); |
Masami Hiramatsu | ce11a60 | 2009-12-15 10:31:28 -0500 | [diff] [blame] | 620 | } |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 621 | if (params.command && params.command != 'a') { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 622 | usage_with_options_msg(probe_usage, options, |
| 623 | "another command except --add is set.\n"); |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 624 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 625 | ret = parse_probe_event_argv(argc, argv); |
| 626 | if (ret < 0) { |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 627 | pr_err_with_code(" Error: Command Parse Error.", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 628 | return ret; |
| 629 | } |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 630 | params.command = 'a'; |
Masami Hiramatsu | ce11a60 | 2009-12-15 10:31:28 -0500 | [diff] [blame] | 631 | } |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 632 | |
James Clark | 7cc7255 | 2021-10-18 14:48:42 +0100 | [diff] [blame] | 633 | ret = symbol__validate_sym_arguments(); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 637 | if (params.quiet) { |
| 638 | if (verbose != 0) { |
| 639 | pr_err(" Error: -v and -q are exclusive.\n"); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | verbose = -1; |
| 643 | } |
| 644 | |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 645 | if (probe_conf.max_probes == 0) |
| 646 | probe_conf.max_probes = MAX_PROBES; |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 647 | |
Franck Bui-Huu | fd930ff | 2010-12-10 14:06:03 +0100 | [diff] [blame] | 648 | /* |
| 649 | * Only consider the user's kernel image path if given. |
| 650 | */ |
| 651 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); |
| 652 | |
Masami Hiramatsu | e50243b | 2016-08-26 23:57:58 +0900 | [diff] [blame] | 653 | /* |
| 654 | * Except for --list, --del and --add, other command doesn't depend |
| 655 | * nor change running kernel. So if user gives offline vmlinux, |
| 656 | * ignore its buildid. |
| 657 | */ |
| 658 | if (!strchr("lda", params.command) && symbol_conf.vmlinux_name) |
| 659 | symbol_conf.ignore_vmlinux_buildid = true; |
| 660 | |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 661 | switch (params.command) { |
| 662 | case 'l': |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 663 | if (params.uprobes) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 664 | pr_err(" Error: Don't use --list with --exec.\n"); |
| 665 | parse_options_usage(probe_usage, options, "l", true); |
| 666 | parse_options_usage(NULL, options, "x", true); |
| 667 | return -EINVAL; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 668 | } |
Masami Hiramatsu | b6a8964 | 2015-04-24 18:47:50 +0900 | [diff] [blame] | 669 | ret = show_perf_probe_events(params.filter); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 670 | if (ret < 0) |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 671 | pr_err_with_code(" Error: Failed to show event list.", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 672 | return ret; |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 673 | case 'F': |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 674 | ret = show_available_funcs(params.target, params.nsi, |
| 675 | params.filter, params.uprobes); |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 676 | if (ret < 0) |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 677 | pr_err_with_code(" Error: Failed to show functions.", ret); |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 678 | return ret; |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 679 | #ifdef HAVE_DWARF_SUPPORT |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 680 | case 'L': |
Masami Hiramatsu | 2b394bc | 2014-09-17 08:40:54 +0000 | [diff] [blame] | 681 | ret = show_line_range(¶ms.line_range, params.target, |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 682 | params.nsi, params.uprobes); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 683 | if (ret < 0) |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 684 | pr_err_with_code(" Error: Failed to show lines.", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 685 | return ret; |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 686 | case 'V': |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 687 | if (!params.filter) |
| 688 | params.filter = strfilter__new(DEFAULT_VAR_FILTER, |
| 689 | NULL); |
| 690 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 691 | ret = show_available_vars(params.events, params.nevents, |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 692 | params.filter); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 693 | if (ret < 0) |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 694 | pr_err_with_code(" Error: Failed to show vars.", ret); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 695 | return ret; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 696 | #endif |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 697 | case 'd': |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 698 | ret = perf_del_probe_events(params.filter); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 699 | if (ret < 0) { |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 700 | pr_err_with_code(" Error: Failed to delete events.", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 701 | return ret; |
| 702 | } |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 703 | break; |
Masami Hiramatsu | 1c20b1d | 2016-08-26 01:24:27 +0900 | [diff] [blame] | 704 | case 'D': |
Masami Hiramatsu | 45237f9 | 2021-06-05 01:28:43 +0900 | [diff] [blame] | 705 | if (probe_conf.bootconfig && params.uprobes) { |
| 706 | pr_err(" Error: --bootconfig doesn't support uprobes.\n"); |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | __fallthrough; |
Masami Hiramatsu | 428aff8 | 2016-08-26 01:24:42 +0900 | [diff] [blame] | 710 | case 'a': |
| 711 | |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 712 | /* Ensure the last given target is used */ |
| 713 | if (params.target && !params.target_used) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 714 | pr_err(" Error: -x/-m must follow the probe definitions.\n"); |
| 715 | parse_options_usage(probe_usage, options, "m", true); |
| 716 | parse_options_usage(NULL, options, "x", true); |
| 717 | return -EINVAL; |
Masami Hiramatsu | 8cb0aa4 | 2015-04-01 19:25:42 +0900 | [diff] [blame] | 718 | } |
| 719 | |
Namhyung Kim | b02137c | 2015-09-04 21:16:01 +0900 | [diff] [blame] | 720 | ret = perf_add_probe_events(params.events, params.nevents); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 721 | if (ret < 0) { |
Arnaldo Carvalho de Melo | d95daf5 | 2019-07-18 11:28:37 -0300 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * When perf_add_probe_events() fails it calls |
| 725 | * cleanup_perf_probe_events(pevs, npevs), i.e. |
| 726 | * cleanup_perf_probe_events(params.events, params.nevents), which |
| 727 | * will call clear_perf_probe_event(), so set nevents to zero |
| 728 | * to avoid cleanup_params() to call clear_perf_probe_event() again |
| 729 | * on the same pevs. |
| 730 | */ |
| 731 | params.nevents = 0; |
Masami Hiramatsu | b4bf113 | 2014-06-06 07:13:52 +0000 | [diff] [blame] | 732 | pr_err_with_code(" Error: Failed to add events.", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 733 | return ret; |
| 734 | } |
Masami Hiramatsu | b1019d5 | 2015-05-06 00:22:57 +0900 | [diff] [blame] | 735 | break; |
| 736 | default: |
| 737 | usage_with_options(probe_usage, options); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 738 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 739 | return 0; |
| 740 | } |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 741 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 742 | int cmd_probe(int argc, const char **argv) |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 743 | { |
| 744 | int ret; |
| 745 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 746 | ret = init_params(); |
| 747 | if (!ret) { |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 748 | ret = __cmd_probe(argc, argv); |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 749 | cleanup_params(); |
| 750 | } |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 751 | |
Masami Hiramatsu | 9bc9f3b | 2015-05-06 21:46:45 +0900 | [diff] [blame] | 752 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 753 | } |