blob: 2ca62154f79b8c49c9973ea732449fd9893c29df [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>
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050072 * perf probe [EVENT=]SRC:LN
73 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
74 *
75 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050076 */
77
Masami Hiramatsuaf663d72009-12-15 10:32:18 -050078 ptr = strchr(arg, '=');
79 if (ptr) { /* Event name */
80 *ptr = '\0';
81 tmp = ptr + 1;
82 ptr = strchr(arg, ':');
83 if (ptr) /* Group name is not supported yet. */
84 semantic_error("Group name is not supported yet.");
85 pp->event = strdup(arg);
86 arg = tmp;
87 }
88
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050089 ptr = strpbrk(arg, ":+@%");
90 if (ptr) {
91 nc = *ptr;
92 *ptr++ = '\0';
93 }
94
95 /* Check arg is function or file and copy it */
96 if (strchr(arg, '.')) /* File */
97 pp->file = strdup(arg);
98 else /* Function */
99 pp->function = strdup(arg);
100 DIE_IF(pp->file == NULL && pp->function == NULL);
101
102 /* Parse other options */
103 while (ptr) {
104 arg = ptr;
105 c = nc;
106 ptr = strpbrk(arg, ":+@%");
107 if (ptr) {
108 nc = *ptr;
109 *ptr++ = '\0';
110 }
111 switch (c) {
112 case ':': /* Line number */
113 pp->line = strtoul(arg, &tmp, 0);
114 if (*tmp != '\0')
115 semantic_error("There is non-digit charactor"
116 " in line number.");
117 break;
118 case '+': /* Byte offset from a symbol */
119 pp->offset = strtoul(arg, &tmp, 0);
120 if (*tmp != '\0')
121 semantic_error("There is non-digit charactor"
122 " in offset.");
123 break;
124 case '@': /* File name */
125 if (pp->file)
126 semantic_error("SRC@SRC is not allowed.");
127 pp->file = strdup(arg);
128 DIE_IF(pp->file == NULL);
129 if (ptr)
130 semantic_error("@SRC must be the last "
131 "option.");
132 break;
133 case '%': /* Probe places */
134 if (strcmp(arg, "return") == 0) {
135 pp->retprobe = 1;
136 } else /* Others not supported yet */
137 semantic_error("%%%s is not supported.", arg);
138 break;
139 default:
140 DIE_IF("Program has a bug.");
141 break;
142 }
143 }
144
145 /* Exclusion check */
146 if (pp->line && pp->offset)
147 semantic_error("Offset can't be used with line number.");
148
149 if (!pp->line && pp->file && !pp->function)
150 semantic_error("File always requires line number.");
151
152 if (pp->offset && !pp->function)
153 semantic_error("Offset requires an entry function.");
154
155 if (pp->retprobe && !pp->function)
156 semantic_error("Return probe requires an entry function.");
157
158 if ((pp->offset || pp->line) && pp->retprobe)
159 semantic_error("Offset/Line can't be used with return probe.");
160
161 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
162 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
163}
164
165/* Parse perf-probe event definition */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500166void parse_perf_probe_event(const char *str, struct probe_point *pp,
167 bool *need_dwarf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500168{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500169 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500170 int argc, i;
171
172 *need_dwarf = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500173
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500174 argv = argv_split(str, &argc);
175 if (!argv)
176 die("argv_split failed.");
177 if (argc > MAX_PROBE_ARGS + 1)
178 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500179
180 /* Parse probe point */
181 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500182 if (pp->file || pp->line)
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500183 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500184
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500185 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500186 pp->nr_args = argc - 1;
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500187 pp->args = zalloc(sizeof(char *) * pp->nr_args);
188 for (i = 0; i < pp->nr_args; i++) {
189 pp->args[i] = strdup(argv[i + 1]);
190 if (!pp->args[i])
191 die("Failed to copy argument.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500192 if (is_c_varname(pp->args[i])) {
193 if (pp->retprobe)
194 semantic_error("You can't specify local"
195 " variable for kretprobe");
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500196 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500197 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500198 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500199
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500200 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500201}
202
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500203/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500204void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500205{
206 char pr;
207 char *p;
208 int ret, i, argc;
209 char **argv;
210
211 pr_debug("Parsing kprobe_events: %s\n", str);
212 argv = argv_split(str, &argc);
213 if (!argv)
214 die("argv_split failed.");
215 if (argc < 2)
216 semantic_error("Too less arguments.");
217
218 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800219 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500220 &pr, (float *)(void *)&pp->group,
221 (float *)(void *)&pp->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500222 if (ret != 3)
223 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500224 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500225
226 pp->retprobe = (pr == 'r');
227
228 /* Scan function name and offset */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500229 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
230 &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500231 if (ret == 1)
232 pp->offset = 0;
233
234 /* kprobe_events doesn't have this information */
235 pp->line = 0;
236 pp->file = NULL;
237
238 pp->nr_args = argc - 2;
239 pp->args = zalloc(sizeof(char *) * pp->nr_args);
240 for (i = 0; i < pp->nr_args; i++) {
241 p = strchr(argv[i + 2], '=');
242 if (p) /* We don't need which register is assigned. */
243 *p = '\0';
244 pp->args[i] = strdup(argv[i + 2]);
245 if (!pp->args[i])
246 die("Failed to copy argument.");
247 }
248
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500249 argv_free(argv);
250}
251
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500252/* Synthesize only probe point (not argument) */
253int synthesize_perf_probe_point(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500254{
255 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500256 char offs[64] = "", line[64] = "";
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500257 int ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500258
259 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
260 if (!buf)
261 die("Failed to allocate memory by zalloc.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500262 if (pp->offset) {
263 ret = e_snprintf(offs, 64, "+%d", pp->offset);
264 if (ret <= 0)
265 goto error;
266 }
267 if (pp->line) {
268 ret = e_snprintf(line, 64, ":%d", pp->line);
269 if (ret <= 0)
270 goto error;
271 }
272
273 if (pp->function)
274 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
275 offs, pp->retprobe ? "%return" : "", line);
276 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500277 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500278 if (ret <= 0) {
279error:
280 free(pp->probes[0]);
281 pp->probes[0] = NULL;
282 }
283 return ret;
284}
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500285
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500286int synthesize_perf_probe_event(struct probe_point *pp)
287{
288 char *buf;
289 int i, len, ret;
290
291 len = synthesize_perf_probe_point(pp);
292 if (len < 0)
293 return 0;
294
295 buf = pp->probes[0];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500296 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500297 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
298 pp->args[i]);
299 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500300 goto error;
301 len += ret;
302 }
303 pp->found = 1;
304
305 return pp->found;
306error:
307 free(pp->probes[0]);
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500308 pp->probes[0] = NULL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500309
310 return ret;
311}
312
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500313int synthesize_trace_kprobe_event(struct probe_point *pp)
314{
315 char *buf;
316 int i, len, ret;
317
318 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
319 if (!buf)
320 die("Failed to allocate memory by zalloc.");
321 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
322 if (ret <= 0)
323 goto error;
324 len = ret;
325
326 for (i = 0; i < pp->nr_args; i++) {
327 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
328 pp->args[i]);
329 if (ret <= 0)
330 goto error;
331 len += ret;
332 }
333 pp->found = 1;
334
335 return pp->found;
336error:
337 free(pp->probes[0]);
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500338 pp->probes[0] = NULL;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500339
340 return ret;
341}
342
343static int open_kprobe_events(int flags, int mode)
344{
345 char buf[PATH_MAX];
346 int ret;
347
348 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
349 if (ret < 0)
350 die("Failed to make kprobe_events path.");
351
352 ret = open(buf, flags, mode);
353 if (ret < 0) {
354 if (errno == ENOENT)
355 die("kprobe_events file does not exist -"
356 " please rebuild with CONFIG_KPROBE_TRACER.");
357 else
358 die("Could not open kprobe_events file: %s",
359 strerror(errno));
360 }
361 return ret;
362}
363
364/* Get raw string list of current kprobe_events */
365static struct strlist *get_trace_kprobe_event_rawlist(int fd)
366{
367 int ret, idx;
368 FILE *fp;
369 char buf[MAX_CMDLEN];
370 char *p;
371 struct strlist *sl;
372
373 sl = strlist__new(true, NULL);
374
375 fp = fdopen(dup(fd), "r");
376 while (!feof(fp)) {
377 p = fgets(buf, MAX_CMDLEN, fp);
378 if (!p)
379 break;
380
381 idx = strlen(p) - 1;
382 if (p[idx] == '\n')
383 p[idx] = '\0';
384 ret = strlist__add(sl, buf);
385 if (ret < 0)
386 die("strlist__add failed: %s", strerror(-ret));
387 }
388 fclose(fp);
389
390 return sl;
391}
392
393/* Free and zero clear probe_point */
394static void clear_probe_point(struct probe_point *pp)
395{
396 int i;
397
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500398 if (pp->event)
399 free(pp->event);
400 if (pp->group)
401 free(pp->group);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500402 if (pp->function)
403 free(pp->function);
404 if (pp->file)
405 free(pp->file);
406 for (i = 0; i < pp->nr_args; i++)
407 free(pp->args[i]);
408 if (pp->args)
409 free(pp->args);
410 for (i = 0; i < pp->found; i++)
411 free(pp->probes[i]);
Julia Lawall5660ce32009-12-09 20:26:18 +0100412 memset(pp, 0, sizeof(*pp));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500413}
414
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500415/* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500416static void show_perf_probe_event(const char *event, const char *place,
417 struct probe_point *pp)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500418{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500419 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500420 char buf[128];
421
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500422 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500423 if (ret < 0)
424 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500425 printf(" %-40s (on %s", buf, place);
426
427 if (pp->nr_args > 0) {
428 printf(" with");
429 for (i = 0; i < pp->nr_args; i++)
430 printf(" %s", pp->args[i]);
431 }
432 printf(")\n");
433}
434
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500435/* List up current perf-probe events */
436void show_perf_probe_events(void)
437{
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500438 int fd;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500439 struct probe_point pp;
440 struct strlist *rawlist;
441 struct str_node *ent;
442
443 fd = open_kprobe_events(O_RDONLY, 0);
444 rawlist = get_trace_kprobe_event_rawlist(fd);
445 close(fd);
446
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500447 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500448 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500449 /* Synthesize only event probe point */
Masami Hiramatsu7ef17aaf2009-12-15 10:32:47 -0500450 synthesize_perf_probe_point(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500451 /* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500452 show_perf_probe_event(pp.event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500453 clear_probe_point(&pp);
454 }
455
456 strlist__delete(rawlist);
457}
458
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500459/* Get current perf-probe event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500460static struct strlist *get_perf_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500461{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500462 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500463 struct strlist *sl, *rawlist;
464 struct str_node *ent;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500465 struct probe_point pp;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500466
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500467 memset(&pp, 0, sizeof(pp));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500468 rawlist = get_trace_kprobe_event_rawlist(fd);
469
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500470 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500471 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500472 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500473 if (include_group) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500474 if (e_snprintf(buf, 128, "%s:%s", pp.group,
475 pp.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500476 die("Failed to copy group:event name.");
477 strlist__add(sl, buf);
478 } else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500479 strlist__add(sl, pp.event);
480 clear_probe_point(&pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500481 }
482
483 strlist__delete(rawlist);
484
485 return sl;
486}
487
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500488static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500489{
490 int ret;
491
Masami Hiramatsufa282442009-12-08 17:03:23 -0500492 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500493 ret = write(fd, buf, strlen(buf));
494 if (ret <= 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500495 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500496}
497
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500498static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500499 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500500{
501 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500502
503 /* Try no suffix */
504 ret = e_snprintf(buf, len, "%s", base);
505 if (ret < 0)
506 die("snprintf() failed: %s", strerror(-ret));
507 if (!strlist__has_entry(namelist, buf))
508 return;
509
Masami Hiramatsud761b082009-12-15 10:32:25 -0500510 if (!allow_suffix) {
511 pr_warning("Error: event \"%s\" already exists. "
512 "(Use -f to force duplicates.)\n", base);
513 die("Can't add new event.");
514 }
515
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500516 /* Try to add suffix */
517 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500518 ret = e_snprintf(buf, len, "%s_%d", base, i);
519 if (ret < 0)
520 die("snprintf() failed: %s", strerror(-ret));
521 if (!strlist__has_entry(namelist, buf))
522 break;
523 }
524 if (i == MAX_EVENT_INDEX)
525 die("Too many events are on the same function.");
526}
527
Masami Hiramatsud761b082009-12-15 10:32:25 -0500528void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
529 bool force_add)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500530{
531 int i, j, fd;
532 struct probe_point *pp;
533 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500534 char event[64];
535 struct strlist *namelist;
Masami Hiramatsud761b082009-12-15 10:32:25 -0500536 bool allow_suffix;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500537
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500538 fd = open_kprobe_events(O_RDWR, O_APPEND);
539 /* Get current event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500540 namelist = get_perf_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500541
542 for (j = 0; j < nr_probes; j++) {
543 pp = probes + j;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500544 if (!pp->event)
545 pp->event = strdup(pp->function);
546 if (!pp->group)
547 pp->group = strdup(PERFPROBE_GROUP);
548 DIE_IF(!pp->event || !pp->group);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500549 /* If force_add is true, suffix search is allowed */
550 allow_suffix = force_add;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500551 for (i = 0; i < pp->found; i++) {
552 /* Get an unused new event name */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500553 get_new_event_name(event, 64, pp->event, namelist,
554 allow_suffix);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500555 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
556 pp->retprobe ? 'r' : 'p',
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500557 pp->group, event,
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500558 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500559 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500560 printf("Added new event:\n");
561 /* Get the first parameter (probe-point) */
562 sscanf(pp->probes[i], "%s", buf);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500563 show_perf_probe_event(event, buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500564 /* Add added event name to namelist */
565 strlist__add(namelist, event);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500566 /*
567 * Probes after the first probe which comes from same
568 * user input are always allowed to add suffix, because
569 * there might be several addresses corresponding to
570 * one code line.
571 */
572 allow_suffix = true;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500573 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500574 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500575 /* Show how to use the event. */
576 printf("\nYou can now use it on all perf tools, such as:\n\n");
577 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
578
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500579 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500580 close(fd);
581}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500582
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500583static void __del_trace_kprobe_event(int fd, struct str_node *ent)
584{
585 char *p;
586 char buf[128];
587
588 /* Convert from perf-probe event to trace-kprobe event */
589 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
590 die("Failed to copy event.");
591 p = strchr(buf + 2, ':');
592 if (!p)
593 die("Internal error: %s should have ':' but not.", ent->s);
594 *p = '/';
595
596 write_trace_kprobe_event(fd, buf);
597 printf("Remove event: %s\n", ent->s);
598}
599
Masami Hiramatsufa282442009-12-08 17:03:23 -0500600static void del_trace_kprobe_event(int fd, const char *group,
601 const char *event, struct strlist *namelist)
602{
603 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500604 struct str_node *ent, *n;
605 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500606
607 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
608 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -0500609
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500610 if (strpbrk(buf, "*?")) { /* Glob-exp */
611 strlist__for_each_safe(ent, n, namelist)
612 if (strglobmatch(ent->s, buf)) {
613 found++;
614 __del_trace_kprobe_event(fd, ent);
615 strlist__remove(namelist, ent);
616 }
617 } else {
618 ent = strlist__find(namelist, buf);
619 if (ent) {
620 found++;
621 __del_trace_kprobe_event(fd, ent);
622 strlist__remove(namelist, ent);
623 }
624 }
625 if (found == 0)
626 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500627}
628
629void del_trace_kprobe_events(struct strlist *dellist)
630{
631 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500632 const char *group, *event;
633 char *p, *str;
634 struct str_node *ent;
635 struct strlist *namelist;
636
637 fd = open_kprobe_events(O_RDWR, O_APPEND);
638 /* Get current event names */
639 namelist = get_perf_event_names(fd, true);
640
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500641 strlist__for_each(ent, dellist) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500642 str = strdup(ent->s);
643 if (!str)
644 die("Failed to copy event.");
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500645 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500646 p = strchr(str, ':');
647 if (p) {
648 group = str;
649 *p = '\0';
650 event = p + 1;
651 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500652 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -0500653 event = str;
654 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500655 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500656 del_trace_kprobe_event(fd, group, event, namelist);
657 free(str);
658 }
659 strlist__delete(namelist);
660 close(fd);
661}
662