blob: 29465d440043587462c61cb7dc3483bc68203b07 [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 Hiramatsub7702a22009-12-16 17:24:15 -050065/* Check the name is good for event/group */
66static bool check_event_name(const char *name)
67{
68 if (!isalpha(*name) && *name != '_')
69 return false;
70 while (*++name != '\0') {
71 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
72 return false;
73 }
74 return true;
75}
76
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050077/* Parse probepoint definition. */
78static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
79{
80 char *ptr, *tmp;
81 char c, nc = 0;
82 /*
83 * <Syntax>
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050084 * perf probe [EVENT=]SRC:LN
85 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
86 *
87 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050088 */
89
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050090 ptr = strchr(arg, '=');
91 if (ptr) { /* Event name */
92 *ptr = '\0';
93 tmp = ptr + 1;
94 ptr = strchr(arg, ':');
95 if (ptr) /* Group name is not supported yet. */
96 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -050097 if (!check_event_name(arg))
98 semantic_error("%s is bad for event name -it must "
99 "follow C symbol-naming rule.", arg);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500100 pp->event = strdup(arg);
101 arg = tmp;
102 }
103
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500104 ptr = strpbrk(arg, ":+@%");
105 if (ptr) {
106 nc = *ptr;
107 *ptr++ = '\0';
108 }
109
110 /* Check arg is function or file and copy it */
111 if (strchr(arg, '.')) /* File */
112 pp->file = strdup(arg);
113 else /* Function */
114 pp->function = strdup(arg);
115 DIE_IF(pp->file == NULL && pp->function == NULL);
116
117 /* Parse other options */
118 while (ptr) {
119 arg = ptr;
120 c = nc;
121 ptr = strpbrk(arg, ":+@%");
122 if (ptr) {
123 nc = *ptr;
124 *ptr++ = '\0';
125 }
126 switch (c) {
127 case ':': /* Line number */
128 pp->line = strtoul(arg, &tmp, 0);
129 if (*tmp != '\0')
130 semantic_error("There is non-digit charactor"
131 " in line number.");
132 break;
133 case '+': /* Byte offset from a symbol */
134 pp->offset = strtoul(arg, &tmp, 0);
135 if (*tmp != '\0')
136 semantic_error("There is non-digit charactor"
137 " in offset.");
138 break;
139 case '@': /* File name */
140 if (pp->file)
141 semantic_error("SRC@SRC is not allowed.");
142 pp->file = strdup(arg);
143 DIE_IF(pp->file == NULL);
144 if (ptr)
145 semantic_error("@SRC must be the last "
146 "option.");
147 break;
148 case '%': /* Probe places */
149 if (strcmp(arg, "return") == 0) {
150 pp->retprobe = 1;
151 } else /* Others not supported yet */
152 semantic_error("%%%s is not supported.", arg);
153 break;
154 default:
155 DIE_IF("Program has a bug.");
156 break;
157 }
158 }
159
160 /* Exclusion check */
161 if (pp->line && pp->offset)
162 semantic_error("Offset can't be used with line number.");
163
164 if (!pp->line && pp->file && !pp->function)
165 semantic_error("File always requires line number.");
166
167 if (pp->offset && !pp->function)
168 semantic_error("Offset requires an entry function.");
169
170 if (pp->retprobe && !pp->function)
171 semantic_error("Return probe requires an entry function.");
172
173 if ((pp->offset || pp->line) && pp->retprobe)
174 semantic_error("Offset/Line can't be used with return probe.");
175
176 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
177 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
178}
179
180/* Parse perf-probe event definition */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500181void parse_perf_probe_event(const char *str, struct probe_point *pp,
182 bool *need_dwarf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500183{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500184 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500185 int argc, i;
186
187 *need_dwarf = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500188
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500189 argv = argv_split(str, &argc);
190 if (!argv)
191 die("argv_split failed.");
192 if (argc > MAX_PROBE_ARGS + 1)
193 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500194
195 /* Parse probe point */
196 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500197 if (pp->file || pp->line)
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500198 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500199
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500200 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500201 pp->nr_args = argc - 1;
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500202 pp->args = zalloc(sizeof(char *) * pp->nr_args);
203 for (i = 0; i < pp->nr_args; i++) {
204 pp->args[i] = strdup(argv[i + 1]);
205 if (!pp->args[i])
206 die("Failed to copy argument.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500207 if (is_c_varname(pp->args[i])) {
208 if (pp->retprobe)
209 semantic_error("You can't specify local"
210 " variable for kretprobe");
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500211 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500212 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500213 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500214
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500215 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500216}
217
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500218/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500219void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500220{
221 char pr;
222 char *p;
223 int ret, i, argc;
224 char **argv;
225
226 pr_debug("Parsing kprobe_events: %s\n", str);
227 argv = argv_split(str, &argc);
228 if (!argv)
229 die("argv_split failed.");
230 if (argc < 2)
231 semantic_error("Too less arguments.");
232
233 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800234 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500235 &pr, (float *)(void *)&pp->group,
236 (float *)(void *)&pp->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500237 if (ret != 3)
238 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500239 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500240
241 pp->retprobe = (pr == 'r');
242
243 /* Scan function name and offset */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500244 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
245 &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500246 if (ret == 1)
247 pp->offset = 0;
248
249 /* kprobe_events doesn't have this information */
250 pp->line = 0;
251 pp->file = NULL;
252
253 pp->nr_args = argc - 2;
254 pp->args = zalloc(sizeof(char *) * pp->nr_args);
255 for (i = 0; i < pp->nr_args; i++) {
256 p = strchr(argv[i + 2], '=');
257 if (p) /* We don't need which register is assigned. */
258 *p = '\0';
259 pp->args[i] = strdup(argv[i + 2]);
260 if (!pp->args[i])
261 die("Failed to copy argument.");
262 }
263
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500264 argv_free(argv);
265}
266
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500267/* Synthesize only probe point (not argument) */
268int synthesize_perf_probe_point(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500269{
270 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500271 char offs[64] = "", line[64] = "";
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500272 int ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500273
274 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
275 if (!buf)
276 die("Failed to allocate memory by zalloc.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500277 if (pp->offset) {
278 ret = e_snprintf(offs, 64, "+%d", pp->offset);
279 if (ret <= 0)
280 goto error;
281 }
282 if (pp->line) {
283 ret = e_snprintf(line, 64, ":%d", pp->line);
284 if (ret <= 0)
285 goto error;
286 }
287
288 if (pp->function)
289 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
290 offs, pp->retprobe ? "%return" : "", line);
291 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500292 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500293 if (ret <= 0) {
294error:
295 free(pp->probes[0]);
296 pp->probes[0] = NULL;
297 }
298 return ret;
299}
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500300
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500301int synthesize_perf_probe_event(struct probe_point *pp)
302{
303 char *buf;
304 int i, len, ret;
305
306 len = synthesize_perf_probe_point(pp);
307 if (len < 0)
308 return 0;
309
310 buf = pp->probes[0];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500311 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500312 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
313 pp->args[i]);
314 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500315 goto error;
316 len += ret;
317 }
318 pp->found = 1;
319
320 return pp->found;
321error:
322 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500323 pp->probes[0] = NULL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500324
325 return ret;
326}
327
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500328int synthesize_trace_kprobe_event(struct probe_point *pp)
329{
330 char *buf;
331 int i, len, ret;
332
333 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
334 if (!buf)
335 die("Failed to allocate memory by zalloc.");
336 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
337 if (ret <= 0)
338 goto error;
339 len = ret;
340
341 for (i = 0; i < pp->nr_args; i++) {
342 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
343 pp->args[i]);
344 if (ret <= 0)
345 goto error;
346 len += ret;
347 }
348 pp->found = 1;
349
350 return pp->found;
351error:
352 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500353 pp->probes[0] = NULL;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500354
355 return ret;
356}
357
358static int open_kprobe_events(int flags, int mode)
359{
360 char buf[PATH_MAX];
361 int ret;
362
363 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
364 if (ret < 0)
365 die("Failed to make kprobe_events path.");
366
367 ret = open(buf, flags, mode);
368 if (ret < 0) {
369 if (errno == ENOENT)
370 die("kprobe_events file does not exist -"
371 " please rebuild with CONFIG_KPROBE_TRACER.");
372 else
373 die("Could not open kprobe_events file: %s",
374 strerror(errno));
375 }
376 return ret;
377}
378
379/* Get raw string list of current kprobe_events */
380static struct strlist *get_trace_kprobe_event_rawlist(int fd)
381{
382 int ret, idx;
383 FILE *fp;
384 char buf[MAX_CMDLEN];
385 char *p;
386 struct strlist *sl;
387
388 sl = strlist__new(true, NULL);
389
390 fp = fdopen(dup(fd), "r");
391 while (!feof(fp)) {
392 p = fgets(buf, MAX_CMDLEN, fp);
393 if (!p)
394 break;
395
396 idx = strlen(p) - 1;
397 if (p[idx] == '\n')
398 p[idx] = '\0';
399 ret = strlist__add(sl, buf);
400 if (ret < 0)
401 die("strlist__add failed: %s", strerror(-ret));
402 }
403 fclose(fp);
404
405 return sl;
406}
407
408/* Free and zero clear probe_point */
409static void clear_probe_point(struct probe_point *pp)
410{
411 int i;
412
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500413 if (pp->event)
414 free(pp->event);
415 if (pp->group)
416 free(pp->group);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500417 if (pp->function)
418 free(pp->function);
419 if (pp->file)
420 free(pp->file);
421 for (i = 0; i < pp->nr_args; i++)
422 free(pp->args[i]);
423 if (pp->args)
424 free(pp->args);
425 for (i = 0; i < pp->found; i++)
426 free(pp->probes[i]);
Julia Lawall5660ce32009-12-09 20:26:18 +0100427 memset(pp, 0, sizeof(*pp));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500428}
429
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500430/* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500431static void show_perf_probe_event(const char *event, const char *place,
432 struct probe_point *pp)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500433{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500434 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500435 char buf[128];
436
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500437 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500438 if (ret < 0)
439 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500440 printf(" %-40s (on %s", buf, place);
441
442 if (pp->nr_args > 0) {
443 printf(" with");
444 for (i = 0; i < pp->nr_args; i++)
445 printf(" %s", pp->args[i]);
446 }
447 printf(")\n");
448}
449
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500450/* List up current perf-probe events */
451void show_perf_probe_events(void)
452{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500453 int fd;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500454 struct probe_point pp;
455 struct strlist *rawlist;
456 struct str_node *ent;
457
458 fd = open_kprobe_events(O_RDONLY, 0);
459 rawlist = get_trace_kprobe_event_rawlist(fd);
460 close(fd);
461
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500462 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500463 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500464 /* Synthesize only event probe point */
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500465 synthesize_perf_probe_point(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500466 /* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500467 show_perf_probe_event(pp.event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500468 clear_probe_point(&pp);
469 }
470
471 strlist__delete(rawlist);
472}
473
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500474/* Get current perf-probe event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500475static struct strlist *get_perf_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500476{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500477 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500478 struct strlist *sl, *rawlist;
479 struct str_node *ent;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500480 struct probe_point pp;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500481
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500482 memset(&pp, 0, sizeof(pp));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500483 rawlist = get_trace_kprobe_event_rawlist(fd);
484
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500485 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500486 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500487 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500488 if (include_group) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500489 if (e_snprintf(buf, 128, "%s:%s", pp.group,
490 pp.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500491 die("Failed to copy group:event name.");
492 strlist__add(sl, buf);
493 } else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500494 strlist__add(sl, pp.event);
495 clear_probe_point(&pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500496 }
497
498 strlist__delete(rawlist);
499
500 return sl;
501}
502
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500503static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500504{
505 int ret;
506
Masami Hiramatsufa282442009-12-08 17:03:23 -0500507 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500508 ret = write(fd, buf, strlen(buf));
509 if (ret <= 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500510 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500511}
512
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500513static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500514 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500515{
516 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500517
518 /* Try no suffix */
519 ret = e_snprintf(buf, len, "%s", base);
520 if (ret < 0)
521 die("snprintf() failed: %s", strerror(-ret));
522 if (!strlist__has_entry(namelist, buf))
523 return;
524
Masami Hiramatsud761b082009-12-15 10:32:25 -0500525 if (!allow_suffix) {
526 pr_warning("Error: event \"%s\" already exists. "
527 "(Use -f to force duplicates.)\n", base);
528 die("Can't add new event.");
529 }
530
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500531 /* Try to add suffix */
532 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500533 ret = e_snprintf(buf, len, "%s_%d", base, i);
534 if (ret < 0)
535 die("snprintf() failed: %s", strerror(-ret));
536 if (!strlist__has_entry(namelist, buf))
537 break;
538 }
539 if (i == MAX_EVENT_INDEX)
540 die("Too many events are on the same function.");
541}
542
Masami Hiramatsud761b082009-12-15 10:32:25 -0500543void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
544 bool force_add)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500545{
546 int i, j, fd;
547 struct probe_point *pp;
548 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500549 char event[64];
550 struct strlist *namelist;
Masami Hiramatsud761b082009-12-15 10:32:25 -0500551 bool allow_suffix;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500552
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500553 fd = open_kprobe_events(O_RDWR, O_APPEND);
554 /* Get current event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500555 namelist = get_perf_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500556
557 for (j = 0; j < nr_probes; j++) {
558 pp = probes + j;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500559 if (!pp->event)
560 pp->event = strdup(pp->function);
561 if (!pp->group)
562 pp->group = strdup(PERFPROBE_GROUP);
563 DIE_IF(!pp->event || !pp->group);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500564 /* If force_add is true, suffix search is allowed */
565 allow_suffix = force_add;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500566 for (i = 0; i < pp->found; i++) {
567 /* Get an unused new event name */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500568 get_new_event_name(event, 64, pp->event, namelist,
569 allow_suffix);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500570 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
571 pp->retprobe ? 'r' : 'p',
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500572 pp->group, event,
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500573 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500574 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500575 printf("Added new event:\n");
576 /* Get the first parameter (probe-point) */
577 sscanf(pp->probes[i], "%s", buf);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500578 show_perf_probe_event(event, buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500579 /* Add added event name to namelist */
580 strlist__add(namelist, event);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500581 /*
582 * Probes after the first probe which comes from same
583 * user input are always allowed to add suffix, because
584 * there might be several addresses corresponding to
585 * one code line.
586 */
587 allow_suffix = true;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500588 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500589 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500590 /* Show how to use the event. */
591 printf("\nYou can now use it on all perf tools, such as:\n\n");
592 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
593
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500594 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500595 close(fd);
596}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500597
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500598static void __del_trace_kprobe_event(int fd, struct str_node *ent)
599{
600 char *p;
601 char buf[128];
602
603 /* Convert from perf-probe event to trace-kprobe event */
604 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
605 die("Failed to copy event.");
606 p = strchr(buf + 2, ':');
607 if (!p)
608 die("Internal error: %s should have ':' but not.", ent->s);
609 *p = '/';
610
611 write_trace_kprobe_event(fd, buf);
612 printf("Remove event: %s\n", ent->s);
613}
614
Masami Hiramatsufa282442009-12-08 17:03:23 -0500615static void del_trace_kprobe_event(int fd, const char *group,
616 const char *event, struct strlist *namelist)
617{
618 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500619 struct str_node *ent, *n;
620 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500621
622 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
623 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -0500624
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500625 if (strpbrk(buf, "*?")) { /* Glob-exp */
626 strlist__for_each_safe(ent, n, namelist)
627 if (strglobmatch(ent->s, buf)) {
628 found++;
629 __del_trace_kprobe_event(fd, ent);
630 strlist__remove(namelist, ent);
631 }
632 } else {
633 ent = strlist__find(namelist, buf);
634 if (ent) {
635 found++;
636 __del_trace_kprobe_event(fd, ent);
637 strlist__remove(namelist, ent);
638 }
639 }
640 if (found == 0)
641 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500642}
643
644void del_trace_kprobe_events(struct strlist *dellist)
645{
646 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500647 const char *group, *event;
648 char *p, *str;
649 struct str_node *ent;
650 struct strlist *namelist;
651
652 fd = open_kprobe_events(O_RDWR, O_APPEND);
653 /* Get current event names */
654 namelist = get_perf_event_names(fd, true);
655
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500656 strlist__for_each(ent, dellist) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500657 str = strdup(ent->s);
658 if (!str)
659 die("Failed to copy event.");
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500660 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500661 p = strchr(str, ':');
662 if (p) {
663 group = str;
664 *p = '\0';
665 event = p + 1;
666 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500667 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -0500668 event = str;
669 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500670 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500671 del_trace_kprobe_event(fd, group, event, namelist);
672 free(str);
673 }
674 strlist__delete(namelist);
675 close(fd);
676}
677