perf tools: No need to test against NULL before calling free()
[deliverable/linux.git] / tools / perf / util / probe-event.c
CommitLineData
50656eec 1/*
0e60836b 2 * probe-event.c : perf-probe definition to probe_events format converter
50656eec
MH
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
50656eec
MH
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
4de189fe
MH
31#include <stdarg.h>
32#include <limits.h>
e80711ca 33#include <elf.h>
50656eec 34
31facc5f 35#include "util.h"
50656eec 36#include "event.h"
4de189fe 37#include "strlist.h"
50656eec 38#include "debug.h"
72041334 39#include "cache.h"
631c9def 40#include "color.h"
e0faa8d3
MH
41#include "symbol.h"
42#include "thread.h"
553873e1 43#include <api/fs/debugfs.h>
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;
359 free(tevs[i].point.symbol);
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) {
509 free(*new_path);
510 *new_path = NULL;
511 return -ENOENT;
512 }
513 continue;
514
515 default:
516 free(*new_path);
517 *new_path = NULL;
518 return -errno;
519 }
520 }
521}
522
4b4da7f7
MH
523#define LINEBUF_SIZE 256
524#define NR_ADDITIONAL_LINES 2
525
fde52dbd 526static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
4b4da7f7
MH
527{
528 char buf[LINEBUF_SIZE];
befe3414
FBH
529 const char *color = show_num ? "" : PERF_COLOR_BLUE;
530 const char *prefix = NULL;
4b4da7f7 531
befe3414 532 do {
4b4da7f7
MH
533 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
534 goto error;
befe3414
FBH
535 if (skip)
536 continue;
537 if (!prefix) {
538 prefix = show_num ? "%7d " : " ";
539 color_fprintf(stdout, color, prefix, l);
4b4da7f7 540 }
befe3414
FBH
541 color_fprintf(stdout, color, "%s", buf);
542
543 } while (strchr(buf, '\n') == NULL);
146a1439 544
fde52dbd 545 return 1;
4b4da7f7 546error:
fde52dbd 547 if (ferror(fp)) {
32b2b6ec 548 pr_warning("File read error: %s\n", strerror(errno));
fde52dbd
FBH
549 return -1;
550 }
551 return 0;
552}
146a1439 553
fde52dbd
FBH
554static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
555{
556 int rv = __show_one_line(fp, l, skip, show_num);
557 if (rv == 0) {
558 pr_warning("Source file is shorter than expected.\n");
559 rv = -1;
560 }
561 return rv;
4b4da7f7
MH
562}
563
fde52dbd
FBH
564#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
565#define show_one_line(f,l) _show_one_line(f,l,false,false)
566#define skip_one_line(f,l) _show_one_line(f,l,true,false)
567#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
568
4b4da7f7
MH
569/*
570 * Show line-range always requires debuginfo to find source file and
571 * line number.
572 */
469b9b88 573int show_line_range(struct line_range *lr, const char *module)
4b4da7f7 574{
d3b63d7a 575 int l = 1;
4b4da7f7 576 struct line_node *ln;
ff741783 577 struct debuginfo *dinfo;
4b4da7f7 578 FILE *fp;
ff741783 579 int ret;
7cf0b79e 580 char *tmp;
4b4da7f7
MH
581
582 /* Search a line range */
146a1439
MH
583 ret = init_vmlinux();
584 if (ret < 0)
585 return ret;
586
ff741783
MH
587 dinfo = open_debuginfo(module);
588 if (!dinfo) {
146a1439 589 pr_warning("Failed to open debuginfo file.\n");
ff741783 590 return -ENOENT;
146a1439
MH
591 }
592
ff741783
MH
593 ret = debuginfo__find_line_range(dinfo, lr);
594 debuginfo__delete(dinfo);
146a1439
MH
595 if (ret == 0) {
596 pr_warning("Specified source line is not found.\n");
597 return -ENOENT;
598 } else if (ret < 0) {
599 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
600 return ret;
601 }
4b4da7f7 602
7cf0b79e
MH
603 /* Convert source file path */
604 tmp = lr->path;
6a330a3c 605 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
7cf0b79e
MH
606 free(tmp); /* Free old path */
607 if (ret < 0) {
608 pr_warning("Failed to find source file. (%d)\n", ret);
609 return ret;
610 }
611
4b4da7f7
MH
612 setup_pager();
613
614 if (lr->function)
8737ebde 615 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
4b4da7f7
MH
616 lr->start - lr->offset);
617 else
62c15fc4 618 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
4b4da7f7
MH
619
620 fp = fopen(lr->path, "r");
146a1439
MH
621 if (fp == NULL) {
622 pr_warning("Failed to open %s: %s\n", lr->path,
623 strerror(errno));
624 return -errno;
625 }
4b4da7f7 626 /* Skip to starting line number */
44b81e92 627 while (l < lr->start) {
fde52dbd 628 ret = skip_one_line(fp, l++);
44b81e92
FBH
629 if (ret < 0)
630 goto end;
631 }
4b4da7f7
MH
632
633 list_for_each_entry(ln, &lr->line_list, list) {
44b81e92 634 for (; ln->line > l; l++) {
fde52dbd 635 ret = show_one_line(fp, l - lr->offset);
44b81e92
FBH
636 if (ret < 0)
637 goto end;
638 }
fde52dbd 639 ret = show_one_line_with_num(fp, l++ - lr->offset);
146a1439
MH
640 if (ret < 0)
641 goto end;
4b4da7f7
MH
642 }
643
644 if (lr->end == INT_MAX)
645 lr->end = l + NR_ADDITIONAL_LINES;
fde52dbd
FBH
646 while (l <= lr->end) {
647 ret = show_one_line_or_eof(fp, l++ - lr->offset);
648 if (ret <= 0)
44b81e92
FBH
649 break;
650 }
146a1439 651end:
4b4da7f7 652 fclose(fp);
146a1439 653 return ret;
4b4da7f7
MH
654}
655
ff741783
MH
656static int show_available_vars_at(struct debuginfo *dinfo,
657 struct perf_probe_event *pev,
bd09d7b5
MH
658 int max_vls, struct strfilter *_filter,
659 bool externs)
cf6eb489
MH
660{
661 char *buf;
bd09d7b5 662 int ret, i, nvars;
cf6eb489
MH
663 struct str_node *node;
664 struct variable_list *vls = NULL, *vl;
bd09d7b5 665 const char *var;
cf6eb489
MH
666
667 buf = synthesize_perf_probe_point(&pev->point);
668 if (!buf)
669 return -EINVAL;
670 pr_debug("Searching variables at %s\n", buf);
671
ff741783
MH
672 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
673 max_vls, externs);
bd09d7b5
MH
674 if (ret <= 0) {
675 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
676 goto end;
677 }
678 /* Some variables are found */
679 fprintf(stdout, "Available variables at %s\n", buf);
680 for (i = 0; i < ret; i++) {
681 vl = &vls[i];
682 /*
683 * A probe point might be converted to
684 * several trace points.
685 */
686 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
687 vl->point.offset);
688 free(vl->point.symbol);
689 nvars = 0;
690 if (vl->vars) {
691 strlist__for_each(node, vl->vars) {
692 var = strchr(node->s, '\t') + 1;
693 if (strfilter__compare(_filter, var)) {
cf6eb489 694 fprintf(stdout, "\t\t%s\n", node->s);
bd09d7b5
MH
695 nvars++;
696 }
697 }
698 strlist__delete(vl->vars);
cf6eb489 699 }
bd09d7b5
MH
700 if (nvars == 0)
701 fprintf(stdout, "\t\t(No matched variables)\n");
702 }
703 free(vls);
704end:
cf6eb489
MH
705 free(buf);
706 return ret;
707}
708
709/* Show available variables on given probe point */
710int show_available_vars(struct perf_probe_event *pevs, int npevs,
bd09d7b5
MH
711 int max_vls, const char *module,
712 struct strfilter *_filter, bool externs)
cf6eb489 713{
ff741783
MH
714 int i, ret = 0;
715 struct debuginfo *dinfo;
cf6eb489
MH
716
717 ret = init_vmlinux();
718 if (ret < 0)
719 return ret;
720
ff741783
MH
721 dinfo = open_debuginfo(module);
722 if (!dinfo) {
723 pr_warning("Failed to open debuginfo file.\n");
724 return -ENOENT;
725 }
726
cf6eb489
MH
727 setup_pager();
728
ff741783
MH
729 for (i = 0; i < npevs && ret >= 0; i++)
730 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
bd09d7b5 731 externs);
ff741783
MH
732
733 debuginfo__delete(dinfo);
cf6eb489
MH
734 return ret;
735}
736
89fe808a 737#else /* !HAVE_DWARF_SUPPORT */
4b4da7f7 738
0e60836b 739static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
469b9b88 740 struct perf_probe_point *pp)
4b4da7f7 741{
469b9b88
MH
742 struct symbol *sym;
743
744 sym = __find_kernel_function_by_name(tp->symbol, NULL);
745 if (!sym) {
746 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
747 return -ENOENT;
748 }
146a1439 749
225466f1 750 return convert_to_perf_probe_point(tp, pp);
4b4da7f7
MH
751}
752
0e60836b 753static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1d037ca1
IT
754 struct probe_trace_event **tevs __maybe_unused,
755 int max_tevs __maybe_unused, const char *target)
4b4da7f7 756{
146a1439
MH
757 if (perf_probe_event_need_dwarf(pev)) {
758 pr_warning("Debuginfo-analysis is not supported.\n");
759 return -ENOSYS;
760 }
225466f1 761
4b4da7f7
MH
762 return 0;
763}
764
1d037ca1
IT
765int show_line_range(struct line_range *lr __maybe_unused,
766 const char *module __maybe_unused)
4b4da7f7 767{
146a1439
MH
768 pr_warning("Debuginfo-analysis is not supported.\n");
769 return -ENOSYS;
4b4da7f7
MH
770}
771
1d037ca1
IT
772int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
773 int npevs __maybe_unused, int max_vls __maybe_unused,
774 const char *module __maybe_unused,
775 struct strfilter *filter __maybe_unused,
776 bool externs __maybe_unused)
cf6eb489
MH
777{
778 pr_warning("Debuginfo-analysis is not supported.\n");
779 return -ENOSYS;
780}
e0faa8d3
MH
781#endif
782
21dd9ae5
FBH
783static int parse_line_num(char **ptr, int *val, const char *what)
784{
785 const char *start = *ptr;
786
787 errno = 0;
788 *val = strtol(*ptr, ptr, 0);
789 if (errno || *ptr == start) {
790 semantic_error("'%s' is not a valid number.\n", what);
791 return -EINVAL;
792 }
793 return 0;
794}
795
9d95b580
FBH
796/*
797 * Stuff 'lr' according to the line range described by 'arg'.
798 * The line range syntax is described by:
799 *
800 * SRC[:SLN[+NUM|-ELN]]
e116dfa1 801 * FNC[@SRC][:SLN[+NUM|-ELN]]
9d95b580 802 */
146a1439 803int parse_line_range_desc(const char *arg, struct line_range *lr)
631c9def 804{
e116dfa1 805 char *range, *file, *name = strdup(arg);
21dd9ae5
FBH
806 int err;
807
808 if (!name)
809 return -ENOMEM;
810
811 lr->start = 0;
812 lr->end = INT_MAX;
813
814 range = strchr(name, ':');
815 if (range) {
816 *range++ = '\0';
817
818 err = parse_line_num(&range, &lr->start, "start line");
819 if (err)
820 goto err;
821
822 if (*range == '+' || *range == '-') {
823 const char c = *range++;
824
825 err = parse_line_num(&range, &lr->end, "end line");
826 if (err)
827 goto err;
828
829 if (c == '+') {
830 lr->end += lr->start;
831 /*
832 * Adjust the number of lines here.
833 * If the number of lines == 1, the
834 * the end of line should be equal to
835 * the start of line.
836 */
837 lr->end--;
838 }
839 }
9d95b580 840
d3b63d7a 841 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
21dd9ae5
FBH
842
843 err = -EINVAL;
d3b63d7a 844 if (lr->start > lr->end) {
631c9def 845 semantic_error("Start line must be smaller"
146a1439 846 " than end line.\n");
21dd9ae5 847 goto err;
146a1439 848 }
21dd9ae5
FBH
849 if (*range != '\0') {
850 semantic_error("Tailing with invalid str '%s'.\n", range);
851 goto err;
146a1439 852 }
d3b63d7a 853 }
02b95dad 854
e116dfa1
MH
855 file = strchr(name, '@');
856 if (file) {
857 *file = '\0';
858 lr->file = strdup(++file);
859 if (lr->file == NULL) {
860 err = -ENOMEM;
861 goto err;
862 }
863 lr->function = name;
864 } else if (strchr(name, '.'))
21dd9ae5 865 lr->file = name;
631c9def 866 else
21dd9ae5 867 lr->function = name;
146a1439
MH
868
869 return 0;
21dd9ae5
FBH
870err:
871 free(name);
872 return err;
631c9def
MH
873}
874
b7702a21
MH
875/* Check the name is good for event/group */
876static bool check_event_name(const char *name)
877{
878 if (!isalpha(*name) && *name != '_')
879 return false;
880 while (*++name != '\0') {
881 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
882 return false;
883 }
884 return true;
885}
886
50656eec 887/* Parse probepoint definition. */
146a1439 888static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 889{
4235b045 890 struct perf_probe_point *pp = &pev->point;
50656eec
MH
891 char *ptr, *tmp;
892 char c, nc = 0;
893 /*
894 * <Syntax>
2a9c8c36
MH
895 * perf probe [EVENT=]SRC[:LN|;PTN]
896 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
897 *
898 * TODO:Group name support
50656eec
MH
899 */
900
2a9c8c36
MH
901 ptr = strpbrk(arg, ";=@+%");
902 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
903 *ptr = '\0';
904 tmp = ptr + 1;
146a1439
MH
905 if (strchr(arg, ':')) {
906 semantic_error("Group name is not supported yet.\n");
907 return -ENOTSUP;
908 }
909 if (!check_event_name(arg)) {
b7702a21 910 semantic_error("%s is bad for event name -it must "
146a1439
MH
911 "follow C symbol-naming rule.\n", arg);
912 return -EINVAL;
913 }
02b95dad
MH
914 pev->event = strdup(arg);
915 if (pev->event == NULL)
916 return -ENOMEM;
4235b045 917 pev->group = NULL;
af663d75
MH
918 arg = tmp;
919 }
920
2a9c8c36 921 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
922 if (ptr) {
923 nc = *ptr;
924 *ptr++ = '\0';
925 }
926
02b95dad
MH
927 tmp = strdup(arg);
928 if (tmp == NULL)
929 return -ENOMEM;
930
50656eec 931 /* Check arg is function or file and copy it */
02b95dad
MH
932 if (strchr(tmp, '.')) /* File */
933 pp->file = tmp;
50656eec 934 else /* Function */
02b95dad 935 pp->function = tmp;
50656eec
MH
936
937 /* Parse other options */
938 while (ptr) {
939 arg = ptr;
940 c = nc;
2a9c8c36 941 if (c == ';') { /* Lazy pattern must be the last part */
02b95dad
MH
942 pp->lazy_line = strdup(arg);
943 if (pp->lazy_line == NULL)
944 return -ENOMEM;
2a9c8c36
MH
945 break;
946 }
947 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
948 if (ptr) {
949 nc = *ptr;
950 *ptr++ = '\0';
951 }
952 switch (c) {
953 case ':': /* Line number */
954 pp->line = strtoul(arg, &tmp, 0);
146a1439 955 if (*tmp != '\0') {
2a9c8c36 956 semantic_error("There is non-digit char"
146a1439
MH
957 " in line number.\n");
958 return -EINVAL;
959 }
50656eec
MH
960 break;
961 case '+': /* Byte offset from a symbol */
962 pp->offset = strtoul(arg, &tmp, 0);
146a1439 963 if (*tmp != '\0') {
2a9c8c36 964 semantic_error("There is non-digit character"
146a1439
MH
965 " in offset.\n");
966 return -EINVAL;
967 }
50656eec
MH
968 break;
969 case '@': /* File name */
146a1439
MH
970 if (pp->file) {
971 semantic_error("SRC@SRC is not allowed.\n");
972 return -EINVAL;
973 }
02b95dad
MH
974 pp->file = strdup(arg);
975 if (pp->file == NULL)
976 return -ENOMEM;
50656eec
MH
977 break;
978 case '%': /* Probe places */
979 if (strcmp(arg, "return") == 0) {
980 pp->retprobe = 1;
146a1439
MH
981 } else { /* Others not supported yet */
982 semantic_error("%%%s is not supported.\n", arg);
983 return -ENOTSUP;
984 }
50656eec 985 break;
146a1439
MH
986 default: /* Buggy case */
987 pr_err("This program has a bug at %s:%d.\n",
988 __FILE__, __LINE__);
989 return -ENOTSUP;
50656eec
MH
990 break;
991 }
992 }
993
994 /* Exclusion check */
146a1439 995 if (pp->lazy_line && pp->line) {
0e43e5d2
MH
996 semantic_error("Lazy pattern can't be used with"
997 " line number.\n");
146a1439
MH
998 return -EINVAL;
999 }
2a9c8c36 1000
146a1439 1001 if (pp->lazy_line && pp->offset) {
0e43e5d2 1002 semantic_error("Lazy pattern can't be used with offset.\n");
146a1439
MH
1003 return -EINVAL;
1004 }
2a9c8c36 1005
146a1439 1006 if (pp->line && pp->offset) {
0e43e5d2 1007 semantic_error("Offset can't be used with line number.\n");
146a1439
MH
1008 return -EINVAL;
1009 }
50656eec 1010
146a1439 1011 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
2a9c8c36 1012 semantic_error("File always requires line number or "
0e43e5d2 1013 "lazy pattern.\n");
146a1439
MH
1014 return -EINVAL;
1015 }
50656eec 1016
146a1439 1017 if (pp->offset && !pp->function) {
0e43e5d2 1018 semantic_error("Offset requires an entry function.\n");
146a1439
MH
1019 return -EINVAL;
1020 }
50656eec 1021
146a1439 1022 if (pp->retprobe && !pp->function) {
0e43e5d2 1023 semantic_error("Return probe requires an entry function.\n");
146a1439
MH
1024 return -EINVAL;
1025 }
50656eec 1026
146a1439 1027 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
2a9c8c36 1028 semantic_error("Offset/Line/Lazy pattern can't be used with "
0e43e5d2 1029 "return probe.\n");
146a1439
MH
1030 return -EINVAL;
1031 }
50656eec 1032
4235b045 1033 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
1034 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1035 pp->lazy_line);
146a1439 1036 return 0;
50656eec
MH
1037}
1038
7df2f329 1039/* Parse perf-probe event argument */
146a1439 1040static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
7df2f329 1041{
b2a3c12b 1042 char *tmp, *goodname;
7df2f329
MH
1043 struct perf_probe_arg_field **fieldp;
1044
1045 pr_debug("parsing arg: %s into ", str);
1046
48481938
MH
1047 tmp = strchr(str, '=');
1048 if (tmp) {
02b95dad
MH
1049 arg->name = strndup(str, tmp - str);
1050 if (arg->name == NULL)
1051 return -ENOMEM;
11a1ca35 1052 pr_debug("name:%s ", arg->name);
48481938
MH
1053 str = tmp + 1;
1054 }
1055
11a1ca35
MH
1056 tmp = strchr(str, ':');
1057 if (tmp) { /* Type setting */
1058 *tmp = '\0';
02b95dad
MH
1059 arg->type = strdup(tmp + 1);
1060 if (arg->type == NULL)
1061 return -ENOMEM;
11a1ca35
MH
1062 pr_debug("type:%s ", arg->type);
1063 }
1064
b2a3c12b 1065 tmp = strpbrk(str, "-.[");
7df2f329
MH
1066 if (!is_c_varname(str) || !tmp) {
1067 /* A variable, register, symbol or special value */
02b95dad
MH
1068 arg->var = strdup(str);
1069 if (arg->var == NULL)
1070 return -ENOMEM;
48481938 1071 pr_debug("%s\n", arg->var);
146a1439 1072 return 0;
7df2f329
MH
1073 }
1074
b2a3c12b 1075 /* Structure fields or array element */
02b95dad
MH
1076 arg->var = strndup(str, tmp - str);
1077 if (arg->var == NULL)
1078 return -ENOMEM;
b2a3c12b 1079 goodname = arg->var;
48481938 1080 pr_debug("%s, ", arg->var);
7df2f329
MH
1081 fieldp = &arg->field;
1082
1083 do {
e334016f
MH
1084 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1085 if (*fieldp == NULL)
1086 return -ENOMEM;
b2a3c12b
MH
1087 if (*tmp == '[') { /* Array */
1088 str = tmp;
1089 (*fieldp)->index = strtol(str + 1, &tmp, 0);
7df2f329 1090 (*fieldp)->ref = true;
b2a3c12b
MH
1091 if (*tmp != ']' || tmp == str + 1) {
1092 semantic_error("Array index must be a"
1093 " number.\n");
1094 return -EINVAL;
1095 }
1096 tmp++;
1097 if (*tmp == '\0')
1098 tmp = NULL;
1099 } else { /* Structure */
1100 if (*tmp == '.') {
1101 str = tmp + 1;
1102 (*fieldp)->ref = false;
1103 } else if (tmp[1] == '>') {
1104 str = tmp + 2;
1105 (*fieldp)->ref = true;
1106 } else {
1107 semantic_error("Argument parse error: %s\n",
1108 str);
1109 return -EINVAL;
1110 }
1111 tmp = strpbrk(str, "-.[");
146a1439 1112 }
7df2f329 1113 if (tmp) {
02b95dad
MH
1114 (*fieldp)->name = strndup(str, tmp - str);
1115 if ((*fieldp)->name == NULL)
1116 return -ENOMEM;
b2a3c12b
MH
1117 if (*str != '[')
1118 goodname = (*fieldp)->name;
7df2f329
MH
1119 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1120 fieldp = &(*fieldp)->next;
1121 }
1122 } while (tmp);
02b95dad
MH
1123 (*fieldp)->name = strdup(str);
1124 if ((*fieldp)->name == NULL)
1125 return -ENOMEM;
b2a3c12b
MH
1126 if (*str != '[')
1127 goodname = (*fieldp)->name;
7df2f329 1128 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
df0faf4b 1129
b2a3c12b 1130 /* If no name is specified, set the last field name (not array index)*/
02b95dad 1131 if (!arg->name) {
b2a3c12b 1132 arg->name = strdup(goodname);
02b95dad
MH
1133 if (arg->name == NULL)
1134 return -ENOMEM;
1135 }
146a1439 1136 return 0;
7df2f329
MH
1137}
1138
4235b045 1139/* Parse perf-probe event command */
146a1439 1140int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 1141{
e1c01d61 1142 char **argv;
146a1439 1143 int argc, i, ret = 0;
fac13fd5 1144
4235b045 1145 argv = argv_split(cmd, &argc);
146a1439
MH
1146 if (!argv) {
1147 pr_debug("Failed to split arguments.\n");
1148 return -ENOMEM;
1149 }
1150 if (argc - 1 > MAX_PROBE_ARGS) {
1151 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1152 ret = -ERANGE;
1153 goto out;
1154 }
50656eec 1155 /* Parse probe point */
146a1439
MH
1156 ret = parse_perf_probe_point(argv[0], pev);
1157 if (ret < 0)
1158 goto out;
50656eec 1159
e1c01d61 1160 /* Copy arguments and ensure return probe has no C argument */
4235b045 1161 pev->nargs = argc - 1;
e334016f
MH
1162 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1163 if (pev->args == NULL) {
1164 ret = -ENOMEM;
1165 goto out;
1166 }
146a1439
MH
1167 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1168 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1169 if (ret >= 0 &&
1170 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
4235b045 1171 semantic_error("You can't specify local variable for"
146a1439
MH
1172 " kretprobe.\n");
1173 ret = -EINVAL;
1174 }
e1c01d61 1175 }
146a1439 1176out:
e1c01d61 1177 argv_free(argv);
146a1439
MH
1178
1179 return ret;
50656eec
MH
1180}
1181
4235b045
MH
1182/* Return true if this perf_probe_event requires debuginfo */
1183bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1184{
1185 int i;
1186
1187 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1188 return true;
1189
1190 for (i = 0; i < pev->nargs; i++)
48481938 1191 if (is_c_varname(pev->args[i].var))
4235b045
MH
1192 return true;
1193
1194 return false;
1195}
1196
0e60836b
SD
1197/* Parse probe_events event into struct probe_point */
1198static int parse_probe_trace_command(const char *cmd,
190b57fc 1199 struct probe_trace_event *tev)
4de189fe 1200{
0e60836b 1201 struct probe_trace_point *tp = &tev->point;
4de189fe
MH
1202 char pr;
1203 char *p;
bcbd0040 1204 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
4de189fe
MH
1205 int ret, i, argc;
1206 char **argv;
1207
0e60836b 1208 pr_debug("Parsing probe_events: %s\n", cmd);
4235b045 1209 argv = argv_split(cmd, &argc);
146a1439
MH
1210 if (!argv) {
1211 pr_debug("Failed to split arguments.\n");
1212 return -ENOMEM;
1213 }
1214 if (argc < 2) {
1215 semantic_error("Too few probe arguments.\n");
1216 ret = -ERANGE;
1217 goto out;
1218 }
4de189fe
MH
1219
1220 /* Scan event and group name. */
bcbd0040
IT
1221 argv0_str = strdup(argv[0]);
1222 if (argv0_str == NULL) {
1223 ret = -ENOMEM;
1224 goto out;
1225 }
1226 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1227 fmt2_str = strtok_r(NULL, "/", &fmt);
1228 fmt3_str = strtok_r(NULL, " \t", &fmt);
1229 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1230 || fmt3_str == NULL) {
146a1439
MH
1231 semantic_error("Failed to parse event name: %s\n", argv[0]);
1232 ret = -EINVAL;
1233 goto out;
1234 }
bcbd0040
IT
1235 pr = fmt1_str[0];
1236 tev->group = strdup(fmt2_str);
1237 tev->event = strdup(fmt3_str);
1238 if (tev->group == NULL || tev->event == NULL) {
1239 ret = -ENOMEM;
1240 goto out;
1241 }
4235b045 1242 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 1243
4235b045 1244 tp->retprobe = (pr == 'r');
4de189fe 1245
190b57fc
MH
1246 /* Scan module name(if there), function name and offset */
1247 p = strchr(argv[1], ':');
1248 if (p) {
1249 tp->module = strndup(argv[1], p - argv[1]);
1250 p++;
1251 } else
1252 p = argv[1];
bcbd0040
IT
1253 fmt1_str = strtok_r(p, "+", &fmt);
1254 tp->symbol = strdup(fmt1_str);
1255 if (tp->symbol == NULL) {
1256 ret = -ENOMEM;
1257 goto out;
1258 }
1259 fmt2_str = strtok_r(NULL, "", &fmt);
1260 if (fmt2_str == NULL)
4235b045 1261 tp->offset = 0;
bcbd0040
IT
1262 else
1263 tp->offset = strtoul(fmt2_str, NULL, 10);
4de189fe 1264
4235b045 1265 tev->nargs = argc - 2;
0e60836b 1266 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
e334016f
MH
1267 if (tev->args == NULL) {
1268 ret = -ENOMEM;
1269 goto out;
1270 }
4235b045 1271 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
1272 p = strchr(argv[i + 2], '=');
1273 if (p) /* We don't need which register is assigned. */
4235b045
MH
1274 *p++ = '\0';
1275 else
1276 p = argv[i + 2];
02b95dad 1277 tev->args[i].name = strdup(argv[i + 2]);
4235b045 1278 /* TODO: parse regs and offset */
02b95dad
MH
1279 tev->args[i].value = strdup(p);
1280 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1281 ret = -ENOMEM;
1282 goto out;
1283 }
4de189fe 1284 }
146a1439
MH
1285 ret = 0;
1286out:
bcbd0040 1287 free(argv0_str);
4de189fe 1288 argv_free(argv);
146a1439 1289 return ret;
4de189fe
MH
1290}
1291
7df2f329
MH
1292/* Compose only probe arg */
1293int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1294{
1295 struct perf_probe_arg_field *field = pa->field;
1296 int ret;
1297 char *tmp = buf;
1298
48481938
MH
1299 if (pa->name && pa->var)
1300 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1301 else
1302 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
1303 if (ret <= 0)
1304 goto error;
1305 tmp += ret;
1306 len -= ret;
1307
1308 while (field) {
b2a3c12b
MH
1309 if (field->name[0] == '[')
1310 ret = e_snprintf(tmp, len, "%s", field->name);
1311 else
1312 ret = e_snprintf(tmp, len, "%s%s",
1313 field->ref ? "->" : ".", field->name);
7df2f329
MH
1314 if (ret <= 0)
1315 goto error;
1316 tmp += ret;
1317 len -= ret;
1318 field = field->next;
1319 }
11a1ca35
MH
1320
1321 if (pa->type) {
1322 ret = e_snprintf(tmp, len, ":%s", pa->type);
1323 if (ret <= 0)
1324 goto error;
1325 tmp += ret;
1326 len -= ret;
1327 }
1328
7df2f329
MH
1329 return tmp - buf;
1330error:
0e43e5d2 1331 pr_debug("Failed to synthesize perf probe argument: %s\n",
146a1439
MH
1332 strerror(-ret));
1333 return ret;
7df2f329
MH
1334}
1335
4235b045
MH
1336/* Compose only probe point (not argument) */
1337static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 1338{
fb1587d8
MH
1339 char *buf, *tmp;
1340 char offs[32] = "", line[32] = "", file[32] = "";
1341 int ret, len;
4de189fe 1342
e334016f
MH
1343 buf = zalloc(MAX_CMDLEN);
1344 if (buf == NULL) {
1345 ret = -ENOMEM;
1346 goto error;
1347 }
4de189fe 1348 if (pp->offset) {
fb1587d8 1349 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
1350 if (ret <= 0)
1351 goto error;
1352 }
1353 if (pp->line) {
fb1587d8
MH
1354 ret = e_snprintf(line, 32, ":%d", pp->line);
1355 if (ret <= 0)
1356 goto error;
1357 }
1358 if (pp->file) {
32ae2ade
FBH
1359 tmp = pp->file;
1360 len = strlen(tmp);
1361 if (len > 30) {
1362 tmp = strchr(pp->file + len - 30, '/');
1363 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1364 }
1365 ret = e_snprintf(file, 32, "@%s", tmp);
4de189fe
MH
1366 if (ret <= 0)
1367 goto error;
1368 }
1369
1370 if (pp->function)
fb1587d8
MH
1371 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1372 offs, pp->retprobe ? "%return" : "", line,
1373 file);
4de189fe 1374 else
fb1587d8 1375 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
1376 if (ret <= 0)
1377 goto error;
1378
1379 return buf;
7ef17aaf 1380error:
0e43e5d2 1381 pr_debug("Failed to synthesize perf probe point: %s\n",
146a1439 1382 strerror(-ret));
f5385650 1383 free(buf);
146a1439 1384 return NULL;
7ef17aaf
MH
1385}
1386
4235b045
MH
1387#if 0
1388char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
1389{
1390 char *buf;
1391 int i, len, ret;
1392
4235b045
MH
1393 buf = synthesize_perf_probe_point(&pev->point);
1394 if (!buf)
1395 return NULL;
4de189fe 1396
4235b045
MH
1397 len = strlen(buf);
1398 for (i = 0; i < pev->nargs; i++) {
4de189fe 1399 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
1400 pev->args[i].name);
1401 if (ret <= 0) {
1402 free(buf);
1403 return NULL;
1404 }
4de189fe
MH
1405 len += ret;
1406 }
4de189fe 1407
4235b045
MH
1408 return buf;
1409}
1410#endif
1411
0e60836b 1412static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
4235b045
MH
1413 char **buf, size_t *buflen,
1414 int depth)
1415{
1416 int ret;
1417 if (ref->next) {
0e60836b 1418 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
4235b045
MH
1419 buflen, depth + 1);
1420 if (depth < 0)
1421 goto out;
1422 }
1423
1424 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1425 if (ret < 0)
1426 depth = ret;
1427 else {
1428 *buf += ret;
1429 *buflen -= ret;
1430 }
1431out:
1432 return depth;
4de189fe 1433
4de189fe
MH
1434}
1435
0e60836b 1436static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
4235b045 1437 char *buf, size_t buflen)
50656eec 1438{
0e60836b 1439 struct probe_trace_arg_ref *ref = arg->ref;
4235b045
MH
1440 int ret, depth = 0;
1441 char *tmp = buf;
1442
1443 /* Argument name or separator */
1444 if (arg->name)
1445 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1446 else
1447 ret = e_snprintf(buf, buflen, " ");
1448 if (ret < 0)
1449 return ret;
1450 buf += ret;
1451 buflen -= ret;
1452
b7dcb857
MH
1453 /* Special case: @XXX */
1454 if (arg->value[0] == '@' && arg->ref)
1455 ref = ref->next;
1456
4235b045 1457 /* Dereferencing arguments */
b7dcb857 1458 if (ref) {
0e60836b 1459 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
4235b045
MH
1460 &buflen, 1);
1461 if (depth < 0)
1462 return depth;
1463 }
1464
1465 /* Print argument value */
b7dcb857
MH
1466 if (arg->value[0] == '@' && arg->ref)
1467 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1468 arg->ref->offset);
1469 else
1470 ret = e_snprintf(buf, buflen, "%s", arg->value);
4235b045
MH
1471 if (ret < 0)
1472 return ret;
1473 buf += ret;
1474 buflen -= ret;
1475
1476 /* Closing */
1477 while (depth--) {
1478 ret = e_snprintf(buf, buflen, ")");
1479 if (ret < 0)
1480 return ret;
1481 buf += ret;
1482 buflen -= ret;
1483 }
4984912e
MH
1484 /* Print argument type */
1485 if (arg->type) {
1486 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1487 if (ret <= 0)
1488 return ret;
1489 buf += ret;
1490 }
4235b045
MH
1491
1492 return buf - tmp;
1493}
1494
0e60836b 1495char *synthesize_probe_trace_command(struct probe_trace_event *tev)
4235b045 1496{
0e60836b 1497 struct probe_trace_point *tp = &tev->point;
50656eec
MH
1498 char *buf;
1499 int i, len, ret;
1500
e334016f
MH
1501 buf = zalloc(MAX_CMDLEN);
1502 if (buf == NULL)
1503 return NULL;
1504
225466f1
SD
1505 if (tev->uprobes)
1506 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1507 tp->retprobe ? 'r' : 'p',
1508 tev->group, tev->event,
1509 tp->module, tp->symbol);
1510 else
1511 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1512 tp->retprobe ? 'r' : 'p',
1513 tev->group, tev->event,
1514 tp->module ?: "", tp->module ? ":" : "",
1515 tp->symbol, tp->offset);
1516
4235b045 1517 if (len <= 0)
50656eec 1518 goto error;
50656eec 1519
4235b045 1520 for (i = 0; i < tev->nargs; i++) {
0e60836b 1521 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
4235b045 1522 MAX_CMDLEN - len);
4de189fe 1523 if (ret <= 0)
50656eec
MH
1524 goto error;
1525 len += ret;
1526 }
50656eec 1527
4235b045 1528 return buf;
50656eec 1529error:
4235b045
MH
1530 free(buf);
1531 return NULL;
1532}
50656eec 1533
0e60836b 1534static int convert_to_perf_probe_event(struct probe_trace_event *tev,
225466f1 1535 struct perf_probe_event *pev, bool is_kprobe)
4235b045 1536{
02b95dad 1537 char buf[64] = "";
146a1439 1538 int i, ret;
4235b045 1539
4b4da7f7 1540 /* Convert event/group name */
02b95dad
MH
1541 pev->event = strdup(tev->event);
1542 pev->group = strdup(tev->group);
1543 if (pev->event == NULL || pev->group == NULL)
1544 return -ENOMEM;
fb1587d8 1545
4b4da7f7 1546 /* Convert trace_point to probe_point */
225466f1
SD
1547 if (is_kprobe)
1548 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1549 else
1550 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1551
146a1439
MH
1552 if (ret < 0)
1553 return ret;
4b4da7f7 1554
4235b045
MH
1555 /* Convert trace_arg to probe_arg */
1556 pev->nargs = tev->nargs;
e334016f
MH
1557 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1558 if (pev->args == NULL)
1559 return -ENOMEM;
02b95dad 1560 for (i = 0; i < tev->nargs && ret >= 0; i++) {
4235b045 1561 if (tev->args[i].name)
02b95dad 1562 pev->args[i].name = strdup(tev->args[i].name);
4235b045 1563 else {
0e60836b 1564 ret = synthesize_probe_trace_arg(&tev->args[i],
146a1439 1565 buf, 64);
02b95dad 1566 pev->args[i].name = strdup(buf);
4235b045 1567 }
02b95dad
MH
1568 if (pev->args[i].name == NULL && ret >= 0)
1569 ret = -ENOMEM;
1570 }
146a1439
MH
1571
1572 if (ret < 0)
1573 clear_perf_probe_event(pev);
1574
1575 return ret;
4235b045
MH
1576}
1577
1578void clear_perf_probe_event(struct perf_probe_event *pev)
1579{
1580 struct perf_probe_point *pp = &pev->point;
7df2f329 1581 struct perf_probe_arg_field *field, *next;
4235b045
MH
1582 int i;
1583
f5385650
ACM
1584 free(pev->event);
1585 free(pev->group);
1586 free(pp->file);
1587 free(pp->function);
1588 free(pp->lazy_line);
1589
7df2f329 1590 for (i = 0; i < pev->nargs; i++) {
f5385650
ACM
1591 free(pev->args[i].name);
1592 free(pev->args[i].var);
1593 free(pev->args[i].type);
7df2f329
MH
1594 field = pev->args[i].field;
1595 while (field) {
1596 next = field->next;
f5385650 1597 free(field->name);
7df2f329
MH
1598 free(field);
1599 field = next;
1600 }
1601 }
f5385650 1602 free(pev->args);
4235b045
MH
1603 memset(pev, 0, sizeof(*pev));
1604}
1605
0e60836b 1606static void clear_probe_trace_event(struct probe_trace_event *tev)
4235b045 1607{
0e60836b 1608 struct probe_trace_arg_ref *ref, *next;
4235b045
MH
1609 int i;
1610
f5385650
ACM
1611 free(tev->event);
1612 free(tev->group);
1613 free(tev->point.symbol);
1614 free(tev->point.module);
4235b045 1615 for (i = 0; i < tev->nargs; i++) {
f5385650
ACM
1616 free(tev->args[i].name);
1617 free(tev->args[i].value);
1618 free(tev->args[i].type);
4235b045
MH
1619 ref = tev->args[i].ref;
1620 while (ref) {
1621 next = ref->next;
1622 free(ref);
1623 ref = next;
1624 }
1625 }
f5385650 1626 free(tev->args);
4235b045 1627 memset(tev, 0, sizeof(*tev));
50656eec
MH
1628}
1629
225466f1
SD
1630static void print_warn_msg(const char *file, bool is_kprobe)
1631{
1632
1633 if (errno == ENOENT) {
1634 const char *config;
1635
1636 if (!is_kprobe)
1637 config = "CONFIG_UPROBE_EVENTS";
1638 else
1639 config = "CONFIG_KPROBE_EVENTS";
1640
1641 pr_warning("%s file does not exist - please rebuild kernel"
1642 " with %s.\n", file, config);
1643 } else
1644 pr_warning("Failed to open %s file: %s\n", file,
1645 strerror(errno));
1646}
1647
1648static int open_probe_events(const char *trace_file, bool readwrite,
1649 bool is_kprobe)
4de189fe
MH
1650{
1651 char buf[PATH_MAX];
7ca5989d 1652 const char *__debugfs;
4de189fe
MH
1653 int ret;
1654
7ca5989d
MH
1655 __debugfs = debugfs_find_mountpoint();
1656 if (__debugfs == NULL) {
1657 pr_warning("Debugfs is not mounted.\n");
1658 return -ENOENT;
1659 }
1660
225466f1 1661 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
146a1439 1662 if (ret >= 0) {
7ca5989d 1663 pr_debug("Opening %s write=%d\n", buf, readwrite);
146a1439
MH
1664 if (readwrite && !probe_event_dry_run)
1665 ret = open(buf, O_RDWR, O_APPEND);
1666 else
1667 ret = open(buf, O_RDONLY, 0);
f4d7da49 1668
225466f1
SD
1669 if (ret < 0)
1670 print_warn_msg(buf, is_kprobe);
4de189fe
MH
1671 }
1672 return ret;
1673}
1674
225466f1
SD
1675static int open_kprobe_events(bool readwrite)
1676{
1677 return open_probe_events("tracing/kprobe_events", readwrite, true);
1678}
1679
1680static int open_uprobe_events(bool readwrite)
1681{
1682 return open_probe_events("tracing/uprobe_events", readwrite, false);
1683}
1684
1685/* Get raw string list of current kprobe_events or uprobe_events */
0e60836b 1686static struct strlist *get_probe_trace_command_rawlist(int fd)
4de189fe
MH
1687{
1688 int ret, idx;
1689 FILE *fp;
1690 char buf[MAX_CMDLEN];
1691 char *p;
1692 struct strlist *sl;
1693
1694 sl = strlist__new(true, NULL);
1695
1696 fp = fdopen(dup(fd), "r");
1697 while (!feof(fp)) {
1698 p = fgets(buf, MAX_CMDLEN, fp);
1699 if (!p)
1700 break;
1701
1702 idx = strlen(p) - 1;
1703 if (p[idx] == '\n')
1704 p[idx] = '\0';
1705 ret = strlist__add(sl, buf);
146a1439
MH
1706 if (ret < 0) {
1707 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1708 strlist__delete(sl);
1709 return NULL;
1710 }
4de189fe
MH
1711 }
1712 fclose(fp);
1713
1714 return sl;
1715}
1716
278498d4 1717/* Show an event */
146a1439 1718static int show_perf_probe_event(struct perf_probe_event *pev)
278498d4 1719{
7e990a51 1720 int i, ret;
278498d4 1721 char buf[128];
4235b045 1722 char *place;
278498d4 1723
4235b045
MH
1724 /* Synthesize only event probe point */
1725 place = synthesize_perf_probe_point(&pev->point);
146a1439
MH
1726 if (!place)
1727 return -EINVAL;
4235b045
MH
1728
1729 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
7e990a51 1730 if (ret < 0)
146a1439
MH
1731 return ret;
1732
fb1587d8 1733 printf(" %-20s (on %s", buf, place);
278498d4 1734
4235b045 1735 if (pev->nargs > 0) {
278498d4 1736 printf(" with");
7df2f329 1737 for (i = 0; i < pev->nargs; i++) {
146a1439
MH
1738 ret = synthesize_perf_probe_arg(&pev->args[i],
1739 buf, 128);
1740 if (ret < 0)
1741 break;
7df2f329
MH
1742 printf(" %s", buf);
1743 }
278498d4
MH
1744 }
1745 printf(")\n");
4235b045 1746 free(place);
146a1439 1747 return ret;
278498d4
MH
1748}
1749
225466f1 1750static int __show_perf_probe_events(int fd, bool is_kprobe)
4de189fe 1751{
225466f1 1752 int ret = 0;
0e60836b 1753 struct probe_trace_event tev;
4235b045 1754 struct perf_probe_event pev;
4de189fe
MH
1755 struct strlist *rawlist;
1756 struct str_node *ent;
1757
4235b045
MH
1758 memset(&tev, 0, sizeof(tev));
1759 memset(&pev, 0, sizeof(pev));
72041334 1760
0e60836b 1761 rawlist = get_probe_trace_command_rawlist(fd);
146a1439
MH
1762 if (!rawlist)
1763 return -ENOENT;
4de189fe 1764
adf365f4 1765 strlist__for_each(ent, rawlist) {
0e60836b 1766 ret = parse_probe_trace_command(ent->s, &tev);
146a1439 1767 if (ret >= 0) {
225466f1
SD
1768 ret = convert_to_perf_probe_event(&tev, &pev,
1769 is_kprobe);
146a1439
MH
1770 if (ret >= 0)
1771 ret = show_perf_probe_event(&pev);
1772 }
4235b045 1773 clear_perf_probe_event(&pev);
0e60836b 1774 clear_probe_trace_event(&tev);
146a1439
MH
1775 if (ret < 0)
1776 break;
4de189fe 1777 }
4de189fe 1778 strlist__delete(rawlist);
146a1439
MH
1779
1780 return ret;
4de189fe
MH
1781}
1782
225466f1
SD
1783/* List up current perf-probe events */
1784int show_perf_probe_events(void)
1785{
1786 int fd, ret;
1787
1788 setup_pager();
1789 fd = open_kprobe_events(false);
1790
1791 if (fd < 0)
1792 return fd;
1793
1794 ret = init_vmlinux();
1795 if (ret < 0)
1796 return ret;
1797
1798 ret = __show_perf_probe_events(fd, true);
1799 close(fd);
1800
1801 fd = open_uprobe_events(false);
1802 if (fd >= 0) {
1803 ret = __show_perf_probe_events(fd, false);
1804 close(fd);
1805 }
1806
1807 return ret;
1808}
1809
b498ce1f 1810/* Get current perf-probe event names */
0e60836b 1811static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
b498ce1f 1812{
fa28244d 1813 char buf[128];
b498ce1f
MH
1814 struct strlist *sl, *rawlist;
1815 struct str_node *ent;
0e60836b 1816 struct probe_trace_event tev;
146a1439 1817 int ret = 0;
b498ce1f 1818
4235b045 1819 memset(&tev, 0, sizeof(tev));
0e60836b 1820 rawlist = get_probe_trace_command_rawlist(fd);
e1d2017b 1821 sl = strlist__new(true, NULL);
adf365f4 1822 strlist__for_each(ent, rawlist) {
0e60836b 1823 ret = parse_probe_trace_command(ent->s, &tev);
146a1439
MH
1824 if (ret < 0)
1825 break;
fa28244d 1826 if (include_group) {
146a1439
MH
1827 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1828 tev.event);
1829 if (ret >= 0)
1830 ret = strlist__add(sl, buf);
fa28244d 1831 } else
146a1439 1832 ret = strlist__add(sl, tev.event);
0e60836b 1833 clear_probe_trace_event(&tev);
146a1439
MH
1834 if (ret < 0)
1835 break;
b498ce1f 1836 }
b498ce1f
MH
1837 strlist__delete(rawlist);
1838
146a1439
MH
1839 if (ret < 0) {
1840 strlist__delete(sl);
1841 return NULL;
1842 }
b498ce1f
MH
1843 return sl;
1844}
1845
0e60836b 1846static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
50656eec 1847{
6eca8cc3 1848 int ret = 0;
0e60836b 1849 char *buf = synthesize_probe_trace_command(tev);
50656eec 1850
146a1439 1851 if (!buf) {
0e60836b 1852 pr_debug("Failed to synthesize probe trace event.\n");
146a1439
MH
1853 return -EINVAL;
1854 }
1855
fa28244d 1856 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
1857 if (!probe_event_dry_run) {
1858 ret = write(fd, buf, strlen(buf));
1859 if (ret <= 0)
146a1439
MH
1860 pr_warning("Failed to write event: %s\n",
1861 strerror(errno));
f4d7da49 1862 }
4235b045 1863 free(buf);
146a1439 1864 return ret;
50656eec
MH
1865}
1866
146a1439
MH
1867static int get_new_event_name(char *buf, size_t len, const char *base,
1868 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
1869{
1870 int i, ret;
17f88fcd
MH
1871
1872 /* Try no suffix */
1873 ret = e_snprintf(buf, len, "%s", base);
146a1439
MH
1874 if (ret < 0) {
1875 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1876 return ret;
1877 }
17f88fcd 1878 if (!strlist__has_entry(namelist, buf))
146a1439 1879 return 0;
17f88fcd 1880
d761b08b
MH
1881 if (!allow_suffix) {
1882 pr_warning("Error: event \"%s\" already exists. "
1883 "(Use -f to force duplicates.)\n", base);
146a1439 1884 return -EEXIST;
d761b08b
MH
1885 }
1886
17f88fcd
MH
1887 /* Try to add suffix */
1888 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f 1889 ret = e_snprintf(buf, len, "%s_%d", base, i);
146a1439
MH
1890 if (ret < 0) {
1891 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1892 return ret;
1893 }
b498ce1f
MH
1894 if (!strlist__has_entry(namelist, buf))
1895 break;
1896 }
146a1439
MH
1897 if (i == MAX_EVENT_INDEX) {
1898 pr_warning("Too many events are on the same function.\n");
1899 ret = -ERANGE;
1900 }
1901
1902 return ret;
b498ce1f
MH
1903}
1904
0e60836b
SD
1905static int __add_probe_trace_events(struct perf_probe_event *pev,
1906 struct probe_trace_event *tevs,
146a1439 1907 int ntevs, bool allow_suffix)
50656eec 1908{
146a1439 1909 int i, fd, ret;
0e60836b 1910 struct probe_trace_event *tev = NULL;
4235b045
MH
1911 char buf[64];
1912 const char *event, *group;
b498ce1f 1913 struct strlist *namelist;
50656eec 1914
225466f1
SD
1915 if (pev->uprobes)
1916 fd = open_uprobe_events(true);
1917 else
1918 fd = open_kprobe_events(true);
1919
146a1439
MH
1920 if (fd < 0)
1921 return fd;
b498ce1f 1922 /* Get current event names */
0e60836b 1923 namelist = get_probe_trace_event_names(fd, false);
146a1439
MH
1924 if (!namelist) {
1925 pr_debug("Failed to get current event list.\n");
1926 return -EIO;
1927 }
4235b045 1928
146a1439 1929 ret = 0;
a844d1ef 1930 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
02b95dad 1931 for (i = 0; i < ntevs; i++) {
4235b045
MH
1932 tev = &tevs[i];
1933 if (pev->event)
1934 event = pev->event;
1935 else
1936 if (pev->point.function)
1937 event = pev->point.function;
1938 else
1939 event = tev->point.symbol;
1940 if (pev->group)
1941 group = pev->group;
1942 else
1943 group = PERFPROBE_GROUP;
1944
1945 /* Get an unused new event name */
146a1439
MH
1946 ret = get_new_event_name(buf, 64, event,
1947 namelist, allow_suffix);
1948 if (ret < 0)
1949 break;
4235b045
MH
1950 event = buf;
1951
02b95dad
MH
1952 tev->event = strdup(event);
1953 tev->group = strdup(group);
1954 if (tev->event == NULL || tev->group == NULL) {
1955 ret = -ENOMEM;
1956 break;
1957 }
0e60836b 1958 ret = write_probe_trace_event(fd, tev);
146a1439
MH
1959 if (ret < 0)
1960 break;
4235b045
MH
1961 /* Add added event name to namelist */
1962 strlist__add(namelist, event);
1963
1964 /* Trick here - save current event/group */
1965 event = pev->event;
1966 group = pev->group;
1967 pev->event = tev->event;
1968 pev->group = tev->group;
1969 show_perf_probe_event(pev);
1970 /* Trick here - restore current event/group */
1971 pev->event = (char *)event;
1972 pev->group = (char *)group;
1973
1974 /*
1975 * Probes after the first probe which comes from same
1976 * user input are always allowed to add suffix, because
1977 * there might be several addresses corresponding to
1978 * one code line.
1979 */
1980 allow_suffix = true;
50656eec 1981 }
146a1439
MH
1982
1983 if (ret >= 0) {
1984 /* Show how to use the event. */
a844d1ef 1985 printf("\nYou can now use it in all perf tools, such as:\n\n");
146a1439
MH
1986 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1987 tev->event);
1988 }
a9b495b0 1989
e1d2017b 1990 strlist__delete(namelist);
50656eec 1991 close(fd);
146a1439 1992 return ret;
50656eec 1993}
fa28244d 1994
0e60836b
SD
1995static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1996 struct probe_trace_event **tevs,
4eced234 1997 int max_tevs, const char *target)
e0faa8d3
MH
1998{
1999 struct symbol *sym;
fb7345bb 2000 int ret, i;
0e60836b 2001 struct probe_trace_event *tev;
4235b045 2002
fb7345bb
MH
2003 if (pev->uprobes && !pev->group) {
2004 /* Replace group name if not given */
2005 ret = convert_exec_to_group(target, &pev->group);
2006 if (ret != 0) {
2007 pr_warning("Failed to make a group name.\n");
2008 return ret;
2009 }
2010 }
2011
4b4da7f7 2012 /* Convert perf_probe_event with debuginfo */
4eced234 2013 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
e334016f 2014 if (ret != 0)
190b57fc 2015 return ret; /* Found in debuginfo or got an error */
e0faa8d3 2016
fb7345bb
MH
2017 if (pev->uprobes) {
2018 ret = convert_name_to_addr(pev, target);
2019 if (ret < 0)
2020 return ret;
2021 }
2022
4235b045 2023 /* Allocate trace event buffer */
0e60836b 2024 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
e334016f
MH
2025 if (tev == NULL)
2026 return -ENOMEM;
4235b045
MH
2027
2028 /* Copy parameters */
02b95dad
MH
2029 tev->point.symbol = strdup(pev->point.function);
2030 if (tev->point.symbol == NULL) {
2031 ret = -ENOMEM;
2032 goto error;
2033 }
ce27a443 2034
4eced234
SD
2035 if (target) {
2036 tev->point.module = strdup(target);
ce27a443
JZ
2037 if (tev->point.module == NULL) {
2038 ret = -ENOMEM;
2039 goto error;
2040 }
190b57fc 2041 }
ce27a443 2042
4235b045 2043 tev->point.offset = pev->point.offset;
04ddd04b 2044 tev->point.retprobe = pev->point.retprobe;
4235b045 2045 tev->nargs = pev->nargs;
225466f1
SD
2046 tev->uprobes = pev->uprobes;
2047
4235b045 2048 if (tev->nargs) {
0e60836b 2049 tev->args = zalloc(sizeof(struct probe_trace_arg)
e334016f
MH
2050 * tev->nargs);
2051 if (tev->args == NULL) {
02b95dad
MH
2052 ret = -ENOMEM;
2053 goto error;
e334016f 2054 }
48481938 2055 for (i = 0; i < tev->nargs; i++) {
02b95dad
MH
2056 if (pev->args[i].name) {
2057 tev->args[i].name = strdup(pev->args[i].name);
2058 if (tev->args[i].name == NULL) {
2059 ret = -ENOMEM;
2060 goto error;
2061 }
2062 }
2063 tev->args[i].value = strdup(pev->args[i].var);
2064 if (tev->args[i].value == NULL) {
2065 ret = -ENOMEM;
2066 goto error;
2067 }
2068 if (pev->args[i].type) {
2069 tev->args[i].type = strdup(pev->args[i].type);
2070 if (tev->args[i].type == NULL) {
2071 ret = -ENOMEM;
2072 goto error;
2073 }
2074 }
48481938 2075 }
4235b045
MH
2076 }
2077
225466f1
SD
2078 if (pev->uprobes)
2079 return 1;
2080
4235b045 2081 /* Currently just checking function name from symbol map */
469b9b88 2082 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
146a1439
MH
2083 if (!sym) {
2084 pr_warning("Kernel symbol \'%s\' not found.\n",
2085 tev->point.symbol);
02b95dad
MH
2086 ret = -ENOENT;
2087 goto error;
1c1bc922
PN
2088 } else if (tev->point.offset > sym->end - sym->start) {
2089 pr_warning("Offset specified is greater than size of %s\n",
2090 tev->point.symbol);
2091 ret = -ENOENT;
2092 goto error;
2093
02b95dad 2094 }
e334016f 2095
02b95dad
MH
2096 return 1;
2097error:
0e60836b 2098 clear_probe_trace_event(tev);
02b95dad
MH
2099 free(tev);
2100 *tevs = NULL;
e334016f 2101 return ret;
4235b045
MH
2102}
2103
2104struct __event_package {
2105 struct perf_probe_event *pev;
0e60836b 2106 struct probe_trace_event *tevs;
4235b045
MH
2107 int ntevs;
2108};
2109
146a1439 2110int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
4eced234 2111 int max_tevs, const char *target, bool force_add)
4235b045 2112{
146a1439 2113 int i, j, ret;
4235b045
MH
2114 struct __event_package *pkgs;
2115
225466f1 2116 ret = 0;
e334016f 2117 pkgs = zalloc(sizeof(struct __event_package) * npevs);
225466f1 2118
e334016f
MH
2119 if (pkgs == NULL)
2120 return -ENOMEM;
4235b045 2121
225466f1
SD
2122 if (!pevs->uprobes)
2123 /* Init vmlinux path */
2124 ret = init_vmlinux();
2125 else
2126 ret = init_user_exec();
2127
449e5b24
MH
2128 if (ret < 0) {
2129 free(pkgs);
146a1439 2130 return ret;
449e5b24 2131 }
4235b045
MH
2132
2133 /* Loop 1: convert all events */
2134 for (i = 0; i < npevs; i++) {
2135 pkgs[i].pev = &pevs[i];
2136 /* Convert with or without debuginfo */
0e60836b 2137 ret = convert_to_probe_trace_events(pkgs[i].pev,
469b9b88
MH
2138 &pkgs[i].tevs,
2139 max_tevs,
4eced234 2140 target);
146a1439
MH
2141 if (ret < 0)
2142 goto end;
2143 pkgs[i].ntevs = ret;
e0faa8d3
MH
2144 }
2145
4235b045 2146 /* Loop 2: add all events */
8635bf6e 2147 for (i = 0; i < npevs; i++) {
0e60836b 2148 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
146a1439 2149 pkgs[i].ntevs, force_add);
fbee632d
ACM
2150 if (ret < 0)
2151 break;
2152 }
146a1439 2153end:
449e5b24
MH
2154 /* Loop 3: cleanup and free trace events */
2155 for (i = 0; i < npevs; i++) {
146a1439 2156 for (j = 0; j < pkgs[i].ntevs; j++)
0e60836b 2157 clear_probe_trace_event(&pkgs[i].tevs[j]);
449e5b24
MH
2158 free(pkgs[i].tevs);
2159 }
2160 free(pkgs);
146a1439
MH
2161
2162 return ret;
e0faa8d3
MH
2163}
2164
0e60836b 2165static int __del_trace_probe_event(int fd, struct str_node *ent)
bbbb521b
MH
2166{
2167 char *p;
2168 char buf[128];
4235b045 2169 int ret;
bbbb521b 2170
0e60836b 2171 /* Convert from perf-probe event to trace-probe event */
146a1439
MH
2172 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2173 if (ret < 0)
2174 goto error;
2175
bbbb521b 2176 p = strchr(buf + 2, ':');
146a1439
MH
2177 if (!p) {
2178 pr_debug("Internal error: %s should have ':' but not.\n",
2179 ent->s);
2180 ret = -ENOTSUP;
2181 goto error;
2182 }
bbbb521b
MH
2183 *p = '/';
2184
4235b045
MH
2185 pr_debug("Writing event: %s\n", buf);
2186 ret = write(fd, buf, strlen(buf));
44a56040
MH
2187 if (ret < 0) {
2188 ret = -errno;
146a1439 2189 goto error;
44a56040 2190 }
146a1439 2191
a844d1ef 2192 printf("Removed event: %s\n", ent->s);
146a1439
MH
2193 return 0;
2194error:
2195 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2196 return ret;
bbbb521b
MH
2197}
2198
225466f1
SD
2199static int del_trace_probe_event(int fd, const char *buf,
2200 struct strlist *namelist)
fa28244d 2201{
bbbb521b 2202 struct str_node *ent, *n;
225466f1 2203 int ret = -1;
fa28244d 2204
bbbb521b
MH
2205 if (strpbrk(buf, "*?")) { /* Glob-exp */
2206 strlist__for_each_safe(ent, n, namelist)
2207 if (strglobmatch(ent->s, buf)) {
0e60836b 2208 ret = __del_trace_probe_event(fd, ent);
146a1439
MH
2209 if (ret < 0)
2210 break;
bbbb521b
MH
2211 strlist__remove(namelist, ent);
2212 }
2213 } else {
2214 ent = strlist__find(namelist, buf);
2215 if (ent) {
0e60836b 2216 ret = __del_trace_probe_event(fd, ent);
146a1439
MH
2217 if (ret >= 0)
2218 strlist__remove(namelist, ent);
bbbb521b
MH
2219 }
2220 }
146a1439
MH
2221
2222 return ret;
fa28244d
MH
2223}
2224
146a1439 2225int del_perf_probe_events(struct strlist *dellist)
fa28244d 2226{
225466f1
SD
2227 int ret = -1, ufd = -1, kfd = -1;
2228 char buf[128];
fa28244d
MH
2229 const char *group, *event;
2230 char *p, *str;
2231 struct str_node *ent;
225466f1 2232 struct strlist *namelist = NULL, *unamelist = NULL;
146a1439 2233
fa28244d 2234 /* Get current event names */
225466f1
SD
2235 kfd = open_kprobe_events(true);
2236 if (kfd < 0)
2237 return kfd;
2238
2239 namelist = get_probe_trace_event_names(kfd, true);
2240 ufd = open_uprobe_events(true);
2241
2242 if (ufd >= 0)
2243 unamelist = get_probe_trace_event_names(ufd, true);
2244
2245 if (namelist == NULL && unamelist == NULL)
2246 goto error;
fa28244d 2247
adf365f4 2248 strlist__for_each(ent, dellist) {
02b95dad
MH
2249 str = strdup(ent->s);
2250 if (str == NULL) {
2251 ret = -ENOMEM;
225466f1 2252 goto error;
02b95dad 2253 }
bbbb521b 2254 pr_debug("Parsing: %s\n", str);
fa28244d
MH
2255 p = strchr(str, ':');
2256 if (p) {
2257 group = str;
2258 *p = '\0';
2259 event = p + 1;
2260 } else {
bbbb521b 2261 group = "*";
fa28244d
MH
2262 event = str;
2263 }
225466f1
SD
2264
2265 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2266 if (ret < 0) {
2267 pr_err("Failed to copy event.");
2268 free(str);
2269 goto error;
2270 }
2271
bbbb521b 2272 pr_debug("Group: %s, Event: %s\n", group, event);
225466f1
SD
2273
2274 if (namelist)
2275 ret = del_trace_probe_event(kfd, buf, namelist);
2276
2277 if (unamelist && ret != 0)
2278 ret = del_trace_probe_event(ufd, buf, unamelist);
2279
2280 if (ret != 0)
2281 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2282
fa28244d
MH
2283 free(str);
2284 }
225466f1
SD
2285
2286error:
2287 if (kfd >= 0) {
a23c4dc4 2288 strlist__delete(namelist);
225466f1
SD
2289 close(kfd);
2290 }
2291
2292 if (ufd >= 0) {
a23c4dc4 2293 strlist__delete(unamelist);
225466f1
SD
2294 close(ufd);
2295 }
146a1439
MH
2296
2297 return ret;
fa28244d 2298}
225466f1 2299
3c42258c
MH
2300/* TODO: don't use a global variable for filter ... */
2301static struct strfilter *available_func_filter;
fa28244d 2302
e80711ca 2303/*
3c42258c
MH
2304 * If a symbol corresponds to a function with global binding and
2305 * matches filter return 0. For all others return 1.
e80711ca 2306 */
1d037ca1 2307static int filter_available_functions(struct map *map __maybe_unused,
3c42258c 2308 struct symbol *sym)
e80711ca 2309{
3c42258c
MH
2310 if (sym->binding == STB_GLOBAL &&
2311 strfilter__compare(available_func_filter, sym->name))
2312 return 0;
2313 return 1;
e80711ca
MH
2314}
2315
225466f1
SD
2316static int __show_available_funcs(struct map *map)
2317{
2318 if (map__load(map, filter_available_functions)) {
2319 pr_err("Failed to load map.\n");
2320 return -EINVAL;
2321 }
2322 if (!dso__sorted_by_name(map->dso, map->type))
2323 dso__sort_by_name(map->dso, map->type);
2324
2325 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2326 return 0;
2327}
2328
2329static int available_kernel_funcs(const char *module)
e80711ca
MH
2330{
2331 struct map *map;
2332 int ret;
2333
e80711ca
MH
2334 ret = init_vmlinux();
2335 if (ret < 0)
2336 return ret;
2337
225466f1 2338 map = kernel_get_module_map(module);
e80711ca 2339 if (!map) {
225466f1 2340 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
e80711ca
MH
2341 return -EINVAL;
2342 }
225466f1
SD
2343 return __show_available_funcs(map);
2344}
2345
2346static int available_user_funcs(const char *target)
2347{
2348 struct map *map;
2349 int ret;
2350
2351 ret = init_user_exec();
2352 if (ret < 0)
2353 return ret;
2354
2355 map = dso__new_map(target);
2356 ret = __show_available_funcs(map);
2357 dso__delete(map->dso);
2358 map__delete(map);
2359 return ret;
2360}
2361
2362int show_available_funcs(const char *target, struct strfilter *_filter,
2363 bool user)
2364{
2365 setup_pager();
3c42258c 2366 available_func_filter = _filter;
225466f1
SD
2367
2368 if (!user)
2369 return available_kernel_funcs(target);
2370
2371 return available_user_funcs(target);
2372}
2373
2374/*
2375 * uprobe_events only accepts address:
2376 * Convert function and any offset to address
2377 */
2378static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2379{
2380 struct perf_probe_point *pp = &pev->point;
2381 struct symbol *sym;
2382 struct map *map = NULL;
8a613d40 2383 char *function = NULL;
225466f1
SD
2384 int ret = -EINVAL;
2385 unsigned long long vaddr = 0;
2386
2387 if (!pp->function) {
2388 pr_warning("No function specified for uprobes");
2389 goto out;
2390 }
2391
2392 function = strdup(pp->function);
2393 if (!function) {
2394 pr_warning("Failed to allocate memory by strdup.\n");
2395 ret = -ENOMEM;
2396 goto out;
2397 }
2398
8a613d40 2399 map = dso__new_map(exec);
225466f1
SD
2400 if (!map) {
2401 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2402 goto out;
2403 }
2404 available_func_filter = strfilter__new(function, NULL);
3c42258c 2405 if (map__load(map, filter_available_functions)) {
e80711ca 2406 pr_err("Failed to load map.\n");
225466f1 2407 goto out;
e80711ca 2408 }
e80711ca 2409
225466f1
SD
2410 sym = map__find_symbol_by_name(map, function, NULL);
2411 if (!sym) {
2412 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2413 goto out;
2414 }
2415
2416 if (map->start > sym->start)
2417 vaddr = map->start;
2418 vaddr += sym->start + pp->offset + map->pgoff;
2419 pp->offset = 0;
2420
2421 if (!pev->event) {
2422 pev->event = function;
2423 function = NULL;
2424 }
2425 if (!pev->group) {
1fb89448 2426 char *ptr1, *ptr2, *exec_copy;
225466f1
SD
2427
2428 pev->group = zalloc(sizeof(char *) * 64);
1fb89448
DA
2429 exec_copy = strdup(exec);
2430 if (!exec_copy) {
2431 ret = -ENOMEM;
2432 pr_warning("Failed to copy exec string.\n");
2433 goto out;
2434 }
2435
2436 ptr1 = strdup(basename(exec_copy));
225466f1
SD
2437 if (ptr1) {
2438 ptr2 = strpbrk(ptr1, "-._");
2439 if (ptr2)
2440 *ptr2 = '\0';
2441 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2442 ptr1);
2443 free(ptr1);
2444 }
1fb89448 2445 free(exec_copy);
225466f1
SD
2446 }
2447 free(pp->function);
2448 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2449 if (!pp->function) {
2450 ret = -ENOMEM;
2451 pr_warning("Failed to allocate memory by zalloc.\n");
2452 goto out;
2453 }
2454 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2455 ret = 0;
2456
2457out:
2458 if (map) {
2459 dso__delete(map->dso);
2460 map__delete(map);
2461 }
2462 if (function)
2463 free(function);
225466f1 2464 return ret;
e80711ca 2465}
This page took 0.319371 seconds and 5 git commands to generate.