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