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