perf: Use die() for error cases in perf-probe
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / tools / perf / builtin-probe.c
CommitLineData
4ea42b18
MH
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"
38#include "util/parse-options.h"
39#include "util/parse-events.h" /* For debugfs_path */
40#include "util/probe-finder.h"
41
42/* Default vmlinux search paths */
43#define NR_SEARCH_PATH 3
44const char *default_search_path[NR_SEARCH_PATH] = {
45"/lib/modules/%s/build/vmlinux", /* Custom build kernel */
46"/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
47"/boot/vmlinux-debug-%s", /* Ubuntu */
48};
49
50#define MAX_PATH_LEN 256
51#define MAX_PROBES 128
074fc0e4 52#define MAX_PROBE_ARGS 128
4ea42b18
MH
53
54/* Session management structure */
55static struct {
56 char *vmlinux;
57 char *release;
23e8ec0d 58 int need_dwarf;
4ea42b18
MH
59 int nr_probe;
60 struct probe_point probes[MAX_PROBES];
61 char *events[MAX_PROBES];
62} session;
63
074fc0e4 64#define semantic_error(msg ...) die("Semantic error :" msg)
4ea42b18
MH
65
66static int parse_probepoint(const struct option *opt __used,
67 const char *str, int unset __used)
68{
69 char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
70 int argc, i;
71 char *arg, *ptr;
72 struct probe_point *pp = &session.probes[session.nr_probe];
73 char **event = &session.events[session.nr_probe];
74 int retp = 0;
75
76 if (!str) /* The end of probe points */
77 return 0;
78
79 debug("Probe-define(%d): %s\n", session.nr_probe, str);
80 if (++session.nr_probe == MAX_PROBES)
81 semantic_error("Too many probes");
82
83 /* Separate arguments, similar to argv_split */
84 argc = 0;
85 do {
86 /* Skip separators */
87 while (isspace(*str))
88 str++;
89
90 /* Add an argument */
91 if (*str != '\0') {
92 const char *s = str;
93
94 /* Skip the argument */
95 while (!isspace(*str) && *str != '\0')
96 str++;
97
98 /* Duplicate the argument */
99 argv[argc] = strndup(s, str - s);
100 if (argv[argc] == NULL)
074fc0e4 101 die("strndup");
4ea42b18
MH
102 if (++argc == MAX_PROBE_ARGS)
103 semantic_error("Too many arguments");
104 debug("argv[%d]=%s\n", argc, argv[argc - 1]);
105 }
106 } while (*str != '\0');
107 if (argc < 2)
108 semantic_error("Need event-name and probe-point at least.");
109
110 /* Parse the event name */
111 if (argv[0][0] == 'r')
112 retp = 1;
113 else if (argv[0][0] != 'p')
114 semantic_error("You must specify 'p'(kprobe) or"
115 " 'r'(kretprobe) first.");
116 /* TODO: check event name */
117 *event = argv[0];
118
119 /* Parse probe point */
120 arg = argv[1];
121 if (arg[0] == '@') {
122 /* Source Line */
123 arg++;
124 ptr = strchr(arg, ':');
125 if (!ptr || !isdigit(ptr[1]))
126 semantic_error("Line number is required.");
127 *ptr++ = '\0';
128 if (strlen(arg) == 0)
129 semantic_error("No file name.");
130 pp->file = strdup(arg);
131 pp->line = atoi(ptr);
132 if (!pp->file || !pp->line)
133 semantic_error("Failed to parse line.");
134 debug("file:%s line:%d\n", pp->file, pp->line);
135 } else {
136 /* Function name */
137 ptr = strchr(arg, '+');
138 if (ptr) {
139 if (!isdigit(ptr[1]))
140 semantic_error("Offset is required.");
141 *ptr++ = '\0';
142 pp->offset = atoi(ptr);
143 } else
144 ptr = arg;
145 ptr = strchr(ptr, '@');
146 if (ptr) {
147 *ptr++ = '\0';
148 pp->file = strdup(ptr);
149 }
150 pp->function = strdup(arg);
151 debug("symbol:%s file:%s offset:%d\n",
152 pp->function, pp->file, pp->offset);
153 }
154 free(argv[1]);
23e8ec0d
MH
155 if (pp->file)
156 session.need_dwarf = 1;
4ea42b18
MH
157
158 /* Copy arguments */
159 pp->nr_args = argc - 2;
160 if (pp->nr_args > 0) {
161 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
162 if (!pp->args)
074fc0e4 163 die("malloc");
4ea42b18
MH
164 memcpy(pp->args, &argv[2], sizeof(char *) * pp->nr_args);
165 }
166
167 /* Ensure return probe has no C argument */
23e8ec0d
MH
168 for (i = 0; i < pp->nr_args; i++)
169 if (is_c_varname(pp->args[i])) {
170 if (retp)
4ea42b18
MH
171 semantic_error("You can't specify local"
172 " variable for kretprobe");
23e8ec0d
MH
173 session.need_dwarf = 1;
174 }
175
4ea42b18
MH
176 debug("%d arguments\n", pp->nr_args);
177 return 0;
178}
179
23e8ec0d 180#ifndef NO_LIBDWARF
4ea42b18
MH
181static int open_default_vmlinux(void)
182{
183 struct utsname uts;
184 char fname[MAX_PATH_LEN];
185 int fd, ret, i;
186
187 ret = uname(&uts);
188 if (ret) {
189 debug("uname() failed.\n");
190 return -errno;
191 }
192 session.release = uts.release;
193 for (i = 0; i < NR_SEARCH_PATH; i++) {
194 ret = snprintf(fname, MAX_PATH_LEN,
195 default_search_path[i], session.release);
196 if (ret >= MAX_PATH_LEN || ret < 0) {
197 debug("Filename(%d,%s) is too long.\n", i, uts.release);
198 errno = E2BIG;
199 return -E2BIG;
200 }
201 debug("try to open %s\n", fname);
202 fd = open(fname, O_RDONLY);
203 if (fd >= 0)
204 break;
205 }
206 return fd;
207}
23e8ec0d 208#endif
4ea42b18
MH
209
210static const char * const probe_usage[] = {
211 "perf probe [<options>] -P 'PROBEDEF' [-P 'PROBEDEF' ...]",
212 NULL
213};
214
215static const struct option options[] = {
23e8ec0d 216#ifndef NO_LIBDWARF
4ea42b18
MH
217 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
218 "vmlinux/module pathname"),
23e8ec0d 219#endif
4ea42b18 220 OPT_CALLBACK('P', "probe", NULL,
23e8ec0d
MH
221#ifdef NO_LIBDWARF
222 "p|r:[GRP/]NAME FUNC[+OFFS] [ARG ...]",
223#else
4ea42b18 224 "p|r:[GRP/]NAME FUNC[+OFFS][@SRC]|@SRC:LINE [ARG ...]",
23e8ec0d 225#endif
4ea42b18
MH
226 "probe point definition, where\n"
227 "\t\tp:\tkprobe probe\n"
228 "\t\tr:\tkretprobe probe\n"
229 "\t\tGRP:\tGroup name (optional)\n"
230 "\t\tNAME:\tEvent name\n"
231 "\t\tFUNC:\tFunction name\n"
232 "\t\tOFFS:\tOffset from function entry (in byte)\n"
23e8ec0d
MH
233#ifdef NO_LIBDWARF
234 "\t\tARG:\tProbe argument (only \n"
235#else
4ea42b18
MH
236 "\t\tSRC:\tSource code path\n"
237 "\t\tLINE:\tLine number\n"
238 "\t\tARG:\tProbe argument (local variable name or\n"
23e8ec0d 239#endif
4ea42b18
MH
240 "\t\t\tkprobe-tracer argument format is supported.)\n",
241 parse_probepoint),
242 OPT_END()
243};
244
245static int write_new_event(int fd, const char *buf)
246{
247 int ret;
248
249 printf("Adding new event: %s\n", buf);
250 ret = write(fd, buf, strlen(buf));
251 if (ret <= 0)
074fc0e4 252 die("failed to create event.");
4ea42b18
MH
253
254 return ret;
255}
256
257#define MAX_CMDLEN 256
258
259static int synthesize_probepoint(struct probe_point *pp)
260{
261 char *buf;
262 int i, len, ret;
263 pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
264 if (!buf)
074fc0e4 265 die("calloc");
4ea42b18
MH
266 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
267 if (ret <= 0 || ret >= MAX_CMDLEN)
268 goto error;
269 len = ret;
270
271 for (i = 0; i < pp->nr_args; i++) {
272 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
273 pp->args[i]);
274 if (ret <= 0 || ret >= MAX_CMDLEN - len)
275 goto error;
276 len += ret;
277 }
278 pp->found = 1;
279 return pp->found;
280error:
281 free(pp->probes[0]);
282 if (ret > 0)
283 ret = -E2BIG;
284 return ret;
285}
286
287int cmd_probe(int argc, const char **argv, const char *prefix __used)
288{
23e8ec0d 289 int i, j, fd, ret;
4ea42b18
MH
290 struct probe_point *pp;
291 char buf[MAX_CMDLEN];
292
293 argc = parse_options(argc, argv, options, probe_usage,
294 PARSE_OPT_STOP_AT_NON_OPTION);
295 if (argc || session.nr_probe == 0)
296 usage_with_options(probe_usage, options);
297
23e8ec0d
MH
298#ifdef NO_LIBDWARF
299 if (session.need_dwarf)
300 semantic_error("Dwarf-analysis is not supported");
301#endif
302
303 /* Synthesize probes without dwarf */
4ea42b18 304 for (j = 0; j < session.nr_probe; j++) {
23e8ec0d 305#ifndef NO_LIBDWARF
4ea42b18 306 if (session.events[j][0] != 'r') {
23e8ec0d 307 session.need_dwarf = 1;
4ea42b18
MH
308 continue;
309 }
23e8ec0d 310#endif
4ea42b18
MH
311 ret = synthesize_probepoint(&session.probes[j]);
312 if (ret == -E2BIG)
313 semantic_error("probe point is too long.");
074fc0e4
MH
314 else if (ret < 0)
315 die("snprintf");
4ea42b18
MH
316 }
317
23e8ec0d
MH
318#ifndef NO_LIBDWARF
319 if (!session.need_dwarf)
4ea42b18
MH
320 goto setup_probes;
321
322 if (session.vmlinux)
323 fd = open(session.vmlinux, O_RDONLY);
324 else
325 fd = open_default_vmlinux();
074fc0e4
MH
326 if (fd < 0)
327 die("vmlinux/module file open");
4ea42b18
MH
328
329 /* Searching probe points */
330 for (j = 0; j < session.nr_probe; j++) {
331 pp = &session.probes[j];
332 if (pp->found)
333 continue;
334
335 lseek(fd, SEEK_SET, 0);
336 ret = find_probepoint(fd, pp);
074fc0e4
MH
337 if (ret <= 0)
338 die("No probe point found.\n");
4ea42b18
MH
339 debug("probe event %s found\n", session.events[j]);
340 }
341 close(fd);
342
343setup_probes:
23e8ec0d
MH
344#endif /* !NO_LIBDWARF */
345
4ea42b18
MH
346 /* Settng up probe points */
347 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
348 fd = open(buf, O_WRONLY, O_APPEND);
074fc0e4
MH
349 if (fd < 0)
350 die("kprobe_events open");
4ea42b18
MH
351 for (j = 0; j < session.nr_probe; j++) {
352 pp = &session.probes[j];
353 if (pp->found == 1) {
354 snprintf(buf, MAX_CMDLEN, "%s %s\n",
355 session.events[j], pp->probes[0]);
356 write_new_event(fd, buf);
357 } else
358 for (i = 0; i < pp->found; i++) {
359 snprintf(buf, MAX_CMDLEN, "%s%d %s\n",
360 session.events[j], i, pp->probes[i]);
361 write_new_event(fd, buf);
362 }
363 }
364 close(fd);
365 return 0;
366}
367