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