blob: a22141a773bcfee53146171598f5f22fbe447376 [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 Hiramatsu50656ee2009-11-30 19:19:58 -050041#include "parse-events.h" /* For debugfs_path */
42#include "probe-event.h"
43
44#define MAX_CMDLEN 256
45#define MAX_PROBE_ARGS 128
46#define PERFPROBE_GROUP "probe"
47
48#define semantic_error(msg ...) die("Semantic error :" msg)
49
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050050/* If there is no space to write, returns -E2BIG. */
51static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050052 __attribute__((format(printf, 3, 4)));
53
54static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050055{
56 int ret;
57 va_list ap;
58 va_start(ap, format);
59 ret = vsnprintf(str, size, format, ap);
60 va_end(ap);
61 if (ret >= (int)size)
62 ret = -E2BIG;
63 return ret;
64}
65
Masami Hiramatsub7702a22009-12-16 17:24:15 -050066/* Check the name is good for event/group */
67static bool check_event_name(const char *name)
68{
69 if (!isalpha(*name) && *name != '_')
70 return false;
71 while (*++name != '\0') {
72 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
73 return false;
74 }
75 return true;
76}
77
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050078/* Parse probepoint definition. */
79static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
80{
81 char *ptr, *tmp;
82 char c, nc = 0;
83 /*
84 * <Syntax>
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050085 * perf probe [EVENT=]SRC:LN
86 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
87 *
88 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050089 */
90
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050091 ptr = strchr(arg, '=');
92 if (ptr) { /* Event name */
93 *ptr = '\0';
94 tmp = ptr + 1;
95 ptr = strchr(arg, ':');
96 if (ptr) /* Group name is not supported yet. */
97 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -050098 if (!check_event_name(arg))
99 semantic_error("%s is bad for event name -it must "
100 "follow C symbol-naming rule.", arg);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500101 pp->event = strdup(arg);
102 arg = tmp;
103 }
104
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500105 ptr = strpbrk(arg, ":+@%");
106 if (ptr) {
107 nc = *ptr;
108 *ptr++ = '\0';
109 }
110
111 /* Check arg is function or file and copy it */
112 if (strchr(arg, '.')) /* File */
113 pp->file = strdup(arg);
114 else /* Function */
115 pp->function = strdup(arg);
116 DIE_IF(pp->file == NULL && pp->function == NULL);
117
118 /* Parse other options */
119 while (ptr) {
120 arg = ptr;
121 c = nc;
122 ptr = strpbrk(arg, ":+@%");
123 if (ptr) {
124 nc = *ptr;
125 *ptr++ = '\0';
126 }
127 switch (c) {
128 case ':': /* Line number */
129 pp->line = strtoul(arg, &tmp, 0);
130 if (*tmp != '\0')
131 semantic_error("There is non-digit charactor"
132 " in line number.");
133 break;
134 case '+': /* Byte offset from a symbol */
135 pp->offset = strtoul(arg, &tmp, 0);
136 if (*tmp != '\0')
137 semantic_error("There is non-digit charactor"
138 " in offset.");
139 break;
140 case '@': /* File name */
141 if (pp->file)
142 semantic_error("SRC@SRC is not allowed.");
143 pp->file = strdup(arg);
144 DIE_IF(pp->file == NULL);
145 if (ptr)
146 semantic_error("@SRC must be the last "
147 "option.");
148 break;
149 case '%': /* Probe places */
150 if (strcmp(arg, "return") == 0) {
151 pp->retprobe = 1;
152 } else /* Others not supported yet */
153 semantic_error("%%%s is not supported.", arg);
154 break;
155 default:
156 DIE_IF("Program has a bug.");
157 break;
158 }
159 }
160
161 /* Exclusion check */
162 if (pp->line && pp->offset)
163 semantic_error("Offset can't be used with line number.");
164
165 if (!pp->line && pp->file && !pp->function)
166 semantic_error("File always requires line number.");
167
168 if (pp->offset && !pp->function)
169 semantic_error("Offset requires an entry function.");
170
171 if (pp->retprobe && !pp->function)
172 semantic_error("Return probe requires an entry function.");
173
174 if ((pp->offset || pp->line) && pp->retprobe)
175 semantic_error("Offset/Line can't be used with return probe.");
176
177 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
178 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
179}
180
181/* Parse perf-probe event definition */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500182void parse_perf_probe_event(const char *str, struct probe_point *pp,
183 bool *need_dwarf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500184{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500185 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500186 int argc, i;
187
188 *need_dwarf = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500189
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500190 argv = argv_split(str, &argc);
191 if (!argv)
192 die("argv_split failed.");
193 if (argc > MAX_PROBE_ARGS + 1)
194 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500195
196 /* Parse probe point */
197 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500198 if (pp->file || pp->line)
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500199 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500200
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500201 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500202 pp->nr_args = argc - 1;
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500203 pp->args = zalloc(sizeof(char *) * pp->nr_args);
204 for (i = 0; i < pp->nr_args; i++) {
205 pp->args[i] = strdup(argv[i + 1]);
206 if (!pp->args[i])
207 die("Failed to copy argument.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500208 if (is_c_varname(pp->args[i])) {
209 if (pp->retprobe)
210 semantic_error("You can't specify local"
211 " variable for kretprobe");
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500212 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500213 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500214 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500215
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500216 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500217}
218
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500219/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500220void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500221{
222 char pr;
223 char *p;
224 int ret, i, argc;
225 char **argv;
226
227 pr_debug("Parsing kprobe_events: %s\n", str);
228 argv = argv_split(str, &argc);
229 if (!argv)
230 die("argv_split failed.");
231 if (argc < 2)
232 semantic_error("Too less arguments.");
233
234 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800235 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500236 &pr, (float *)(void *)&pp->group,
237 (float *)(void *)&pp->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500238 if (ret != 3)
239 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500240 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500241
242 pp->retprobe = (pr == 'r');
243
244 /* Scan function name and offset */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500245 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
246 &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500247 if (ret == 1)
248 pp->offset = 0;
249
250 /* kprobe_events doesn't have this information */
251 pp->line = 0;
252 pp->file = NULL;
253
254 pp->nr_args = argc - 2;
255 pp->args = zalloc(sizeof(char *) * pp->nr_args);
256 for (i = 0; i < pp->nr_args; i++) {
257 p = strchr(argv[i + 2], '=');
258 if (p) /* We don't need which register is assigned. */
259 *p = '\0';
260 pp->args[i] = strdup(argv[i + 2]);
261 if (!pp->args[i])
262 die("Failed to copy argument.");
263 }
264
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500265 argv_free(argv);
266}
267
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500268/* Synthesize only probe point (not argument) */
269int synthesize_perf_probe_point(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500270{
271 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500272 char offs[64] = "", line[64] = "";
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500273 int ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500274
275 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
276 if (!buf)
277 die("Failed to allocate memory by zalloc.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500278 if (pp->offset) {
279 ret = e_snprintf(offs, 64, "+%d", pp->offset);
280 if (ret <= 0)
281 goto error;
282 }
283 if (pp->line) {
284 ret = e_snprintf(line, 64, ":%d", pp->line);
285 if (ret <= 0)
286 goto error;
287 }
288
289 if (pp->function)
290 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
291 offs, pp->retprobe ? "%return" : "", line);
292 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500293 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500294 if (ret <= 0) {
295error:
296 free(pp->probes[0]);
297 pp->probes[0] = NULL;
298 }
299 return ret;
300}
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500301
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500302int synthesize_perf_probe_event(struct probe_point *pp)
303{
304 char *buf;
305 int i, len, ret;
306
307 len = synthesize_perf_probe_point(pp);
308 if (len < 0)
309 return 0;
310
311 buf = pp->probes[0];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500312 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500313 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
314 pp->args[i]);
315 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500316 goto error;
317 len += ret;
318 }
319 pp->found = 1;
320
321 return pp->found;
322error:
323 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500324 pp->probes[0] = NULL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500325
326 return ret;
327}
328
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500329int synthesize_trace_kprobe_event(struct probe_point *pp)
330{
331 char *buf;
332 int i, len, ret;
333
334 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
335 if (!buf)
336 die("Failed to allocate memory by zalloc.");
337 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
338 if (ret <= 0)
339 goto error;
340 len = ret;
341
342 for (i = 0; i < pp->nr_args; i++) {
343 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
344 pp->args[i]);
345 if (ret <= 0)
346 goto error;
347 len += ret;
348 }
349 pp->found = 1;
350
351 return pp->found;
352error:
353 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500354 pp->probes[0] = NULL;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500355
356 return ret;
357}
358
359static int open_kprobe_events(int flags, int mode)
360{
361 char buf[PATH_MAX];
362 int ret;
363
364 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
365 if (ret < 0)
366 die("Failed to make kprobe_events path.");
367
368 ret = open(buf, flags, mode);
369 if (ret < 0) {
370 if (errno == ENOENT)
371 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800372 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500373 else
374 die("Could not open kprobe_events file: %s",
375 strerror(errno));
376 }
377 return ret;
378}
379
380/* Get raw string list of current kprobe_events */
381static struct strlist *get_trace_kprobe_event_rawlist(int fd)
382{
383 int ret, idx;
384 FILE *fp;
385 char buf[MAX_CMDLEN];
386 char *p;
387 struct strlist *sl;
388
389 sl = strlist__new(true, NULL);
390
391 fp = fdopen(dup(fd), "r");
392 while (!feof(fp)) {
393 p = fgets(buf, MAX_CMDLEN, fp);
394 if (!p)
395 break;
396
397 idx = strlen(p) - 1;
398 if (p[idx] == '\n')
399 p[idx] = '\0';
400 ret = strlist__add(sl, buf);
401 if (ret < 0)
402 die("strlist__add failed: %s", strerror(-ret));
403 }
404 fclose(fp);
405
406 return sl;
407}
408
409/* Free and zero clear probe_point */
410static void clear_probe_point(struct probe_point *pp)
411{
412 int i;
413
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500414 if (pp->event)
415 free(pp->event);
416 if (pp->group)
417 free(pp->group);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500418 if (pp->function)
419 free(pp->function);
420 if (pp->file)
421 free(pp->file);
422 for (i = 0; i < pp->nr_args; i++)
423 free(pp->args[i]);
424 if (pp->args)
425 free(pp->args);
426 for (i = 0; i < pp->found; i++)
427 free(pp->probes[i]);
Julia Lawall5660ce32009-12-09 20:26:18 +0100428 memset(pp, 0, sizeof(*pp));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500429}
430
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500431/* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500432static void show_perf_probe_event(const char *event, const char *place,
433 struct probe_point *pp)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500434{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500435 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500436 char buf[128];
437
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500438 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500439 if (ret < 0)
440 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500441 printf(" %-40s (on %s", buf, place);
442
443 if (pp->nr_args > 0) {
444 printf(" with");
445 for (i = 0; i < pp->nr_args; i++)
446 printf(" %s", pp->args[i]);
447 }
448 printf(")\n");
449}
450
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500451/* List up current perf-probe events */
452void show_perf_probe_events(void)
453{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500454 int fd;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500455 struct probe_point pp;
456 struct strlist *rawlist;
457 struct str_node *ent;
458
Masami Hiramatsu72041332010-01-05 17:47:10 -0500459 setup_pager();
460
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500461 fd = open_kprobe_events(O_RDONLY, 0);
462 rawlist = get_trace_kprobe_event_rawlist(fd);
463 close(fd);
464
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500465 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500466 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500467 /* Synthesize only event probe point */
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500468 synthesize_perf_probe_point(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500469 /* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500470 show_perf_probe_event(pp.event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500471 clear_probe_point(&pp);
472 }
473
474 strlist__delete(rawlist);
475}
476
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500477/* Get current perf-probe event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500478static struct strlist *get_perf_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500479{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500480 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500481 struct strlist *sl, *rawlist;
482 struct str_node *ent;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500483 struct probe_point pp;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500484
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500485 memset(&pp, 0, sizeof(pp));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500486 rawlist = get_trace_kprobe_event_rawlist(fd);
487
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500488 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500489 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500490 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500491 if (include_group) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500492 if (e_snprintf(buf, 128, "%s:%s", pp.group,
493 pp.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500494 die("Failed to copy group:event name.");
495 strlist__add(sl, buf);
496 } else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500497 strlist__add(sl, pp.event);
498 clear_probe_point(&pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500499 }
500
501 strlist__delete(rawlist);
502
503 return sl;
504}
505
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500506static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500507{
508 int ret;
509
Masami Hiramatsufa282442009-12-08 17:03:23 -0500510 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500511 ret = write(fd, buf, strlen(buf));
512 if (ret <= 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500513 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500514}
515
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500516static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500517 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500518{
519 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500520
521 /* Try no suffix */
522 ret = e_snprintf(buf, len, "%s", base);
523 if (ret < 0)
524 die("snprintf() failed: %s", strerror(-ret));
525 if (!strlist__has_entry(namelist, buf))
526 return;
527
Masami Hiramatsud761b082009-12-15 10:32:25 -0500528 if (!allow_suffix) {
529 pr_warning("Error: event \"%s\" already exists. "
530 "(Use -f to force duplicates.)\n", base);
531 die("Can't add new event.");
532 }
533
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500534 /* Try to add suffix */
535 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500536 ret = e_snprintf(buf, len, "%s_%d", base, i);
537 if (ret < 0)
538 die("snprintf() failed: %s", strerror(-ret));
539 if (!strlist__has_entry(namelist, buf))
540 break;
541 }
542 if (i == MAX_EVENT_INDEX)
543 die("Too many events are on the same function.");
544}
545
Masami Hiramatsud761b082009-12-15 10:32:25 -0500546void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
547 bool force_add)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500548{
549 int i, j, fd;
550 struct probe_point *pp;
551 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500552 char event[64];
553 struct strlist *namelist;
Masami Hiramatsud761b082009-12-15 10:32:25 -0500554 bool allow_suffix;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500555
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500556 fd = open_kprobe_events(O_RDWR, O_APPEND);
557 /* Get current event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500558 namelist = get_perf_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500559
560 for (j = 0; j < nr_probes; j++) {
561 pp = probes + j;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500562 if (!pp->event)
563 pp->event = strdup(pp->function);
564 if (!pp->group)
565 pp->group = strdup(PERFPROBE_GROUP);
566 DIE_IF(!pp->event || !pp->group);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500567 /* If force_add is true, suffix search is allowed */
568 allow_suffix = force_add;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500569 for (i = 0; i < pp->found; i++) {
570 /* Get an unused new event name */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500571 get_new_event_name(event, 64, pp->event, namelist,
572 allow_suffix);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500573 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
574 pp->retprobe ? 'r' : 'p',
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500575 pp->group, event,
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500576 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500577 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500578 printf("Added new event:\n");
579 /* Get the first parameter (probe-point) */
580 sscanf(pp->probes[i], "%s", buf);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500581 show_perf_probe_event(event, buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500582 /* Add added event name to namelist */
583 strlist__add(namelist, event);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500584 /*
585 * Probes after the first probe which comes from same
586 * user input are always allowed to add suffix, because
587 * there might be several addresses corresponding to
588 * one code line.
589 */
590 allow_suffix = true;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500591 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500592 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500593 /* Show how to use the event. */
594 printf("\nYou can now use it on all perf tools, such as:\n\n");
595 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
596
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500597 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500598 close(fd);
599}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500600
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500601static void __del_trace_kprobe_event(int fd, struct str_node *ent)
602{
603 char *p;
604 char buf[128];
605
606 /* Convert from perf-probe event to trace-kprobe event */
607 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
608 die("Failed to copy event.");
609 p = strchr(buf + 2, ':');
610 if (!p)
611 die("Internal error: %s should have ':' but not.", ent->s);
612 *p = '/';
613
614 write_trace_kprobe_event(fd, buf);
615 printf("Remove event: %s\n", ent->s);
616}
617
Masami Hiramatsufa282442009-12-08 17:03:23 -0500618static void del_trace_kprobe_event(int fd, const char *group,
619 const char *event, struct strlist *namelist)
620{
621 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500622 struct str_node *ent, *n;
623 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500624
625 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
626 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -0500627
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500628 if (strpbrk(buf, "*?")) { /* Glob-exp */
629 strlist__for_each_safe(ent, n, namelist)
630 if (strglobmatch(ent->s, buf)) {
631 found++;
632 __del_trace_kprobe_event(fd, ent);
633 strlist__remove(namelist, ent);
634 }
635 } else {
636 ent = strlist__find(namelist, buf);
637 if (ent) {
638 found++;
639 __del_trace_kprobe_event(fd, ent);
640 strlist__remove(namelist, ent);
641 }
642 }
643 if (found == 0)
644 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500645}
646
647void del_trace_kprobe_events(struct strlist *dellist)
648{
649 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500650 const char *group, *event;
651 char *p, *str;
652 struct str_node *ent;
653 struct strlist *namelist;
654
655 fd = open_kprobe_events(O_RDWR, O_APPEND);
656 /* Get current event names */
657 namelist = get_perf_event_names(fd, true);
658
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500659 strlist__for_each(ent, dellist) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500660 str = strdup(ent->s);
661 if (!str)
662 die("Failed to copy event.");
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500663 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500664 p = strchr(str, ':');
665 if (p) {
666 group = str;
667 *p = '\0';
668 event = p + 1;
669 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500670 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -0500671 event = str;
672 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500673 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500674 del_trace_kprobe_event(fd, group, event, namelist);
675 free(str);
676 }
677 strlist__delete(namelist);
678 close(fd);
679}
680