blob: fa156f008e0b61968a4d40c61e938dc6087841c8 [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"
Masami Hiramatsu72041332010-01-05 17:47:10 -050040#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050041#include "color.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050042#include "parse-events.h" /* For debugfs_path */
43#include "probe-event.h"
44
45#define MAX_CMDLEN 256
46#define MAX_PROBE_ARGS 128
47#define PERFPROBE_GROUP "probe"
48
49#define semantic_error(msg ...) die("Semantic error :" msg)
50
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050051/* If there is no space to write, returns -E2BIG. */
52static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050053 __attribute__((format(printf, 3, 4)));
54
55static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050056{
57 int ret;
58 va_list ap;
59 va_start(ap, format);
60 ret = vsnprintf(str, size, format, ap);
61 va_end(ap);
62 if (ret >= (int)size)
63 ret = -E2BIG;
64 return ret;
65}
66
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050067void parse_line_range_desc(const char *arg, struct line_range *lr)
68{
69 const char *ptr;
70 char *tmp;
71 /*
72 * <Syntax>
73 * SRC:SLN[+NUM|-ELN]
74 * FUNC[:SLN[+NUM|-ELN]]
75 */
76 ptr = strchr(arg, ':');
77 if (ptr) {
78 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
79 if (*tmp == '+')
80 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
81 &tmp, 0);
82 else if (*tmp == '-')
83 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
84 else
85 lr->end = 0;
86 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
87 if (lr->end && lr->start > lr->end)
88 semantic_error("Start line must be smaller"
89 " than end line.");
90 if (*tmp != '\0')
91 semantic_error("Tailing with invalid character '%d'.",
92 *tmp);
93 tmp = strndup(arg, (ptr - arg));
94 } else
95 tmp = strdup(arg);
96
97 if (strchr(tmp, '.'))
98 lr->file = tmp;
99 else
100 lr->function = tmp;
101}
102
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500103/* Check the name is good for event/group */
104static bool check_event_name(const char *name)
105{
106 if (!isalpha(*name) && *name != '_')
107 return false;
108 while (*++name != '\0') {
109 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
110 return false;
111 }
112 return true;
113}
114
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500115/* Parse probepoint definition. */
116static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
117{
118 char *ptr, *tmp;
119 char c, nc = 0;
120 /*
121 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500122 * perf probe [EVENT=]SRC[:LN|;PTN]
123 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500124 *
125 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500126 */
127
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500128 ptr = strpbrk(arg, ";=@+%");
129 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500130 *ptr = '\0';
131 tmp = ptr + 1;
132 ptr = strchr(arg, ':');
133 if (ptr) /* Group name is not supported yet. */
134 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500135 if (!check_event_name(arg))
136 semantic_error("%s is bad for event name -it must "
137 "follow C symbol-naming rule.", arg);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500138 pp->event = strdup(arg);
139 arg = tmp;
140 }
141
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500142 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500143 if (ptr) {
144 nc = *ptr;
145 *ptr++ = '\0';
146 }
147
148 /* Check arg is function or file and copy it */
149 if (strchr(arg, '.')) /* File */
150 pp->file = strdup(arg);
151 else /* Function */
152 pp->function = strdup(arg);
153 DIE_IF(pp->file == NULL && pp->function == NULL);
154
155 /* Parse other options */
156 while (ptr) {
157 arg = ptr;
158 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500159 if (c == ';') { /* Lazy pattern must be the last part */
160 pp->lazy_line = strdup(arg);
161 break;
162 }
163 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500164 if (ptr) {
165 nc = *ptr;
166 *ptr++ = '\0';
167 }
168 switch (c) {
169 case ':': /* Line number */
170 pp->line = strtoul(arg, &tmp, 0);
171 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500172 semantic_error("There is non-digit char"
173 " in line number.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500174 break;
175 case '+': /* Byte offset from a symbol */
176 pp->offset = strtoul(arg, &tmp, 0);
177 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500178 semantic_error("There is non-digit character"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500179 " in offset.");
180 break;
181 case '@': /* File name */
182 if (pp->file)
183 semantic_error("SRC@SRC is not allowed.");
184 pp->file = strdup(arg);
185 DIE_IF(pp->file == NULL);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500186 break;
187 case '%': /* Probe places */
188 if (strcmp(arg, "return") == 0) {
189 pp->retprobe = 1;
190 } else /* Others not supported yet */
191 semantic_error("%%%s is not supported.", arg);
192 break;
193 default:
194 DIE_IF("Program has a bug.");
195 break;
196 }
197 }
198
199 /* Exclusion check */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500200 if (pp->lazy_line && pp->line)
201 semantic_error("Lazy pattern can't be used with line number.");
202
203 if (pp->lazy_line && pp->offset)
204 semantic_error("Lazy pattern can't be used with offset.");
205
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500206 if (pp->line && pp->offset)
207 semantic_error("Offset can't be used with line number.");
208
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500209 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
210 semantic_error("File always requires line number or "
211 "lazy pattern.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500212
213 if (pp->offset && !pp->function)
214 semantic_error("Offset requires an entry function.");
215
216 if (pp->retprobe && !pp->function)
217 semantic_error("Return probe requires an entry function.");
218
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500219 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
220 semantic_error("Offset/Line/Lazy pattern can't be used with "
221 "return probe.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500222
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500223 pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n",
224 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
225 pp->lazy_line);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500226}
227
228/* Parse perf-probe event definition */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500229void parse_perf_probe_event(const char *str, struct probe_point *pp,
230 bool *need_dwarf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500231{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500232 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500233 int argc, i;
234
235 *need_dwarf = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500236
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500237 argv = argv_split(str, &argc);
238 if (!argv)
239 die("argv_split failed.");
240 if (argc > MAX_PROBE_ARGS + 1)
241 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500242
243 /* Parse probe point */
244 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500245 if (pp->file || pp->line)
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500246 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500247
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500248 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500249 pp->nr_args = argc - 1;
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500250 pp->args = zalloc(sizeof(char *) * pp->nr_args);
251 for (i = 0; i < pp->nr_args; i++) {
252 pp->args[i] = strdup(argv[i + 1]);
253 if (!pp->args[i])
254 die("Failed to copy argument.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500255 if (is_c_varname(pp->args[i])) {
256 if (pp->retprobe)
257 semantic_error("You can't specify local"
258 " variable for kretprobe");
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500259 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500260 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500261 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500262
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500263 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500264}
265
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500266/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500267void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500268{
269 char pr;
270 char *p;
271 int ret, i, argc;
272 char **argv;
273
274 pr_debug("Parsing kprobe_events: %s\n", str);
275 argv = argv_split(str, &argc);
276 if (!argv)
277 die("argv_split failed.");
278 if (argc < 2)
279 semantic_error("Too less arguments.");
280
281 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800282 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500283 &pr, (float *)(void *)&pp->group,
284 (float *)(void *)&pp->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500285 if (ret != 3)
286 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500287 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500288
289 pp->retprobe = (pr == 'r');
290
291 /* Scan function name and offset */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500292 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
293 &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500294 if (ret == 1)
295 pp->offset = 0;
296
297 /* kprobe_events doesn't have this information */
298 pp->line = 0;
299 pp->file = NULL;
300
301 pp->nr_args = argc - 2;
302 pp->args = zalloc(sizeof(char *) * pp->nr_args);
303 for (i = 0; i < pp->nr_args; i++) {
304 p = strchr(argv[i + 2], '=');
305 if (p) /* We don't need which register is assigned. */
306 *p = '\0';
307 pp->args[i] = strdup(argv[i + 2]);
308 if (!pp->args[i])
309 die("Failed to copy argument.");
310 }
311
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500312 argv_free(argv);
313}
314
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500315/* Synthesize only probe point (not argument) */
316int synthesize_perf_probe_point(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500317{
318 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500319 char offs[64] = "", line[64] = "";
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500320 int ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500321
322 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
323 if (!buf)
324 die("Failed to allocate memory by zalloc.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500325 if (pp->offset) {
326 ret = e_snprintf(offs, 64, "+%d", pp->offset);
327 if (ret <= 0)
328 goto error;
329 }
330 if (pp->line) {
331 ret = e_snprintf(line, 64, ":%d", pp->line);
332 if (ret <= 0)
333 goto error;
334 }
335
336 if (pp->function)
337 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
338 offs, pp->retprobe ? "%return" : "", line);
339 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500340 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500341 if (ret <= 0) {
342error:
343 free(pp->probes[0]);
344 pp->probes[0] = NULL;
345 }
346 return ret;
347}
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500348
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500349int synthesize_perf_probe_event(struct probe_point *pp)
350{
351 char *buf;
352 int i, len, ret;
353
354 len = synthesize_perf_probe_point(pp);
355 if (len < 0)
356 return 0;
357
358 buf = pp->probes[0];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500359 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500360 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
361 pp->args[i]);
362 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500363 goto error;
364 len += ret;
365 }
366 pp->found = 1;
367
368 return pp->found;
369error:
370 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500371 pp->probes[0] = NULL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500372
373 return ret;
374}
375
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500376int synthesize_trace_kprobe_event(struct probe_point *pp)
377{
378 char *buf;
379 int i, len, ret;
380
381 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
382 if (!buf)
383 die("Failed to allocate memory by zalloc.");
384 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
385 if (ret <= 0)
386 goto error;
387 len = ret;
388
389 for (i = 0; i < pp->nr_args; i++) {
390 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
391 pp->args[i]);
392 if (ret <= 0)
393 goto error;
394 len += ret;
395 }
396 pp->found = 1;
397
398 return pp->found;
399error:
400 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500401 pp->probes[0] = NULL;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500402
403 return ret;
404}
405
406static int open_kprobe_events(int flags, int mode)
407{
408 char buf[PATH_MAX];
409 int ret;
410
411 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
412 if (ret < 0)
413 die("Failed to make kprobe_events path.");
414
415 ret = open(buf, flags, mode);
416 if (ret < 0) {
417 if (errno == ENOENT)
418 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800419 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500420 else
421 die("Could not open kprobe_events file: %s",
422 strerror(errno));
423 }
424 return ret;
425}
426
427/* Get raw string list of current kprobe_events */
428static struct strlist *get_trace_kprobe_event_rawlist(int fd)
429{
430 int ret, idx;
431 FILE *fp;
432 char buf[MAX_CMDLEN];
433 char *p;
434 struct strlist *sl;
435
436 sl = strlist__new(true, NULL);
437
438 fp = fdopen(dup(fd), "r");
439 while (!feof(fp)) {
440 p = fgets(buf, MAX_CMDLEN, fp);
441 if (!p)
442 break;
443
444 idx = strlen(p) - 1;
445 if (p[idx] == '\n')
446 p[idx] = '\0';
447 ret = strlist__add(sl, buf);
448 if (ret < 0)
449 die("strlist__add failed: %s", strerror(-ret));
450 }
451 fclose(fp);
452
453 return sl;
454}
455
456/* Free and zero clear probe_point */
457static void clear_probe_point(struct probe_point *pp)
458{
459 int i;
460
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500461 if (pp->event)
462 free(pp->event);
463 if (pp->group)
464 free(pp->group);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500465 if (pp->function)
466 free(pp->function);
467 if (pp->file)
468 free(pp->file);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500469 if (pp->lazy_line)
470 free(pp->lazy_line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500471 for (i = 0; i < pp->nr_args; i++)
472 free(pp->args[i]);
473 if (pp->args)
474 free(pp->args);
475 for (i = 0; i < pp->found; i++)
476 free(pp->probes[i]);
Julia Lawall5660ce32009-12-09 20:26:18 +0100477 memset(pp, 0, sizeof(*pp));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500478}
479
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500480/* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500481static void show_perf_probe_event(const char *event, const char *place,
482 struct probe_point *pp)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500483{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500484 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500485 char buf[128];
486
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500487 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500488 if (ret < 0)
489 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500490 printf(" %-40s (on %s", buf, place);
491
492 if (pp->nr_args > 0) {
493 printf(" with");
494 for (i = 0; i < pp->nr_args; i++)
495 printf(" %s", pp->args[i]);
496 }
497 printf(")\n");
498}
499
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500500/* List up current perf-probe events */
501void show_perf_probe_events(void)
502{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500503 int fd;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500504 struct probe_point pp;
505 struct strlist *rawlist;
506 struct str_node *ent;
507
Masami Hiramatsu72041332010-01-05 17:47:10 -0500508 setup_pager();
509
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500510 fd = open_kprobe_events(O_RDONLY, 0);
511 rawlist = get_trace_kprobe_event_rawlist(fd);
512 close(fd);
513
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500514 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500515 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500516 /* Synthesize only event probe point */
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500517 synthesize_perf_probe_point(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500518 /* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500519 show_perf_probe_event(pp.event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500520 clear_probe_point(&pp);
521 }
522
523 strlist__delete(rawlist);
524}
525
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500526/* Get current perf-probe event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500527static struct strlist *get_perf_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500528{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500529 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500530 struct strlist *sl, *rawlist;
531 struct str_node *ent;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500532 struct probe_point pp;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500533
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500534 memset(&pp, 0, sizeof(pp));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500535 rawlist = get_trace_kprobe_event_rawlist(fd);
536
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500537 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500538 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500539 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500540 if (include_group) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500541 if (e_snprintf(buf, 128, "%s:%s", pp.group,
542 pp.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500543 die("Failed to copy group:event name.");
544 strlist__add(sl, buf);
545 } else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500546 strlist__add(sl, pp.event);
547 clear_probe_point(&pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500548 }
549
550 strlist__delete(rawlist);
551
552 return sl;
553}
554
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500555static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500556{
557 int ret;
558
Masami Hiramatsufa282442009-12-08 17:03:23 -0500559 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500560 ret = write(fd, buf, strlen(buf));
561 if (ret <= 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500562 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500563}
564
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500565static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500566 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500567{
568 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500569
570 /* Try no suffix */
571 ret = e_snprintf(buf, len, "%s", base);
572 if (ret < 0)
573 die("snprintf() failed: %s", strerror(-ret));
574 if (!strlist__has_entry(namelist, buf))
575 return;
576
Masami Hiramatsud761b082009-12-15 10:32:25 -0500577 if (!allow_suffix) {
578 pr_warning("Error: event \"%s\" already exists. "
579 "(Use -f to force duplicates.)\n", base);
580 die("Can't add new event.");
581 }
582
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500583 /* Try to add suffix */
584 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500585 ret = e_snprintf(buf, len, "%s_%d", base, i);
586 if (ret < 0)
587 die("snprintf() failed: %s", strerror(-ret));
588 if (!strlist__has_entry(namelist, buf))
589 break;
590 }
591 if (i == MAX_EVENT_INDEX)
592 die("Too many events are on the same function.");
593}
594
Masami Hiramatsud761b082009-12-15 10:32:25 -0500595void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
596 bool force_add)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500597{
598 int i, j, fd;
599 struct probe_point *pp;
600 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500601 char event[64];
602 struct strlist *namelist;
Masami Hiramatsud761b082009-12-15 10:32:25 -0500603 bool allow_suffix;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500604
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500605 fd = open_kprobe_events(O_RDWR, O_APPEND);
606 /* Get current event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500607 namelist = get_perf_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500608
609 for (j = 0; j < nr_probes; j++) {
610 pp = probes + j;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500611 if (!pp->event)
612 pp->event = strdup(pp->function);
613 if (!pp->group)
614 pp->group = strdup(PERFPROBE_GROUP);
615 DIE_IF(!pp->event || !pp->group);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500616 /* If force_add is true, suffix search is allowed */
617 allow_suffix = force_add;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500618 for (i = 0; i < pp->found; i++) {
619 /* Get an unused new event name */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500620 get_new_event_name(event, 64, pp->event, namelist,
621 allow_suffix);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500622 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
623 pp->retprobe ? 'r' : 'p',
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500624 pp->group, event,
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500625 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500626 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500627 printf("Added new event:\n");
628 /* Get the first parameter (probe-point) */
629 sscanf(pp->probes[i], "%s", buf);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500630 show_perf_probe_event(event, buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500631 /* Add added event name to namelist */
632 strlist__add(namelist, event);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500633 /*
634 * Probes after the first probe which comes from same
635 * user input are always allowed to add suffix, because
636 * there might be several addresses corresponding to
637 * one code line.
638 */
639 allow_suffix = true;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500640 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500641 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500642 /* Show how to use the event. */
643 printf("\nYou can now use it on all perf tools, such as:\n\n");
644 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
645
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500646 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500647 close(fd);
648}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500649
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500650static void __del_trace_kprobe_event(int fd, struct str_node *ent)
651{
652 char *p;
653 char buf[128];
654
655 /* Convert from perf-probe event to trace-kprobe event */
656 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
657 die("Failed to copy event.");
658 p = strchr(buf + 2, ':');
659 if (!p)
660 die("Internal error: %s should have ':' but not.", ent->s);
661 *p = '/';
662
663 write_trace_kprobe_event(fd, buf);
664 printf("Remove event: %s\n", ent->s);
665}
666
Masami Hiramatsufa282442009-12-08 17:03:23 -0500667static void del_trace_kprobe_event(int fd, const char *group,
668 const char *event, struct strlist *namelist)
669{
670 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500671 struct str_node *ent, *n;
672 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500673
674 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
675 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -0500676
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500677 if (strpbrk(buf, "*?")) { /* Glob-exp */
678 strlist__for_each_safe(ent, n, namelist)
679 if (strglobmatch(ent->s, buf)) {
680 found++;
681 __del_trace_kprobe_event(fd, ent);
682 strlist__remove(namelist, ent);
683 }
684 } else {
685 ent = strlist__find(namelist, buf);
686 if (ent) {
687 found++;
688 __del_trace_kprobe_event(fd, ent);
689 strlist__remove(namelist, ent);
690 }
691 }
692 if (found == 0)
693 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500694}
695
696void del_trace_kprobe_events(struct strlist *dellist)
697{
698 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500699 const char *group, *event;
700 char *p, *str;
701 struct str_node *ent;
702 struct strlist *namelist;
703
704 fd = open_kprobe_events(O_RDWR, O_APPEND);
705 /* Get current event names */
706 namelist = get_perf_event_names(fd, true);
707
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500708 strlist__for_each(ent, dellist) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500709 str = strdup(ent->s);
710 if (!str)
711 die("Failed to copy event.");
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500712 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500713 p = strchr(str, ':');
714 if (p) {
715 group = str;
716 *p = '\0';
717 event = p + 1;
718 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500719 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -0500720 event = str;
721 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500722 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500723 del_trace_kprobe_event(fd, group, event, namelist);
724 free(str);
725 }
726 strlist__delete(namelist);
727 close(fd);
728}
729
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500730#define LINEBUF_SIZE 256
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -0500731#define NR_ADDITIONAL_LINES 2
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500732
733static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
734{
735 char buf[LINEBUF_SIZE];
736 const char *color = PERF_COLOR_BLUE;
737
738 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
739 goto error;
740 if (!skip) {
741 if (show_num)
742 fprintf(stdout, "%7u %s", l, buf);
743 else
744 color_fprintf(stdout, color, " %s", buf);
745 }
746
747 while (strlen(buf) == LINEBUF_SIZE - 1 &&
748 buf[LINEBUF_SIZE - 2] != '\n') {
749 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
750 goto error;
751 if (!skip) {
752 if (show_num)
753 fprintf(stdout, "%s", buf);
754 else
755 color_fprintf(stdout, color, "%s", buf);
756 }
757 }
758 return;
759error:
760 if (feof(fp))
761 die("Source file is shorter than expected.");
762 else
763 die("File read error: %s", strerror(errno));
764}
765
766void show_line_range(struct line_range *lr)
767{
768 unsigned int l = 1;
769 struct line_node *ln;
770 FILE *fp;
771
772 setup_pager();
773
774 if (lr->function)
775 fprintf(stdout, "<%s:%d>\n", lr->function,
776 lr->start - lr->offset);
777 else
778 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
779
780 fp = fopen(lr->path, "r");
781 if (fp == NULL)
782 die("Failed to open %s: %s", lr->path, strerror(errno));
783 /* Skip to starting line number */
784 while (l < lr->start)
785 show_one_line(fp, l++, true, false);
786
787 list_for_each_entry(ln, &lr->line_list, list) {
788 while (ln->line > l)
789 show_one_line(fp, (l++) - lr->offset, false, false);
790 show_one_line(fp, (l++) - lr->offset, false, true);
791 }
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -0500792
793 if (lr->end == INT_MAX)
794 lr->end = l + NR_ADDITIONAL_LINES;
795 while (l < lr->end && !feof(fp))
796 show_one_line(fp, (l++) - lr->offset, false, false);
797
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500798 fclose(fp);
799}