perf: hw_breakpoints: Fix percpu namespace clash
[deliverable/linux.git] / tools / perf / util / probe-event.c
CommitLineData
50656eec
MH
1/*
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>
4de189fe
MH
32#include <stdarg.h>
33#include <limits.h>
50656eec
MH
34
35#undef _GNU_SOURCE
36#include "event.h"
e1c01d61 37#include "string.h"
4de189fe 38#include "strlist.h"
50656eec
MH
39#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
4de189fe 49/* If there is no space to write, returns -E2BIG. */
84988450
MH
50static int e_snprintf(char *str, size_t size, const char *format, ...)
51 __attribute__((format(printf, 3, 4)));
52
4de189fe
MH
53static int e_snprintf(char *str, size_t size, const char *format, ...)
54{
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
50656eec
MH
65/* Parse probepoint definition. */
66static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
67{
68 char *ptr, *tmp;
69 char c, nc = 0;
70 /*
71 * <Syntax>
72 * perf probe SRC:LN
73 * perf probe FUNC[+OFFS|%return][@SRC]
74 */
75
76 ptr = strpbrk(arg, ":+@%");
77 if (ptr) {
78 nc = *ptr;
79 *ptr++ = '\0';
80 }
81
82 /* Check arg is function or file and copy it */
83 if (strchr(arg, '.')) /* File */
84 pp->file = strdup(arg);
85 else /* Function */
86 pp->function = strdup(arg);
87 DIE_IF(pp->file == NULL && pp->function == NULL);
88
89 /* Parse other options */
90 while (ptr) {
91 arg = ptr;
92 c = nc;
93 ptr = strpbrk(arg, ":+@%");
94 if (ptr) {
95 nc = *ptr;
96 *ptr++ = '\0';
97 }
98 switch (c) {
99 case ':': /* Line number */
100 pp->line = strtoul(arg, &tmp, 0);
101 if (*tmp != '\0')
102 semantic_error("There is non-digit charactor"
103 " in line number.");
104 break;
105 case '+': /* Byte offset from a symbol */
106 pp->offset = strtoul(arg, &tmp, 0);
107 if (*tmp != '\0')
108 semantic_error("There is non-digit charactor"
109 " in offset.");
110 break;
111 case '@': /* File name */
112 if (pp->file)
113 semantic_error("SRC@SRC is not allowed.");
114 pp->file = strdup(arg);
115 DIE_IF(pp->file == NULL);
116 if (ptr)
117 semantic_error("@SRC must be the last "
118 "option.");
119 break;
120 case '%': /* Probe places */
121 if (strcmp(arg, "return") == 0) {
122 pp->retprobe = 1;
123 } else /* Others not supported yet */
124 semantic_error("%%%s is not supported.", arg);
125 break;
126 default:
127 DIE_IF("Program has a bug.");
128 break;
129 }
130 }
131
132 /* Exclusion check */
133 if (pp->line && pp->offset)
134 semantic_error("Offset can't be used with line number.");
135
136 if (!pp->line && pp->file && !pp->function)
137 semantic_error("File always requires line number.");
138
139 if (pp->offset && !pp->function)
140 semantic_error("Offset requires an entry function.");
141
142 if (pp->retprobe && !pp->function)
143 semantic_error("Return probe requires an entry function.");
144
145 if ((pp->offset || pp->line) && pp->retprobe)
146 semantic_error("Offset/Line can't be used with return probe.");
147
148 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
149 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
150}
151
152/* Parse perf-probe event definition */
153int parse_perf_probe_event(const char *str, struct probe_point *pp)
154{
e1c01d61 155 char **argv;
50656eec
MH
156 int argc, i, need_dwarf = 0;
157
e1c01d61
MH
158 argv = argv_split(str, &argc);
159 if (!argv)
160 die("argv_split failed.");
161 if (argc > MAX_PROBE_ARGS + 1)
162 semantic_error("Too many arguments");
50656eec
MH
163
164 /* Parse probe point */
165 parse_perf_probe_probepoint(argv[0], pp);
50656eec
MH
166 if (pp->file || pp->line)
167 need_dwarf = 1;
168
e1c01d61 169 /* Copy arguments and ensure return probe has no C argument */
50656eec 170 pp->nr_args = argc - 1;
e1c01d61
MH
171 pp->args = zalloc(sizeof(char *) * pp->nr_args);
172 for (i = 0; i < pp->nr_args; i++) {
173 pp->args[i] = strdup(argv[i + 1]);
174 if (!pp->args[i])
175 die("Failed to copy argument.");
50656eec
MH
176 if (is_c_varname(pp->args[i])) {
177 if (pp->retprobe)
178 semantic_error("You can't specify local"
179 " variable for kretprobe");
180 need_dwarf = 1;
181 }
e1c01d61 182 }
50656eec 183
e1c01d61 184 argv_free(argv);
50656eec
MH
185 return need_dwarf;
186}
187
4de189fe
MH
188/* Parse kprobe_events event into struct probe_point */
189void parse_trace_kprobe_event(const char *str, char **group, char **event,
190 struct probe_point *pp)
191{
192 char pr;
193 char *p;
194 int ret, i, argc;
195 char **argv;
196
197 pr_debug("Parsing kprobe_events: %s\n", str);
198 argv = argv_split(str, &argc);
199 if (!argv)
200 die("argv_split failed.");
201 if (argc < 2)
202 semantic_error("Too less arguments.");
203
204 /* Scan event and group name. */
93aaa45a
LW
205 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
206 &pr, (float *)(void *)group, (float *)(void *)event);
4de189fe
MH
207 if (ret != 3)
208 semantic_error("Failed to parse event name: %s", argv[0]);
209 pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr);
210
211 if (!pp)
212 goto end;
213
214 pp->retprobe = (pr == 'r');
215
216 /* Scan function name and offset */
93aaa45a 217 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset);
4de189fe
MH
218 if (ret == 1)
219 pp->offset = 0;
220
221 /* kprobe_events doesn't have this information */
222 pp->line = 0;
223 pp->file = NULL;
224
225 pp->nr_args = argc - 2;
226 pp->args = zalloc(sizeof(char *) * pp->nr_args);
227 for (i = 0; i < pp->nr_args; i++) {
228 p = strchr(argv[i + 2], '=');
229 if (p) /* We don't need which register is assigned. */
230 *p = '\0';
231 pp->args[i] = strdup(argv[i + 2]);
232 if (!pp->args[i])
233 die("Failed to copy argument.");
234 }
235
236end:
237 argv_free(argv);
238}
239
240int synthesize_perf_probe_event(struct probe_point *pp)
241{
242 char *buf;
243 char offs[64] = "", line[64] = "";
244 int i, len, ret;
245
246 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
247 if (!buf)
248 die("Failed to allocate memory by zalloc.");
249 if (pp->offset) {
250 ret = e_snprintf(offs, 64, "+%d", pp->offset);
251 if (ret <= 0)
252 goto error;
253 }
254 if (pp->line) {
255 ret = e_snprintf(line, 64, ":%d", pp->line);
256 if (ret <= 0)
257 goto error;
258 }
259
260 if (pp->function)
261 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
262 offs, pp->retprobe ? "%return" : "", line);
263 else
84988450 264 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
4de189fe
MH
265 if (ret <= 0)
266 goto error;
267 len = ret;
268
269 for (i = 0; i < pp->nr_args; i++) {
270 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
271 pp->args[i]);
272 if (ret <= 0)
273 goto error;
274 len += ret;
275 }
276 pp->found = 1;
277
278 return pp->found;
279error:
280 free(pp->probes[0]);
281
282 return ret;
283}
284
50656eec
MH
285int synthesize_trace_kprobe_event(struct probe_point *pp)
286{
287 char *buf;
288 int i, len, ret;
289
290 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
291 if (!buf)
292 die("Failed to allocate memory by zalloc.");
4de189fe
MH
293 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
294 if (ret <= 0)
50656eec
MH
295 goto error;
296 len = ret;
297
298 for (i = 0; i < pp->nr_args; i++) {
4de189fe
MH
299 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
300 pp->args[i]);
301 if (ret <= 0)
50656eec
MH
302 goto error;
303 len += ret;
304 }
305 pp->found = 1;
306
307 return pp->found;
308error:
309 free(pp->probes[0]);
50656eec
MH
310
311 return ret;
312}
313
4de189fe
MH
314static int open_kprobe_events(int flags, int mode)
315{
316 char buf[PATH_MAX];
317 int ret;
318
319 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
320 if (ret < 0)
321 die("Failed to make kprobe_events path.");
322
323 ret = open(buf, flags, mode);
324 if (ret < 0) {
325 if (errno == ENOENT)
326 die("kprobe_events file does not exist -"
327 " please rebuild with CONFIG_KPROBE_TRACER.");
328 else
329 die("Could not open kprobe_events file: %s",
330 strerror(errno));
331 }
332 return ret;
333}
334
335/* Get raw string list of current kprobe_events */
336static struct strlist *get_trace_kprobe_event_rawlist(int fd)
337{
338 int ret, idx;
339 FILE *fp;
340 char buf[MAX_CMDLEN];
341 char *p;
342 struct strlist *sl;
343
344 sl = strlist__new(true, NULL);
345
346 fp = fdopen(dup(fd), "r");
347 while (!feof(fp)) {
348 p = fgets(buf, MAX_CMDLEN, fp);
349 if (!p)
350 break;
351
352 idx = strlen(p) - 1;
353 if (p[idx] == '\n')
354 p[idx] = '\0';
355 ret = strlist__add(sl, buf);
356 if (ret < 0)
357 die("strlist__add failed: %s", strerror(-ret));
358 }
359 fclose(fp);
360
361 return sl;
362}
363
364/* Free and zero clear probe_point */
365static void clear_probe_point(struct probe_point *pp)
366{
367 int i;
368
369 if (pp->function)
370 free(pp->function);
371 if (pp->file)
372 free(pp->file);
373 for (i = 0; i < pp->nr_args; i++)
374 free(pp->args[i]);
375 if (pp->args)
376 free(pp->args);
377 for (i = 0; i < pp->found; i++)
378 free(pp->probes[i]);
379 memset(pp, 0, sizeof(pp));
380}
381
382/* List up current perf-probe events */
383void show_perf_probe_events(void)
384{
385 unsigned int i;
386 int fd;
387 char *group, *event;
388 struct probe_point pp;
389 struct strlist *rawlist;
390 struct str_node *ent;
391
392 fd = open_kprobe_events(O_RDONLY, 0);
393 rawlist = get_trace_kprobe_event_rawlist(fd);
394 close(fd);
395
396 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
397 ent = strlist__entry(rawlist, i);
398 parse_trace_kprobe_event(ent->s, &group, &event, &pp);
399 synthesize_perf_probe_event(&pp);
400 printf("[%s:%s]\t%s\n", group, event, pp.probes[0]);
401 free(group);
402 free(event);
403 clear_probe_point(&pp);
404 }
405
406 strlist__delete(rawlist);
407}
408
b498ce1f
MH
409/* Get current perf-probe event names */
410static struct strlist *get_perf_event_names(int fd)
411{
412 unsigned int i;
413 char *group, *event;
414 struct strlist *sl, *rawlist;
415 struct str_node *ent;
416
417 rawlist = get_trace_kprobe_event_rawlist(fd);
418
e1d2017b 419 sl = strlist__new(true, NULL);
b498ce1f
MH
420 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
421 ent = strlist__entry(rawlist, i);
422 parse_trace_kprobe_event(ent->s, &group, &event, NULL);
423 strlist__add(sl, event);
424 free(group);
e1d2017b 425 free(event);
b498ce1f
MH
426 }
427
428 strlist__delete(rawlist);
429
430 return sl;
431}
432
50656eec
MH
433static int write_trace_kprobe_event(int fd, const char *buf)
434{
435 int ret;
436
437 ret = write(fd, buf, strlen(buf));
438 if (ret <= 0)
439 die("Failed to create event.");
440 else
441 printf("Added new event: %s\n", buf);
442
443 return ret;
444}
445
b498ce1f
MH
446static void get_new_event_name(char *buf, size_t len, const char *base,
447 struct strlist *namelist)
448{
449 int i, ret;
450 for (i = 0; i < MAX_EVENT_INDEX; i++) {
451 ret = e_snprintf(buf, len, "%s_%d", base, i);
452 if (ret < 0)
453 die("snprintf() failed: %s", strerror(-ret));
454 if (!strlist__has_entry(namelist, buf))
455 break;
456 }
457 if (i == MAX_EVENT_INDEX)
458 die("Too many events are on the same function.");
459}
460
50656eec
MH
461void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
462{
463 int i, j, fd;
464 struct probe_point *pp;
465 char buf[MAX_CMDLEN];
b498ce1f
MH
466 char event[64];
467 struct strlist *namelist;
50656eec 468
b498ce1f
MH
469 fd = open_kprobe_events(O_RDWR, O_APPEND);
470 /* Get current event names */
471 namelist = get_perf_event_names(fd);
50656eec
MH
472
473 for (j = 0; j < nr_probes; j++) {
474 pp = probes + j;
b498ce1f
MH
475 for (i = 0; i < pp->found; i++) {
476 /* Get an unused new event name */
477 get_new_event_name(event, 64, pp->function, namelist);
478 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
479 pp->retprobe ? 'r' : 'p',
480 PERFPROBE_GROUP, event,
481 pp->probes[i]);
50656eec 482 write_trace_kprobe_event(fd, buf);
b498ce1f
MH
483 /* Add added event name to namelist */
484 strlist__add(namelist, event);
485 }
50656eec 486 }
e1d2017b 487 strlist__delete(namelist);
50656eec
MH
488 close(fd);
489}
This page took 0.045838 seconds and 5 git commands to generate.