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