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