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