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