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