blob: 2c4d3017441b06dd4ed6f19b5724d5b4615dca7f [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
2 * probe-event.c : perf-probe definition to kprobe_events format converter
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#define _GNU_SOURCE
23#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050032#include <stdarg.h>
33#include <limits.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
35#undef _GNU_SOURCE
36#include "event.h"
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050037#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050038#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050039#include "debug.h"
40#include "parse-events.h" /* For debugfs_path */
41#include "probe-event.h"
42
43#define MAX_CMDLEN 256
44#define MAX_PROBE_ARGS 128
45#define PERFPROBE_GROUP "probe"
46
47#define semantic_error(msg ...) die("Semantic error :" msg)
48
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050049/* If there is no space to write, returns -E2BIG. */
50static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050051 __attribute__((format(printf, 3, 4)));
52
53static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050054{
55 int ret;
56 va_list ap;
57 va_start(ap, format);
58 ret = vsnprintf(str, size, format, ap);
59 va_end(ap);
60 if (ret >= (int)size)
61 ret = -E2BIG;
62 return ret;
63}
64
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050065/* Parse probepoint definition. */
66static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
67{
68 char *ptr, *tmp;
69 char c, nc = 0;
70 /*
71 * <Syntax>
72 * perf probe SRC:LN
73 * perf probe FUNC[+OFFS|%return][@SRC]
74 */
75
76 ptr = strpbrk(arg, ":+@%");
77 if (ptr) {
78 nc = *ptr;
79 *ptr++ = '\0';
80 }
81
82 /* Check arg is function or file and copy it */
83 if (strchr(arg, '.')) /* File */
84 pp->file = strdup(arg);
85 else /* Function */
86 pp->function = strdup(arg);
87 DIE_IF(pp->file == NULL && pp->function == NULL);
88
89 /* Parse other options */
90 while (ptr) {
91 arg = ptr;
92 c = nc;
93 ptr = strpbrk(arg, ":+@%");
94 if (ptr) {
95 nc = *ptr;
96 *ptr++ = '\0';
97 }
98 switch (c) {
99 case ':': /* Line number */
100 pp->line = strtoul(arg, &tmp, 0);
101 if (*tmp != '\0')
102 semantic_error("There is non-digit charactor"
103 " in line number.");
104 break;
105 case '+': /* Byte offset from a symbol */
106 pp->offset = strtoul(arg, &tmp, 0);
107 if (*tmp != '\0')
108 semantic_error("There is non-digit charactor"
109 " in offset.");
110 break;
111 case '@': /* File name */
112 if (pp->file)
113 semantic_error("SRC@SRC is not allowed.");
114 pp->file = strdup(arg);
115 DIE_IF(pp->file == NULL);
116 if (ptr)
117 semantic_error("@SRC must be the last "
118 "option.");
119 break;
120 case '%': /* Probe places */
121 if (strcmp(arg, "return") == 0) {
122 pp->retprobe = 1;
123 } else /* Others not supported yet */
124 semantic_error("%%%s is not supported.", arg);
125 break;
126 default:
127 DIE_IF("Program has a bug.");
128 break;
129 }
130 }
131
132 /* Exclusion check */
133 if (pp->line && pp->offset)
134 semantic_error("Offset can't be used with line number.");
135
136 if (!pp->line && pp->file && !pp->function)
137 semantic_error("File always requires line number.");
138
139 if (pp->offset && !pp->function)
140 semantic_error("Offset requires an entry function.");
141
142 if (pp->retprobe && !pp->function)
143 semantic_error("Return probe requires an entry function.");
144
145 if ((pp->offset || pp->line) && pp->retprobe)
146 semantic_error("Offset/Line can't be used with return probe.");
147
148 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
149 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
150}
151
152/* Parse perf-probe event definition */
153int parse_perf_probe_event(const char *str, struct probe_point *pp)
154{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500155 char **argv;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500156 int argc, i, need_dwarf = 0;
157
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500158 argv = argv_split(str, &argc);
159 if (!argv)
160 die("argv_split failed.");
161 if (argc > MAX_PROBE_ARGS + 1)
162 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500163
164 /* Parse probe point */
165 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500166 if (pp->file || pp->line)
167 need_dwarf = 1;
168
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500169 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500170 pp->nr_args = argc - 1;
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500171 pp->args = zalloc(sizeof(char *) * pp->nr_args);
172 for (i = 0; i < pp->nr_args; i++) {
173 pp->args[i] = strdup(argv[i + 1]);
174 if (!pp->args[i])
175 die("Failed to copy argument.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500176 if (is_c_varname(pp->args[i])) {
177 if (pp->retprobe)
178 semantic_error("You can't specify local"
179 " variable for kretprobe");
180 need_dwarf = 1;
181 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500182 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500183
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500184 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500185 return need_dwarf;
186}
187
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500188/* Parse kprobe_events event into struct probe_point */
189void parse_trace_kprobe_event(const char *str, char **group, char **event,
190 struct probe_point *pp)
191{
192 char pr;
193 char *p;
194 int ret, i, argc;
195 char **argv;
196
197 pr_debug("Parsing kprobe_events: %s\n", str);
198 argv = argv_split(str, &argc);
199 if (!argv)
200 die("argv_split failed.");
201 if (argc < 2)
202 semantic_error("Too less arguments.");
203
204 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800205 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
206 &pr, (float *)(void *)group, (float *)(void *)event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500207 if (ret != 3)
208 semantic_error("Failed to parse event name: %s", argv[0]);
209 pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr);
210
211 if (!pp)
212 goto end;
213
214 pp->retprobe = (pr == 'r');
215
216 /* Scan function name and offset */
Liming Wang93aaa452009-12-02 16:42:54 +0800217 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500218 if (ret == 1)
219 pp->offset = 0;
220
221 /* kprobe_events doesn't have this information */
222 pp->line = 0;
223 pp->file = NULL;
224
225 pp->nr_args = argc - 2;
226 pp->args = zalloc(sizeof(char *) * pp->nr_args);
227 for (i = 0; i < pp->nr_args; i++) {
228 p = strchr(argv[i + 2], '=');
229 if (p) /* We don't need which register is assigned. */
230 *p = '\0';
231 pp->args[i] = strdup(argv[i + 2]);
232 if (!pp->args[i])
233 die("Failed to copy argument.");
234 }
235
236end:
237 argv_free(argv);
238}
239
240int synthesize_perf_probe_event(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500241{
242 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500243 char offs[64] = "", line[64] = "";
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500244 int i, len, ret;
245
246 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
247 if (!buf)
248 die("Failed to allocate memory by zalloc.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500249 if (pp->offset) {
250 ret = e_snprintf(offs, 64, "+%d", pp->offset);
251 if (ret <= 0)
252 goto error;
253 }
254 if (pp->line) {
255 ret = e_snprintf(line, 64, ":%d", pp->line);
256 if (ret <= 0)
257 goto error;
258 }
259
260 if (pp->function)
261 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
262 offs, pp->retprobe ? "%return" : "", line);
263 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500264 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500265 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500266 goto error;
267 len = ret;
268
269 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500270 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
271 pp->args[i]);
272 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500273 goto error;
274 len += ret;
275 }
276 pp->found = 1;
277
278 return pp->found;
279error:
280 free(pp->probes[0]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500281
282 return ret;
283}
284
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500285int synthesize_trace_kprobe_event(struct probe_point *pp)
286{
287 char *buf;
288 int i, len, ret;
289
290 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
291 if (!buf)
292 die("Failed to allocate memory by zalloc.");
293 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
294 if (ret <= 0)
295 goto error;
296 len = ret;
297
298 for (i = 0; i < pp->nr_args; i++) {
299 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
300 pp->args[i]);
301 if (ret <= 0)
302 goto error;
303 len += ret;
304 }
305 pp->found = 1;
306
307 return pp->found;
308error:
309 free(pp->probes[0]);
310
311 return ret;
312}
313
314static int open_kprobe_events(int flags, int mode)
315{
316 char buf[PATH_MAX];
317 int ret;
318
319 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
320 if (ret < 0)
321 die("Failed to make kprobe_events path.");
322
323 ret = open(buf, flags, mode);
324 if (ret < 0) {
325 if (errno == ENOENT)
326 die("kprobe_events file does not exist -"
327 " please rebuild with CONFIG_KPROBE_TRACER.");
328 else
329 die("Could not open kprobe_events file: %s",
330 strerror(errno));
331 }
332 return ret;
333}
334
335/* Get raw string list of current kprobe_events */
336static struct strlist *get_trace_kprobe_event_rawlist(int fd)
337{
338 int ret, idx;
339 FILE *fp;
340 char buf[MAX_CMDLEN];
341 char *p;
342 struct strlist *sl;
343
344 sl = strlist__new(true, NULL);
345
346 fp = fdopen(dup(fd), "r");
347 while (!feof(fp)) {
348 p = fgets(buf, MAX_CMDLEN, fp);
349 if (!p)
350 break;
351
352 idx = strlen(p) - 1;
353 if (p[idx] == '\n')
354 p[idx] = '\0';
355 ret = strlist__add(sl, buf);
356 if (ret < 0)
357 die("strlist__add failed: %s", strerror(-ret));
358 }
359 fclose(fp);
360
361 return sl;
362}
363
364/* Free and zero clear probe_point */
365static void clear_probe_point(struct probe_point *pp)
366{
367 int i;
368
369 if (pp->function)
370 free(pp->function);
371 if (pp->file)
372 free(pp->file);
373 for (i = 0; i < pp->nr_args; i++)
374 free(pp->args[i]);
375 if (pp->args)
376 free(pp->args);
377 for (i = 0; i < pp->found; i++)
378 free(pp->probes[i]);
379 memset(pp, 0, sizeof(pp));
380}
381
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500382/* Show an event */
383static void show_perf_probe_event(const char *group, const char *event,
384 const char *place, struct probe_point *pp)
385{
386 int i;
387 char buf[128];
388
389 e_snprintf(buf, 128, "%s:%s", group, event);
390 printf(" %-40s (on %s", buf, place);
391
392 if (pp->nr_args > 0) {
393 printf(" with");
394 for (i = 0; i < pp->nr_args; i++)
395 printf(" %s", pp->args[i]);
396 }
397 printf(")\n");
398}
399
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500400/* List up current perf-probe events */
401void show_perf_probe_events(void)
402{
403 unsigned int i;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500404 int fd, nr;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500405 char *group, *event;
406 struct probe_point pp;
407 struct strlist *rawlist;
408 struct str_node *ent;
409
410 fd = open_kprobe_events(O_RDONLY, 0);
411 rawlist = get_trace_kprobe_event_rawlist(fd);
412 close(fd);
413
414 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
415 ent = strlist__entry(rawlist, i);
416 parse_trace_kprobe_event(ent->s, &group, &event, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500417 /* Synthesize only event probe point */
418 nr = pp.nr_args;
419 pp.nr_args = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500420 synthesize_perf_probe_event(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500421 pp.nr_args = nr;
422 /* Show an event */
423 show_perf_probe_event(group, event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500424 free(group);
425 free(event);
426 clear_probe_point(&pp);
427 }
428
429 strlist__delete(rawlist);
430}
431
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500432/* Get current perf-probe event names */
433static struct strlist *get_perf_event_names(int fd)
434{
435 unsigned int i;
436 char *group, *event;
437 struct strlist *sl, *rawlist;
438 struct str_node *ent;
439
440 rawlist = get_trace_kprobe_event_rawlist(fd);
441
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500442 sl = strlist__new(true, NULL);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500443 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
444 ent = strlist__entry(rawlist, i);
445 parse_trace_kprobe_event(ent->s, &group, &event, NULL);
446 strlist__add(sl, event);
447 free(group);
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500448 free(event);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500449 }
450
451 strlist__delete(rawlist);
452
453 return sl;
454}
455
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500456static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500457{
458 int ret;
459
460 ret = write(fd, buf, strlen(buf));
461 if (ret <= 0)
462 die("Failed to create event.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500463}
464
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500465static void get_new_event_name(char *buf, size_t len, const char *base,
466 struct strlist *namelist)
467{
468 int i, ret;
469 for (i = 0; i < MAX_EVENT_INDEX; i++) {
470 ret = e_snprintf(buf, len, "%s_%d", base, i);
471 if (ret < 0)
472 die("snprintf() failed: %s", strerror(-ret));
473 if (!strlist__has_entry(namelist, buf))
474 break;
475 }
476 if (i == MAX_EVENT_INDEX)
477 die("Too many events are on the same function.");
478}
479
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500480void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
481{
482 int i, j, fd;
483 struct probe_point *pp;
484 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500485 char event[64];
486 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500487
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500488 fd = open_kprobe_events(O_RDWR, O_APPEND);
489 /* Get current event names */
490 namelist = get_perf_event_names(fd);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500491
492 for (j = 0; j < nr_probes; j++) {
493 pp = probes + j;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500494 for (i = 0; i < pp->found; i++) {
495 /* Get an unused new event name */
496 get_new_event_name(event, 64, pp->function, namelist);
497 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
498 pp->retprobe ? 'r' : 'p',
499 PERFPROBE_GROUP, event,
500 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500501 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500502 printf("Added new event:\n");
503 /* Get the first parameter (probe-point) */
504 sscanf(pp->probes[i], "%s", buf);
505 show_perf_probe_event(PERFPROBE_GROUP, event,
506 buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500507 /* Add added event name to namelist */
508 strlist__add(namelist, event);
509 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500510 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500511 /* Show how to use the event. */
512 printf("\nYou can now use it on all perf tools, such as:\n\n");
513 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
514
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500515 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500516 close(fd);
517}