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