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