perf tools: Initialize reference counts in map__clone()
[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"
553873e1 43#include <api/fs/debugfs.h>
23773ca1 44#include <api/fs/tracefs.h>
1d037ca1 45#include "trace-event.h" /* For __maybe_unused */
50656eec 46#include "probe-event.h"
4235b045 47#include "probe-finder.h"
92f6c72e 48#include "probe-file.h"
225466f1 49#include "session.h"
50656eec
MH
50
51#define MAX_CMDLEN 256
50656eec
MH
52#define PERFPROBE_GROUP "probe"
53
f4d7da49 54bool probe_event_dry_run; /* Dry run flag */
ddb2f58f 55struct probe_conf probe_conf;
f4d7da49 56
146a1439 57#define semantic_error(msg ...) pr_err("Semantic error :" msg)
50656eec 58
92f6c72e 59int e_snprintf(char *str, size_t size, const char *format, ...)
4de189fe
MH
60{
61 int ret;
62 va_list ap;
63 va_start(ap, format);
64 ret = vsnprintf(str, size, format, ap);
65 va_end(ap);
66 if (ret >= (int)size)
67 ret = -E2BIG;
68 return ret;
69}
70
4b4da7f7 71static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
ee45b6c2 72static struct machine *host_machine;
e0faa8d3 73
469b9b88 74/* Initialize symbol maps and path of vmlinux/modules */
ee45b6c2 75static int init_symbol_maps(bool user_only)
e0faa8d3 76{
146a1439
MH
77 int ret;
78
e0faa8d3 79 symbol_conf.sort_by_name = true;
680d926a 80 symbol_conf.allow_aliases = true;
0a7e6d1b 81 ret = symbol__init(NULL);
146a1439
MH
82 if (ret < 0) {
83 pr_debug("Failed to init symbol map.\n");
84 goto out;
85 }
e0faa8d3 86
ee45b6c2
MH
87 if (host_machine || user_only) /* already initialized */
88 return 0;
d28c6223 89
ee45b6c2
MH
90 if (symbol_conf.vmlinux_name)
91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92
93 host_machine = machine__new_host();
94 if (!host_machine) {
95 pr_debug("machine__new_host() failed.\n");
96 symbol__exit();
97 ret = -1;
469b9b88 98 }
146a1439
MH
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
e0faa8d3
MH
103}
104
ee45b6c2
MH
105static void exit_symbol_maps(void)
106{
107 if (host_machine) {
108 machine__delete(host_machine);
109 host_machine = NULL;
110 }
111 symbol__exit();
112}
113
469b9b88
MH
114static struct symbol *__find_kernel_function_by_name(const char *name,
115 struct map **mapp)
116{
ee45b6c2 117 return machine__find_kernel_function_by_name(host_machine, name, mapp,
469b9b88
MH
118 NULL);
119}
120
8f33f7de
MH
121static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
122{
123 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
124}
125
126static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
127{
128 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
129 struct kmap *kmap;
130
131 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
132 return NULL;
133
134 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
ba92732e
WN
135 if (!kmap)
136 return NULL;
8f33f7de
MH
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
f56847c2 154 ((reloc) ? 0 : map->reloc);
8f33f7de
MH
155 }
156 return 0;
157}
158
e80711ca
MH
159static struct map *kernel_get_module_map(const char *module)
160{
ee45b6c2 161 struct map_groups *grp = &host_machine->kmaps;
1eee78ae 162 struct maps *maps = &grp->maps[MAP__FUNCTION];
4bb7123d 163 struct map *pos;
e80711ca 164
14a8fd7c
MH
165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
9f2de315 167 return machine__findnew_module_map(host_machine, 0, module);
14a8fd7c 168
e80711ca
MH
169 if (!module)
170 module = "kernel";
171
4bb7123d 172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
e80711ca
MH
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
9b118aca
MH
181static struct map *get_target_map(const char *target, bool user)
182{
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193 /* Only the user map needs to be released */
84c2cafa 194 map__put(map);
9b118aca
MH
195 }
196}
197
198
fb7345bb
MH
199static int convert_exec_to_group(const char *exec, char **result)
200{
201 char *ptr1, *ptr2, *exec_copy;
202 char buf[64];
203 int ret;
204
205 exec_copy = strdup(exec);
206 if (!exec_copy)
207 return -ENOMEM;
208
209 ptr1 = basename(exec_copy);
210 if (!ptr1) {
211 ret = -EINVAL;
212 goto out;
213 }
214
215 ptr2 = strpbrk(ptr1, "-._");
216 if (ptr2)
217 *ptr2 = '\0';
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219 if (ret < 0)
220 goto out;
221
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
224
225out:
226 free(exec_copy);
227 return ret;
228}
229
9b118aca
MH
230static void clear_perf_probe_point(struct perf_probe_point *pp)
231{
232 free(pp->file);
233 free(pp->function);
234 free(pp->lazy_line);
235}
236
eb948e50
MH
237static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238{
239 int i;
240
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
243}
244
b031220d
MH
245static bool kprobe_blacklist__listed(unsigned long address);
246static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247{
7c31bb8c
HK
248 u64 etext_addr;
249
b031220d 250 /* Get the address of _etext for checking non-probable text symbol */
7c31bb8c
HK
251 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
252
253 if (etext_addr != 0 && etext_addr < address)
b031220d
MH
254 pr_warning("%s is out of .text, skip it.\n", symbol);
255 else if (kprobe_blacklist__listed(address))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol);
257 else
258 return false;
259
260 return true;
261}
262
89fe808a 263#ifdef HAVE_DWARF_SUPPORT
60fb7742
WN
264
265static int kernel_get_module_dso(const char *module, struct dso **pdso)
266{
267 struct dso *dso;
268 struct map *map;
269 const char *vmlinux_name;
270 int ret = 0;
271
272 if (module) {
3d39ac53
ACM
273 list_for_each_entry(dso, &host_machine->dsos.head, node) {
274 if (!dso->kernel)
275 continue;
60fb7742
WN
276 if (strncmp(dso->short_name + 1, module,
277 dso->short_name_len - 2) == 0)
278 goto found;
279 }
280 pr_debug("Failed to find module %s.\n", module);
281 return -ENOENT;
282 }
283
284 map = host_machine->vmlinux_maps[MAP__FUNCTION];
285 dso = map->dso;
286
287 vmlinux_name = symbol_conf.vmlinux_name;
288 dso->load_errno = 0;
289 if (vmlinux_name)
290 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
291 else
292 ret = dso__load_vmlinux_path(dso, map, NULL);
293found:
294 *pdso = dso;
295 return ret;
296}
297
9b118aca
MH
298/*
299 * Some binaries like glibc have special symbols which are on the symbol
300 * table, but not in the debuginfo. If we can find the address of the
301 * symbol from map, we can translate the address back to the probe point.
302 */
303static int find_alternative_probe_point(struct debuginfo *dinfo,
304 struct perf_probe_point *pp,
305 struct perf_probe_point *result,
306 const char *target, bool uprobes)
307{
308 struct map *map = NULL;
309 struct symbol *sym;
310 u64 address = 0;
311 int ret = -ENOENT;
312
313 /* This can work only for function-name based one */
314 if (!pp->function || pp->file)
315 return -ENOTSUP;
316
317 map = get_target_map(target, uprobes);
318 if (!map)
319 return -EINVAL;
320
321 /* Find the address of given function */
322 map__for_each_symbol_by_name(map, pp->function, sym) {
e6d7c91c
MH
323 if (uprobes)
324 address = sym->start;
325 else
326 address = map->unmap_ip(map, sym->start);
e578da3b 327 break;
9b118aca
MH
328 }
329 if (!address) {
330 ret = -ENOENT;
331 goto out;
332 }
f6c15621
WN
333 pr_debug("Symbol %s address found : %" PRIx64 "\n",
334 pp->function, address);
9b118aca
MH
335
336 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
337 result);
338 if (ret <= 0)
339 ret = (!ret) ? -ENOENT : ret;
340 else {
341 result->offset += pp->offset;
342 result->line += pp->line;
9d7b45c5 343 result->retprobe = pp->retprobe;
9b118aca
MH
344 ret = 0;
345 }
346
347out:
348 put_target_map(map, uprobes);
349 return ret;
350
351}
352
353static int get_alternative_probe_event(struct debuginfo *dinfo,
354 struct perf_probe_event *pev,
44225521 355 struct perf_probe_point *tmp)
9b118aca
MH
356{
357 int ret;
358
359 memcpy(tmp, &pev->point, sizeof(*tmp));
360 memset(&pev->point, 0, sizeof(pev->point));
361 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
44225521 362 pev->target, pev->uprobes);
9b118aca
MH
363 if (ret < 0)
364 memcpy(&pev->point, tmp, sizeof(*tmp));
365
366 return ret;
367}
a15ad2f5 368
811dd2ae
MH
369static int get_alternative_line_range(struct debuginfo *dinfo,
370 struct line_range *lr,
371 const char *target, bool user)
372{
6d4a4896
DA
373 struct perf_probe_point pp = { .function = lr->function,
374 .file = lr->file,
375 .line = lr->start };
376 struct perf_probe_point result;
811dd2ae
MH
377 int ret, len = 0;
378
6d4a4896
DA
379 memset(&result, 0, sizeof(result));
380
811dd2ae
MH
381 if (lr->end != INT_MAX)
382 len = lr->end - lr->start;
383 ret = find_alternative_probe_point(dinfo, &pp, &result,
384 target, user);
385 if (!ret) {
386 lr->function = result.function;
387 lr->file = result.file;
388 lr->start = result.line;
389 if (lr->end != INT_MAX)
390 lr->end = lr->start + len;
391 clear_perf_probe_point(&pp);
392 }
393 return ret;
394}
395
ff741783 396/* Open new debuginfo of given module */
92561cb7 397static struct debuginfo *open_debuginfo(const char *module, bool silent)
e0faa8d3 398{
a15ad2f5 399 const char *path = module;
419e8738
MH
400 char reason[STRERR_BUFSIZE];
401 struct debuginfo *ret = NULL;
402 struct dso *dso = NULL;
403 int err;
ff741783 404
a15ad2f5 405 if (!module || !strchr(module, '/')) {
419e8738
MH
406 err = kernel_get_module_dso(module, &dso);
407 if (err < 0) {
408 if (!dso || dso->load_errno == 0) {
409 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
410 strcpy(reason, "(unknown)");
411 } else
412 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
92561cb7 413 if (!silent)
419e8738
MH
414 pr_err("Failed to find the path for %s: %s\n",
415 module ?: "kernel", reason);
14a8fd7c
MH
416 return NULL;
417 }
419e8738 418 path = dso->long_name;
e0faa8d3 419 }
92561cb7
MH
420 ret = debuginfo__new(path);
421 if (!ret && !silent) {
422 pr_warning("The %s file has no debug information.\n", path);
423 if (!module || !strtailcmp(path, ".ko"))
424 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
425 else
426 pr_warning("Rebuild with -g, ");
427 pr_warning("or install an appropriate debuginfo package.\n");
428 }
429 return ret;
e0faa8d3 430}
4b4da7f7 431
7737af01
MH
432/* For caching the last debuginfo */
433static struct debuginfo *debuginfo_cache;
434static char *debuginfo_cache_path;
435
436static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
437{
438 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
439 (!debuginfo_cache_path && !module && debuginfo_cache))
440 goto out;
441
442 /* Copy module path */
443 free(debuginfo_cache_path);
444 if (module) {
445 debuginfo_cache_path = strdup(module);
446 if (!debuginfo_cache_path) {
447 debuginfo__delete(debuginfo_cache);
448 debuginfo_cache = NULL;
449 goto out;
450 }
451 }
452
453 debuginfo_cache = open_debuginfo(module, silent);
454 if (!debuginfo_cache)
455 zfree(&debuginfo_cache_path);
456out:
457 return debuginfo_cache;
458}
459
460static void debuginfo_cache__exit(void)
461{
462 debuginfo__delete(debuginfo_cache);
463 debuginfo_cache = NULL;
464 zfree(&debuginfo_cache_path);
465}
466
92561cb7 467
99ca4233
MH
468static int get_text_start_address(const char *exec, unsigned long *address)
469{
470 Elf *elf;
471 GElf_Ehdr ehdr;
472 GElf_Shdr shdr;
473 int fd, ret = -ENOENT;
474
475 fd = open(exec, O_RDONLY);
476 if (fd < 0)
477 return -errno;
478
479 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
480 if (elf == NULL)
481 return -EINVAL;
482
483 if (gelf_getehdr(elf, &ehdr) == NULL)
484 goto out;
485
486 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
487 goto out;
488
489 *address = shdr.sh_addr - shdr.sh_offset;
490 ret = 0;
491out:
492 elf_end(elf);
493 return ret;
494}
495
5a6f6314
MH
496/*
497 * Convert trace point to probe point with debuginfo
498 */
499static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
500 struct perf_probe_point *pp,
501 bool is_kprobe)
502{
503 struct debuginfo *dinfo = NULL;
504 unsigned long stext = 0;
505 u64 addr = tp->address;
506 int ret = -ENOENT;
507
508 /* convert the address to dwarf address */
509 if (!is_kprobe) {
510 if (!addr) {
511 ret = -EINVAL;
512 goto error;
513 }
514 ret = get_text_start_address(tp->module, &stext);
515 if (ret < 0)
516 goto error;
517 addr += stext;
518 } else {
519 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
520 if (addr == 0)
521 goto error;
522 addr += tp->offset;
523 }
524
525 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
526 tp->module ? : "kernel");
527
7737af01
MH
528 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
529 if (dinfo)
5a6f6314
MH
530 ret = debuginfo__find_probe_point(dinfo,
531 (unsigned long)addr, pp);
7737af01 532 else
5a6f6314 533 ret = -ENOENT;
5a6f6314
MH
534
535 if (ret > 0) {
536 pp->retprobe = tp->retprobe;
537 return 0;
538 }
539error:
540 pr_debug("Failed to find corresponding probes from debuginfo.\n");
541 return ret ? : -ENOENT;
542}
543
fb7345bb
MH
544static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
545 int ntevs, const char *exec)
546{
547 int i, ret = 0;
eb948e50 548 unsigned long stext = 0;
fb7345bb
MH
549
550 if (!exec)
551 return 0;
552
553 ret = get_text_start_address(exec, &stext);
554 if (ret < 0)
555 return ret;
556
557 for (i = 0; i < ntevs && ret >= 0; i++) {
981a2379 558 /* point.address is the addres of point.symbol + point.offset */
eb948e50 559 tevs[i].point.address -= stext;
fb7345bb 560 tevs[i].point.module = strdup(exec);
eb948e50 561 if (!tevs[i].point.module) {
fb7345bb
MH
562 ret = -ENOMEM;
563 break;
564 }
565 tevs[i].uprobes = true;
566 }
567
568 return ret;
569}
570
190b57fc
MH
571static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
572 int ntevs, const char *module)
573{
14a8fd7c
MH
574 int i, ret = 0;
575 char *tmp;
576
577 if (!module)
578 return 0;
579
580 tmp = strrchr(module, '/');
581 if (tmp) {
582 /* This is a module path -- get the module name */
583 module = strdup(tmp + 1);
584 if (!module)
585 return -ENOMEM;
586 tmp = strchr(module, '.');
587 if (tmp)
588 *tmp = '\0';
589 tmp = (char *)module; /* For free() */
590 }
591
190b57fc
MH
592 for (i = 0; i < ntevs; i++) {
593 tevs[i].point.module = strdup(module);
14a8fd7c
MH
594 if (!tevs[i].point.module) {
595 ret = -ENOMEM;
596 break;
597 }
190b57fc 598 }
14a8fd7c 599
f5385650 600 free(tmp);
14a8fd7c 601 return ret;
190b57fc
MH
602}
603
dfef99cd
MH
604/* Post processing the probe events */
605static int post_process_probe_trace_events(struct probe_trace_event *tevs,
606 int ntevs, const char *module,
607 bool uprobe)
608{
609 struct ref_reloc_sym *reloc_sym;
610 char *tmp;
5a51fcd1 611 int i, skipped = 0;
dfef99cd
MH
612
613 if (uprobe)
614 return add_exec_to_probe_trace_events(tevs, ntevs, module);
615
616 /* Note that currently ref_reloc_sym based probe is not for drivers */
617 if (module)
618 return add_module_to_probe_trace_events(tevs, ntevs, module);
619
8f33f7de 620 reloc_sym = kernel_get_ref_reloc_sym();
dfef99cd
MH
621 if (!reloc_sym) {
622 pr_warning("Relocated base symbol is not found!\n");
623 return -EINVAL;
624 }
625
626 for (i = 0; i < ntevs; i++) {
b031220d
MH
627 if (!tevs[i].point.address || tevs[i].point.retprobe)
628 continue;
629 /* If we found a wrong one, mark it by NULL symbol */
630 if (kprobe_warn_out_range(tevs[i].point.symbol,
631 tevs[i].point.address)) {
632 tmp = NULL;
633 skipped++;
634 } else {
635 tmp = strdup(reloc_sym->name);
636 if (!tmp)
637 return -ENOMEM;
dfef99cd 638 }
b031220d
MH
639 /* If we have no realname, use symbol for it */
640 if (!tevs[i].point.realname)
641 tevs[i].point.realname = tevs[i].point.symbol;
642 else
643 free(tevs[i].point.symbol);
644 tevs[i].point.symbol = tmp;
645 tevs[i].point.offset = tevs[i].point.address -
646 reloc_sym->unrelocated_addr;
dfef99cd 647 }
5a51fcd1 648 return skipped;
dfef99cd
MH
649}
650
4b4da7f7 651/* Try to find perf_probe_event with debuginfo */
0e60836b 652static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
ddb2f58f 653 struct probe_trace_event **tevs)
4b4da7f7
MH
654{
655 bool need_dwarf = perf_probe_event_need_dwarf(pev);
9b118aca 656 struct perf_probe_point tmp;
225466f1 657 struct debuginfo *dinfo;
190b57fc 658 int ntevs, ret = 0;
4b4da7f7 659
44225521 660 dinfo = open_debuginfo(pev->target, !need_dwarf);
ff741783 661 if (!dinfo) {
92561cb7 662 if (need_dwarf)
ff741783 663 return -ENOENT;
ff741783 664 pr_debug("Could not open debuginfo. Try to use symbols.\n");
4b4da7f7
MH
665 return 0;
666 }
667
dfef99cd 668 pr_debug("Try to find probe point from debuginfo.\n");
ff741783 669 /* Searching trace events corresponding to a probe event */
ddb2f58f 670 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
ff741783 671
9b118aca 672 if (ntevs == 0) { /* Not found, retry with an alternative */
44225521 673 ret = get_alternative_probe_event(dinfo, pev, &tmp);
9b118aca 674 if (!ret) {
ddb2f58f 675 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
9b118aca
MH
676 /*
677 * Write back to the original probe_event for
678 * setting appropriate (user given) event name
679 */
680 clear_perf_probe_point(&pev->point);
681 memcpy(&pev->point, &tmp, sizeof(tmp));
682 }
683 }
684
ff741783 685 debuginfo__delete(dinfo);
4b4da7f7 686
146a1439 687 if (ntevs > 0) { /* Succeeded to find trace events */
dfef99cd
MH
688 pr_debug("Found %d probe_trace_events.\n", ntevs);
689 ret = post_process_probe_trace_events(*tevs, ntevs,
44225521 690 pev->target, pev->uprobes);
5a51fcd1 691 if (ret < 0 || ret == ntevs) {
981d05ad
MH
692 clear_probe_trace_events(*tevs, ntevs);
693 zfree(tevs);
694 }
5a51fcd1
MH
695 if (ret != ntevs)
696 return ret < 0 ? ret : ntevs;
697 ntevs = 0;
698 /* Fall through */
146a1439 699 }
4b4da7f7 700
146a1439 701 if (ntevs == 0) { /* No error but failed to find probe point. */
0687eba7 702 pr_warning("Probe point '%s' not found.\n",
146a1439 703 synthesize_perf_probe_point(&pev->point));
0687eba7 704 return -ENOENT;
146a1439
MH
705 }
706 /* Error path : ntevs < 0 */
15eca306
MH
707 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
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");
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
02b95dad
MH
1196 tmp = strdup(arg);
1197 if (tmp == NULL)
1198 return -ENOMEM;
1199
3099c026 1200 if (file_spec)
02b95dad 1201 pp->file = tmp;
3099c026 1202 else
02b95dad 1203 pp->function = tmp;
50656eec
MH
1204
1205 /* Parse other options */
1206 while (ptr) {
1207 arg = ptr;
1208 c = nc;
2a9c8c36 1209 if (c == ';') { /* Lazy pattern must be the last part */
02b95dad
MH
1210 pp->lazy_line = strdup(arg);
1211 if (pp->lazy_line == NULL)
1212 return -ENOMEM;
2a9c8c36
MH
1213 break;
1214 }
1215 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
1216 if (ptr) {
1217 nc = *ptr;
1218 *ptr++ = '\0';
1219 }
1220 switch (c) {
1221 case ':': /* Line number */
1222 pp->line = strtoul(arg, &tmp, 0);
146a1439 1223 if (*tmp != '\0') {
2a9c8c36 1224 semantic_error("There is non-digit char"
146a1439
MH
1225 " in line number.\n");
1226 return -EINVAL;
1227 }
50656eec
MH
1228 break;
1229 case '+': /* Byte offset from a symbol */
1230 pp->offset = strtoul(arg, &tmp, 0);
146a1439 1231 if (*tmp != '\0') {
2a9c8c36 1232 semantic_error("There is non-digit character"
146a1439
MH
1233 " in offset.\n");
1234 return -EINVAL;
1235 }
50656eec
MH
1236 break;
1237 case '@': /* File name */
146a1439
MH
1238 if (pp->file) {
1239 semantic_error("SRC@SRC is not allowed.\n");
1240 return -EINVAL;
1241 }
02b95dad
MH
1242 pp->file = strdup(arg);
1243 if (pp->file == NULL)
1244 return -ENOMEM;
50656eec
MH
1245 break;
1246 case '%': /* Probe places */
1247 if (strcmp(arg, "return") == 0) {
1248 pp->retprobe = 1;
146a1439
MH
1249 } else { /* Others not supported yet */
1250 semantic_error("%%%s is not supported.\n", arg);
1251 return -ENOTSUP;
1252 }
50656eec 1253 break;
146a1439
MH
1254 default: /* Buggy case */
1255 pr_err("This program has a bug at %s:%d.\n",
1256 __FILE__, __LINE__);
1257 return -ENOTSUP;
50656eec
MH
1258 break;
1259 }
1260 }
1261
1262 /* Exclusion check */
146a1439 1263 if (pp->lazy_line && pp->line) {
0e43e5d2
MH
1264 semantic_error("Lazy pattern can't be used with"
1265 " line number.\n");
146a1439
MH
1266 return -EINVAL;
1267 }
2a9c8c36 1268
146a1439 1269 if (pp->lazy_line && pp->offset) {
0e43e5d2 1270 semantic_error("Lazy pattern can't be used with offset.\n");
146a1439
MH
1271 return -EINVAL;
1272 }
2a9c8c36 1273
146a1439 1274 if (pp->line && pp->offset) {
0e43e5d2 1275 semantic_error("Offset can't be used with line number.\n");
146a1439
MH
1276 return -EINVAL;
1277 }
50656eec 1278
146a1439 1279 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
2a9c8c36 1280 semantic_error("File always requires line number or "
0e43e5d2 1281 "lazy pattern.\n");
146a1439
MH
1282 return -EINVAL;
1283 }
50656eec 1284
146a1439 1285 if (pp->offset && !pp->function) {
0e43e5d2 1286 semantic_error("Offset requires an entry function.\n");
146a1439
MH
1287 return -EINVAL;
1288 }
50656eec 1289
146a1439 1290 if (pp->retprobe && !pp->function) {
0e43e5d2 1291 semantic_error("Return probe requires an entry function.\n");
146a1439
MH
1292 return -EINVAL;
1293 }
50656eec 1294
146a1439 1295 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
2a9c8c36 1296 semantic_error("Offset/Line/Lazy pattern can't be used with "
0e43e5d2 1297 "return probe.\n");
146a1439
MH
1298 return -EINVAL;
1299 }
50656eec 1300
4235b045 1301 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
1302 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1303 pp->lazy_line);
146a1439 1304 return 0;
50656eec
MH
1305}
1306
7df2f329 1307/* Parse perf-probe event argument */
146a1439 1308static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
7df2f329 1309{
b2a3c12b 1310 char *tmp, *goodname;
7df2f329
MH
1311 struct perf_probe_arg_field **fieldp;
1312
1313 pr_debug("parsing arg: %s into ", str);
1314
48481938
MH
1315 tmp = strchr(str, '=');
1316 if (tmp) {
02b95dad
MH
1317 arg->name = strndup(str, tmp - str);
1318 if (arg->name == NULL)
1319 return -ENOMEM;
11a1ca35 1320 pr_debug("name:%s ", arg->name);
48481938
MH
1321 str = tmp + 1;
1322 }
1323
11a1ca35
MH
1324 tmp = strchr(str, ':');
1325 if (tmp) { /* Type setting */
1326 *tmp = '\0';
02b95dad
MH
1327 arg->type = strdup(tmp + 1);
1328 if (arg->type == NULL)
1329 return -ENOMEM;
11a1ca35
MH
1330 pr_debug("type:%s ", arg->type);
1331 }
1332
b2a3c12b 1333 tmp = strpbrk(str, "-.[");
7df2f329
MH
1334 if (!is_c_varname(str) || !tmp) {
1335 /* A variable, register, symbol or special value */
02b95dad
MH
1336 arg->var = strdup(str);
1337 if (arg->var == NULL)
1338 return -ENOMEM;
48481938 1339 pr_debug("%s\n", arg->var);
146a1439 1340 return 0;
7df2f329
MH
1341 }
1342
b2a3c12b 1343 /* Structure fields or array element */
02b95dad
MH
1344 arg->var = strndup(str, tmp - str);
1345 if (arg->var == NULL)
1346 return -ENOMEM;
b2a3c12b 1347 goodname = arg->var;
48481938 1348 pr_debug("%s, ", arg->var);
7df2f329
MH
1349 fieldp = &arg->field;
1350
1351 do {
e334016f
MH
1352 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1353 if (*fieldp == NULL)
1354 return -ENOMEM;
b2a3c12b
MH
1355 if (*tmp == '[') { /* Array */
1356 str = tmp;
1357 (*fieldp)->index = strtol(str + 1, &tmp, 0);
7df2f329 1358 (*fieldp)->ref = true;
b2a3c12b
MH
1359 if (*tmp != ']' || tmp == str + 1) {
1360 semantic_error("Array index must be a"
1361 " number.\n");
1362 return -EINVAL;
1363 }
1364 tmp++;
1365 if (*tmp == '\0')
1366 tmp = NULL;
1367 } else { /* Structure */
1368 if (*tmp == '.') {
1369 str = tmp + 1;
1370 (*fieldp)->ref = false;
1371 } else if (tmp[1] == '>') {
1372 str = tmp + 2;
1373 (*fieldp)->ref = true;
1374 } else {
1375 semantic_error("Argument parse error: %s\n",
1376 str);
1377 return -EINVAL;
1378 }
1379 tmp = strpbrk(str, "-.[");
146a1439 1380 }
7df2f329 1381 if (tmp) {
02b95dad
MH
1382 (*fieldp)->name = strndup(str, tmp - str);
1383 if ((*fieldp)->name == NULL)
1384 return -ENOMEM;
b2a3c12b
MH
1385 if (*str != '[')
1386 goodname = (*fieldp)->name;
7df2f329
MH
1387 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1388 fieldp = &(*fieldp)->next;
1389 }
1390 } while (tmp);
02b95dad
MH
1391 (*fieldp)->name = strdup(str);
1392 if ((*fieldp)->name == NULL)
1393 return -ENOMEM;
b2a3c12b
MH
1394 if (*str != '[')
1395 goodname = (*fieldp)->name;
7df2f329 1396 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
df0faf4b 1397
b2a3c12b 1398 /* If no name is specified, set the last field name (not array index)*/
02b95dad 1399 if (!arg->name) {
b2a3c12b 1400 arg->name = strdup(goodname);
02b95dad
MH
1401 if (arg->name == NULL)
1402 return -ENOMEM;
1403 }
146a1439 1404 return 0;
7df2f329
MH
1405}
1406
4235b045 1407/* Parse perf-probe event command */
146a1439 1408int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 1409{
e1c01d61 1410 char **argv;
146a1439 1411 int argc, i, ret = 0;
fac13fd5 1412
4235b045 1413 argv = argv_split(cmd, &argc);
146a1439
MH
1414 if (!argv) {
1415 pr_debug("Failed to split arguments.\n");
1416 return -ENOMEM;
1417 }
1418 if (argc - 1 > MAX_PROBE_ARGS) {
1419 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1420 ret = -ERANGE;
1421 goto out;
1422 }
50656eec 1423 /* Parse probe point */
146a1439
MH
1424 ret = parse_perf_probe_point(argv[0], pev);
1425 if (ret < 0)
1426 goto out;
50656eec 1427
e1c01d61 1428 /* Copy arguments and ensure return probe has no C argument */
4235b045 1429 pev->nargs = argc - 1;
e334016f
MH
1430 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1431 if (pev->args == NULL) {
1432 ret = -ENOMEM;
1433 goto out;
1434 }
146a1439
MH
1435 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1436 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1437 if (ret >= 0 &&
1438 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
4235b045 1439 semantic_error("You can't specify local variable for"
146a1439
MH
1440 " kretprobe.\n");
1441 ret = -EINVAL;
1442 }
e1c01d61 1443 }
146a1439 1444out:
e1c01d61 1445 argv_free(argv);
146a1439
MH
1446
1447 return ret;
50656eec
MH
1448}
1449
4235b045
MH
1450/* Return true if this perf_probe_event requires debuginfo */
1451bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1452{
1453 int i;
1454
1455 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1456 return true;
1457
1458 for (i = 0; i < pev->nargs; i++)
48481938 1459 if (is_c_varname(pev->args[i].var))
4235b045
MH
1460 return true;
1461
1462 return false;
1463}
1464
0e60836b 1465/* Parse probe_events event into struct probe_point */
92f6c72e 1466int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
4de189fe 1467{
0e60836b 1468 struct probe_trace_point *tp = &tev->point;
4de189fe
MH
1469 char pr;
1470 char *p;
bcbd0040 1471 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
4de189fe
MH
1472 int ret, i, argc;
1473 char **argv;
1474
0e60836b 1475 pr_debug("Parsing probe_events: %s\n", cmd);
4235b045 1476 argv = argv_split(cmd, &argc);
146a1439
MH
1477 if (!argv) {
1478 pr_debug("Failed to split arguments.\n");
1479 return -ENOMEM;
1480 }
1481 if (argc < 2) {
1482 semantic_error("Too few probe arguments.\n");
1483 ret = -ERANGE;
1484 goto out;
1485 }
4de189fe
MH
1486
1487 /* Scan event and group name. */
bcbd0040
IT
1488 argv0_str = strdup(argv[0]);
1489 if (argv0_str == NULL) {
1490 ret = -ENOMEM;
1491 goto out;
1492 }
1493 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1494 fmt2_str = strtok_r(NULL, "/", &fmt);
1495 fmt3_str = strtok_r(NULL, " \t", &fmt);
1496 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1497 || fmt3_str == NULL) {
146a1439
MH
1498 semantic_error("Failed to parse event name: %s\n", argv[0]);
1499 ret = -EINVAL;
1500 goto out;
1501 }
bcbd0040
IT
1502 pr = fmt1_str[0];
1503 tev->group = strdup(fmt2_str);
1504 tev->event = strdup(fmt3_str);
1505 if (tev->group == NULL || tev->event == NULL) {
1506 ret = -ENOMEM;
1507 goto out;
1508 }
4235b045 1509 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 1510
4235b045 1511 tp->retprobe = (pr == 'r');
4de189fe 1512
190b57fc
MH
1513 /* Scan module name(if there), function name and offset */
1514 p = strchr(argv[1], ':');
1515 if (p) {
1516 tp->module = strndup(argv[1], p - argv[1]);
1517 p++;
1518 } else
1519 p = argv[1];
bcbd0040 1520 fmt1_str = strtok_r(p, "+", &fmt);
5a6f6314
MH
1521 if (fmt1_str[0] == '0') /* only the address started with 0x */
1522 tp->address = strtoul(fmt1_str, NULL, 0);
1523 else {
1524 /* Only the symbol-based probe has offset */
1525 tp->symbol = strdup(fmt1_str);
1526 if (tp->symbol == NULL) {
1527 ret = -ENOMEM;
1528 goto out;
1529 }
1530 fmt2_str = strtok_r(NULL, "", &fmt);
1531 if (fmt2_str == NULL)
1532 tp->offset = 0;
1533 else
1534 tp->offset = strtoul(fmt2_str, NULL, 10);
bcbd0040 1535 }
4de189fe 1536
4235b045 1537 tev->nargs = argc - 2;
0e60836b 1538 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
e334016f
MH
1539 if (tev->args == NULL) {
1540 ret = -ENOMEM;
1541 goto out;
1542 }
4235b045 1543 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
1544 p = strchr(argv[i + 2], '=');
1545 if (p) /* We don't need which register is assigned. */
4235b045
MH
1546 *p++ = '\0';
1547 else
1548 p = argv[i + 2];
02b95dad 1549 tev->args[i].name = strdup(argv[i + 2]);
4235b045 1550 /* TODO: parse regs and offset */
02b95dad
MH
1551 tev->args[i].value = strdup(p);
1552 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1553 ret = -ENOMEM;
1554 goto out;
1555 }
4de189fe 1556 }
146a1439
MH
1557 ret = 0;
1558out:
bcbd0040 1559 free(argv0_str);
4de189fe 1560 argv_free(argv);
146a1439 1561 return ret;
4de189fe
MH
1562}
1563
7df2f329
MH
1564/* Compose only probe arg */
1565int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1566{
1567 struct perf_probe_arg_field *field = pa->field;
1568 int ret;
1569 char *tmp = buf;
1570
48481938
MH
1571 if (pa->name && pa->var)
1572 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1573 else
1574 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
1575 if (ret <= 0)
1576 goto error;
1577 tmp += ret;
1578 len -= ret;
1579
1580 while (field) {
b2a3c12b
MH
1581 if (field->name[0] == '[')
1582 ret = e_snprintf(tmp, len, "%s", field->name);
1583 else
1584 ret = e_snprintf(tmp, len, "%s%s",
1585 field->ref ? "->" : ".", field->name);
7df2f329
MH
1586 if (ret <= 0)
1587 goto error;
1588 tmp += ret;
1589 len -= ret;
1590 field = field->next;
1591 }
11a1ca35
MH
1592
1593 if (pa->type) {
1594 ret = e_snprintf(tmp, len, ":%s", pa->type);
1595 if (ret <= 0)
1596 goto error;
1597 tmp += ret;
1598 len -= ret;
1599 }
1600
7df2f329
MH
1601 return tmp - buf;
1602error:
5f03cba4 1603 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
146a1439 1604 return ret;
7df2f329
MH
1605}
1606
4235b045
MH
1607/* Compose only probe point (not argument) */
1608static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 1609{
fb1587d8
MH
1610 char *buf, *tmp;
1611 char offs[32] = "", line[32] = "", file[32] = "";
1612 int ret, len;
4de189fe 1613
e334016f
MH
1614 buf = zalloc(MAX_CMDLEN);
1615 if (buf == NULL) {
1616 ret = -ENOMEM;
1617 goto error;
1618 }
4de189fe 1619 if (pp->offset) {
fb1587d8 1620 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
1621 if (ret <= 0)
1622 goto error;
1623 }
1624 if (pp->line) {
fb1587d8
MH
1625 ret = e_snprintf(line, 32, ":%d", pp->line);
1626 if (ret <= 0)
1627 goto error;
1628 }
1629 if (pp->file) {
32ae2ade
FBH
1630 tmp = pp->file;
1631 len = strlen(tmp);
1632 if (len > 30) {
1633 tmp = strchr(pp->file + len - 30, '/');
1634 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1635 }
1636 ret = e_snprintf(file, 32, "@%s", tmp);
4de189fe
MH
1637 if (ret <= 0)
1638 goto error;
1639 }
1640
1641 if (pp->function)
fb1587d8
MH
1642 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1643 offs, pp->retprobe ? "%return" : "", line,
1644 file);
4de189fe 1645 else
fb1587d8 1646 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
1647 if (ret <= 0)
1648 goto error;
1649
1650 return buf;
7ef17aaf 1651error:
5f03cba4 1652 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
f5385650 1653 free(buf);
146a1439 1654 return NULL;
7ef17aaf
MH
1655}
1656
4235b045
MH
1657#if 0
1658char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
1659{
1660 char *buf;
1661 int i, len, ret;
1662
4235b045
MH
1663 buf = synthesize_perf_probe_point(&pev->point);
1664 if (!buf)
1665 return NULL;
4de189fe 1666
4235b045
MH
1667 len = strlen(buf);
1668 for (i = 0; i < pev->nargs; i++) {
4de189fe 1669 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
1670 pev->args[i].name);
1671 if (ret <= 0) {
1672 free(buf);
1673 return NULL;
1674 }
4de189fe
MH
1675 len += ret;
1676 }
4de189fe 1677
4235b045
MH
1678 return buf;
1679}
1680#endif
1681
0e60836b 1682static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
4235b045
MH
1683 char **buf, size_t *buflen,
1684 int depth)
1685{
1686 int ret;
1687 if (ref->next) {
0e60836b 1688 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
4235b045
MH
1689 buflen, depth + 1);
1690 if (depth < 0)
1691 goto out;
1692 }
1693
1694 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1695 if (ret < 0)
1696 depth = ret;
1697 else {
1698 *buf += ret;
1699 *buflen -= ret;
1700 }
1701out:
1702 return depth;
4de189fe 1703
4de189fe
MH
1704}
1705
0e60836b 1706static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
4235b045 1707 char *buf, size_t buflen)
50656eec 1708{
0e60836b 1709 struct probe_trace_arg_ref *ref = arg->ref;
4235b045
MH
1710 int ret, depth = 0;
1711 char *tmp = buf;
1712
1713 /* Argument name or separator */
1714 if (arg->name)
1715 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1716 else
1717 ret = e_snprintf(buf, buflen, " ");
1718 if (ret < 0)
1719 return ret;
1720 buf += ret;
1721 buflen -= ret;
1722
b7dcb857
MH
1723 /* Special case: @XXX */
1724 if (arg->value[0] == '@' && arg->ref)
1725 ref = ref->next;
1726
4235b045 1727 /* Dereferencing arguments */
b7dcb857 1728 if (ref) {
0e60836b 1729 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
4235b045
MH
1730 &buflen, 1);
1731 if (depth < 0)
1732 return depth;
1733 }
1734
1735 /* Print argument value */
b7dcb857
MH
1736 if (arg->value[0] == '@' && arg->ref)
1737 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1738 arg->ref->offset);
1739 else
1740 ret = e_snprintf(buf, buflen, "%s", arg->value);
4235b045
MH
1741 if (ret < 0)
1742 return ret;
1743 buf += ret;
1744 buflen -= ret;
1745
1746 /* Closing */
1747 while (depth--) {
1748 ret = e_snprintf(buf, buflen, ")");
1749 if (ret < 0)
1750 return ret;
1751 buf += ret;
1752 buflen -= ret;
1753 }
4984912e
MH
1754 /* Print argument type */
1755 if (arg->type) {
1756 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1757 if (ret <= 0)
1758 return ret;
1759 buf += ret;
1760 }
4235b045
MH
1761
1762 return buf - tmp;
1763}
1764
0e60836b 1765char *synthesize_probe_trace_command(struct probe_trace_event *tev)
4235b045 1766{
0e60836b 1767 struct probe_trace_point *tp = &tev->point;
50656eec
MH
1768 char *buf;
1769 int i, len, ret;
1770
e334016f
MH
1771 buf = zalloc(MAX_CMDLEN);
1772 if (buf == NULL)
1773 return NULL;
1774
eb948e50
MH
1775 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1776 tev->group, tev->event);
1777 if (len <= 0)
1778 goto error;
1779
1780 /* Uprobes must have tp->address and tp->module */
1781 if (tev->uprobes && (!tp->address || !tp->module))
1782 goto error;
1783
1784 /* Use the tp->address for uprobes */
225466f1 1785 if (tev->uprobes)
eb948e50
MH
1786 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1787 tp->module, tp->address);
225466f1 1788 else
eb948e50 1789 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
225466f1
SD
1790 tp->module ?: "", tp->module ? ":" : "",
1791 tp->symbol, tp->offset);
1792
eb948e50 1793 if (ret <= 0)
50656eec 1794 goto error;
eb948e50 1795 len += ret;
50656eec 1796
4235b045 1797 for (i = 0; i < tev->nargs; i++) {
0e60836b 1798 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
4235b045 1799 MAX_CMDLEN - len);
4de189fe 1800 if (ret <= 0)
50656eec
MH
1801 goto error;
1802 len += ret;
1803 }
50656eec 1804
4235b045 1805 return buf;
50656eec 1806error:
4235b045
MH
1807 free(buf);
1808 return NULL;
1809}
50656eec 1810
5a6f6314
MH
1811static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1812 struct perf_probe_point *pp,
1813 bool is_kprobe)
1814{
1815 struct symbol *sym = NULL;
1816 struct map *map;
1817 u64 addr;
1818 int ret = -ENOENT;
1819
1820 if (!is_kprobe) {
1821 map = dso__new_map(tp->module);
1822 if (!map)
1823 goto out;
1824 addr = tp->address;
1825 sym = map__find_symbol(map, addr, NULL);
1826 } else {
1827 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1828 if (addr) {
1829 addr += tp->offset;
1830 sym = __find_kernel_function(addr, &map);
1831 }
1832 }
1833 if (!sym)
1834 goto out;
1835
1836 pp->retprobe = tp->retprobe;
1837 pp->offset = addr - map->unmap_ip(map, sym->start);
1838 pp->function = strdup(sym->name);
1839 ret = pp->function ? 0 : -ENOMEM;
1840
1841out:
1842 if (map && !is_kprobe) {
84c2cafa 1843 map__put(map);
5a6f6314
MH
1844 }
1845
1846 return ret;
1847}
1848
1849static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1850 struct perf_probe_point *pp,
1851 bool is_kprobe)
1852{
1853 char buf[128];
1854 int ret;
1855
1856 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1857 if (!ret)
1858 return 0;
1859 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1860 if (!ret)
1861 return 0;
1862
1863 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1864
1865 if (tp->symbol) {
1866 pp->function = strdup(tp->symbol);
1867 pp->offset = tp->offset;
1868 } else if (!tp->module && !is_kprobe) {
1869 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1870 if (ret < 0)
1871 return ret;
1872 pp->function = strdup(buf);
1873 pp->offset = 0;
1874 }
1875 if (pp->function == NULL)
1876 return -ENOMEM;
1877
1878 pp->retprobe = tp->retprobe;
1879
1880 return 0;
1881}
1882
0e60836b 1883static int convert_to_perf_probe_event(struct probe_trace_event *tev,
225466f1 1884 struct perf_probe_event *pev, bool is_kprobe)
4235b045 1885{
02b95dad 1886 char buf[64] = "";
146a1439 1887 int i, ret;
4235b045 1888
4b4da7f7 1889 /* Convert event/group name */
02b95dad
MH
1890 pev->event = strdup(tev->event);
1891 pev->group = strdup(tev->group);
1892 if (pev->event == NULL || pev->group == NULL)
1893 return -ENOMEM;
fb1587d8 1894
4b4da7f7 1895 /* Convert trace_point to probe_point */
5a6f6314 1896 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
146a1439
MH
1897 if (ret < 0)
1898 return ret;
4b4da7f7 1899
4235b045
MH
1900 /* Convert trace_arg to probe_arg */
1901 pev->nargs = tev->nargs;
e334016f
MH
1902 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1903 if (pev->args == NULL)
1904 return -ENOMEM;
02b95dad 1905 for (i = 0; i < tev->nargs && ret >= 0; i++) {
4235b045 1906 if (tev->args[i].name)
02b95dad 1907 pev->args[i].name = strdup(tev->args[i].name);
4235b045 1908 else {
0e60836b 1909 ret = synthesize_probe_trace_arg(&tev->args[i],
146a1439 1910 buf, 64);
02b95dad 1911 pev->args[i].name = strdup(buf);
4235b045 1912 }
02b95dad
MH
1913 if (pev->args[i].name == NULL && ret >= 0)
1914 ret = -ENOMEM;
1915 }
146a1439
MH
1916
1917 if (ret < 0)
1918 clear_perf_probe_event(pev);
1919
1920 return ret;
4235b045
MH
1921}
1922
1923void clear_perf_probe_event(struct perf_probe_event *pev)
1924{
7df2f329 1925 struct perf_probe_arg_field *field, *next;
4235b045
MH
1926 int i;
1927
f5385650
ACM
1928 free(pev->event);
1929 free(pev->group);
7afb3fab 1930 free(pev->target);
9b118aca 1931 clear_perf_probe_point(&pev->point);
f5385650 1932
7df2f329 1933 for (i = 0; i < pev->nargs; i++) {
f5385650
ACM
1934 free(pev->args[i].name);
1935 free(pev->args[i].var);
1936 free(pev->args[i].type);
7df2f329
MH
1937 field = pev->args[i].field;
1938 while (field) {
1939 next = field->next;
74cf249d 1940 zfree(&field->name);
7df2f329
MH
1941 free(field);
1942 field = next;
1943 }
1944 }
f5385650 1945 free(pev->args);
4235b045
MH
1946 memset(pev, 0, sizeof(*pev));
1947}
1948
92f6c72e 1949void clear_probe_trace_event(struct probe_trace_event *tev)
4235b045 1950{
0e60836b 1951 struct probe_trace_arg_ref *ref, *next;
4235b045
MH
1952 int i;
1953
f5385650
ACM
1954 free(tev->event);
1955 free(tev->group);
1956 free(tev->point.symbol);
4c859351 1957 free(tev->point.realname);
f5385650 1958 free(tev->point.module);
4235b045 1959 for (i = 0; i < tev->nargs; i++) {
f5385650
ACM
1960 free(tev->args[i].name);
1961 free(tev->args[i].value);
1962 free(tev->args[i].type);
4235b045
MH
1963 ref = tev->args[i].ref;
1964 while (ref) {
1965 next = ref->next;
1966 free(ref);
1967 ref = next;
1968 }
1969 }
f5385650 1970 free(tev->args);
4235b045 1971 memset(tev, 0, sizeof(*tev));
50656eec
MH
1972}
1973
9aaf5a5f
MH
1974struct kprobe_blacklist_node {
1975 struct list_head list;
1976 unsigned long start;
1977 unsigned long end;
1978 char *symbol;
1979};
1980
1981static void kprobe_blacklist__delete(struct list_head *blacklist)
1982{
1983 struct kprobe_blacklist_node *node;
1984
1985 while (!list_empty(blacklist)) {
1986 node = list_first_entry(blacklist,
1987 struct kprobe_blacklist_node, list);
1988 list_del(&node->list);
1989 free(node->symbol);
1990 free(node);
1991 }
1992}
1993
1994static int kprobe_blacklist__load(struct list_head *blacklist)
1995{
1996 struct kprobe_blacklist_node *node;
1997 const char *__debugfs = debugfs_find_mountpoint();
1998 char buf[PATH_MAX], *p;
1999 FILE *fp;
2000 int ret;
2001
2002 if (__debugfs == NULL)
2003 return -ENOTSUP;
2004
2005 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2006 if (ret < 0)
2007 return ret;
2008
2009 fp = fopen(buf, "r");
2010 if (!fp)
2011 return -errno;
2012
2013 ret = 0;
2014 while (fgets(buf, PATH_MAX, fp)) {
2015 node = zalloc(sizeof(*node));
2016 if (!node) {
2017 ret = -ENOMEM;
2018 break;
2019 }
2020 INIT_LIST_HEAD(&node->list);
2021 list_add_tail(&node->list, blacklist);
2022 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2023 ret = -EINVAL;
2024 break;
2025 }
2026 p = strchr(buf, '\t');
2027 if (p) {
2028 p++;
2029 if (p[strlen(p) - 1] == '\n')
2030 p[strlen(p) - 1] = '\0';
2031 } else
2032 p = (char *)"unknown";
2033 node->symbol = strdup(p);
2034 if (!node->symbol) {
2035 ret = -ENOMEM;
2036 break;
2037 }
2038 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2039 node->start, node->end, node->symbol);
2040 ret++;
2041 }
2042 if (ret < 0)
2043 kprobe_blacklist__delete(blacklist);
2044 fclose(fp);
2045
2046 return ret;
2047}
2048
2049static struct kprobe_blacklist_node *
2050kprobe_blacklist__find_by_address(struct list_head *blacklist,
2051 unsigned long address)
2052{
2053 struct kprobe_blacklist_node *node;
2054
2055 list_for_each_entry(node, blacklist, list) {
2056 if (node->start <= address && address <= node->end)
2057 return node;
2058 }
2059
2060 return NULL;
2061}
2062
b031220d
MH
2063static LIST_HEAD(kprobe_blacklist);
2064
2065static void kprobe_blacklist__init(void)
2066{
2067 if (!list_empty(&kprobe_blacklist))
2068 return;
2069
2070 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2071 pr_debug("No kprobe blacklist support, ignored\n");
2072}
2073
2074static void kprobe_blacklist__release(void)
2075{
2076 kprobe_blacklist__delete(&kprobe_blacklist);
2077}
2078
2079static bool kprobe_blacklist__listed(unsigned long address)
2080{
2081 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2082}
2083
d350bd57
MH
2084static int perf_probe_event__sprintf(const char *group, const char *event,
2085 struct perf_probe_event *pev,
ba7ecb02
MH
2086 const char *module,
2087 struct strbuf *result)
278498d4 2088{
7e990a51 2089 int i, ret;
278498d4 2090 char buf[128];
4235b045 2091 char *place;
278498d4 2092
4235b045
MH
2093 /* Synthesize only event probe point */
2094 place = synthesize_perf_probe_point(&pev->point);
146a1439
MH
2095 if (!place)
2096 return -EINVAL;
4235b045 2097
d350bd57 2098 ret = e_snprintf(buf, 128, "%s:%s", group, event);
7e990a51 2099 if (ret < 0)
ba7ecb02 2100 goto out;
146a1439 2101
ba7ecb02 2102 strbuf_addf(result, " %-20s (on %s", buf, place);
fb226ccd 2103 if (module)
ba7ecb02 2104 strbuf_addf(result, " in %s", module);
278498d4 2105
4235b045 2106 if (pev->nargs > 0) {
ba7ecb02 2107 strbuf_addstr(result, " with");
7df2f329 2108 for (i = 0; i < pev->nargs; i++) {
146a1439
MH
2109 ret = synthesize_perf_probe_arg(&pev->args[i],
2110 buf, 128);
2111 if (ret < 0)
ba7ecb02
MH
2112 goto out;
2113 strbuf_addf(result, " %s", buf);
7df2f329 2114 }
278498d4 2115 }
ba7ecb02
MH
2116 strbuf_addch(result, ')');
2117out:
4235b045 2118 free(place);
146a1439 2119 return ret;
278498d4
MH
2120}
2121
ba7ecb02 2122/* Show an event */
d350bd57
MH
2123static int show_perf_probe_event(const char *group, const char *event,
2124 struct perf_probe_event *pev,
ba7ecb02
MH
2125 const char *module, bool use_stdout)
2126{
2127 struct strbuf buf = STRBUF_INIT;
2128 int ret;
2129
d350bd57 2130 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
ba7ecb02
MH
2131 if (ret >= 0) {
2132 if (use_stdout)
2133 printf("%s\n", buf.buf);
2134 else
2135 pr_info("%s\n", buf.buf);
2136 }
2137 strbuf_release(&buf);
2138
2139 return ret;
2140}
2141
b6a89643
MH
2142static bool filter_probe_trace_event(struct probe_trace_event *tev,
2143 struct strfilter *filter)
2144{
2145 char tmp[128];
2146
2147 /* At first, check the event name itself */
2148 if (strfilter__compare(filter, tev->event))
2149 return true;
2150
2151 /* Next, check the combination of name and group */
2152 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2153 return false;
2154 return strfilter__compare(filter, tmp);
2155}
2156
2157static int __show_perf_probe_events(int fd, bool is_kprobe,
2158 struct strfilter *filter)
4de189fe 2159{
225466f1 2160 int ret = 0;
0e60836b 2161 struct probe_trace_event tev;
4235b045 2162 struct perf_probe_event pev;
4de189fe
MH
2163 struct strlist *rawlist;
2164 struct str_node *ent;
2165
4235b045
MH
2166 memset(&tev, 0, sizeof(tev));
2167 memset(&pev, 0, sizeof(pev));
72041334 2168
92f6c72e 2169 rawlist = probe_file__get_rawlist(fd);
146a1439 2170 if (!rawlist)
6eb08660 2171 return -ENOMEM;
4de189fe 2172
adf365f4 2173 strlist__for_each(ent, rawlist) {
0e60836b 2174 ret = parse_probe_trace_command(ent->s, &tev);
146a1439 2175 if (ret >= 0) {
b6a89643
MH
2176 if (!filter_probe_trace_event(&tev, filter))
2177 goto next;
225466f1
SD
2178 ret = convert_to_perf_probe_event(&tev, &pev,
2179 is_kprobe);
ba7ecb02
MH
2180 if (ret < 0)
2181 goto next;
d350bd57
MH
2182 ret = show_perf_probe_event(pev.group, pev.event,
2183 &pev, tev.point.module,
ba7ecb02 2184 true);
146a1439 2185 }
b6a89643 2186next:
4235b045 2187 clear_perf_probe_event(&pev);
0e60836b 2188 clear_probe_trace_event(&tev);
146a1439
MH
2189 if (ret < 0)
2190 break;
4de189fe 2191 }
4de189fe 2192 strlist__delete(rawlist);
7737af01
MH
2193 /* Cleanup cached debuginfo if needed */
2194 debuginfo_cache__exit();
146a1439
MH
2195
2196 return ret;
4de189fe
MH
2197}
2198
225466f1 2199/* List up current perf-probe events */
b6a89643 2200int show_perf_probe_events(struct strfilter *filter)
225466f1 2201{
5e45187c 2202 int kp_fd, up_fd, ret;
225466f1
SD
2203
2204 setup_pager();
225466f1 2205
ee45b6c2 2206 ret = init_symbol_maps(false);
225466f1
SD
2207 if (ret < 0)
2208 return ret;
2209
92f6c72e
MH
2210 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2211 if (ret < 0)
2212 return ret;
225466f1 2213
92f6c72e
MH
2214 if (kp_fd >= 0)
2215 ret = __show_perf_probe_events(kp_fd, true, filter);
2216 if (up_fd >= 0 && ret >= 0)
b6a89643 2217 ret = __show_perf_probe_events(up_fd, false, filter);
92f6c72e
MH
2218 if (kp_fd > 0)
2219 close(kp_fd);
2220 if (up_fd > 0)
5e45187c 2221 close(up_fd);
ee45b6c2 2222 exit_symbol_maps();
225466f1 2223
146a1439 2224 return ret;
50656eec
MH
2225}
2226
146a1439
MH
2227static int get_new_event_name(char *buf, size_t len, const char *base,
2228 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
2229{
2230 int i, ret;
35a23ff9 2231 char *p;
17f88fcd 2232
3099c026
NR
2233 if (*base == '.')
2234 base++;
2235
17f88fcd
MH
2236 /* Try no suffix */
2237 ret = e_snprintf(buf, len, "%s", base);
146a1439 2238 if (ret < 0) {
5f03cba4 2239 pr_debug("snprintf() failed: %d\n", ret);
146a1439
MH
2240 return ret;
2241 }
35a23ff9
MH
2242 /* Cut off the postfixes (e.g. .const, .isra)*/
2243 p = strchr(buf, '.');
2244 if (p && p != buf)
2245 *p = '\0';
17f88fcd 2246 if (!strlist__has_entry(namelist, buf))
146a1439 2247 return 0;
17f88fcd 2248
d761b08b
MH
2249 if (!allow_suffix) {
2250 pr_warning("Error: event \"%s\" already exists. "
2251 "(Use -f to force duplicates.)\n", base);
146a1439 2252 return -EEXIST;
d761b08b
MH
2253 }
2254
17f88fcd
MH
2255 /* Try to add suffix */
2256 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f 2257 ret = e_snprintf(buf, len, "%s_%d", base, i);
146a1439 2258 if (ret < 0) {
5f03cba4 2259 pr_debug("snprintf() failed: %d\n", ret);
146a1439
MH
2260 return ret;
2261 }
b498ce1f
MH
2262 if (!strlist__has_entry(namelist, buf))
2263 break;
2264 }
146a1439
MH
2265 if (i == MAX_EVENT_INDEX) {
2266 pr_warning("Too many events are on the same function.\n");
2267 ret = -ERANGE;
2268 }
2269
2270 return ret;
b498ce1f
MH
2271}
2272
79702f61
MH
2273/* Warn if the current kernel's uprobe implementation is old */
2274static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2275{
2276 int i;
2277 char *buf = synthesize_probe_trace_command(tev);
2278
2279 /* Old uprobe event doesn't support memory dereference */
2280 if (!tev->uprobes || tev->nargs == 0 || !buf)
2281 goto out;
2282
2283 for (i = 0; i < tev->nargs; i++)
2284 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2285 pr_warning("Please upgrade your kernel to at least "
2286 "3.14 to have access to feature %s\n",
2287 tev->args[i].value);
2288 break;
2289 }
2290out:
2291 free(buf);
2292}
2293
a3c9de62
MH
2294/* Set new name from original perf_probe_event and namelist */
2295static int probe_trace_event__set_name(struct probe_trace_event *tev,
2296 struct perf_probe_event *pev,
2297 struct strlist *namelist,
2298 bool allow_suffix)
2299{
2300 const char *event, *group;
2301 char buf[64];
2302 int ret;
2303
2304 if (pev->event)
2305 event = pev->event;
2306 else
2307 if (pev->point.function && !strisglob(pev->point.function))
2308 event = pev->point.function;
2309 else
2310 event = tev->point.realname;
2311 if (pev->group)
2312 group = pev->group;
2313 else
2314 group = PERFPROBE_GROUP;
2315
2316 /* Get an unused new event name */
2317 ret = get_new_event_name(buf, 64, event,
2318 namelist, allow_suffix);
2319 if (ret < 0)
2320 return ret;
2321
2322 event = buf;
2323
2324 tev->event = strdup(event);
2325 tev->group = strdup(group);
2326 if (tev->event == NULL || tev->group == NULL)
2327 return -ENOMEM;
2328
2329 /* Add added event name to namelist */
2330 strlist__add(namelist, event);
2331 return 0;
2332}
2333
0e60836b
SD
2334static int __add_probe_trace_events(struct perf_probe_event *pev,
2335 struct probe_trace_event *tevs,
146a1439 2336 int ntevs, bool allow_suffix)
50656eec 2337{
146a1439 2338 int i, fd, ret;
0e60836b 2339 struct probe_trace_event *tev = NULL;
d350bd57 2340 const char *event = NULL, *group = NULL;
b498ce1f 2341 struct strlist *namelist;
50656eec 2342
92f6c72e
MH
2343 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2344 if (fd < 0)
146a1439 2345 return fd;
5e45187c 2346
b498ce1f 2347 /* Get current event names */
92f6c72e 2348 namelist = probe_file__get_namelist(fd);
146a1439
MH
2349 if (!namelist) {
2350 pr_debug("Failed to get current event list.\n");
ae2cb1ac
MH
2351 ret = -ENOMEM;
2352 goto close_out;
146a1439 2353 }
4235b045 2354
146a1439 2355 ret = 0;
5e17b28f 2356 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
02b95dad 2357 for (i = 0; i < ntevs; i++) {
4235b045 2358 tev = &tevs[i];
b031220d 2359 /* Skip if the symbol is out of .text or blacklisted */
5a51fcd1
MH
2360 if (!tev->point.symbol)
2361 continue;
9aaf5a5f 2362
a3c9de62
MH
2363 /* Set new name for tev (and update namelist) */
2364 ret = probe_trace_event__set_name(tev, pev, namelist,
2365 allow_suffix);
146a1439
MH
2366 if (ret < 0)
2367 break;
4235b045 2368
92f6c72e 2369 ret = probe_file__add_event(fd, tev);
146a1439
MH
2370 if (ret < 0)
2371 break;
4235b045 2372
d350bd57
MH
2373 /* We use tev's name for showing new events */
2374 show_perf_probe_event(tev->group, tev->event, pev,
2375 tev->point.module, false);
2376 /* Save the last valid name */
2377 event = tev->event;
2378 group = tev->group;
4235b045
MH
2379
2380 /*
2381 * Probes after the first probe which comes from same
2382 * user input are always allowed to add suffix, because
2383 * there might be several addresses corresponding to
2384 * one code line.
2385 */
2386 allow_suffix = true;
50656eec 2387 }
79702f61
MH
2388 if (ret == -EINVAL && pev->uprobes)
2389 warn_uprobe_event_compat(tev);
146a1439 2390
9aaf5a5f 2391 /* Note that it is possible to skip all events because of blacklist */
d350bd57 2392 if (ret >= 0 && event) {
146a1439 2393 /* Show how to use the event. */
5e17b28f 2394 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
d350bd57 2395 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
146a1439 2396 }
a9b495b0 2397
e1d2017b 2398 strlist__delete(namelist);
ae2cb1ac 2399close_out:
50656eec 2400 close(fd);
146a1439 2401 return ret;
50656eec 2402}
fa28244d 2403
6bb536cc
WN
2404static int find_probe_functions(struct map *map, char *name,
2405 struct symbol **syms)
eb948e50 2406{
564c62a4 2407 int found = 0;
0a3873a8 2408 struct symbol *sym;
4c859351 2409 struct rb_node *tmp;
564c62a4 2410
75e4a2a6
WN
2411 if (map__load(map, NULL) < 0)
2412 return 0;
2413
4c859351 2414 map__for_each_symbol(map, sym, tmp) {
6bb536cc 2415 if (strglobmatch(sym->name, name)) {
4c859351 2416 found++;
6bb536cc
WN
2417 if (syms && found < probe_conf.max_probes)
2418 syms[found - 1] = sym;
2419 }
eb948e50 2420 }
564c62a4
NK
2421
2422 return found;
eb948e50
MH
2423}
2424
2425#define strdup_or_goto(str, label) \
2426 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2427
7b6ff0bd
NR
2428void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2429 struct probe_trace_event *tev __maybe_unused,
2430 struct map *map __maybe_unused) { }
2431
eb948e50
MH
2432/*
2433 * Find probe function addresses from map.
2434 * Return an error or the number of found probe_trace_event
2435 */
2436static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
ddb2f58f 2437 struct probe_trace_event **tevs)
e0faa8d3 2438{
eb948e50 2439 struct map *map = NULL;
eb948e50 2440 struct ref_reloc_sym *reloc_sym = NULL;
e0faa8d3 2441 struct symbol *sym;
6bb536cc 2442 struct symbol **syms = NULL;
0e60836b 2443 struct probe_trace_event *tev;
eb948e50
MH
2444 struct perf_probe_point *pp = &pev->point;
2445 struct probe_trace_point *tp;
564c62a4 2446 int num_matched_functions;
b031220d 2447 int ret, i, j, skipped = 0;
4235b045 2448
44225521 2449 map = get_target_map(pev->target, pev->uprobes);
eb948e50
MH
2450 if (!map) {
2451 ret = -EINVAL;
2452 goto out;
fb7345bb
MH
2453 }
2454
6bb536cc
WN
2455 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2456 if (!syms) {
2457 ret = -ENOMEM;
2458 goto out;
2459 }
2460
eb948e50
MH
2461 /*
2462 * Load matched symbols: Since the different local symbols may have
2463 * same name but different addresses, this lists all the symbols.
2464 */
6bb536cc 2465 num_matched_functions = find_probe_functions(map, pp->function, syms);
564c62a4 2466 if (num_matched_functions == 0) {
eb948e50 2467 pr_err("Failed to find symbol %s in %s\n", pp->function,
44225521 2468 pev->target ? : "kernel");
eb948e50
MH
2469 ret = -ENOENT;
2470 goto out;
ddb2f58f 2471 } else if (num_matched_functions > probe_conf.max_probes) {
eb948e50 2472 pr_err("Too many functions matched in %s\n",
44225521 2473 pev->target ? : "kernel");
eb948e50
MH
2474 ret = -E2BIG;
2475 goto out;
fb7345bb
MH
2476 }
2477
25dd9171 2478 if (!pev->uprobes && !pp->retprobe) {
0560a0c4 2479 reloc_sym = kernel_get_ref_reloc_sym();
eb948e50
MH
2480 if (!reloc_sym) {
2481 pr_warning("Relocated base symbol is not found!\n");
2482 ret = -EINVAL;
2483 goto out;
2484 }
2485 }
4235b045 2486
eb948e50
MH
2487 /* Setup result trace-probe-events */
2488 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2489 if (!*tevs) {
02b95dad 2490 ret = -ENOMEM;
eb948e50 2491 goto out;
02b95dad 2492 }
ce27a443 2493
eb948e50 2494 ret = 0;
564c62a4 2495
6bb536cc
WN
2496 for (j = 0; j < num_matched_functions; j++) {
2497 sym = syms[j];
2498
eb948e50
MH
2499 tev = (*tevs) + ret;
2500 tp = &tev->point;
2501 if (ret == num_matched_functions) {
2502 pr_warning("Too many symbols are listed. Skip it.\n");
2503 break;
ce27a443 2504 }
eb948e50 2505 ret++;
ce27a443 2506
eb948e50
MH
2507 if (pp->offset > sym->end - sym->start) {
2508 pr_warning("Offset %ld is bigger than the size of %s\n",
2509 pp->offset, sym->name);
2510 ret = -ENOENT;
2511 goto err_out;
2512 }
2513 /* Add one probe point */
2514 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
b031220d
MH
2515 /* If we found a wrong one, mark it by NULL symbol */
2516 if (!pev->uprobes &&
2517 kprobe_warn_out_range(sym->name, tp->address)) {
2518 tp->symbol = NULL; /* Skip it */
2519 skipped++;
2520 } else if (reloc_sym) {
eb948e50
MH
2521 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2522 tp->offset = tp->address - reloc_sym->addr;
2523 } else {
2524 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2525 tp->offset = pp->offset;
2526 }
6bb536cc
WN
2527 tp->realname = strdup_or_goto(sym->name, nomem_out);
2528
eb948e50 2529 tp->retprobe = pp->retprobe;
44225521
MH
2530 if (pev->target)
2531 tev->point.module = strdup_or_goto(pev->target,
2532 nomem_out);
eb948e50
MH
2533 tev->uprobes = pev->uprobes;
2534 tev->nargs = pev->nargs;
2535 if (tev->nargs) {
2536 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2537 tev->nargs);
2538 if (tev->args == NULL)
2539 goto nomem_out;
e334016f 2540 }
48481938 2541 for (i = 0; i < tev->nargs; i++) {
eb948e50
MH
2542 if (pev->args[i].name)
2543 tev->args[i].name =
2544 strdup_or_goto(pev->args[i].name,
2545 nomem_out);
2546
2547 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2548 nomem_out);
2549 if (pev->args[i].type)
2550 tev->args[i].type =
2551 strdup_or_goto(pev->args[i].type,
2552 nomem_out);
48481938 2553 }
7b6ff0bd 2554 arch__fix_tev_from_maps(pev, tev, map);
4235b045 2555 }
b031220d
MH
2556 if (ret == skipped) {
2557 ret = -ENOENT;
2558 goto err_out;
2559 }
4235b045 2560
eb948e50 2561out:
9b118aca 2562 put_target_map(map, pev->uprobes);
6bb536cc 2563 free(syms);
eb948e50 2564 return ret;
225466f1 2565
eb948e50
MH
2566nomem_out:
2567 ret = -ENOMEM;
2568err_out:
2569 clear_probe_trace_events(*tevs, num_matched_functions);
2570 zfree(tevs);
2571 goto out;
2572}
1c1bc922 2573
d5c2e2c1
NR
2574bool __weak arch__prefers_symtab(void) { return false; }
2575
eb948e50 2576static int convert_to_probe_trace_events(struct perf_probe_event *pev,
ddb2f58f 2577 struct probe_trace_event **tevs)
eb948e50
MH
2578{
2579 int ret;
2580
2581 if (pev->uprobes && !pev->group) {
2582 /* Replace group name if not given */
44225521 2583 ret = convert_exec_to_group(pev->target, &pev->group);
eb948e50
MH
2584 if (ret != 0) {
2585 pr_warning("Failed to make a group name.\n");
2586 return ret;
2587 }
02b95dad 2588 }
e334016f 2589
d5c2e2c1 2590 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
ddb2f58f 2591 ret = find_probe_trace_events_from_map(pev, tevs);
d5c2e2c1
NR
2592 if (ret > 0)
2593 return ret; /* Found in symbol table */
2594 }
2595
eb948e50 2596 /* Convert perf_probe_event with debuginfo */
ddb2f58f 2597 ret = try_to_find_probe_trace_events(pev, tevs);
eb948e50
MH
2598 if (ret != 0)
2599 return ret; /* Found in debuginfo or got an error */
2600
ddb2f58f 2601 return find_probe_trace_events_from_map(pev, tevs);
4235b045
MH
2602}
2603
2604struct __event_package {
2605 struct perf_probe_event *pev;
0e60836b 2606 struct probe_trace_event *tevs;
4235b045
MH
2607 int ntevs;
2608};
2609
ddb2f58f 2610int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
4235b045 2611{
146a1439 2612 int i, j, ret;
4235b045
MH
2613 struct __event_package *pkgs;
2614
225466f1 2615 ret = 0;
e334016f 2616 pkgs = zalloc(sizeof(struct __event_package) * npevs);
225466f1 2617
e334016f
MH
2618 if (pkgs == NULL)
2619 return -ENOMEM;
4235b045 2620
ee45b6c2 2621 ret = init_symbol_maps(pevs->uprobes);
449e5b24
MH
2622 if (ret < 0) {
2623 free(pkgs);
146a1439 2624 return ret;
449e5b24 2625 }
4235b045
MH
2626
2627 /* Loop 1: convert all events */
2628 for (i = 0; i < npevs; i++) {
2629 pkgs[i].pev = &pevs[i];
b031220d
MH
2630 /* Init kprobe blacklist if needed */
2631 if (!pkgs[i].pev->uprobes)
2632 kprobe_blacklist__init();
4235b045 2633 /* Convert with or without debuginfo */
0e60836b 2634 ret = convert_to_probe_trace_events(pkgs[i].pev,
ddb2f58f 2635 &pkgs[i].tevs);
146a1439
MH
2636 if (ret < 0)
2637 goto end;
2638 pkgs[i].ntevs = ret;
e0faa8d3 2639 }
b031220d
MH
2640 /* This just release blacklist only if allocated */
2641 kprobe_blacklist__release();
e0faa8d3 2642
4235b045 2643 /* Loop 2: add all events */
8635bf6e 2644 for (i = 0; i < npevs; i++) {
0e60836b 2645 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
ddb2f58f
MH
2646 pkgs[i].ntevs,
2647 probe_conf.force_add);
fbee632d
ACM
2648 if (ret < 0)
2649 break;
2650 }
146a1439 2651end:
449e5b24
MH
2652 /* Loop 3: cleanup and free trace events */
2653 for (i = 0; i < npevs; i++) {
146a1439 2654 for (j = 0; j < pkgs[i].ntevs; j++)
0e60836b 2655 clear_probe_trace_event(&pkgs[i].tevs[j]);
74cf249d 2656 zfree(&pkgs[i].tevs);
449e5b24
MH
2657 }
2658 free(pkgs);
ee45b6c2 2659 exit_symbol_maps();
146a1439
MH
2660
2661 return ret;
e0faa8d3
MH
2662}
2663
307a464b 2664int del_perf_probe_events(struct strfilter *filter)
fa28244d 2665{
307a464b 2666 int ret, ret2, ufd = -1, kfd = -1;
307a464b
MH
2667 char *str = strfilter__string(filter);
2668
2669 if (!str)
2670 return -EINVAL;
2671
2672 pr_debug("Delete filter: \'%s\'\n", str);
146a1439 2673
fa28244d 2674 /* Get current event names */
92f6c72e
MH
2675 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2676 if (ret < 0)
2677 goto out;
467ec085 2678
92f6c72e 2679 ret = probe_file__del_events(kfd, filter);
307a464b 2680 if (ret < 0 && ret != -ENOENT)
225466f1 2681 goto error;
225466f1 2682
92f6c72e 2683 ret2 = probe_file__del_events(ufd, filter);
dddc7ee3 2684 if (ret2 < 0 && ret2 != -ENOENT) {
307a464b 2685 ret = ret2;
dddc7ee3
MH
2686 goto error;
2687 }
2688 if (ret == -ENOENT && ret2 == -ENOENT)
307a464b
MH
2689 pr_debug("\"%s\" does not hit any event.\n", str);
2690 /* Note that this is silently ignored */
dddc7ee3 2691 ret = 0;
225466f1
SD
2692
2693error:
92f6c72e 2694 if (kfd >= 0)
225466f1 2695 close(kfd);
92f6c72e 2696 if (ufd >= 0)
225466f1 2697 close(ufd);
92f6c72e 2698out:
307a464b 2699 free(str);
146a1439
MH
2700
2701 return ret;
fa28244d 2702}
225466f1 2703
3c42258c
MH
2704/* TODO: don't use a global variable for filter ... */
2705static struct strfilter *available_func_filter;
fa28244d 2706
e80711ca 2707/*
3c42258c
MH
2708 * If a symbol corresponds to a function with global binding and
2709 * matches filter return 0. For all others return 1.
e80711ca 2710 */
1d037ca1 2711static int filter_available_functions(struct map *map __maybe_unused,
3c42258c 2712 struct symbol *sym)
e80711ca 2713{
e578da3b 2714 if (strfilter__compare(available_func_filter, sym->name))
3c42258c
MH
2715 return 0;
2716 return 1;
e80711ca
MH
2717}
2718
2df58634
MH
2719int show_available_funcs(const char *target, struct strfilter *_filter,
2720 bool user)
e80711ca
MH
2721{
2722 struct map *map;
2723 int ret;
2724
2df58634 2725 ret = init_symbol_maps(user);
e80711ca
MH
2726 if (ret < 0)
2727 return ret;
2728
2df58634
MH
2729 /* Get a symbol map */
2730 if (user)
2731 map = dso__new_map(target);
2732 else
2733 map = kernel_get_module_map(target);
e80711ca 2734 if (!map) {
2df58634 2735 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
e80711ca
MH
2736 return -EINVAL;
2737 }
225466f1 2738
2df58634 2739 /* Load symbols with given filter */
3c42258c 2740 available_func_filter = _filter;
2df58634
MH
2741 if (map__load(map, filter_available_functions)) {
2742 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2743 goto end;
2744 }
2745 if (!dso__sorted_by_name(map->dso, map->type))
2746 dso__sort_by_name(map->dso, map->type);
225466f1 2747
2df58634
MH
2748 /* Show all (filtered) symbols */
2749 setup_pager();
2750 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2751end:
2752 if (user) {
84c2cafa 2753 map__put(map);
2df58634
MH
2754 }
2755 exit_symbol_maps();
225466f1 2756
2df58634 2757 return ret;
225466f1
SD
2758}
2759
This page took 0.347932 seconds and 5 git commands to generate.