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