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