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