Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * builtin-probe.c |
| 3 | * |
| 4 | * Builtin probe command: Set up probe events by C expression |
| 5 | * |
| 6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | * |
| 22 | */ |
| 23 | #define _GNU_SOURCE |
| 24 | #include <sys/utsname.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <errno.h> |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #undef _GNU_SOURCE |
| 35 | #include "perf.h" |
| 36 | #include "builtin.h" |
| 37 | #include "util/util.h" |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 38 | #include "util/event.h" |
| 39 | #include "util/debug.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 40 | #include "util/parse-options.h" |
| 41 | #include "util/parse-events.h" /* For debugfs_path */ |
| 42 | #include "util/probe-finder.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 43 | #include "util/probe-event.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 44 | |
| 45 | /* Default vmlinux search paths */ |
| 46 | #define NR_SEARCH_PATH 3 |
| 47 | const char *default_search_path[NR_SEARCH_PATH] = { |
| 48 | "/lib/modules/%s/build/vmlinux", /* Custom build kernel */ |
| 49 | "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */ |
| 50 | "/boot/vmlinux-debug-%s", /* Ubuntu */ |
| 51 | }; |
| 52 | |
| 53 | #define MAX_PATH_LEN 256 |
| 54 | #define MAX_PROBES 128 |
| 55 | |
| 56 | /* Session management structure */ |
| 57 | static struct { |
| 58 | char *vmlinux; |
| 59 | char *release; |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 60 | int need_dwarf; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 61 | int nr_probe; |
| 62 | struct probe_point probes[MAX_PROBES]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 63 | } session; |
| 64 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 65 | /* Parse an event definition. Note that any error must die. */ |
| 66 | static void parse_probe_event(const char *str) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 67 | { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 68 | struct probe_point *pp = &session.probes[session.nr_probe]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 69 | |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 70 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 71 | if (++session.nr_probe == MAX_PROBES) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 72 | die("Too many probes (> %d) are specified.", MAX_PROBES); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 73 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 74 | /* Parse perf-probe event into probe_point */ |
| 75 | session.need_dwarf = parse_perf_probe_event(str, pp); |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 76 | |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 77 | pr_debug("%d arguments\n", pp->nr_args); |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 80 | static int opt_add_probe_event(const struct option *opt __used, |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 81 | const char *str, int unset __used) |
| 82 | { |
| 83 | if (str) |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 84 | parse_probe_event(str); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 88 | #ifndef NO_LIBDWARF |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 89 | static int open_default_vmlinux(void) |
| 90 | { |
| 91 | struct utsname uts; |
| 92 | char fname[MAX_PATH_LEN]; |
| 93 | int fd, ret, i; |
| 94 | |
| 95 | ret = uname(&uts); |
| 96 | if (ret) { |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 97 | pr_debug("uname() failed.\n"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 98 | return -errno; |
| 99 | } |
| 100 | session.release = uts.release; |
| 101 | for (i = 0; i < NR_SEARCH_PATH; i++) { |
| 102 | ret = snprintf(fname, MAX_PATH_LEN, |
| 103 | default_search_path[i], session.release); |
| 104 | if (ret >= MAX_PATH_LEN || ret < 0) { |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 105 | pr_debug("Filename(%d,%s) is too long.\n", i, |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 106 | uts.release); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 107 | errno = E2BIG; |
| 108 | return -E2BIG; |
| 109 | } |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 110 | pr_debug("try to open %s\n", fname); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 111 | fd = open(fname, O_RDONLY); |
| 112 | if (fd >= 0) |
| 113 | break; |
| 114 | } |
| 115 | return fd; |
| 116 | } |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 117 | #endif |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 118 | |
| 119 | static const char * const probe_usage[] = { |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 120 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
| 121 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 122 | NULL |
| 123 | }; |
| 124 | |
| 125 | static const struct option options[] = { |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 126 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 127 | "be more verbose (show parsed arguments, etc)"), |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 128 | #ifndef NO_LIBDWARF |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 129 | OPT_STRING('k', "vmlinux", &session.vmlinux, "file", |
| 130 | "vmlinux/module pathname"), |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 131 | #endif |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 132 | OPT_CALLBACK('a', "add", NULL, |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 133 | #ifdef NO_LIBDWARF |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 134 | "FUNC[+OFFS|%return] [ARG ...]", |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 135 | #else |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 136 | "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 137 | #endif |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 138 | "probe point definition, where\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 139 | "\t\tGRP:\tGroup name (optional)\n" |
| 140 | "\t\tNAME:\tEvent name\n" |
| 141 | "\t\tFUNC:\tFunction name\n" |
| 142 | "\t\tOFFS:\tOffset from function entry (in byte)\n" |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 143 | "\t\t%return:\tPut the probe at function return\n" |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 144 | #ifdef NO_LIBDWARF |
| 145 | "\t\tARG:\tProbe argument (only \n" |
| 146 | #else |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 147 | "\t\tSRC:\tSource code path\n" |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 148 | "\t\tRLN:\tRelative line number from function entry.\n" |
| 149 | "\t\tALN:\tAbsolute line number in file.\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 150 | "\t\tARG:\tProbe argument (local variable name or\n" |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 151 | #endif |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 152 | "\t\t\tkprobe-tracer argument format.)\n", |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 153 | opt_add_probe_event), |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 154 | OPT_END() |
| 155 | }; |
| 156 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 157 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
| 158 | { |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 159 | int i, j, fd, ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 160 | struct probe_point *pp; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 161 | |
| 162 | argc = parse_options(argc, argv, options, probe_usage, |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 163 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 164 | for (i = 0; i < argc; i++) |
| 165 | parse_probe_event(argv[i]); |
| 166 | |
| 167 | if (session.nr_probe == 0) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 168 | usage_with_options(probe_usage, options); |
| 169 | |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 170 | if (session.need_dwarf) |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 171 | #ifdef NO_LIBDWARF |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 172 | die("Debuginfo-analysis is not supported"); |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 173 | #else /* !NO_LIBDWARF */ |
Masami Hiramatsu | f41b1e4 | 2009-11-30 19:19:27 -0500 | [diff] [blame] | 174 | pr_debug("Some probes require debuginfo.\n"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 175 | |
| 176 | if (session.vmlinux) |
| 177 | fd = open(session.vmlinux, O_RDONLY); |
| 178 | else |
| 179 | fd = open_default_vmlinux(); |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 180 | if (fd < 0) { |
| 181 | if (session.need_dwarf) |
| 182 | die("Could not open vmlinux/module file."); |
| 183 | |
| 184 | pr_warning("Could not open vmlinux/module file." |
| 185 | " Try to use symbols.\n"); |
| 186 | goto end_dwarf; |
| 187 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 188 | |
| 189 | /* Searching probe points */ |
| 190 | for (j = 0; j < session.nr_probe; j++) { |
| 191 | pp = &session.probes[j]; |
| 192 | if (pp->found) |
| 193 | continue; |
| 194 | |
| 195 | lseek(fd, SEEK_SET, 0); |
| 196 | ret = find_probepoint(fd, pp); |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 197 | if (ret < 0) { |
| 198 | if (session.need_dwarf) |
| 199 | die("Could not analyze debuginfo."); |
| 200 | |
| 201 | pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n"); |
| 202 | break; |
| 203 | } |
| 204 | if (ret == 0) /* No error but failed to find probe point. */ |
| 205 | die("No probe point found."); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 206 | } |
| 207 | close(fd); |
| 208 | |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 209 | end_dwarf: |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 210 | #endif /* !NO_LIBDWARF */ |
| 211 | |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 212 | /* Synthesize probes without dwarf */ |
| 213 | for (j = 0; j < session.nr_probe; j++) { |
| 214 | pp = &session.probes[j]; |
| 215 | if (pp->found) /* This probe is already found. */ |
| 216 | continue; |
| 217 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 218 | ret = synthesize_trace_kprobe_event(pp); |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 219 | if (ret == -E2BIG) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 220 | die("probe point definition becomes too long."); |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 221 | else if (ret < 0) |
| 222 | die("Failed to synthesize a probe point."); |
| 223 | } |
| 224 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 225 | /* Settng up probe points */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame^] | 226 | add_trace_kprobe_events(session.probes, session.nr_probe); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |