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