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