perf record: Allow filtering perf's pid via --exclude-perf
[linux-2.6-block.git] / tools / perf / util / probe-event.c
CommitLineData
50656eec 1/*
0e60836b 2 * probe-event.c : perf-probe definition to probe_events format converter
50656eec
MH
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
50656eec
MH
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
4de189fe
MH
31#include <stdarg.h>
32#include <limits.h>
e80711ca 33#include <elf.h>
50656eec 34
31facc5f 35#include "util.h"
50656eec 36#include "event.h"
4de189fe 37#include "strlist.h"
50656eec 38#include "debug.h"
72041334 39#include "cache.h"
631c9def 40#include "color.h"
e0faa8d3
MH
41#include "symbol.h"
42#include "thread.h"
553873e1 43#include <api/fs/debugfs.h>
23773ca1 44#include <api/fs/tracefs.h>
1d037ca1 45#include "trace-event.h" /* For __maybe_unused */
50656eec 46#include "probe-event.h"
4235b045 47#include "probe-finder.h"
225466f1 48#include "session.h"
50656eec
MH
49
50#define MAX_CMDLEN 256
50656eec
MH
51#define PERFPROBE_GROUP "probe"
52
f4d7da49 53bool probe_event_dry_run; /* Dry run flag */
ddb2f58f 54struct probe_conf probe_conf;
f4d7da49 55
146a1439 56#define semantic_error(msg ...) pr_err("Semantic error :" msg)
50656eec 57
4de189fe 58/* If there is no space to write, returns -E2BIG. */
84988450
MH
59static int e_snprintf(char *str, size_t size, const char *format, ...)
60 __attribute__((format(printf, 3, 4)));
61
4de189fe
MH
62static int e_snprintf(char *str, size_t size, const char *format, ...)
63{
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
72}
73
4b4da7f7 74static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
981d05ad 75static void clear_probe_trace_event(struct probe_trace_event *tev);
ee45b6c2 76static struct machine *host_machine;
e0faa8d3 77
469b9b88 78/* Initialize symbol maps and path of vmlinux/modules */
ee45b6c2 79static int init_symbol_maps(bool user_only)
e0faa8d3 80{
146a1439
MH
81 int ret;
82
e0faa8d3 83 symbol_conf.sort_by_name = true;
680d926a 84 symbol_conf.allow_aliases = true;
0a7e6d1b 85 ret = symbol__init(NULL);
146a1439
MH
86 if (ret < 0) {
87 pr_debug("Failed to init symbol map.\n");
88 goto out;
89 }
e0faa8d3 90
ee45b6c2
MH
91 if (host_machine || user_only) /* already initialized */
92 return 0;
d28c6223 93
ee45b6c2
MH
94 if (symbol_conf.vmlinux_name)
95 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
96
97 host_machine = machine__new_host();
98 if (!host_machine) {
99 pr_debug("machine__new_host() failed.\n");
100 symbol__exit();
101 ret = -1;
469b9b88 102 }
146a1439
MH
103out:
104 if (ret < 0)
105 pr_warning("Failed to init vmlinux path.\n");
106 return ret;
e0faa8d3
MH
107}
108
ee45b6c2
MH
109static void exit_symbol_maps(void)
110{
111 if (host_machine) {
112 machine__delete(host_machine);
113 host_machine = NULL;
114 }
115 symbol__exit();
116}
117
469b9b88
MH
118static struct symbol *__find_kernel_function_by_name(const char *name,
119 struct map **mapp)
120{
ee45b6c2 121 return machine__find_kernel_function_by_name(host_machine, name, mapp,
469b9b88
MH
122 NULL);
123}
124
8f33f7de
MH
125static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
126{
127 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
128}
129
130static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
131{
132 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
133 struct kmap *kmap;
134
135 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
136 return NULL;
137
138 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
ba92732e
WN
139 if (!kmap)
140 return NULL;
8f33f7de
MH
141 return kmap->ref_reloc_sym;
142}
143
144static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
145{
146 struct ref_reloc_sym *reloc_sym;
147 struct symbol *sym;
148 struct map *map;
149
150 /* ref_reloc_sym is just a label. Need a special fix*/
151 reloc_sym = kernel_get_ref_reloc_sym();
152 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
153 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
154 else {
155 sym = __find_kernel_function_by_name(name, &map);
156 if (sym)
157 return map->unmap_ip(map, sym->start) -
f56847c2 158 ((reloc) ? 0 : map->reloc);
8f33f7de
MH
159 }
160 return 0;
161}
162
e80711ca
MH
163static struct map *kernel_get_module_map(const char *module)
164{
ee45b6c2 165 struct map_groups *grp = &host_machine->kmaps;
1eee78ae 166 struct maps *maps = &grp->maps[MAP__FUNCTION];
4bb7123d 167 struct map *pos;
e80711ca 168
14a8fd7c
MH
169 /* A file path -- this is an offline module */
170 if (module && strchr(module, '/'))
9f2de315 171 return machine__findnew_module_map(host_machine, 0, module);
14a8fd7c 172
e80711ca
MH
173 if (!module)
174 module = "kernel";
175
4bb7123d 176 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
e80711ca
MH
177 if (strncmp(pos->dso->short_name + 1, module,
178 pos->dso->short_name_len - 2) == 0) {
179 return pos;
180 }
181 }
182 return NULL;
183}
184
9b118aca
MH
185static struct map *get_target_map(const char *target, bool user)
186{
187 /* Init maps of given executable or kernel */
188 if (user)
189 return dso__new_map(target);
190 else
191 return kernel_get_module_map(target);
192}
193
194static void put_target_map(struct map *map, bool user)
195{
196 if (map && user) {
197 /* Only the user map needs to be released */
84c2cafa 198 map__put(map);
9b118aca
MH
199 }
200}
201
202
fb7345bb
MH
203static int convert_exec_to_group(const char *exec, char **result)
204{
205 char *ptr1, *ptr2, *exec_copy;
206 char buf[64];
207 int ret;
208
209 exec_copy = strdup(exec);
210 if (!exec_copy)
211 return -ENOMEM;
212
213 ptr1 = basename(exec_copy);
214 if (!ptr1) {
215 ret = -EINVAL;
216 goto out;
217 }
218
219 ptr2 = strpbrk(ptr1, "-._");
220 if (ptr2)
221 *ptr2 = '\0';
222 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
223 if (ret < 0)
224 goto out;
225
226 *result = strdup(buf);
227 ret = *result ? 0 : -ENOMEM;
228
229out:
230 free(exec_copy);
231 return ret;
232}
233
9b118aca
MH
234static void clear_perf_probe_point(struct perf_probe_point *pp)
235{
236 free(pp->file);
237 free(pp->function);
238 free(pp->lazy_line);
239}
240
eb948e50
MH
241static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
242{
243 int i;
244
245 for (i = 0; i < ntevs; i++)
246 clear_probe_trace_event(tevs + i);
247}
248
b031220d
MH
249static bool kprobe_blacklist__listed(unsigned long address);
250static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
251{
7c31bb8c
HK
252 u64 etext_addr;
253
b031220d 254 /* Get the address of _etext for checking non-probable text symbol */
7c31bb8c
HK
255 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
256
257 if (etext_addr != 0 && etext_addr < address)
b031220d
MH
258 pr_warning("%s is out of .text, skip it.\n", symbol);
259 else if (kprobe_blacklist__listed(address))
260 pr_warning("%s is blacklisted function, skip it.\n", symbol);
261 else
262 return false;
263
264 return true;
265}
266
89fe808a 267#ifdef HAVE_DWARF_SUPPORT
60fb7742
WN
268
269static int kernel_get_module_dso(const char *module, struct dso **pdso)
270{
271 struct dso *dso;
272 struct map *map;
273 const char *vmlinux_name;
274 int ret = 0;
275
276 if (module) {
3d39ac53
ACM
277 list_for_each_entry(dso, &host_machine->dsos.head, node) {
278 if (!dso->kernel)
279 continue;
60fb7742
WN
280 if (strncmp(dso->short_name + 1, module,
281 dso->short_name_len - 2) == 0)
282 goto found;
283 }
284 pr_debug("Failed to find module %s.\n", module);
285 return -ENOENT;
286 }
287
288 map = host_machine->vmlinux_maps[MAP__FUNCTION];
289 dso = map->dso;
290
291 vmlinux_name = symbol_conf.vmlinux_name;
292 dso->load_errno = 0;
293 if (vmlinux_name)
294 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
295 else
296 ret = dso__load_vmlinux_path(dso, map, NULL);
297found:
298 *pdso = dso;
299 return ret;
300}
301
9b118aca
MH
302/*
303 * Some binaries like glibc have special symbols which are on the symbol
304 * table, but not in the debuginfo. If we can find the address of the
305 * symbol from map, we can translate the address back to the probe point.
306 */
307static int find_alternative_probe_point(struct debuginfo *dinfo,
308 struct perf_probe_point *pp,
309 struct perf_probe_point *result,
310 const char *target, bool uprobes)
311{
312 struct map *map = NULL;
313 struct symbol *sym;
314 u64 address = 0;
315 int ret = -ENOENT;
316
317 /* This can work only for function-name based one */
318 if (!pp->function || pp->file)
319 return -ENOTSUP;
320
321 map = get_target_map(target, uprobes);
322 if (!map)
323 return -EINVAL;
324
325 /* Find the address of given function */
326 map__for_each_symbol_by_name(map, pp->function, sym) {
e6d7c91c
MH
327 if (uprobes)
328 address = sym->start;
329 else
330 address = map->unmap_ip(map, sym->start);
e578da3b 331 break;
9b118aca
MH
332 }
333 if (!address) {
334 ret = -ENOENT;
335 goto out;
336 }
f6c15621
WN
337 pr_debug("Symbol %s address found : %" PRIx64 "\n",
338 pp->function, address);
9b118aca
MH
339
340 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
341 result);
342 if (ret <= 0)
343 ret = (!ret) ? -ENOENT : ret;
344 else {
345 result->offset += pp->offset;
346 result->line += pp->line;
9d7b45c5 347 result->retprobe = pp->retprobe;
9b118aca
MH
348 ret = 0;
349 }
350
351out:
352 put_target_map(map, uprobes);
353 return ret;
354
355}
356
357static int get_alternative_probe_event(struct debuginfo *dinfo,
358 struct perf_probe_event *pev,
44225521 359 struct perf_probe_point *tmp)
9b118aca
MH
360{
361 int ret;
362
363 memcpy(tmp, &pev->point, sizeof(*tmp));
364 memset(&pev->point, 0, sizeof(pev->point));
365 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
44225521 366 pev->target, pev->uprobes);
9b118aca
MH
367 if (ret < 0)
368 memcpy(&pev->point, tmp, sizeof(*tmp));
369
370 return ret;
371}
a15ad2f5 372
811dd2ae
MH
373static int get_alternative_line_range(struct debuginfo *dinfo,
374 struct line_range *lr,
375 const char *target, bool user)
376{
6d4a4896
DA
377 struct perf_probe_point pp = { .function = lr->function,
378 .file = lr->file,
379 .line = lr->start };
380 struct perf_probe_point result;
811dd2ae
MH
381 int ret, len = 0;
382
6d4a4896
DA
383 memset(&result, 0, sizeof(result));
384
811dd2ae
MH
385 if (lr->end != INT_MAX)
386 len = lr->end - lr->start;
387 ret = find_alternative_probe_point(dinfo, &pp, &result,
388 target, user);
389 if (!ret) {
390 lr->function = result.function;
391 lr->file = result.file;
392 lr->start = result.line;
393 if (lr->end != INT_MAX)
394 lr->end = lr->start + len;
395 clear_perf_probe_point(&pp);
396 }
397 return ret;
398}
399
ff741783 400/* Open new debuginfo of given module */
92561cb7 401static struct debuginfo *open_debuginfo(const char *module, bool silent)
e0faa8d3 402{
a15ad2f5 403 const char *path = module;
419e8738
MH
404 char reason[STRERR_BUFSIZE];
405 struct debuginfo *ret = NULL;
406 struct dso *dso = NULL;
407 int err;
ff741783 408
a15ad2f5 409 if (!module || !strchr(module, '/')) {
419e8738
MH
410 err = kernel_get_module_dso(module, &dso);
411 if (err < 0) {
412 if (!dso || dso->load_errno == 0) {
413 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
414 strcpy(reason, "(unknown)");
415 } else
416 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
92561cb7 417 if (!silent)
419e8738
MH
418 pr_err("Failed to find the path for %s: %s\n",
419 module ?: "kernel", reason);
14a8fd7c
MH
420 return NULL;
421 }
419e8738 422 path = dso->long_name;
e0faa8d3 423 }
92561cb7
MH
424 ret = debuginfo__new(path);
425 if (!ret && !silent) {
426 pr_warning("The %s file has no debug information.\n", path);
427 if (!module || !strtailcmp(path, ".ko"))
428 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
429 else
430 pr_warning("Rebuild with -g, ");
431 pr_warning("or install an appropriate debuginfo package.\n");
432 }
433 return ret;
e0faa8d3 434}
4b4da7f7 435
7737af01
MH
436/* For caching the last debuginfo */
437static struct debuginfo *debuginfo_cache;
438static char *debuginfo_cache_path;
439
440static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
441{
442 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
443 (!debuginfo_cache_path && !module && debuginfo_cache))
444 goto out;
445
446 /* Copy module path */
447 free(debuginfo_cache_path);
448 if (module) {
449 debuginfo_cache_path = strdup(module);
450 if (!debuginfo_cache_path) {
451 debuginfo__delete(debuginfo_cache);
452 debuginfo_cache = NULL;
453 goto out;
454 }
455 }
456
457 debuginfo_cache = open_debuginfo(module, silent);
458 if (!debuginfo_cache)
459 zfree(&debuginfo_cache_path);
460out:
461 return debuginfo_cache;
462}
463
464static void debuginfo_cache__exit(void)
465{
466 debuginfo__delete(debuginfo_cache);
467 debuginfo_cache = NULL;
468 zfree(&debuginfo_cache_path);
469}
470
92561cb7 471
99ca4233
MH
472static int get_text_start_address(const char *exec, unsigned long *address)
473{
474 Elf *elf;
475 GElf_Ehdr ehdr;
476 GElf_Shdr shdr;
477 int fd, ret = -ENOENT;
478
479 fd = open(exec, O_RDONLY);
480 if (fd < 0)
481 return -errno;
482
483 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
484 if (elf == NULL)
485 return -EINVAL;
486
487 if (gelf_getehdr(elf, &ehdr) == NULL)
488 goto out;
489
490 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
491 goto out;
492
493 *address = shdr.sh_addr - shdr.sh_offset;
494 ret = 0;
495out:
496 elf_end(elf);
497 return ret;
498}
499
5a6f6314
MH
500/*
501 * Convert trace point to probe point with debuginfo
502 */
503static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
504 struct perf_probe_point *pp,
505 bool is_kprobe)
506{
507 struct debuginfo *dinfo = NULL;
508 unsigned long stext = 0;
509 u64 addr = tp->address;
510 int ret = -ENOENT;
511
512 /* convert the address to dwarf address */
513 if (!is_kprobe) {
514 if (!addr) {
515 ret = -EINVAL;
516 goto error;
517 }
518 ret = get_text_start_address(tp->module, &stext);
519 if (ret < 0)
520 goto error;
521 addr += stext;
522 } else {
523 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
524 if (addr == 0)
525 goto error;
526 addr += tp->offset;
527 }
528
529 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
530 tp->module ? : "kernel");
531
7737af01
MH
532 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
533 if (dinfo)
5a6f6314
MH
534 ret = debuginfo__find_probe_point(dinfo,
535 (unsigned long)addr, pp);
7737af01 536 else
5a6f6314 537 ret = -ENOENT;
5a6f6314
MH
538
539 if (ret > 0) {
540 pp->retprobe = tp->retprobe;
541 return 0;
542 }
543error:
544 pr_debug("Failed to find corresponding probes from debuginfo.\n");
545 return ret ? : -ENOENT;
546}
547
fb7345bb
MH
548static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
549 int ntevs, const char *exec)
550{
551 int i, ret = 0;
eb948e50 552 unsigned long stext = 0;
fb7345bb
MH
553
554 if (!exec)
555 return 0;
556
557 ret = get_text_start_address(exec, &stext);
558 if (ret < 0)
559 return ret;
560
561 for (i = 0; i < ntevs && ret >= 0; i++) {
981a2379 562 /* point.address is the addres of point.symbol + point.offset */
eb948e50 563 tevs[i].point.address -= stext;
fb7345bb 564 tevs[i].point.module = strdup(exec);
eb948e50 565 if (!tevs[i].point.module) {
fb7345bb
MH
566 ret = -ENOMEM;
567 break;
568 }
569 tevs[i].uprobes = true;
570 }
571
572 return ret;
573}
574
190b57fc
MH
575static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
576 int ntevs, const char *module)
577{
14a8fd7c
MH
578 int i, ret = 0;
579 char *tmp;
580
581 if (!module)
582 return 0;
583
584 tmp = strrchr(module, '/');
585 if (tmp) {
586 /* This is a module path -- get the module name */
587 module = strdup(tmp + 1);
588 if (!module)
589 return -ENOMEM;
590 tmp = strchr(module, '.');
591 if (tmp)
592 *tmp = '\0';
593 tmp = (char *)module; /* For free() */
594 }
595
190b57fc
MH
596 for (i = 0; i < ntevs; i++) {
597 tevs[i].point.module = strdup(module);
14a8fd7c
MH
598 if (!tevs[i].point.module) {
599 ret = -ENOMEM;
600 break;
601 }
190b57fc 602 }
14a8fd7c 603
f5385650 604 free(tmp);
14a8fd7c 605 return ret;
190b57fc
MH
606}
607
dfef99cd
MH
608/* Post processing the probe events */
609static int post_process_probe_trace_events(struct probe_trace_event *tevs,
610 int ntevs, const char *module,
611 bool uprobe)
612{
613 struct ref_reloc_sym *reloc_sym;
614 char *tmp;
5a51fcd1 615 int i, skipped = 0;
dfef99cd
MH
616
617 if (uprobe)
618 return add_exec_to_probe_trace_events(tevs, ntevs, module);
619
620 /* Note that currently ref_reloc_sym based probe is not for drivers */
621 if (module)
622 return add_module_to_probe_trace_events(tevs, ntevs, module);
623
8f33f7de 624 reloc_sym = kernel_get_ref_reloc_sym();
dfef99cd
MH
625 if (!reloc_sym) {
626 pr_warning("Relocated base symbol is not found!\n");
627 return -EINVAL;
628 }
629
630 for (i = 0; i < ntevs; i++) {
b031220d
MH
631 if (!tevs[i].point.address || tevs[i].point.retprobe)
632 continue;
633 /* If we found a wrong one, mark it by NULL symbol */
634 if (kprobe_warn_out_range(tevs[i].point.symbol,
635 tevs[i].point.address)) {
636 tmp = NULL;
637 skipped++;
638 } else {
639 tmp = strdup(reloc_sym->name);
640 if (!tmp)
641 return -ENOMEM;
dfef99cd 642 }
b031220d
MH
643 /* If we have no realname, use symbol for it */
644 if (!tevs[i].point.realname)
645 tevs[i].point.realname = tevs[i].point.symbol;
646 else
647 free(tevs[i].point.symbol);
648 tevs[i].point.symbol = tmp;
649 tevs[i].point.offset = tevs[i].point.address -
650 reloc_sym->unrelocated_addr;
dfef99cd 651 }
5a51fcd1 652 return skipped;
dfef99cd
MH
653}
654
4b4da7f7 655/* Try to find perf_probe_event with debuginfo */
0e60836b 656static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
ddb2f58f 657 struct probe_trace_event **tevs)
4b4da7f7
MH
658{
659 bool need_dwarf = perf_probe_event_need_dwarf(pev);
9b118aca 660 struct perf_probe_point tmp;
225466f1 661 struct debuginfo *dinfo;
190b57fc 662 int ntevs, ret = 0;
4b4da7f7 663
44225521 664 dinfo = open_debuginfo(pev->target, !need_dwarf);
ff741783 665 if (!dinfo) {
92561cb7 666 if (need_dwarf)
ff741783 667 return -ENOENT;
ff741783 668 pr_debug("Could not open debuginfo. Try to use symbols.\n");
4b4da7f7
MH
669 return 0;
670 }
671
dfef99cd 672 pr_debug("Try to find probe point from debuginfo.\n");
ff741783 673 /* Searching trace events corresponding to a probe event */
ddb2f58f 674 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
ff741783 675
9b118aca 676 if (ntevs == 0) { /* Not found, retry with an alternative */
44225521 677 ret = get_alternative_probe_event(dinfo, pev, &tmp);
9b118aca 678 if (!ret) {
ddb2f58f 679 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
9b118aca
MH
680 /*
681 * Write back to the original probe_event for
682 * setting appropriate (user given) event name
683 */
684 clear_perf_probe_point(&pev->point);
685 memcpy(&pev->point, &tmp, sizeof(tmp));
686 }
687 }
688
ff741783 689 debuginfo__delete(dinfo);
4b4da7f7 690
146a1439 691 if (ntevs > 0) { /* Succeeded to find trace events */
dfef99cd
MH
692 pr_debug("Found %d probe_trace_events.\n", ntevs);
693 ret = post_process_probe_trace_events(*tevs, ntevs,
44225521 694 pev->target, pev->uprobes);
5a51fcd1 695 if (ret < 0 || ret == ntevs) {
981d05ad
MH
696 clear_probe_trace_events(*tevs, ntevs);
697 zfree(tevs);
698 }
5a51fcd1
MH
699 if (ret != ntevs)
700 return ret < 0 ? ret : ntevs;
701 ntevs = 0;
702 /* Fall through */
146a1439 703 }
4b4da7f7 704
146a1439 705 if (ntevs == 0) { /* No error but failed to find probe point. */
0687eba7 706 pr_warning("Probe point '%s' not found.\n",
146a1439 707 synthesize_perf_probe_point(&pev->point));
0687eba7 708 return -ENOENT;
146a1439
MH
709 }
710 /* Error path : ntevs < 0 */
15eca306
MH
711 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
712 if (ntevs == -EBADF) {
713 pr_warning("Warning: No dwarf info found in the vmlinux - "
714 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
715 if (!need_dwarf) {
0e43e5d2 716 pr_debug("Trying to use symbols.\n");
15eca306
MH
717 return 0;
718 }
4b4da7f7 719 }
15eca306 720 return ntevs;
4b4da7f7
MH
721}
722
723#define LINEBUF_SIZE 256
724#define NR_ADDITIONAL_LINES 2
725
fde52dbd 726static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
4b4da7f7 727{
5f03cba4 728 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
befe3414
FBH
729 const char *color = show_num ? "" : PERF_COLOR_BLUE;
730 const char *prefix = NULL;
4b4da7f7 731
befe3414 732 do {
4b4da7f7
MH
733 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
734 goto error;
befe3414
FBH
735 if (skip)
736 continue;
737 if (!prefix) {
738 prefix = show_num ? "%7d " : " ";
739 color_fprintf(stdout, color, prefix, l);
4b4da7f7 740 }
befe3414
FBH
741 color_fprintf(stdout, color, "%s", buf);
742
743 } while (strchr(buf, '\n') == NULL);
146a1439 744
fde52dbd 745 return 1;
4b4da7f7 746error:
fde52dbd 747 if (ferror(fp)) {
5f03cba4
MH
748 pr_warning("File read error: %s\n",
749 strerror_r(errno, sbuf, sizeof(sbuf)));
fde52dbd
FBH
750 return -1;
751 }
752 return 0;
753}
146a1439 754
fde52dbd
FBH
755static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
756{
757 int rv = __show_one_line(fp, l, skip, show_num);
758 if (rv == 0) {
759 pr_warning("Source file is shorter than expected.\n");
760 rv = -1;
761 }
762 return rv;
4b4da7f7
MH
763}
764
fde52dbd
FBH
765#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
766#define show_one_line(f,l) _show_one_line(f,l,false,false)
767#define skip_one_line(f,l) _show_one_line(f,l,true,false)
768#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
769
4b4da7f7
MH
770/*
771 * Show line-range always requires debuginfo to find source file and
772 * line number.
773 */
811dd2ae
MH
774static int __show_line_range(struct line_range *lr, const char *module,
775 bool user)
4b4da7f7 776{
d3b63d7a 777 int l = 1;
5a62257a 778 struct int_node *ln;
ff741783 779 struct debuginfo *dinfo;
4b4da7f7 780 FILE *fp;
ff741783 781 int ret;
7cf0b79e 782 char *tmp;
5f03cba4 783 char sbuf[STRERR_BUFSIZE];
4b4da7f7
MH
784
785 /* Search a line range */
92561cb7
MH
786 dinfo = open_debuginfo(module, false);
787 if (!dinfo)
ff741783 788 return -ENOENT;
146a1439 789
ff741783 790 ret = debuginfo__find_line_range(dinfo, lr);
811dd2ae
MH
791 if (!ret) { /* Not found, retry with an alternative */
792 ret = get_alternative_line_range(dinfo, lr, module, user);
793 if (!ret)
794 ret = debuginfo__find_line_range(dinfo, lr);
795 }
ff741783 796 debuginfo__delete(dinfo);
5ee05b88 797 if (ret == 0 || ret == -ENOENT) {
146a1439
MH
798 pr_warning("Specified source line is not found.\n");
799 return -ENOENT;
800 } else if (ret < 0) {
5ee05b88 801 pr_warning("Debuginfo analysis failed.\n");
146a1439
MH
802 return ret;
803 }
4b4da7f7 804
7cf0b79e
MH
805 /* Convert source file path */
806 tmp = lr->path;
6a330a3c 807 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
a78604de
HK
808
809 /* Free old path when new path is assigned */
810 if (tmp != lr->path)
811 free(tmp);
812
7cf0b79e 813 if (ret < 0) {
5ee05b88 814 pr_warning("Failed to find source file path.\n");
7cf0b79e
MH
815 return ret;
816 }
817
4b4da7f7
MH
818 setup_pager();
819
820 if (lr->function)
8737ebde 821 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
4b4da7f7
MH
822 lr->start - lr->offset);
823 else
62c15fc4 824 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
4b4da7f7
MH
825
826 fp = fopen(lr->path, "r");
146a1439
MH
827 if (fp == NULL) {
828 pr_warning("Failed to open %s: %s\n", lr->path,
5f03cba4 829 strerror_r(errno, sbuf, sizeof(sbuf)));
146a1439
MH
830 return -errno;
831 }
4b4da7f7 832 /* Skip to starting line number */
44b81e92 833 while (l < lr->start) {
fde52dbd 834 ret = skip_one_line(fp, l++);
44b81e92
FBH
835 if (ret < 0)
836 goto end;
837 }
4b4da7f7 838
5a62257a
MH
839 intlist__for_each(ln, lr->line_list) {
840 for (; ln->i > l; l++) {
fde52dbd 841 ret = show_one_line(fp, l - lr->offset);
44b81e92
FBH
842 if (ret < 0)
843 goto end;
844 }
fde52dbd 845 ret = show_one_line_with_num(fp, l++ - lr->offset);
146a1439
MH
846 if (ret < 0)
847 goto end;
4b4da7f7
MH
848 }
849
850 if (lr->end == INT_MAX)
851 lr->end = l + NR_ADDITIONAL_LINES;
fde52dbd
FBH
852 while (l <= lr->end) {
853 ret = show_one_line_or_eof(fp, l++ - lr->offset);
854 if (ret <= 0)
44b81e92
FBH
855 break;
856 }
146a1439 857end:
4b4da7f7 858 fclose(fp);
146a1439 859 return ret;
4b4da7f7
MH
860}
861
2b394bc4 862int show_line_range(struct line_range *lr, const char *module, bool user)
ee45b6c2
MH
863{
864 int ret;
865
2b394bc4 866 ret = init_symbol_maps(user);
ee45b6c2
MH
867 if (ret < 0)
868 return ret;
811dd2ae 869 ret = __show_line_range(lr, module, user);
ee45b6c2
MH
870 exit_symbol_maps();
871
872 return ret;
873}
874
ff741783
MH
875static int show_available_vars_at(struct debuginfo *dinfo,
876 struct perf_probe_event *pev,
ddb2f58f 877 struct strfilter *_filter)
cf6eb489
MH
878{
879 char *buf;
bd09d7b5 880 int ret, i, nvars;
cf6eb489
MH
881 struct str_node *node;
882 struct variable_list *vls = NULL, *vl;
9b118aca 883 struct perf_probe_point tmp;
bd09d7b5 884 const char *var;
cf6eb489
MH
885
886 buf = synthesize_perf_probe_point(&pev->point);
887 if (!buf)
888 return -EINVAL;
889 pr_debug("Searching variables at %s\n", buf);
890
ddb2f58f 891 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
9b118aca 892 if (!ret) { /* Not found, retry with an alternative */
44225521 893 ret = get_alternative_probe_event(dinfo, pev, &tmp);
9b118aca
MH
894 if (!ret) {
895 ret = debuginfo__find_available_vars_at(dinfo, pev,
ddb2f58f 896 &vls);
9b118aca
MH
897 /* Release the old probe_point */
898 clear_perf_probe_point(&tmp);
899 }
900 }
bd09d7b5 901 if (ret <= 0) {
69e96eaa
MH
902 if (ret == 0 || ret == -ENOENT) {
903 pr_err("Failed to find the address of %s\n", buf);
904 ret = -ENOENT;
905 } else
906 pr_warning("Debuginfo analysis failed.\n");
bd09d7b5
MH
907 goto end;
908 }
69e96eaa 909
bd09d7b5
MH
910 /* Some variables are found */
911 fprintf(stdout, "Available variables at %s\n", buf);
912 for (i = 0; i < ret; i++) {
913 vl = &vls[i];
914 /*
915 * A probe point might be converted to
916 * several trace points.
917 */
918 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
919 vl->point.offset);
74cf249d 920 zfree(&vl->point.symbol);
bd09d7b5
MH
921 nvars = 0;
922 if (vl->vars) {
923 strlist__for_each(node, vl->vars) {
924 var = strchr(node->s, '\t') + 1;
925 if (strfilter__compare(_filter, var)) {
cf6eb489 926 fprintf(stdout, "\t\t%s\n", node->s);
bd09d7b5
MH
927 nvars++;
928 }
929 }
930 strlist__delete(vl->vars);
cf6eb489 931 }
bd09d7b5
MH
932 if (nvars == 0)
933 fprintf(stdout, "\t\t(No matched variables)\n");
934 }
935 free(vls);
936end:
cf6eb489
MH
937 free(buf);
938 return ret;
939}
940
941/* Show available variables on given probe point */
942int show_available_vars(struct perf_probe_event *pevs, int npevs,
ddb2f58f 943 struct strfilter *_filter)
cf6eb489 944{
ff741783
MH
945 int i, ret = 0;
946 struct debuginfo *dinfo;
cf6eb489 947
2b394bc4 948 ret = init_symbol_maps(pevs->uprobes);
cf6eb489
MH
949 if (ret < 0)
950 return ret;
951
44225521 952 dinfo = open_debuginfo(pevs->target, false);
ff741783 953 if (!dinfo) {
ee45b6c2
MH
954 ret = -ENOENT;
955 goto out;
ff741783
MH
956 }
957
cf6eb489
MH
958 setup_pager();
959
ff741783 960 for (i = 0; i < npevs && ret >= 0; i++)
ddb2f58f 961 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
ff741783
MH
962
963 debuginfo__delete(dinfo);
ee45b6c2
MH
964out:
965 exit_symbol_maps();
cf6eb489
MH
966 return ret;
967}
968
89fe808a 969#else /* !HAVE_DWARF_SUPPORT */
4b4da7f7 970
7737af01
MH
971static void debuginfo_cache__exit(void)
972{
973}
974
5a6f6314
MH
975static int
976find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
977 struct perf_probe_point *pp __maybe_unused,
978 bool is_kprobe __maybe_unused)
4b4da7f7 979{
5a6f6314 980 return -ENOSYS;
4b4da7f7
MH
981}
982
0e60836b 983static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
ddb2f58f 984 struct probe_trace_event **tevs __maybe_unused)
4b4da7f7 985{
146a1439
MH
986 if (perf_probe_event_need_dwarf(pev)) {
987 pr_warning("Debuginfo-analysis is not supported.\n");
988 return -ENOSYS;
989 }
225466f1 990
4b4da7f7
MH
991 return 0;
992}
993
1d037ca1 994int show_line_range(struct line_range *lr __maybe_unused,
2b394bc4
MH
995 const char *module __maybe_unused,
996 bool user __maybe_unused)
4b4da7f7 997{
146a1439
MH
998 pr_warning("Debuginfo-analysis is not supported.\n");
999 return -ENOSYS;
4b4da7f7
MH
1000}
1001
1d037ca1 1002int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
ddb2f58f
MH
1003 int npevs __maybe_unused,
1004 struct strfilter *filter __maybe_unused)
cf6eb489
MH
1005{
1006 pr_warning("Debuginfo-analysis is not supported.\n");
1007 return -ENOSYS;
1008}
e0faa8d3
MH
1009#endif
1010
e53b00d3
MH
1011void line_range__clear(struct line_range *lr)
1012{
e53b00d3
MH
1013 free(lr->function);
1014 free(lr->file);
1015 free(lr->path);
1016 free(lr->comp_dir);
5a62257a 1017 intlist__delete(lr->line_list);
e53b00d3
MH
1018 memset(lr, 0, sizeof(*lr));
1019}
1020
5a62257a 1021int line_range__init(struct line_range *lr)
e53b00d3
MH
1022{
1023 memset(lr, 0, sizeof(*lr));
5a62257a
MH
1024 lr->line_list = intlist__new(NULL);
1025 if (!lr->line_list)
1026 return -ENOMEM;
1027 else
1028 return 0;
e53b00d3
MH
1029}
1030
21dd9ae5
FBH
1031static int parse_line_num(char **ptr, int *val, const char *what)
1032{
1033 const char *start = *ptr;
1034
1035 errno = 0;
1036 *val = strtol(*ptr, ptr, 0);
1037 if (errno || *ptr == start) {
1038 semantic_error("'%s' is not a valid number.\n", what);
1039 return -EINVAL;
1040 }
1041 return 0;
1042}
1043
573709fd
MH
1044/* Check the name is good for event, group or function */
1045static bool is_c_func_name(const char *name)
1046{
1047 if (!isalpha(*name) && *name != '_')
1048 return false;
1049 while (*++name != '\0') {
1050 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1051 return false;
1052 }
1053 return true;
1054}
1055
9d95b580
FBH
1056/*
1057 * Stuff 'lr' according to the line range described by 'arg'.
1058 * The line range syntax is described by:
1059 *
1060 * SRC[:SLN[+NUM|-ELN]]
e116dfa1 1061 * FNC[@SRC][:SLN[+NUM|-ELN]]
9d95b580 1062 */
146a1439 1063int parse_line_range_desc(const char *arg, struct line_range *lr)
631c9def 1064{
e116dfa1 1065 char *range, *file, *name = strdup(arg);
21dd9ae5
FBH
1066 int err;
1067
1068 if (!name)
1069 return -ENOMEM;
1070
1071 lr->start = 0;
1072 lr->end = INT_MAX;
1073
1074 range = strchr(name, ':');
1075 if (range) {
1076 *range++ = '\0';
1077
1078 err = parse_line_num(&range, &lr->start, "start line");
1079 if (err)
1080 goto err;
1081
1082 if (*range == '+' || *range == '-') {
1083 const char c = *range++;
1084
1085 err = parse_line_num(&range, &lr->end, "end line");
1086 if (err)
1087 goto err;
1088
1089 if (c == '+') {
1090 lr->end += lr->start;
1091 /*
1092 * Adjust the number of lines here.
1093 * If the number of lines == 1, the
1094 * the end of line should be equal to
1095 * the start of line.
1096 */
1097 lr->end--;
1098 }
1099 }
9d95b580 1100
d3b63d7a 1101 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
21dd9ae5
FBH
1102
1103 err = -EINVAL;
d3b63d7a 1104 if (lr->start > lr->end) {
631c9def 1105 semantic_error("Start line must be smaller"
146a1439 1106 " than end line.\n");
21dd9ae5 1107 goto err;
146a1439 1108 }
21dd9ae5
FBH
1109 if (*range != '\0') {
1110 semantic_error("Tailing with invalid str '%s'.\n", range);
1111 goto err;
146a1439 1112 }
d3b63d7a 1113 }
02b95dad 1114
e116dfa1
MH
1115 file = strchr(name, '@');
1116 if (file) {
1117 *file = '\0';
1118 lr->file = strdup(++file);
1119 if (lr->file == NULL) {
1120 err = -ENOMEM;
1121 goto err;
1122 }
1123 lr->function = name;
573709fd 1124 } else if (strchr(name, '/') || strchr(name, '.'))
21dd9ae5 1125 lr->file = name;
573709fd 1126 else if (is_c_func_name(name))/* We reuse it for checking funcname */
21dd9ae5 1127 lr->function = name;
573709fd
MH
1128 else { /* Invalid name */
1129 semantic_error("'%s' is not a valid function name.\n", name);
1130 err = -EINVAL;
1131 goto err;
1132 }
146a1439
MH
1133
1134 return 0;
21dd9ae5
FBH
1135err:
1136 free(name);
1137 return err;
631c9def
MH
1138}
1139
50656eec 1140/* Parse probepoint definition. */
146a1439 1141static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 1142{
4235b045 1143 struct perf_probe_point *pp = &pev->point;
50656eec
MH
1144 char *ptr, *tmp;
1145 char c, nc = 0;
3099c026 1146 bool file_spec = false;
50656eec
MH
1147 /*
1148 * <Syntax>
2a9c8c36
MH
1149 * perf probe [EVENT=]SRC[:LN|;PTN]
1150 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
1151 *
1152 * TODO:Group name support
50656eec 1153 */
e59d29e8
WN
1154 if (!arg)
1155 return -EINVAL;
50656eec 1156
2a9c8c36
MH
1157 ptr = strpbrk(arg, ";=@+%");
1158 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
1159 *ptr = '\0';
1160 tmp = ptr + 1;
146a1439
MH
1161 if (strchr(arg, ':')) {
1162 semantic_error("Group name is not supported yet.\n");
1163 return -ENOTSUP;
1164 }
573709fd 1165 if (!is_c_func_name(arg)) {
b7702a21 1166 semantic_error("%s is bad for event name -it must "
146a1439
MH
1167 "follow C symbol-naming rule.\n", arg);
1168 return -EINVAL;
1169 }
02b95dad
MH
1170 pev->event = strdup(arg);
1171 if (pev->event == NULL)
1172 return -ENOMEM;
4235b045 1173 pev->group = NULL;
af663d75
MH
1174 arg = tmp;
1175 }
1176
3099c026
NR
1177 /*
1178 * Check arg is function or file name and copy it.
1179 *
1180 * We consider arg to be a file spec if and only if it satisfies
1181 * all of the below criteria::
1182 * - it does not include any of "+@%",
1183 * - it includes one of ":;", and
1184 * - it has a period '.' in the name.
1185 *
1186 * Otherwise, we consider arg to be a function specification.
1187 */
1188 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1189 /* This is a file spec if it includes a '.' before ; or : */
1190 if (memchr(arg, '.', ptr - arg))
1191 file_spec = true;
1192 }
1193
2a9c8c36 1194 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
1195 if (ptr) {
1196 nc = *ptr;
1197 *ptr++ = '\0';
1198 }
1199
02b95dad
MH
1200 tmp = strdup(arg);
1201 if (tmp == NULL)
1202 return -ENOMEM;
1203
3099c026 1204 if (file_spec)
02b95dad 1205 pp->file = tmp;
3099c026 1206 else
02b95dad 1207 pp->function = tmp;
50656eec
MH
1208
1209 /* Parse other options */
1210 while (ptr) {
1211 arg = ptr;
1212 c = nc;
2a9c8c36 1213 if (c == ';') { /* Lazy pattern must be the last part */
02b95dad
MH
1214 pp->lazy_line = strdup(arg);
1215 if (pp->lazy_line == NULL)
1216 return -ENOMEM;
2a9c8c36
MH
1217 break;
1218 }
1219 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
1220 if (ptr) {
1221 nc = *ptr;
1222 *ptr++ = '\0';
1223 }
1224 switch (c) {
1225 case ':': /* Line number */
1226 pp->line = strtoul(arg, &tmp, 0);
146a1439 1227 if (*tmp != '\0') {
2a9c8c36 1228 semantic_error("There is non-digit char"
146a1439
MH
1229 " in line number.\n");
1230 return -EINVAL;
1231 }
50656eec
MH
1232 break;
1233 case '+': /* Byte offset from a symbol */
1234 pp->offset = strtoul(arg, &tmp, 0);
146a1439 1235 if (*tmp != '\0') {
2a9c8c36 1236 semantic_error("There is non-digit character"
146a1439
MH
1237 " in offset.\n");
1238 return -EINVAL;
1239 }
50656eec
MH
1240 break;
1241 case '@': /* File name */
146a1439
MH
1242 if (pp->file) {
1243 semantic_error("SRC@SRC is not allowed.\n");
1244 return -EINVAL;
1245 }
02b95dad
MH
1246 pp->file = strdup(arg);
1247 if (pp->file == NULL)
1248 return -ENOMEM;
50656eec
MH
1249 break;
1250 case '%': /* Probe places */
1251 if (strcmp(arg, "return") == 0) {
1252 pp->retprobe = 1;
146a1439
MH
1253 } else { /* Others not supported yet */
1254 semantic_error("%%%s is not supported.\n", arg);
1255 return -ENOTSUP;
1256 }
50656eec 1257 break;
146a1439
MH
1258 default: /* Buggy case */
1259 pr_err("This program has a bug at %s:%d.\n",
1260 __FILE__, __LINE__);
1261 return -ENOTSUP;
50656eec
MH
1262 break;
1263 }
1264 }
1265
1266 /* Exclusion check */
146a1439 1267 if (pp->lazy_line && pp->line) {
0e43e5d2
MH
1268 semantic_error("Lazy pattern can't be used with"
1269 " line number.\n");
146a1439
MH
1270 return -EINVAL;
1271 }
2a9c8c36 1272
146a1439 1273 if (pp->lazy_line && pp->offset) {
0e43e5d2 1274 semantic_error("Lazy pattern can't be used with offset.\n");
146a1439
MH
1275 return -EINVAL;
1276 }
2a9c8c36 1277
146a1439 1278 if (pp->line && pp->offset) {
0e43e5d2 1279 semantic_error("Offset can't be used with line number.\n");
146a1439
MH
1280 return -EINVAL;
1281 }
50656eec 1282
146a1439 1283 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
2a9c8c36 1284 semantic_error("File always requires line number or "
0e43e5d2 1285 "lazy pattern.\n");
146a1439
MH
1286 return -EINVAL;
1287 }
50656eec 1288
146a1439 1289 if (pp->offset && !pp->function) {
0e43e5d2 1290 semantic_error("Offset requires an entry function.\n");
146a1439
MH
1291 return -EINVAL;
1292 }
50656eec 1293
146a1439 1294 if (pp->retprobe && !pp->function) {
0e43e5d2 1295 semantic_error("Return probe requires an entry function.\n");
146a1439
MH
1296 return -EINVAL;
1297 }
50656eec 1298
146a1439 1299 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
2a9c8c36 1300 semantic_error("Offset/Line/Lazy pattern can't be used with "
0e43e5d2 1301 "return probe.\n");
146a1439
MH
1302 return -EINVAL;
1303 }
50656eec 1304
4235b045 1305 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
1306 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1307 pp->lazy_line);
146a1439 1308 return 0;
50656eec
MH
1309}
1310
7df2f329 1311/* Parse perf-probe event argument */
146a1439 1312static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
7df2f329 1313{
b2a3c12b 1314 char *tmp, *goodname;
7df2f329
MH
1315 struct perf_probe_arg_field **fieldp;
1316
1317 pr_debug("parsing arg: %s into ", str);
1318
48481938
MH
1319 tmp = strchr(str, '=');
1320 if (tmp) {
02b95dad
MH
1321 arg->name = strndup(str, tmp - str);
1322 if (arg->name == NULL)
1323 return -ENOMEM;
11a1ca35 1324 pr_debug("name:%s ", arg->name);
48481938
MH
1325 str = tmp + 1;
1326 }
1327
11a1ca35
MH
1328 tmp = strchr(str, ':');
1329 if (tmp) { /* Type setting */
1330 *tmp = '\0';
02b95dad
MH
1331 arg->type = strdup(tmp + 1);
1332 if (arg->type == NULL)
1333 return -ENOMEM;
11a1ca35
MH
1334 pr_debug("type:%s ", arg->type);
1335 }
1336
b2a3c12b 1337 tmp = strpbrk(str, "-.[");
7df2f329
MH
1338 if (!is_c_varname(str) || !tmp) {
1339 /* A variable, register, symbol or special value */
02b95dad
MH
1340 arg->var = strdup(str);
1341 if (arg->var == NULL)
1342 return -ENOMEM;
48481938 1343 pr_debug("%s\n", arg->var);
146a1439 1344 return 0;
7df2f329
MH
1345 }
1346
b2a3c12b 1347 /* Structure fields or array element */
02b95dad
MH
1348 arg->var = strndup(str, tmp - str);
1349 if (arg->var == NULL)
1350 return -ENOMEM;
b2a3c12b 1351 goodname = arg->var;
48481938 1352 pr_debug("%s, ", arg->var);
7df2f329
MH
1353 fieldp = &arg->field;
1354
1355 do {
e334016f
MH
1356 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1357 if (*fieldp == NULL)
1358 return -ENOMEM;
b2a3c12b
MH
1359 if (*tmp == '[') { /* Array */
1360 str = tmp;
1361 (*fieldp)->index = strtol(str + 1, &tmp, 0);
7df2f329 1362 (*fieldp)->ref = true;
b2a3c12b
MH
1363 if (*tmp != ']' || tmp == str + 1) {
1364 semantic_error("Array index must be a"
1365 " number.\n");
1366 return -EINVAL;
1367 }
1368 tmp++;
1369 if (*tmp == '\0')
1370 tmp = NULL;
1371 } else { /* Structure */
1372 if (*tmp == '.') {
1373 str = tmp + 1;
1374 (*fieldp)->ref = false;
1375 } else if (tmp[1] == '>') {
1376 str = tmp + 2;
1377 (*fieldp)->ref = true;
1378 } else {
1379 semantic_error("Argument parse error: %s\n",
1380 str);
1381 return -EINVAL;
1382 }
1383 tmp = strpbrk(str, "-.[");
146a1439 1384 }
7df2f329 1385 if (tmp) {
02b95dad
MH
1386 (*fieldp)->name = strndup(str, tmp - str);
1387 if ((*fieldp)->name == NULL)
1388 return -ENOMEM;
b2a3c12b
MH
1389 if (*str != '[')
1390 goodname = (*fieldp)->name;
7df2f329
MH
1391 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1392 fieldp = &(*fieldp)->next;
1393 }
1394 } while (tmp);
02b95dad
MH
1395 (*fieldp)->name = strdup(str);
1396 if ((*fieldp)->name == NULL)
1397 return -ENOMEM;
b2a3c12b
MH
1398 if (*str != '[')
1399 goodname = (*fieldp)->name;
7df2f329 1400 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
df0faf4b 1401
b2a3c12b 1402 /* If no name is specified, set the last field name (not array index)*/
02b95dad 1403 if (!arg->name) {
b2a3c12b 1404 arg->name = strdup(goodname);
02b95dad
MH
1405 if (arg->name == NULL)
1406 return -ENOMEM;
1407 }
146a1439 1408 return 0;
7df2f329
MH
1409}
1410
4235b045 1411/* Parse perf-probe event command */
146a1439 1412int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 1413{
e1c01d61 1414 char **argv;
146a1439 1415 int argc, i, ret = 0;
fac13fd5 1416
4235b045 1417 argv = argv_split(cmd, &argc);
146a1439
MH
1418 if (!argv) {
1419 pr_debug("Failed to split arguments.\n");
1420 return -ENOMEM;
1421 }
1422 if (argc - 1 > MAX_PROBE_ARGS) {
1423 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1424 ret = -ERANGE;
1425 goto out;
1426 }
50656eec 1427 /* Parse probe point */
146a1439
MH
1428 ret = parse_perf_probe_point(argv[0], pev);
1429 if (ret < 0)
1430 goto out;
50656eec 1431
e1c01d61 1432 /* Copy arguments and ensure return probe has no C argument */
4235b045 1433 pev->nargs = argc - 1;
e334016f
MH
1434 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1435 if (pev->args == NULL) {
1436 ret = -ENOMEM;
1437 goto out;
1438 }
146a1439
MH
1439 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1440 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1441 if (ret >= 0 &&
1442 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
4235b045 1443 semantic_error("You can't specify local variable for"
146a1439
MH
1444 " kretprobe.\n");
1445 ret = -EINVAL;
1446 }
e1c01d61 1447 }
146a1439 1448out:
e1c01d61 1449 argv_free(argv);
146a1439
MH
1450
1451 return ret;
50656eec
MH
1452}
1453
4235b045
MH
1454/* Return true if this perf_probe_event requires debuginfo */
1455bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1456{
1457 int i;
1458
1459 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1460 return true;
1461
1462 for (i = 0; i < pev->nargs; i++)
48481938 1463 if (is_c_varname(pev->args[i].var))
4235b045
MH
1464 return true;
1465
1466 return false;
1467}
1468
0e60836b
SD
1469/* Parse probe_events event into struct probe_point */
1470static int parse_probe_trace_command(const char *cmd,
190b57fc 1471 struct probe_trace_event *tev)
4de189fe 1472{
0e60836b 1473 struct probe_trace_point *tp = &tev->point;
4de189fe
MH
1474 char pr;
1475 char *p;
bcbd0040 1476 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
4de189fe
MH
1477 int ret, i, argc;
1478 char **argv;
1479
0e60836b 1480 pr_debug("Parsing probe_events: %s\n", cmd);
4235b045 1481 argv = argv_split(cmd, &argc);
146a1439
MH
1482 if (!argv) {
1483 pr_debug("Failed to split arguments.\n");
1484 return -ENOMEM;
1485 }
1486 if (argc < 2) {
1487 semantic_error("Too few probe arguments.\n");
1488 ret = -ERANGE;
1489 goto out;
1490 }
4de189fe
MH
1491
1492 /* Scan event and group name. */
bcbd0040
IT
1493 argv0_str = strdup(argv[0]);
1494 if (argv0_str == NULL) {
1495 ret = -ENOMEM;
1496 goto out;
1497 }
1498 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1499 fmt2_str = strtok_r(NULL, "/", &fmt);
1500 fmt3_str = strtok_r(NULL, " \t", &fmt);
1501 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1502 || fmt3_str == NULL) {
146a1439
MH
1503 semantic_error("Failed to parse event name: %s\n", argv[0]);
1504 ret = -EINVAL;
1505 goto out;
1506 }
bcbd0040
IT
1507 pr = fmt1_str[0];
1508 tev->group = strdup(fmt2_str);
1509 tev->event = strdup(fmt3_str);
1510 if (tev->group == NULL || tev->event == NULL) {
1511 ret = -ENOMEM;
1512 goto out;
1513 }
4235b045 1514 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 1515
4235b045 1516 tp->retprobe = (pr == 'r');
4de189fe 1517
190b57fc
MH
1518 /* Scan module name(if there), function name and offset */
1519 p = strchr(argv[1], ':');
1520 if (p) {
1521 tp->module = strndup(argv[1], p - argv[1]);
1522 p++;
1523 } else
1524 p = argv[1];
bcbd0040 1525 fmt1_str = strtok_r(p, "+", &fmt);
5a6f6314
MH
1526 if (fmt1_str[0] == '0') /* only the address started with 0x */
1527 tp->address = strtoul(fmt1_str, NULL, 0);
1528 else {
1529 /* Only the symbol-based probe has offset */
1530 tp->symbol = strdup(fmt1_str);
1531 if (tp->symbol == NULL) {
1532 ret = -ENOMEM;
1533 goto out;
1534 }
1535 fmt2_str = strtok_r(NULL, "", &fmt);
1536 if (fmt2_str == NULL)
1537 tp->offset = 0;
1538 else
1539 tp->offset = strtoul(fmt2_str, NULL, 10);
bcbd0040 1540 }
4de189fe 1541
4235b045 1542 tev->nargs = argc - 2;
0e60836b 1543 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
e334016f
MH
1544 if (tev->args == NULL) {
1545 ret = -ENOMEM;
1546 goto out;
1547 }
4235b045 1548 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
1549 p = strchr(argv[i + 2], '=');
1550 if (p) /* We don't need which register is assigned. */
4235b045
MH
1551 *p++ = '\0';
1552 else
1553 p = argv[i + 2];
02b95dad 1554 tev->args[i].name = strdup(argv[i + 2]);
4235b045 1555 /* TODO: parse regs and offset */
02b95dad
MH
1556 tev->args[i].value = strdup(p);
1557 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1558 ret = -ENOMEM;
1559 goto out;
1560 }
4de189fe 1561 }
146a1439
MH
1562 ret = 0;
1563out:
bcbd0040 1564 free(argv0_str);
4de189fe 1565 argv_free(argv);
146a1439 1566 return ret;
4de189fe
MH
1567}
1568
7df2f329
MH
1569/* Compose only probe arg */
1570int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1571{
1572 struct perf_probe_arg_field *field = pa->field;
1573 int ret;
1574 char *tmp = buf;
1575
48481938
MH
1576 if (pa->name && pa->var)
1577 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1578 else
1579 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
1580 if (ret <= 0)
1581 goto error;
1582 tmp += ret;
1583 len -= ret;
1584
1585 while (field) {
b2a3c12b
MH
1586 if (field->name[0] == '[')
1587 ret = e_snprintf(tmp, len, "%s", field->name);
1588 else
1589 ret = e_snprintf(tmp, len, "%s%s",
1590 field->ref ? "->" : ".", field->name);
7df2f329
MH
1591 if (ret <= 0)
1592 goto error;
1593 tmp += ret;
1594 len -= ret;
1595 field = field->next;
1596 }
11a1ca35
MH
1597
1598 if (pa->type) {
1599 ret = e_snprintf(tmp, len, ":%s", pa->type);
1600 if (ret <= 0)
1601 goto error;
1602 tmp += ret;
1603 len -= ret;
1604 }
1605
7df2f329
MH
1606 return tmp - buf;
1607error:
5f03cba4 1608 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
146a1439 1609 return ret;
7df2f329
MH
1610}
1611
4235b045
MH
1612/* Compose only probe point (not argument) */
1613static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 1614{
fb1587d8
MH
1615 char *buf, *tmp;
1616 char offs[32] = "", line[32] = "", file[32] = "";
1617 int ret, len;
4de189fe 1618
e334016f
MH
1619 buf = zalloc(MAX_CMDLEN);
1620 if (buf == NULL) {
1621 ret = -ENOMEM;
1622 goto error;
1623 }
4de189fe 1624 if (pp->offset) {
fb1587d8 1625 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
1626 if (ret <= 0)
1627 goto error;
1628 }
1629 if (pp->line) {
fb1587d8
MH
1630 ret = e_snprintf(line, 32, ":%d", pp->line);
1631 if (ret <= 0)
1632 goto error;
1633 }
1634 if (pp->file) {
32ae2ade
FBH
1635 tmp = pp->file;
1636 len = strlen(tmp);
1637 if (len > 30) {
1638 tmp = strchr(pp->file + len - 30, '/');
1639 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1640 }
1641 ret = e_snprintf(file, 32, "@%s", tmp);
4de189fe
MH
1642 if (ret <= 0)
1643 goto error;
1644 }
1645
1646 if (pp->function)
fb1587d8
MH
1647 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1648 offs, pp->retprobe ? "%return" : "", line,
1649 file);
4de189fe 1650 else
fb1587d8 1651 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
1652 if (ret <= 0)
1653 goto error;
1654
1655 return buf;
7ef17aaf 1656error:
5f03cba4 1657 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
f5385650 1658 free(buf);
146a1439 1659 return NULL;
7ef17aaf
MH
1660}
1661
4235b045
MH
1662#if 0
1663char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
1664{
1665 char *buf;
1666 int i, len, ret;
1667
4235b045
MH
1668 buf = synthesize_perf_probe_point(&pev->point);
1669 if (!buf)
1670 return NULL;
4de189fe 1671
4235b045
MH
1672 len = strlen(buf);
1673 for (i = 0; i < pev->nargs; i++) {
4de189fe 1674 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
1675 pev->args[i].name);
1676 if (ret <= 0) {
1677 free(buf);
1678 return NULL;
1679 }
4de189fe
MH
1680 len += ret;
1681 }
4de189fe 1682
4235b045
MH
1683 return buf;
1684}
1685#endif
1686
0e60836b 1687static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
4235b045
MH
1688 char **buf, size_t *buflen,
1689 int depth)
1690{
1691 int ret;
1692 if (ref->next) {
0e60836b 1693 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
4235b045
MH
1694 buflen, depth + 1);
1695 if (depth < 0)
1696 goto out;
1697 }
1698
1699 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1700 if (ret < 0)
1701 depth = ret;
1702 else {
1703 *buf += ret;
1704 *buflen -= ret;
1705 }
1706out:
1707 return depth;
4de189fe 1708
4de189fe
MH
1709}
1710
0e60836b 1711static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
4235b045 1712 char *buf, size_t buflen)
50656eec 1713{
0e60836b 1714 struct probe_trace_arg_ref *ref = arg->ref;
4235b045
MH
1715 int ret, depth = 0;
1716 char *tmp = buf;
1717
1718 /* Argument name or separator */
1719 if (arg->name)
1720 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1721 else
1722 ret = e_snprintf(buf, buflen, " ");
1723 if (ret < 0)
1724 return ret;
1725 buf += ret;
1726 buflen -= ret;
1727
b7dcb857
MH
1728 /* Special case: @XXX */
1729 if (arg->value[0] == '@' && arg->ref)
1730 ref = ref->next;
1731
4235b045 1732 /* Dereferencing arguments */
b7dcb857 1733 if (ref) {
0e60836b 1734 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
4235b045
MH
1735 &buflen, 1);
1736 if (depth < 0)
1737 return depth;
1738 }
1739
1740 /* Print argument value */
b7dcb857
MH
1741 if (arg->value[0] == '@' && arg->ref)
1742 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1743 arg->ref->offset);
1744 else
1745 ret = e_snprintf(buf, buflen, "%s", arg->value);
4235b045
MH
1746 if (ret < 0)
1747 return ret;
1748 buf += ret;
1749 buflen -= ret;
1750
1751 /* Closing */
1752 while (depth--) {
1753 ret = e_snprintf(buf, buflen, ")");
1754 if (ret < 0)
1755 return ret;
1756 buf += ret;
1757 buflen -= ret;
1758 }
4984912e
MH
1759 /* Print argument type */
1760 if (arg->type) {
1761 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1762 if (ret <= 0)
1763 return ret;
1764 buf += ret;
1765 }
4235b045
MH
1766
1767 return buf - tmp;
1768}
1769
0e60836b 1770char *synthesize_probe_trace_command(struct probe_trace_event *tev)
4235b045 1771{
0e60836b 1772 struct probe_trace_point *tp = &tev->point;
50656eec
MH
1773 char *buf;
1774 int i, len, ret;
1775
e334016f
MH
1776 buf = zalloc(MAX_CMDLEN);
1777 if (buf == NULL)
1778 return NULL;
1779
eb948e50
MH
1780 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1781 tev->group, tev->event);
1782 if (len <= 0)
1783 goto error;
1784
1785 /* Uprobes must have tp->address and tp->module */
1786 if (tev->uprobes && (!tp->address || !tp->module))
1787 goto error;
1788
1789 /* Use the tp->address for uprobes */
225466f1 1790 if (tev->uprobes)
eb948e50
MH
1791 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1792 tp->module, tp->address);
225466f1 1793 else
eb948e50 1794 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
225466f1
SD
1795 tp->module ?: "", tp->module ? ":" : "",
1796 tp->symbol, tp->offset);
1797
eb948e50 1798 if (ret <= 0)
50656eec 1799 goto error;
eb948e50 1800 len += ret;
50656eec 1801
4235b045 1802 for (i = 0; i < tev->nargs; i++) {
0e60836b 1803 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
4235b045 1804 MAX_CMDLEN - len);
4de189fe 1805 if (ret <= 0)
50656eec
MH
1806 goto error;
1807 len += ret;
1808 }
50656eec 1809
4235b045 1810 return buf;
50656eec 1811error:
4235b045
MH
1812 free(buf);
1813 return NULL;
1814}
50656eec 1815
5a6f6314
MH
1816static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1817 struct perf_probe_point *pp,
1818 bool is_kprobe)
1819{
1820 struct symbol *sym = NULL;
1821 struct map *map;
1822 u64 addr;
1823 int ret = -ENOENT;
1824
1825 if (!is_kprobe) {
1826 map = dso__new_map(tp->module);
1827 if (!map)
1828 goto out;
1829 addr = tp->address;
1830 sym = map__find_symbol(map, addr, NULL);
1831 } else {
1832 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1833 if (addr) {
1834 addr += tp->offset;
1835 sym = __find_kernel_function(addr, &map);
1836 }
1837 }
1838 if (!sym)
1839 goto out;
1840
1841 pp->retprobe = tp->retprobe;
1842 pp->offset = addr - map->unmap_ip(map, sym->start);
1843 pp->function = strdup(sym->name);
1844 ret = pp->function ? 0 : -ENOMEM;
1845
1846out:
1847 if (map && !is_kprobe) {
84c2cafa 1848 map__put(map);
5a6f6314
MH
1849 }
1850
1851 return ret;
1852}
1853
1854static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1855 struct perf_probe_point *pp,
1856 bool is_kprobe)
1857{
1858 char buf[128];
1859 int ret;
1860
1861 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1862 if (!ret)
1863 return 0;
1864 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1865 if (!ret)
1866 return 0;
1867
1868 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1869
1870 if (tp->symbol) {
1871 pp->function = strdup(tp->symbol);
1872 pp->offset = tp->offset;
1873 } else if (!tp->module && !is_kprobe) {
1874 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1875 if (ret < 0)
1876 return ret;
1877 pp->function = strdup(buf);
1878 pp->offset = 0;
1879 }
1880 if (pp->function == NULL)
1881 return -ENOMEM;
1882
1883 pp->retprobe = tp->retprobe;
1884
1885 return 0;
1886}
1887
0e60836b 1888static int convert_to_perf_probe_event(struct probe_trace_event *tev,
225466f1 1889 struct perf_probe_event *pev, bool is_kprobe)
4235b045 1890{
02b95dad 1891 char buf[64] = "";
146a1439 1892 int i, ret;
4235b045 1893
4b4da7f7 1894 /* Convert event/group name */
02b95dad
MH
1895 pev->event = strdup(tev->event);
1896 pev->group = strdup(tev->group);
1897 if (pev->event == NULL || pev->group == NULL)
1898 return -ENOMEM;
fb1587d8 1899
4b4da7f7 1900 /* Convert trace_point to probe_point */
5a6f6314 1901 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
146a1439
MH
1902 if (ret < 0)
1903 return ret;
4b4da7f7 1904
4235b045
MH
1905 /* Convert trace_arg to probe_arg */
1906 pev->nargs = tev->nargs;
e334016f
MH
1907 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1908 if (pev->args == NULL)
1909 return -ENOMEM;
02b95dad 1910 for (i = 0; i < tev->nargs && ret >= 0; i++) {
4235b045 1911 if (tev->args[i].name)
02b95dad 1912 pev->args[i].name = strdup(tev->args[i].name);
4235b045 1913 else {
0e60836b 1914 ret = synthesize_probe_trace_arg(&tev->args[i],
146a1439 1915 buf, 64);
02b95dad 1916 pev->args[i].name = strdup(buf);
4235b045 1917 }
02b95dad
MH
1918 if (pev->args[i].name == NULL && ret >= 0)
1919 ret = -ENOMEM;
1920 }
146a1439
MH
1921
1922 if (ret < 0)
1923 clear_perf_probe_event(pev);
1924
1925 return ret;
4235b045
MH
1926}
1927
1928void clear_perf_probe_event(struct perf_probe_event *pev)
1929{
7df2f329 1930 struct perf_probe_arg_field *field, *next;
4235b045
MH
1931 int i;
1932
f5385650
ACM
1933 free(pev->event);
1934 free(pev->group);
7afb3fab 1935 free(pev->target);
9b118aca 1936 clear_perf_probe_point(&pev->point);
f5385650 1937
7df2f329 1938 for (i = 0; i < pev->nargs; i++) {
f5385650
ACM
1939 free(pev->args[i].name);
1940 free(pev->args[i].var);
1941 free(pev->args[i].type);
7df2f329
MH
1942 field = pev->args[i].field;
1943 while (field) {
1944 next = field->next;
74cf249d 1945 zfree(&field->name);
7df2f329
MH
1946 free(field);
1947 field = next;
1948 }
1949 }
f5385650 1950 free(pev->args);
4235b045
MH
1951 memset(pev, 0, sizeof(*pev));
1952}
1953
0e60836b 1954static void clear_probe_trace_event(struct probe_trace_event *tev)
4235b045 1955{
0e60836b 1956 struct probe_trace_arg_ref *ref, *next;
4235b045
MH
1957 int i;
1958
f5385650
ACM
1959 free(tev->event);
1960 free(tev->group);
1961 free(tev->point.symbol);
4c859351 1962 free(tev->point.realname);
f5385650 1963 free(tev->point.module);
4235b045 1964 for (i = 0; i < tev->nargs; i++) {
f5385650
ACM
1965 free(tev->args[i].name);
1966 free(tev->args[i].value);
1967 free(tev->args[i].type);
4235b045
MH
1968 ref = tev->args[i].ref;
1969 while (ref) {
1970 next = ref->next;
1971 free(ref);
1972 ref = next;
1973 }
1974 }
f5385650 1975 free(tev->args);
4235b045 1976 memset(tev, 0, sizeof(*tev));
50656eec
MH
1977}
1978
5e45187c 1979static void print_open_warning(int err, bool is_kprobe)
225466f1 1980{
5f03cba4 1981 char sbuf[STRERR_BUFSIZE];
225466f1 1982
5e45187c 1983 if (err == -ENOENT) {
225466f1
SD
1984 const char *config;
1985
1986 if (!is_kprobe)
1987 config = "CONFIG_UPROBE_EVENTS";
1988 else
1989 config = "CONFIG_KPROBE_EVENTS";
1990
5e45187c
MH
1991 pr_warning("%cprobe_events file does not exist"
1992 " - please rebuild kernel with %s.\n",
1993 is_kprobe ? 'k' : 'u', config);
1994 } else if (err == -ENOTSUP)
23773ca1 1995 pr_warning("Tracefs or debugfs is not mounted.\n");
5e45187c
MH
1996 else
1997 pr_warning("Failed to open %cprobe_events: %s\n",
1998 is_kprobe ? 'k' : 'u',
1999 strerror_r(-err, sbuf, sizeof(sbuf)));
225466f1
SD
2000}
2001
467ec085
MH
2002static void print_both_open_warning(int kerr, int uerr)
2003{
2004 /* Both kprobes and uprobes are disabled, warn it. */
2005 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
23773ca1 2006 pr_warning("Tracefs or debugfs is not mounted.\n");
467ec085
MH
2007 else if (kerr == -ENOENT && uerr == -ENOENT)
2008 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
2009 "or/and CONFIG_UPROBE_EVENTS.\n");
2010 else {
5f03cba4 2011 char sbuf[STRERR_BUFSIZE];
467ec085
MH
2012 pr_warning("Failed to open kprobe events: %s.\n",
2013 strerror_r(-kerr, sbuf, sizeof(sbuf)));
2014 pr_warning("Failed to open uprobe events: %s.\n",
2015 strerror_r(-uerr, sbuf, sizeof(sbuf)));
2016 }
2017}
2018
5e45187c 2019static int open_probe_events(const char *trace_file, bool readwrite)
4de189fe
MH
2020{
2021 char buf[PATH_MAX];
7ca5989d 2022 const char *__debugfs;
23773ca1 2023 const char *tracing_dir = "";
4de189fe
MH
2024 int ret;
2025
23773ca1
SRRH
2026 __debugfs = tracefs_find_mountpoint();
2027 if (__debugfs == NULL) {
2028 tracing_dir = "tracing/";
7ca5989d 2029
23773ca1
SRRH
2030 __debugfs = debugfs_find_mountpoint();
2031 if (__debugfs == NULL)
2032 return -ENOTSUP;
2033 }
2034
2035 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2036 __debugfs, tracing_dir, trace_file);
146a1439 2037 if (ret >= 0) {
7ca5989d 2038 pr_debug("Opening %s write=%d\n", buf, readwrite);
146a1439 2039 if (readwrite && !probe_event_dry_run)
b8dc3984 2040 ret = open(buf, O_RDWR | O_APPEND, 0);
146a1439
MH
2041 else
2042 ret = open(buf, O_RDONLY, 0);
f4d7da49 2043
225466f1 2044 if (ret < 0)
5e45187c 2045 ret = -errno;
4de189fe
MH
2046 }
2047 return ret;
2048}
2049
225466f1
SD
2050static int open_kprobe_events(bool readwrite)
2051{
23773ca1 2052 return open_probe_events("kprobe_events", readwrite);
225466f1
SD
2053}
2054
2055static int open_uprobe_events(bool readwrite)
2056{
23773ca1 2057 return open_probe_events("uprobe_events", readwrite);
225466f1
SD
2058}
2059
2060/* Get raw string list of current kprobe_events or uprobe_events */
0e60836b 2061static struct strlist *get_probe_trace_command_rawlist(int fd)
4de189fe
MH
2062{
2063 int ret, idx;
2064 FILE *fp;
2065 char buf[MAX_CMDLEN];
2066 char *p;
2067 struct strlist *sl;
2068
4a77e218 2069 sl = strlist__new(NULL, NULL);
4de189fe
MH
2070
2071 fp = fdopen(dup(fd), "r");
2072 while (!feof(fp)) {
2073 p = fgets(buf, MAX_CMDLEN, fp);
2074 if (!p)
2075 break;
2076
2077 idx = strlen(p) - 1;
2078 if (p[idx] == '\n')
2079 p[idx] = '\0';
2080 ret = strlist__add(sl, buf);
146a1439 2081 if (ret < 0) {
6eb08660 2082 pr_debug("strlist__add failed (%d)\n", ret);
146a1439
MH
2083 strlist__delete(sl);
2084 return NULL;
2085 }
4de189fe
MH
2086 }
2087 fclose(fp);
2088
2089 return sl;
2090}
2091
9aaf5a5f
MH
2092struct kprobe_blacklist_node {
2093 struct list_head list;
2094 unsigned long start;
2095 unsigned long end;
2096 char *symbol;
2097};
2098
2099static void kprobe_blacklist__delete(struct list_head *blacklist)
2100{
2101 struct kprobe_blacklist_node *node;
2102
2103 while (!list_empty(blacklist)) {
2104 node = list_first_entry(blacklist,
2105 struct kprobe_blacklist_node, list);
2106 list_del(&node->list);
2107 free(node->symbol);
2108 free(node);
2109 }
2110}
2111
2112static int kprobe_blacklist__load(struct list_head *blacklist)
2113{
2114 struct kprobe_blacklist_node *node;
2115 const char *__debugfs = debugfs_find_mountpoint();
2116 char buf[PATH_MAX], *p;
2117 FILE *fp;
2118 int ret;
2119
2120 if (__debugfs == NULL)
2121 return -ENOTSUP;
2122
2123 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2124 if (ret < 0)
2125 return ret;
2126
2127 fp = fopen(buf, "r");
2128 if (!fp)
2129 return -errno;
2130
2131 ret = 0;
2132 while (fgets(buf, PATH_MAX, fp)) {
2133 node = zalloc(sizeof(*node));
2134 if (!node) {
2135 ret = -ENOMEM;
2136 break;
2137 }
2138 INIT_LIST_HEAD(&node->list);
2139 list_add_tail(&node->list, blacklist);
2140 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2141 ret = -EINVAL;
2142 break;
2143 }
2144 p = strchr(buf, '\t');
2145 if (p) {
2146 p++;
2147 if (p[strlen(p) - 1] == '\n')
2148 p[strlen(p) - 1] = '\0';
2149 } else
2150 p = (char *)"unknown";
2151 node->symbol = strdup(p);
2152 if (!node->symbol) {
2153 ret = -ENOMEM;
2154 break;
2155 }
2156 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2157 node->start, node->end, node->symbol);
2158 ret++;
2159 }
2160 if (ret < 0)
2161 kprobe_blacklist__delete(blacklist);
2162 fclose(fp);
2163
2164 return ret;
2165}
2166
2167static struct kprobe_blacklist_node *
2168kprobe_blacklist__find_by_address(struct list_head *blacklist,
2169 unsigned long address)
2170{
2171 struct kprobe_blacklist_node *node;
2172
2173 list_for_each_entry(node, blacklist, list) {
2174 if (node->start <= address && address <= node->end)
2175 return node;
2176 }
2177
2178 return NULL;
2179}
2180
b031220d
MH
2181static LIST_HEAD(kprobe_blacklist);
2182
2183static void kprobe_blacklist__init(void)
2184{
2185 if (!list_empty(&kprobe_blacklist))
2186 return;
2187
2188 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2189 pr_debug("No kprobe blacklist support, ignored\n");
2190}
2191
2192static void kprobe_blacklist__release(void)
2193{
2194 kprobe_blacklist__delete(&kprobe_blacklist);
2195}
2196
2197static bool kprobe_blacklist__listed(unsigned long address)
2198{
2199 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2200}
2201
d350bd57
MH
2202static int perf_probe_event__sprintf(const char *group, const char *event,
2203 struct perf_probe_event *pev,
ba7ecb02
MH
2204 const char *module,
2205 struct strbuf *result)
278498d4 2206{
7e990a51 2207 int i, ret;
278498d4 2208 char buf[128];
4235b045 2209 char *place;
278498d4 2210
4235b045
MH
2211 /* Synthesize only event probe point */
2212 place = synthesize_perf_probe_point(&pev->point);
146a1439
MH
2213 if (!place)
2214 return -EINVAL;
4235b045 2215
d350bd57 2216 ret = e_snprintf(buf, 128, "%s:%s", group, event);
7e990a51 2217 if (ret < 0)
ba7ecb02 2218 goto out;
146a1439 2219
ba7ecb02 2220 strbuf_addf(result, " %-20s (on %s", buf, place);
fb226ccd 2221 if (module)
ba7ecb02 2222 strbuf_addf(result, " in %s", module);
278498d4 2223
4235b045 2224 if (pev->nargs > 0) {
ba7ecb02 2225 strbuf_addstr(result, " with");
7df2f329 2226 for (i = 0; i < pev->nargs; i++) {
146a1439
MH
2227 ret = synthesize_perf_probe_arg(&pev->args[i],
2228 buf, 128);
2229 if (ret < 0)
ba7ecb02
MH
2230 goto out;
2231 strbuf_addf(result, " %s", buf);
7df2f329 2232 }
278498d4 2233 }
ba7ecb02
MH
2234 strbuf_addch(result, ')');
2235out:
4235b045 2236 free(place);
146a1439 2237 return ret;
278498d4
MH
2238}
2239
ba7ecb02 2240/* Show an event */
d350bd57
MH
2241static int show_perf_probe_event(const char *group, const char *event,
2242 struct perf_probe_event *pev,
ba7ecb02
MH
2243 const char *module, bool use_stdout)
2244{
2245 struct strbuf buf = STRBUF_INIT;
2246 int ret;
2247
d350bd57 2248 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
ba7ecb02
MH
2249 if (ret >= 0) {
2250 if (use_stdout)
2251 printf("%s\n", buf.buf);
2252 else
2253 pr_info("%s\n", buf.buf);
2254 }
2255 strbuf_release(&buf);
2256
2257 return ret;
2258}
2259
b6a89643
MH
2260static bool filter_probe_trace_event(struct probe_trace_event *tev,
2261 struct strfilter *filter)
2262{
2263 char tmp[128];
2264
2265 /* At first, check the event name itself */
2266 if (strfilter__compare(filter, tev->event))
2267 return true;
2268
2269 /* Next, check the combination of name and group */
2270 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2271 return false;
2272 return strfilter__compare(filter, tmp);
2273}
2274
2275static int __show_perf_probe_events(int fd, bool is_kprobe,
2276 struct strfilter *filter)
4de189fe 2277{
225466f1 2278 int ret = 0;
0e60836b 2279 struct probe_trace_event tev;
4235b045 2280 struct perf_probe_event pev;
4de189fe
MH
2281 struct strlist *rawlist;
2282 struct str_node *ent;
2283
4235b045
MH
2284 memset(&tev, 0, sizeof(tev));
2285 memset(&pev, 0, sizeof(pev));
72041334 2286
0e60836b 2287 rawlist = get_probe_trace_command_rawlist(fd);
146a1439 2288 if (!rawlist)
6eb08660 2289 return -ENOMEM;
4de189fe 2290
adf365f4 2291 strlist__for_each(ent, rawlist) {
0e60836b 2292 ret = parse_probe_trace_command(ent->s, &tev);
146a1439 2293 if (ret >= 0) {
b6a89643
MH
2294 if (!filter_probe_trace_event(&tev, filter))
2295 goto next;
225466f1
SD
2296 ret = convert_to_perf_probe_event(&tev, &pev,
2297 is_kprobe);
ba7ecb02
MH
2298 if (ret < 0)
2299 goto next;
d350bd57
MH
2300 ret = show_perf_probe_event(pev.group, pev.event,
2301 &pev, tev.point.module,
ba7ecb02 2302 true);
146a1439 2303 }
b6a89643 2304next:
4235b045 2305 clear_perf_probe_event(&pev);
0e60836b 2306 clear_probe_trace_event(&tev);
146a1439
MH
2307 if (ret < 0)
2308 break;
4de189fe 2309 }
4de189fe 2310 strlist__delete(rawlist);
7737af01
MH
2311 /* Cleanup cached debuginfo if needed */
2312 debuginfo_cache__exit();
146a1439
MH
2313
2314 return ret;
4de189fe
MH
2315}
2316
225466f1 2317/* List up current perf-probe events */
b6a89643 2318int show_perf_probe_events(struct strfilter *filter)
225466f1 2319{
5e45187c 2320 int kp_fd, up_fd, ret;
225466f1
SD
2321
2322 setup_pager();
225466f1 2323
ee45b6c2 2324 ret = init_symbol_maps(false);
225466f1
SD
2325 if (ret < 0)
2326 return ret;
2327
5e45187c
MH
2328 kp_fd = open_kprobe_events(false);
2329 if (kp_fd >= 0) {
b6a89643 2330 ret = __show_perf_probe_events(kp_fd, true, filter);
5e45187c
MH
2331 close(kp_fd);
2332 if (ret < 0)
2333 goto out;
2334 }
225466f1 2335
5e45187c
MH
2336 up_fd = open_uprobe_events(false);
2337 if (kp_fd < 0 && up_fd < 0) {
467ec085 2338 print_both_open_warning(kp_fd, up_fd);
5e45187c
MH
2339 ret = kp_fd;
2340 goto out;
225466f1
SD
2341 }
2342
5e45187c 2343 if (up_fd >= 0) {
b6a89643 2344 ret = __show_perf_probe_events(up_fd, false, filter);
5e45187c
MH
2345 close(up_fd);
2346 }
2347out:
ee45b6c2 2348 exit_symbol_maps();
225466f1
SD
2349 return ret;
2350}
2351
b498ce1f 2352/* Get current perf-probe event names */
0e60836b 2353static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
b498ce1f 2354{
fa28244d 2355 char buf[128];
b498ce1f
MH
2356 struct strlist *sl, *rawlist;
2357 struct str_node *ent;
0e60836b 2358 struct probe_trace_event tev;
146a1439 2359 int ret = 0;
b498ce1f 2360
4235b045 2361 memset(&tev, 0, sizeof(tev));
0e60836b 2362 rawlist = get_probe_trace_command_rawlist(fd);
6eb08660
MH
2363 if (!rawlist)
2364 return NULL;
4a77e218 2365 sl = strlist__new(NULL, NULL);
adf365f4 2366 strlist__for_each(ent, rawlist) {
0e60836b 2367 ret = parse_probe_trace_command(ent->s, &tev);
146a1439
MH
2368 if (ret < 0)
2369 break;
fa28244d 2370 if (include_group) {
146a1439
MH
2371 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2372 tev.event);
2373 if (ret >= 0)
2374 ret = strlist__add(sl, buf);
fa28244d 2375 } else
146a1439 2376 ret = strlist__add(sl, tev.event);
0e60836b 2377 clear_probe_trace_event(&tev);
146a1439
MH
2378 if (ret < 0)
2379 break;
b498ce1f 2380 }
b498ce1f
MH
2381 strlist__delete(rawlist);
2382
146a1439
MH
2383 if (ret < 0) {
2384 strlist__delete(sl);
2385 return NULL;
2386 }
b498ce1f
MH
2387 return sl;
2388}
2389
0e60836b 2390static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
50656eec 2391{
6eca8cc3 2392 int ret = 0;
0e60836b 2393 char *buf = synthesize_probe_trace_command(tev);
5f03cba4 2394 char sbuf[STRERR_BUFSIZE];
50656eec 2395
146a1439 2396 if (!buf) {
0e60836b 2397 pr_debug("Failed to synthesize probe trace event.\n");
146a1439
MH
2398 return -EINVAL;
2399 }
2400
fa28244d 2401 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
2402 if (!probe_event_dry_run) {
2403 ret = write(fd, buf, strlen(buf));
7949ba1f
NK
2404 if (ret <= 0) {
2405 ret = -errno;
146a1439 2406 pr_warning("Failed to write event: %s\n",
5f03cba4 2407 strerror_r(errno, sbuf, sizeof(sbuf)));
7949ba1f 2408 }
f4d7da49 2409 }
4235b045 2410 free(buf);
146a1439 2411 return ret;
50656eec
MH
2412}
2413
146a1439
MH
2414static int get_new_event_name(char *buf, size_t len, const char *base,
2415 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
2416{
2417 int i, ret;
35a23ff9 2418 char *p;
17f88fcd 2419
3099c026
NR
2420 if (*base == '.')
2421 base++;
2422
17f88fcd
MH
2423 /* Try no suffix */
2424 ret = e_snprintf(buf, len, "%s", base);
146a1439 2425 if (ret < 0) {
5f03cba4 2426 pr_debug("snprintf() failed: %d\n", ret);
146a1439
MH
2427 return ret;
2428 }
35a23ff9
MH
2429 /* Cut off the postfixes (e.g. .const, .isra)*/
2430 p = strchr(buf, '.');
2431 if (p && p != buf)
2432 *p = '\0';
17f88fcd 2433 if (!strlist__has_entry(namelist, buf))
146a1439 2434 return 0;
17f88fcd 2435
d761b08b
MH
2436 if (!allow_suffix) {
2437 pr_warning("Error: event \"%s\" already exists. "
2438 "(Use -f to force duplicates.)\n", base);
146a1439 2439 return -EEXIST;
d761b08b
MH
2440 }
2441
17f88fcd
MH
2442 /* Try to add suffix */
2443 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f 2444 ret = e_snprintf(buf, len, "%s_%d", base, i);
146a1439 2445 if (ret < 0) {
5f03cba4 2446 pr_debug("snprintf() failed: %d\n", ret);
146a1439
MH
2447 return ret;
2448 }
b498ce1f
MH
2449 if (!strlist__has_entry(namelist, buf))
2450 break;
2451 }
146a1439
MH
2452 if (i == MAX_EVENT_INDEX) {
2453 pr_warning("Too many events are on the same function.\n");
2454 ret = -ERANGE;
2455 }
2456
2457 return ret;
b498ce1f
MH
2458}
2459
79702f61
MH
2460/* Warn if the current kernel's uprobe implementation is old */
2461static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2462{
2463 int i;
2464 char *buf = synthesize_probe_trace_command(tev);
2465
2466 /* Old uprobe event doesn't support memory dereference */
2467 if (!tev->uprobes || tev->nargs == 0 || !buf)
2468 goto out;
2469
2470 for (i = 0; i < tev->nargs; i++)
2471 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2472 pr_warning("Please upgrade your kernel to at least "
2473 "3.14 to have access to feature %s\n",
2474 tev->args[i].value);
2475 break;
2476 }
2477out:
2478 free(buf);
2479}
2480
0e60836b
SD
2481static int __add_probe_trace_events(struct perf_probe_event *pev,
2482 struct probe_trace_event *tevs,
146a1439 2483 int ntevs, bool allow_suffix)
50656eec 2484{
146a1439 2485 int i, fd, ret;
0e60836b 2486 struct probe_trace_event *tev = NULL;
4235b045 2487 char buf[64];
d350bd57 2488 const char *event = NULL, *group = NULL;
b498ce1f 2489 struct strlist *namelist;
4c859351 2490 bool safename;
50656eec 2491
225466f1
SD
2492 if (pev->uprobes)
2493 fd = open_uprobe_events(true);
2494 else
2495 fd = open_kprobe_events(true);
2496
5e45187c
MH
2497 if (fd < 0) {
2498 print_open_warning(fd, !pev->uprobes);
146a1439 2499 return fd;
5e45187c
MH
2500 }
2501
b498ce1f 2502 /* Get current event names */
0e60836b 2503 namelist = get_probe_trace_event_names(fd, false);
146a1439
MH
2504 if (!namelist) {
2505 pr_debug("Failed to get current event list.\n");
ae2cb1ac
MH
2506 ret = -ENOMEM;
2507 goto close_out;
146a1439 2508 }
4235b045 2509
4c859351 2510 safename = (pev->point.function && !strisglob(pev->point.function));
146a1439 2511 ret = 0;
5e17b28f 2512 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
02b95dad 2513 for (i = 0; i < ntevs; i++) {
4235b045 2514 tev = &tevs[i];
b031220d 2515 /* Skip if the symbol is out of .text or blacklisted */
5a51fcd1
MH
2516 if (!tev->point.symbol)
2517 continue;
9aaf5a5f 2518
4235b045
MH
2519 if (pev->event)
2520 event = pev->event;
2521 else
4c859351 2522 if (safename)
4235b045
MH
2523 event = pev->point.function;
2524 else
4c859351 2525 event = tev->point.realname;
4235b045
MH
2526 if (pev->group)
2527 group = pev->group;
2528 else
2529 group = PERFPROBE_GROUP;
2530
2531 /* Get an unused new event name */
146a1439
MH
2532 ret = get_new_event_name(buf, 64, event,
2533 namelist, allow_suffix);
2534 if (ret < 0)
2535 break;
4235b045
MH
2536 event = buf;
2537
02b95dad
MH
2538 tev->event = strdup(event);
2539 tev->group = strdup(group);
2540 if (tev->event == NULL || tev->group == NULL) {
2541 ret = -ENOMEM;
2542 break;
2543 }
0e60836b 2544 ret = write_probe_trace_event(fd, tev);
146a1439
MH
2545 if (ret < 0)
2546 break;
4235b045
MH
2547 /* Add added event name to namelist */
2548 strlist__add(namelist, event);
2549
d350bd57
MH
2550 /* We use tev's name for showing new events */
2551 show_perf_probe_event(tev->group, tev->event, pev,
2552 tev->point.module, false);
2553 /* Save the last valid name */
2554 event = tev->event;
2555 group = tev->group;
4235b045
MH
2556
2557 /*
2558 * Probes after the first probe which comes from same
2559 * user input are always allowed to add suffix, because
2560 * there might be several addresses corresponding to
2561 * one code line.
2562 */
2563 allow_suffix = true;
50656eec 2564 }
79702f61
MH
2565 if (ret == -EINVAL && pev->uprobes)
2566 warn_uprobe_event_compat(tev);
146a1439 2567
9aaf5a5f 2568 /* Note that it is possible to skip all events because of blacklist */
d350bd57 2569 if (ret >= 0 && event) {
146a1439 2570 /* Show how to use the event. */
5e17b28f 2571 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
d350bd57 2572 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
146a1439 2573 }
a9b495b0 2574
e1d2017b 2575 strlist__delete(namelist);
ae2cb1ac 2576close_out:
50656eec 2577 close(fd);
146a1439 2578 return ret;
50656eec 2579}
fa28244d 2580
6bb536cc
WN
2581static int find_probe_functions(struct map *map, char *name,
2582 struct symbol **syms)
eb948e50 2583{
564c62a4 2584 int found = 0;
0a3873a8 2585 struct symbol *sym;
4c859351 2586 struct rb_node *tmp;
564c62a4 2587
75e4a2a6
WN
2588 if (map__load(map, NULL) < 0)
2589 return 0;
2590
4c859351 2591 map__for_each_symbol(map, sym, tmp) {
6bb536cc 2592 if (strglobmatch(sym->name, name)) {
4c859351 2593 found++;
6bb536cc
WN
2594 if (syms && found < probe_conf.max_probes)
2595 syms[found - 1] = sym;
2596 }
eb948e50 2597 }
564c62a4
NK
2598
2599 return found;
eb948e50
MH
2600}
2601
2602#define strdup_or_goto(str, label) \
2603 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2604
7b6ff0bd
NR
2605void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2606 struct probe_trace_event *tev __maybe_unused,
2607 struct map *map __maybe_unused) { }
2608
eb948e50
MH
2609/*
2610 * Find probe function addresses from map.
2611 * Return an error or the number of found probe_trace_event
2612 */
2613static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
ddb2f58f 2614 struct probe_trace_event **tevs)
e0faa8d3 2615{
eb948e50 2616 struct map *map = NULL;
eb948e50 2617 struct ref_reloc_sym *reloc_sym = NULL;
e0faa8d3 2618 struct symbol *sym;
6bb536cc 2619 struct symbol **syms = NULL;
0e60836b 2620 struct probe_trace_event *tev;
eb948e50
MH
2621 struct perf_probe_point *pp = &pev->point;
2622 struct probe_trace_point *tp;
564c62a4 2623 int num_matched_functions;
b031220d 2624 int ret, i, j, skipped = 0;
4235b045 2625
44225521 2626 map = get_target_map(pev->target, pev->uprobes);
eb948e50
MH
2627 if (!map) {
2628 ret = -EINVAL;
2629 goto out;
fb7345bb
MH
2630 }
2631
6bb536cc
WN
2632 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2633 if (!syms) {
2634 ret = -ENOMEM;
2635 goto out;
2636 }
2637
eb948e50
MH
2638 /*
2639 * Load matched symbols: Since the different local symbols may have
2640 * same name but different addresses, this lists all the symbols.
2641 */
6bb536cc 2642 num_matched_functions = find_probe_functions(map, pp->function, syms);
564c62a4 2643 if (num_matched_functions == 0) {
eb948e50 2644 pr_err("Failed to find symbol %s in %s\n", pp->function,
44225521 2645 pev->target ? : "kernel");
eb948e50
MH
2646 ret = -ENOENT;
2647 goto out;
ddb2f58f 2648 } else if (num_matched_functions > probe_conf.max_probes) {
eb948e50 2649 pr_err("Too many functions matched in %s\n",
44225521 2650 pev->target ? : "kernel");
eb948e50
MH
2651 ret = -E2BIG;
2652 goto out;
fb7345bb
MH
2653 }
2654
25dd9171 2655 if (!pev->uprobes && !pp->retprobe) {
0560a0c4 2656 reloc_sym = kernel_get_ref_reloc_sym();
eb948e50
MH
2657 if (!reloc_sym) {
2658 pr_warning("Relocated base symbol is not found!\n");
2659 ret = -EINVAL;
2660 goto out;
2661 }
2662 }
4235b045 2663
eb948e50
MH
2664 /* Setup result trace-probe-events */
2665 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2666 if (!*tevs) {
02b95dad 2667 ret = -ENOMEM;
eb948e50 2668 goto out;
02b95dad 2669 }
ce27a443 2670
eb948e50 2671 ret = 0;
564c62a4 2672
6bb536cc
WN
2673 for (j = 0; j < num_matched_functions; j++) {
2674 sym = syms[j];
2675
eb948e50
MH
2676 tev = (*tevs) + ret;
2677 tp = &tev->point;
2678 if (ret == num_matched_functions) {
2679 pr_warning("Too many symbols are listed. Skip it.\n");
2680 break;
ce27a443 2681 }
eb948e50 2682 ret++;
ce27a443 2683
eb948e50
MH
2684 if (pp->offset > sym->end - sym->start) {
2685 pr_warning("Offset %ld is bigger than the size of %s\n",
2686 pp->offset, sym->name);
2687 ret = -ENOENT;
2688 goto err_out;
2689 }
2690 /* Add one probe point */
2691 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
b031220d
MH
2692 /* If we found a wrong one, mark it by NULL symbol */
2693 if (!pev->uprobes &&
2694 kprobe_warn_out_range(sym->name, tp->address)) {
2695 tp->symbol = NULL; /* Skip it */
2696 skipped++;
2697 } else if (reloc_sym) {
eb948e50
MH
2698 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2699 tp->offset = tp->address - reloc_sym->addr;
2700 } else {
2701 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2702 tp->offset = pp->offset;
2703 }
6bb536cc
WN
2704 tp->realname = strdup_or_goto(sym->name, nomem_out);
2705
eb948e50 2706 tp->retprobe = pp->retprobe;
44225521
MH
2707 if (pev->target)
2708 tev->point.module = strdup_or_goto(pev->target,
2709 nomem_out);
eb948e50
MH
2710 tev->uprobes = pev->uprobes;
2711 tev->nargs = pev->nargs;
2712 if (tev->nargs) {
2713 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2714 tev->nargs);
2715 if (tev->args == NULL)
2716 goto nomem_out;
e334016f 2717 }
48481938 2718 for (i = 0; i < tev->nargs; i++) {
eb948e50
MH
2719 if (pev->args[i].name)
2720 tev->args[i].name =
2721 strdup_or_goto(pev->args[i].name,
2722 nomem_out);
2723
2724 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2725 nomem_out);
2726 if (pev->args[i].type)
2727 tev->args[i].type =
2728 strdup_or_goto(pev->args[i].type,
2729 nomem_out);
48481938 2730 }
7b6ff0bd 2731 arch__fix_tev_from_maps(pev, tev, map);
4235b045 2732 }
b031220d
MH
2733 if (ret == skipped) {
2734 ret = -ENOENT;
2735 goto err_out;
2736 }
4235b045 2737
eb948e50 2738out:
9b118aca 2739 put_target_map(map, pev->uprobes);
6bb536cc 2740 free(syms);
eb948e50 2741 return ret;
225466f1 2742
eb948e50
MH
2743nomem_out:
2744 ret = -ENOMEM;
2745err_out:
2746 clear_probe_trace_events(*tevs, num_matched_functions);
2747 zfree(tevs);
2748 goto out;
2749}
1c1bc922 2750
d5c2e2c1
NR
2751bool __weak arch__prefers_symtab(void) { return false; }
2752
eb948e50 2753static int convert_to_probe_trace_events(struct perf_probe_event *pev,
ddb2f58f 2754 struct probe_trace_event **tevs)
eb948e50
MH
2755{
2756 int ret;
2757
2758 if (pev->uprobes && !pev->group) {
2759 /* Replace group name if not given */
44225521 2760 ret = convert_exec_to_group(pev->target, &pev->group);
eb948e50
MH
2761 if (ret != 0) {
2762 pr_warning("Failed to make a group name.\n");
2763 return ret;
2764 }
02b95dad 2765 }
e334016f 2766
d5c2e2c1 2767 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
ddb2f58f 2768 ret = find_probe_trace_events_from_map(pev, tevs);
d5c2e2c1
NR
2769 if (ret > 0)
2770 return ret; /* Found in symbol table */
2771 }
2772
eb948e50 2773 /* Convert perf_probe_event with debuginfo */
ddb2f58f 2774 ret = try_to_find_probe_trace_events(pev, tevs);
eb948e50
MH
2775 if (ret != 0)
2776 return ret; /* Found in debuginfo or got an error */
2777
ddb2f58f 2778 return find_probe_trace_events_from_map(pev, tevs);
4235b045
MH
2779}
2780
2781struct __event_package {
2782 struct perf_probe_event *pev;
0e60836b 2783 struct probe_trace_event *tevs;
4235b045
MH
2784 int ntevs;
2785};
2786
ddb2f58f 2787int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
4235b045 2788{
146a1439 2789 int i, j, ret;
4235b045
MH
2790 struct __event_package *pkgs;
2791
225466f1 2792 ret = 0;
e334016f 2793 pkgs = zalloc(sizeof(struct __event_package) * npevs);
225466f1 2794
e334016f
MH
2795 if (pkgs == NULL)
2796 return -ENOMEM;
4235b045 2797
ee45b6c2 2798 ret = init_symbol_maps(pevs->uprobes);
449e5b24
MH
2799 if (ret < 0) {
2800 free(pkgs);
146a1439 2801 return ret;
449e5b24 2802 }
4235b045
MH
2803
2804 /* Loop 1: convert all events */
2805 for (i = 0; i < npevs; i++) {
2806 pkgs[i].pev = &pevs[i];
b031220d
MH
2807 /* Init kprobe blacklist if needed */
2808 if (!pkgs[i].pev->uprobes)
2809 kprobe_blacklist__init();
4235b045 2810 /* Convert with or without debuginfo */
0e60836b 2811 ret = convert_to_probe_trace_events(pkgs[i].pev,
ddb2f58f 2812 &pkgs[i].tevs);
146a1439
MH
2813 if (ret < 0)
2814 goto end;
2815 pkgs[i].ntevs = ret;
e0faa8d3 2816 }
b031220d
MH
2817 /* This just release blacklist only if allocated */
2818 kprobe_blacklist__release();
e0faa8d3 2819
4235b045 2820 /* Loop 2: add all events */
8635bf6e 2821 for (i = 0; i < npevs; i++) {
0e60836b 2822 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
ddb2f58f
MH
2823 pkgs[i].ntevs,
2824 probe_conf.force_add);
fbee632d
ACM
2825 if (ret < 0)
2826 break;
2827 }
146a1439 2828end:
449e5b24
MH
2829 /* Loop 3: cleanup and free trace events */
2830 for (i = 0; i < npevs; i++) {
146a1439 2831 for (j = 0; j < pkgs[i].ntevs; j++)
0e60836b 2832 clear_probe_trace_event(&pkgs[i].tevs[j]);
74cf249d 2833 zfree(&pkgs[i].tevs);
449e5b24
MH
2834 }
2835 free(pkgs);
ee45b6c2 2836 exit_symbol_maps();
146a1439
MH
2837
2838 return ret;
e0faa8d3
MH
2839}
2840
0e60836b 2841static int __del_trace_probe_event(int fd, struct str_node *ent)
bbbb521b
MH
2842{
2843 char *p;
2844 char buf[128];
4235b045 2845 int ret;
bbbb521b 2846
0e60836b 2847 /* Convert from perf-probe event to trace-probe event */
146a1439
MH
2848 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2849 if (ret < 0)
2850 goto error;
2851
bbbb521b 2852 p = strchr(buf + 2, ':');
146a1439
MH
2853 if (!p) {
2854 pr_debug("Internal error: %s should have ':' but not.\n",
2855 ent->s);
2856 ret = -ENOTSUP;
2857 goto error;
2858 }
bbbb521b
MH
2859 *p = '/';
2860
4235b045
MH
2861 pr_debug("Writing event: %s\n", buf);
2862 ret = write(fd, buf, strlen(buf));
44a56040
MH
2863 if (ret < 0) {
2864 ret = -errno;
146a1439 2865 goto error;
44a56040 2866 }
146a1439 2867
5e17b28f 2868 pr_info("Removed event: %s\n", ent->s);
146a1439
MH
2869 return 0;
2870error:
5f03cba4
MH
2871 pr_warning("Failed to delete event: %s\n",
2872 strerror_r(-ret, buf, sizeof(buf)));
146a1439 2873 return ret;
bbbb521b
MH
2874}
2875
307a464b
MH
2876static int del_trace_probe_events(int fd, struct strfilter *filter,
2877 struct strlist *namelist)
fa28244d 2878{
307a464b
MH
2879 struct str_node *ent;
2880 const char *p;
6dbe31f7 2881 int ret = -ENOENT;
fa28244d 2882
307a464b
MH
2883 if (!namelist)
2884 return -ENOENT;
2885
2886 strlist__for_each(ent, namelist) {
2887 p = strchr(ent->s, ':');
2888 if ((p && strfilter__compare(filter, p + 1)) ||
2889 strfilter__compare(filter, ent->s)) {
0e60836b 2890 ret = __del_trace_probe_event(fd, ent);
307a464b
MH
2891 if (ret < 0)
2892 break;
bbbb521b
MH
2893 }
2894 }
146a1439
MH
2895
2896 return ret;
fa28244d
MH
2897}
2898
307a464b 2899int del_perf_probe_events(struct strfilter *filter)
fa28244d 2900{
307a464b 2901 int ret, ret2, ufd = -1, kfd = -1;
225466f1 2902 struct strlist *namelist = NULL, *unamelist = NULL;
307a464b
MH
2903 char *str = strfilter__string(filter);
2904
2905 if (!str)
2906 return -EINVAL;
2907
2908 pr_debug("Delete filter: \'%s\'\n", str);
146a1439 2909
fa28244d 2910 /* Get current event names */
225466f1 2911 kfd = open_kprobe_events(true);
467ec085
MH
2912 if (kfd >= 0)
2913 namelist = get_probe_trace_event_names(kfd, true);
225466f1 2914
225466f1 2915 ufd = open_uprobe_events(true);
467ec085 2916 if (ufd >= 0)
225466f1
SD
2917 unamelist = get_probe_trace_event_names(ufd, true);
2918
467ec085
MH
2919 if (kfd < 0 && ufd < 0) {
2920 print_both_open_warning(kfd, ufd);
307a464b 2921 ret = kfd;
467ec085
MH
2922 goto error;
2923 }
2924
307a464b
MH
2925 ret = del_trace_probe_events(kfd, filter, namelist);
2926 if (ret < 0 && ret != -ENOENT)
225466f1 2927 goto error;
225466f1 2928
307a464b 2929 ret2 = del_trace_probe_events(ufd, filter, unamelist);
dddc7ee3 2930 if (ret2 < 0 && ret2 != -ENOENT) {
307a464b 2931 ret = ret2;
dddc7ee3
MH
2932 goto error;
2933 }
2934 if (ret == -ENOENT && ret2 == -ENOENT)
307a464b
MH
2935 pr_debug("\"%s\" does not hit any event.\n", str);
2936 /* Note that this is silently ignored */
dddc7ee3 2937 ret = 0;
225466f1
SD
2938
2939error:
2940 if (kfd >= 0) {
a23c4dc4 2941 strlist__delete(namelist);
225466f1
SD
2942 close(kfd);
2943 }
2944
2945 if (ufd >= 0) {
a23c4dc4 2946 strlist__delete(unamelist);
225466f1
SD
2947 close(ufd);
2948 }
307a464b 2949 free(str);
146a1439
MH
2950
2951 return ret;
fa28244d 2952}
225466f1 2953
3c42258c
MH
2954/* TODO: don't use a global variable for filter ... */
2955static struct strfilter *available_func_filter;
fa28244d 2956
e80711ca 2957/*
3c42258c
MH
2958 * If a symbol corresponds to a function with global binding and
2959 * matches filter return 0. For all others return 1.
e80711ca 2960 */
1d037ca1 2961static int filter_available_functions(struct map *map __maybe_unused,
3c42258c 2962 struct symbol *sym)
e80711ca 2963{
e578da3b 2964 if (strfilter__compare(available_func_filter, sym->name))
3c42258c
MH
2965 return 0;
2966 return 1;
e80711ca
MH
2967}
2968
2df58634
MH
2969int show_available_funcs(const char *target, struct strfilter *_filter,
2970 bool user)
e80711ca
MH
2971{
2972 struct map *map;
2973 int ret;
2974
2df58634 2975 ret = init_symbol_maps(user);
e80711ca
MH
2976 if (ret < 0)
2977 return ret;
2978
2df58634
MH
2979 /* Get a symbol map */
2980 if (user)
2981 map = dso__new_map(target);
2982 else
2983 map = kernel_get_module_map(target);
e80711ca 2984 if (!map) {
2df58634 2985 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
e80711ca
MH
2986 return -EINVAL;
2987 }
225466f1 2988
2df58634 2989 /* Load symbols with given filter */
3c42258c 2990 available_func_filter = _filter;
2df58634
MH
2991 if (map__load(map, filter_available_functions)) {
2992 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2993 goto end;
2994 }
2995 if (!dso__sorted_by_name(map->dso, map->type))
2996 dso__sort_by_name(map->dso, map->type);
225466f1 2997
2df58634
MH
2998 /* Show all (filtered) symbols */
2999 setup_pager();
3000 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
3001end:
3002 if (user) {
84c2cafa 3003 map__put(map);
2df58634
MH
3004 }
3005 exit_symbol_maps();
225466f1 3006
2df58634 3007 return ret;
225466f1
SD
3008}
3009