blob: 4fd49f0210733a86992400dd004197cebf01bb61 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
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
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#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>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu146a1432010-04-12 13:17:42 -040055#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050056
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030073static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000074static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000075static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000078static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090083 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090084 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040085 if (ret < 0) {
86 pr_debug("Failed to init symbol map.\n");
87 goto out;
88 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040089
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000090 if (host_machine || user_only) /* already initialized */
91 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030092
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000093 if (symbol_conf.vmlinux_name)
94 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95
96 host_machine = machine__new_host();
97 if (!host_machine) {
98 pr_debug("machine__new_host() failed.\n");
99 symbol__exit();
100 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900101 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400102out:
103 if (ret < 0)
104 pr_warning("Failed to init vmlinux path.\n");
105 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106}
107
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000108static void exit_symbol_maps(void)
109{
110 if (host_machine) {
111 machine__delete(host_machine);
112 host_machine = NULL;
113 }
114 symbol__exit();
115}
116
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900117static struct symbol *__find_kernel_function_by_name(const char *name,
118 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400119{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000120 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900121 NULL);
122}
123
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000124static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
125{
126 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
127}
128
129static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
130{
131 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
132 struct kmap *kmap;
133
134 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
135 return NULL;
136
137 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
Wang Nanba927322015-04-07 08:22:45 +0000138 if (!kmap)
139 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000140 return kmap->ref_reloc_sym;
141}
142
143static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
144{
145 struct ref_reloc_sym *reloc_sym;
146 struct symbol *sym;
147 struct map *map;
148
149 /* ref_reloc_sym is just a label. Need a special fix*/
150 reloc_sym = kernel_get_ref_reloc_sym();
151 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
152 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
153 else {
154 sym = __find_kernel_function_by_name(name, &map);
155 if (sym)
156 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800157 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000158 }
159 return 0;
160}
161
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900162static struct map *kernel_get_module_map(const char *module)
163{
164 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000165 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900166
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167 /* A file path -- this is an offline module */
168 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000169 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900170
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900171 if (!module)
172 module = "kernel";
173
174 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
175 struct map *pos = rb_entry(nd, struct map, rb_node);
176 if (strncmp(pos->dso->short_name + 1, module,
177 pos->dso->short_name_len - 2) == 0) {
178 return pos;
179 }
180 }
181 return NULL;
182}
183
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900184static struct map *get_target_map(const char *target, bool user)
185{
186 /* Init maps of given executable or kernel */
187 if (user)
188 return dso__new_map(target);
189 else
190 return kernel_get_module_map(target);
191}
192
193static void put_target_map(struct map *map, bool user)
194{
195 if (map && user) {
196 /* Only the user map needs to be released */
197 dso__delete(map->dso);
198 map__delete(map);
199 }
200}
201
202
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900203static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900204{
205 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100206 struct map *map;
207 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900208
209 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400210 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
211 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900212 if (strncmp(dso->short_name + 1, module,
213 dso->short_name_len - 2) == 0)
214 goto found;
215 }
216 pr_debug("Failed to find module %s.\n", module);
217 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100218 }
219
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000220 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100221 dso = map->dso;
222
223 vmlinux_name = symbol_conf.vmlinux_name;
224 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300225 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100226 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900227 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100228 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900229 pr_debug("Failed to load kernel map.\n");
230 return NULL;
231 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400232 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900233found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900234 return dso;
235}
236
237const char *kernel_get_module_path(const char *module)
238{
239 struct dso *dso = kernel_get_module_dso(module);
240 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900241}
242
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000243static int convert_exec_to_group(const char *exec, char **result)
244{
245 char *ptr1, *ptr2, *exec_copy;
246 char buf[64];
247 int ret;
248
249 exec_copy = strdup(exec);
250 if (!exec_copy)
251 return -ENOMEM;
252
253 ptr1 = basename(exec_copy);
254 if (!ptr1) {
255 ret = -EINVAL;
256 goto out;
257 }
258
259 ptr2 = strpbrk(ptr1, "-._");
260 if (ptr2)
261 *ptr2 = '\0';
262 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
263 if (ret < 0)
264 goto out;
265
266 *result = strdup(buf);
267 ret = *result ? 0 : -ENOMEM;
268
269out:
270 free(exec_copy);
271 return ret;
272}
273
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900274static void clear_perf_probe_point(struct perf_probe_point *pp)
275{
276 free(pp->file);
277 free(pp->function);
278 free(pp->lazy_line);
279}
280
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000281static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
282{
283 int i;
284
285 for (i = 0; i < ntevs; i++)
286 clear_probe_trace_event(tevs + i);
287}
288
Ingo Molnar89fe8082013-09-30 12:07:11 +0200289#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900290/*
291 * Some binaries like glibc have special symbols which are on the symbol
292 * table, but not in the debuginfo. If we can find the address of the
293 * symbol from map, we can translate the address back to the probe point.
294 */
295static int find_alternative_probe_point(struct debuginfo *dinfo,
296 struct perf_probe_point *pp,
297 struct perf_probe_point *result,
298 const char *target, bool uprobes)
299{
300 struct map *map = NULL;
301 struct symbol *sym;
302 u64 address = 0;
303 int ret = -ENOENT;
304
305 /* This can work only for function-name based one */
306 if (!pp->function || pp->file)
307 return -ENOTSUP;
308
309 map = get_target_map(target, uprobes);
310 if (!map)
311 return -EINVAL;
312
313 /* Find the address of given function */
314 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900315 if (uprobes)
316 address = sym->start;
317 else
318 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900319 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900320 }
321 if (!address) {
322 ret = -ENOENT;
323 goto out;
324 }
325 pr_debug("Symbol %s address found : %lx\n", pp->function, address);
326
327 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
328 result);
329 if (ret <= 0)
330 ret = (!ret) ? -ENOENT : ret;
331 else {
332 result->offset += pp->offset;
333 result->line += pp->line;
334 ret = 0;
335 }
336
337out:
338 put_target_map(map, uprobes);
339 return ret;
340
341}
342
343static int get_alternative_probe_event(struct debuginfo *dinfo,
344 struct perf_probe_event *pev,
345 struct perf_probe_point *tmp,
346 const char *target)
347{
348 int ret;
349
350 memcpy(tmp, &pev->point, sizeof(*tmp));
351 memset(&pev->point, 0, sizeof(pev->point));
352 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
353 target, pev->uprobes);
354 if (ret < 0)
355 memcpy(&pev->point, tmp, sizeof(*tmp));
356
357 return ret;
358}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000359
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900360static int get_alternative_line_range(struct debuginfo *dinfo,
361 struct line_range *lr,
362 const char *target, bool user)
363{
David Ahern6d4a4892015-03-11 10:36:20 -0400364 struct perf_probe_point pp = { .function = lr->function,
365 .file = lr->file,
366 .line = lr->start };
367 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900368 int ret, len = 0;
369
David Ahern6d4a4892015-03-11 10:36:20 -0400370 memset(&result, 0, sizeof(result));
371
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900372 if (lr->end != INT_MAX)
373 len = lr->end - lr->start;
374 ret = find_alternative_probe_point(dinfo, &pp, &result,
375 target, user);
376 if (!ret) {
377 lr->function = result.function;
378 lr->file = result.file;
379 lr->start = result.line;
380 if (lr->end != INT_MAX)
381 lr->end = lr->start + len;
382 clear_perf_probe_point(&pp);
383 }
384 return ret;
385}
386
Masami Hiramatsuff741782011-06-27 16:27:39 +0900387/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000388static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900389{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000390 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000391 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900392
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000393 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900394 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900395 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000396 if (!silent)
397 pr_err("Failed to find path of %s module.\n",
398 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900399 return NULL;
400 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900401 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000402 ret = debuginfo__new(path);
403 if (!ret && !silent) {
404 pr_warning("The %s file has no debug information.\n", path);
405 if (!module || !strtailcmp(path, ".ko"))
406 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
407 else
408 pr_warning("Rebuild with -g, ");
409 pr_warning("or install an appropriate debuginfo package.\n");
410 }
411 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400412}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300413
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000414
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000415static int get_text_start_address(const char *exec, unsigned long *address)
416{
417 Elf *elf;
418 GElf_Ehdr ehdr;
419 GElf_Shdr shdr;
420 int fd, ret = -ENOENT;
421
422 fd = open(exec, O_RDONLY);
423 if (fd < 0)
424 return -errno;
425
426 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
427 if (elf == NULL)
428 return -EINVAL;
429
430 if (gelf_getehdr(elf, &ehdr) == NULL)
431 goto out;
432
433 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
434 goto out;
435
436 *address = shdr.sh_addr - shdr.sh_offset;
437 ret = 0;
438out:
439 elf_end(elf);
440 return ret;
441}
442
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000443/*
444 * Convert trace point to probe point with debuginfo
445 */
446static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
447 struct perf_probe_point *pp,
448 bool is_kprobe)
449{
450 struct debuginfo *dinfo = NULL;
451 unsigned long stext = 0;
452 u64 addr = tp->address;
453 int ret = -ENOENT;
454
455 /* convert the address to dwarf address */
456 if (!is_kprobe) {
457 if (!addr) {
458 ret = -EINVAL;
459 goto error;
460 }
461 ret = get_text_start_address(tp->module, &stext);
462 if (ret < 0)
463 goto error;
464 addr += stext;
465 } else {
466 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
467 if (addr == 0)
468 goto error;
469 addr += tp->offset;
470 }
471
472 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
473 tp->module ? : "kernel");
474
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000475 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000476 if (dinfo) {
477 ret = debuginfo__find_probe_point(dinfo,
478 (unsigned long)addr, pp);
479 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000480 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000481 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000482
483 if (ret > 0) {
484 pp->retprobe = tp->retprobe;
485 return 0;
486 }
487error:
488 pr_debug("Failed to find corresponding probes from debuginfo.\n");
489 return ret ? : -ENOENT;
490}
491
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000492static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
493 int ntevs, const char *exec)
494{
495 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000496 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000497
498 if (!exec)
499 return 0;
500
501 ret = get_text_start_address(exec, &stext);
502 if (ret < 0)
503 return ret;
504
505 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000506 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000507 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000508 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000509 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000510 ret = -ENOMEM;
511 break;
512 }
513 tevs[i].uprobes = true;
514 }
515
516 return ret;
517}
518
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900519static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
520 int ntevs, const char *module)
521{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900522 int i, ret = 0;
523 char *tmp;
524
525 if (!module)
526 return 0;
527
528 tmp = strrchr(module, '/');
529 if (tmp) {
530 /* This is a module path -- get the module name */
531 module = strdup(tmp + 1);
532 if (!module)
533 return -ENOMEM;
534 tmp = strchr(module, '.');
535 if (tmp)
536 *tmp = '\0';
537 tmp = (char *)module; /* For free() */
538 }
539
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900540 for (i = 0; i < ntevs; i++) {
541 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900542 if (!tevs[i].point.module) {
543 ret = -ENOMEM;
544 break;
545 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900546 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900547
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300548 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900549 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900550}
551
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000552/* Post processing the probe events */
553static int post_process_probe_trace_events(struct probe_trace_event *tevs,
554 int ntevs, const char *module,
555 bool uprobe)
556{
557 struct ref_reloc_sym *reloc_sym;
558 char *tmp;
559 int i;
560
561 if (uprobe)
562 return add_exec_to_probe_trace_events(tevs, ntevs, module);
563
564 /* Note that currently ref_reloc_sym based probe is not for drivers */
565 if (module)
566 return add_module_to_probe_trace_events(tevs, ntevs, module);
567
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000568 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000569 if (!reloc_sym) {
570 pr_warning("Relocated base symbol is not found!\n");
571 return -EINVAL;
572 }
573
574 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900575 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000576 tmp = strdup(reloc_sym->name);
577 if (!tmp)
578 return -ENOMEM;
579 free(tevs[i].point.symbol);
580 tevs[i].point.symbol = tmp;
581 tevs[i].point.offset = tevs[i].point.address -
582 reloc_sym->unrelocated_addr;
583 }
584 }
585 return 0;
586}
587
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300588/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530589static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900590 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530591 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300592{
593 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900594 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530595 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900596 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300597
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000598 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530599
Masami Hiramatsuff741782011-06-27 16:27:39 +0900600 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000601 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900602 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900603 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300604 return 0;
605 }
606
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000607 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900608 /* Searching trace events corresponding to a probe event */
609 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
610
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900611 if (ntevs == 0) { /* Not found, retry with an alternative */
612 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
613 if (!ret) {
614 ntevs = debuginfo__find_trace_events(dinfo, pev,
615 tevs, max_tevs);
616 /*
617 * Write back to the original probe_event for
618 * setting appropriate (user given) event name
619 */
620 clear_perf_probe_point(&pev->point);
621 memcpy(&pev->point, &tmp, sizeof(tmp));
622 }
623 }
624
Masami Hiramatsuff741782011-06-27 16:27:39 +0900625 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300626
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400627 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000628 pr_debug("Found %d probe_trace_events.\n", ntevs);
629 ret = post_process_probe_trace_events(*tevs, ntevs,
630 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000631 if (ret < 0) {
632 clear_probe_trace_events(*tevs, ntevs);
633 zfree(tevs);
634 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900635 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400636 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300637
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400638 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900639 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400640 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900641 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400642 }
643 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400644 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
645 if (ntevs == -EBADF) {
646 pr_warning("Warning: No dwarf info found in the vmlinux - "
647 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
648 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900649 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400650 return 0;
651 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300652 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400653 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300654}
655
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900656/*
657 * Find a src file from a DWARF tag path. Prepend optional source path prefix
658 * and chop off leading directories that do not exist. Result is passed back as
659 * a newly allocated path on success.
660 * Return 0 if file was found and readable, -errno otherwise.
661 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900662static int get_real_path(const char *raw_path, const char *comp_dir,
663 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900664{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900665 const char *prefix = symbol_conf.source_prefix;
666
667 if (!prefix) {
668 if (raw_path[0] != '/' && comp_dir)
669 /* If not an absolute path, try to use comp_dir */
670 prefix = comp_dir;
671 else {
672 if (access(raw_path, R_OK) == 0) {
673 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300674 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900675 } else
676 return -errno;
677 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900678 }
679
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900680 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900681 if (!*new_path)
682 return -ENOMEM;
683
684 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900685 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900686
687 if (access(*new_path, R_OK) == 0)
688 return 0;
689
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900690 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900691 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900692 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900693 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900694 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900695
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900696 switch (errno) {
697 case ENAMETOOLONG:
698 case ENOENT:
699 case EROFS:
700 case EFAULT:
701 raw_path = strchr(++raw_path, '/');
702 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300703 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900704 return -ENOENT;
705 }
706 continue;
707
708 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300709 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900710 return -errno;
711 }
712 }
713}
714
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300715#define LINEBUF_SIZE 256
716#define NR_ADDITIONAL_LINES 2
717
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100718static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000720 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100721 const char *color = show_num ? "" : PERF_COLOR_BLUE;
722 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300723
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100724 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
726 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100727 if (skip)
728 continue;
729 if (!prefix) {
730 prefix = show_num ? "%7d " : " ";
731 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300732 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100733 color_fprintf(stdout, color, "%s", buf);
734
735 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400736
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100737 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100739 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000740 pr_warning("File read error: %s\n",
741 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100742 return -1;
743 }
744 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300745}
746
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100747static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
748{
749 int rv = __show_one_line(fp, l, skip, show_num);
750 if (rv == 0) {
751 pr_warning("Source file is shorter than expected.\n");
752 rv = -1;
753 }
754 return rv;
755}
756
757#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
758#define show_one_line(f,l) _show_one_line(f,l,false,false)
759#define skip_one_line(f,l) _show_one_line(f,l,true,false)
760#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
761
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300762/*
763 * Show line-range always requires debuginfo to find source file and
764 * line number.
765 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900766static int __show_line_range(struct line_range *lr, const char *module,
767 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300768{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400769 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000770 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900771 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300772 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900773 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900774 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000775 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300776
777 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000778 dinfo = open_debuginfo(module, false);
779 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900780 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400781
Masami Hiramatsuff741782011-06-27 16:27:39 +0900782 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900783 if (!ret) { /* Not found, retry with an alternative */
784 ret = get_alternative_line_range(dinfo, lr, module, user);
785 if (!ret)
786 ret = debuginfo__find_line_range(dinfo, lr);
787 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900788 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000789 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400790 pr_warning("Specified source line is not found.\n");
791 return -ENOENT;
792 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000793 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400794 return ret;
795 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300796
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900797 /* Convert source file path */
798 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900799 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800800
801 /* Free old path when new path is assigned */
802 if (tmp != lr->path)
803 free(tmp);
804
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900805 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000806 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900807 return ret;
808 }
809
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810 setup_pager();
811
812 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900813 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300814 lr->start - lr->offset);
815 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100816 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300817
818 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400819 if (fp == NULL) {
820 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000821 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400822 return -errno;
823 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300824 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100825 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100826 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100827 if (ret < 0)
828 goto end;
829 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000831 intlist__for_each(ln, lr->line_list) {
832 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100833 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100834 if (ret < 0)
835 goto end;
836 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100837 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838 if (ret < 0)
839 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300840 }
841
842 if (lr->end == INT_MAX)
843 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100844 while (l <= lr->end) {
845 ret = show_one_line_or_eof(fp, l++ - lr->offset);
846 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100847 break;
848 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400849end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300850 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400851 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300852}
853
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000854int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000855{
856 int ret;
857
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000858 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000859 if (ret < 0)
860 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900861 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000862 exit_symbol_maps();
863
864 return ret;
865}
866
Masami Hiramatsuff741782011-06-27 16:27:39 +0900867static int show_available_vars_at(struct debuginfo *dinfo,
868 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900869 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900870 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900871{
872 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900873 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900874 struct str_node *node;
875 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900876 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900877 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900878
879 buf = synthesize_perf_probe_point(&pev->point);
880 if (!buf)
881 return -EINVAL;
882 pr_debug("Searching variables at %s\n", buf);
883
Masami Hiramatsuff741782011-06-27 16:27:39 +0900884 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
885 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900886 if (!ret) { /* Not found, retry with an alternative */
887 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
888 if (!ret) {
889 ret = debuginfo__find_available_vars_at(dinfo, pev,
890 &vls, max_vls, externs);
891 /* Release the old probe_point */
892 clear_perf_probe_point(&tmp);
893 }
894 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900895 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000896 if (ret == 0 || ret == -ENOENT) {
897 pr_err("Failed to find the address of %s\n", buf);
898 ret = -ENOENT;
899 } else
900 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900901 goto end;
902 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000903
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900904 /* Some variables are found */
905 fprintf(stdout, "Available variables at %s\n", buf);
906 for (i = 0; i < ret; i++) {
907 vl = &vls[i];
908 /*
909 * A probe point might be converted to
910 * several trace points.
911 */
912 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
913 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300914 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900915 nvars = 0;
916 if (vl->vars) {
917 strlist__for_each(node, vl->vars) {
918 var = strchr(node->s, '\t') + 1;
919 if (strfilter__compare(_filter, var)) {
920 fprintf(stdout, "\t\t%s\n", node->s);
921 nvars++;
922 }
923 }
924 strlist__delete(vl->vars);
925 }
926 if (nvars == 0)
927 fprintf(stdout, "\t\t(No matched variables)\n");
928 }
929 free(vls);
930end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900931 free(buf);
932 return ret;
933}
934
935/* Show available variables on given probe point */
936int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900937 int max_vls, const char *module,
938 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900939{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900940 int i, ret = 0;
941 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900942
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000943 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944 if (ret < 0)
945 return ret;
946
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000947 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900948 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000949 ret = -ENOENT;
950 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900951 }
952
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900953 setup_pager();
954
Masami Hiramatsuff741782011-06-27 16:27:39 +0900955 for (i = 0; i < npevs && ret >= 0; i++)
956 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900957 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900958
959 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000960out:
961 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900962 return ret;
963}
964
Ingo Molnar89fe8082013-09-30 12:07:11 +0200965#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300966
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000967static int
968find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
969 struct perf_probe_point *pp __maybe_unused,
970 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300971{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000972 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300973}
974
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530975static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300976 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300977 int max_tevs __maybe_unused,
978 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300979{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400980 if (perf_probe_event_need_dwarf(pev)) {
981 pr_warning("Debuginfo-analysis is not supported.\n");
982 return -ENOSYS;
983 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530984
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985 return 0;
986}
987
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300988int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000989 const char *module __maybe_unused,
990 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300991{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400992 pr_warning("Debuginfo-analysis is not supported.\n");
993 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300994}
995
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300996int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
997 int npevs __maybe_unused, int max_vls __maybe_unused,
998 const char *module __maybe_unused,
999 struct strfilter *filter __maybe_unused,
1000 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001001{
1002 pr_warning("Debuginfo-analysis is not supported.\n");
1003 return -ENOSYS;
1004}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001005#endif
1006
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001007void line_range__clear(struct line_range *lr)
1008{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001009 free(lr->function);
1010 free(lr->file);
1011 free(lr->path);
1012 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001013 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001014 memset(lr, 0, sizeof(*lr));
1015}
1016
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001017int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001018{
1019 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001020 lr->line_list = intlist__new(NULL);
1021 if (!lr->line_list)
1022 return -ENOMEM;
1023 else
1024 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001025}
1026
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001027static int parse_line_num(char **ptr, int *val, const char *what)
1028{
1029 const char *start = *ptr;
1030
1031 errno = 0;
1032 *val = strtol(*ptr, ptr, 0);
1033 if (errno || *ptr == start) {
1034 semantic_error("'%s' is not a valid number.\n", what);
1035 return -EINVAL;
1036 }
1037 return 0;
1038}
1039
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001040/*
1041 * Stuff 'lr' according to the line range described by 'arg'.
1042 * The line range syntax is described by:
1043 *
1044 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001045 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001046 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001047int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001048{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001049 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001050 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001051
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001052 if (!name)
1053 return -ENOMEM;
1054
1055 lr->start = 0;
1056 lr->end = INT_MAX;
1057
1058 range = strchr(name, ':');
1059 if (range) {
1060 *range++ = '\0';
1061
1062 err = parse_line_num(&range, &lr->start, "start line");
1063 if (err)
1064 goto err;
1065
1066 if (*range == '+' || *range == '-') {
1067 const char c = *range++;
1068
1069 err = parse_line_num(&range, &lr->end, "end line");
1070 if (err)
1071 goto err;
1072
1073 if (c == '+') {
1074 lr->end += lr->start;
1075 /*
1076 * Adjust the number of lines here.
1077 * If the number of lines == 1, the
1078 * the end of line should be equal to
1079 * the start of line.
1080 */
1081 lr->end--;
1082 }
1083 }
1084
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001085 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001086
1087 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001088 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001089 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001090 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001091 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001093 if (*range != '\0') {
1094 semantic_error("Tailing with invalid str '%s'.\n", range);
1095 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001096 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001097 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001098
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001099 file = strchr(name, '@');
1100 if (file) {
1101 *file = '\0';
1102 lr->file = strdup(++file);
1103 if (lr->file == NULL) {
1104 err = -ENOMEM;
1105 goto err;
1106 }
1107 lr->function = name;
1108 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001109 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001110 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001111 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001112
1113 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001114err:
1115 free(name);
1116 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001117}
1118
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001119/* Check the name is good for event/group */
1120static bool check_event_name(const char *name)
1121{
1122 if (!isalpha(*name) && *name != '_')
1123 return false;
1124 while (*++name != '\0') {
1125 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1126 return false;
1127 }
1128 return true;
1129}
1130
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001131/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001132static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001133{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001134 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001135 char *ptr, *tmp;
1136 char c, nc = 0;
1137 /*
1138 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001139 * perf probe [EVENT=]SRC[:LN|;PTN]
1140 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001141 *
1142 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001143 */
1144
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001145 ptr = strpbrk(arg, ";=@+%");
1146 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001147 *ptr = '\0';
1148 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001149 if (strchr(arg, ':')) {
1150 semantic_error("Group name is not supported yet.\n");
1151 return -ENOTSUP;
1152 }
1153 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001154 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001155 "follow C symbol-naming rule.\n", arg);
1156 return -EINVAL;
1157 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001158 pev->event = strdup(arg);
1159 if (pev->event == NULL)
1160 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001161 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001162 arg = tmp;
1163 }
1164
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001165 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001166 if (ptr) {
1167 nc = *ptr;
1168 *ptr++ = '\0';
1169 }
1170
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001171 tmp = strdup(arg);
1172 if (tmp == NULL)
1173 return -ENOMEM;
1174
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001175 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001176 if (strchr(tmp, '.')) /* File */
1177 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001178 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001179 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001180
1181 /* Parse other options */
1182 while (ptr) {
1183 arg = ptr;
1184 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001185 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001186 pp->lazy_line = strdup(arg);
1187 if (pp->lazy_line == NULL)
1188 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001189 break;
1190 }
1191 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001192 if (ptr) {
1193 nc = *ptr;
1194 *ptr++ = '\0';
1195 }
1196 switch (c) {
1197 case ':': /* Line number */
1198 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001200 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 " in line number.\n");
1202 return -EINVAL;
1203 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001204 break;
1205 case '+': /* Byte offset from a symbol */
1206 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001207 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001208 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 " in offset.\n");
1210 return -EINVAL;
1211 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001212 break;
1213 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214 if (pp->file) {
1215 semantic_error("SRC@SRC is not allowed.\n");
1216 return -EINVAL;
1217 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001218 pp->file = strdup(arg);
1219 if (pp->file == NULL)
1220 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001221 break;
1222 case '%': /* Probe places */
1223 if (strcmp(arg, "return") == 0) {
1224 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225 } else { /* Others not supported yet */
1226 semantic_error("%%%s is not supported.\n", arg);
1227 return -ENOTSUP;
1228 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001229 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 default: /* Buggy case */
1231 pr_err("This program has a bug at %s:%d.\n",
1232 __FILE__, __LINE__);
1233 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001234 break;
1235 }
1236 }
1237
1238 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001240 semantic_error("Lazy pattern can't be used with"
1241 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 return -EINVAL;
1243 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001246 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 return -EINVAL;
1248 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001249
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001251 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 return -EINVAL;
1253 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001254
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001256 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001257 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 return -EINVAL;
1259 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001260
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001262 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 return -EINVAL;
1264 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001265
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001266 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001267 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001268 return -EINVAL;
1269 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001270
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001271 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001272 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001273 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001274 return -EINVAL;
1275 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001276
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001278 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1279 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001281}
1282
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001283/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001284static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001285{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001286 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001287 struct perf_probe_arg_field **fieldp;
1288
1289 pr_debug("parsing arg: %s into ", str);
1290
Masami Hiramatsu48481932010-04-12 13:16:53 -04001291 tmp = strchr(str, '=');
1292 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001293 arg->name = strndup(str, tmp - str);
1294 if (arg->name == NULL)
1295 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001296 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001297 str = tmp + 1;
1298 }
1299
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001300 tmp = strchr(str, ':');
1301 if (tmp) { /* Type setting */
1302 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001303 arg->type = strdup(tmp + 1);
1304 if (arg->type == NULL)
1305 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001306 pr_debug("type:%s ", arg->type);
1307 }
1308
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001309 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001310 if (!is_c_varname(str) || !tmp) {
1311 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001312 arg->var = strdup(str);
1313 if (arg->var == NULL)
1314 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001315 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001316 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001317 }
1318
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001319 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 arg->var = strndup(str, tmp - str);
1321 if (arg->var == NULL)
1322 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001323 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001324 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001325 fieldp = &arg->field;
1326
1327 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001328 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1329 if (*fieldp == NULL)
1330 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001331 if (*tmp == '[') { /* Array */
1332 str = tmp;
1333 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001334 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001335 if (*tmp != ']' || tmp == str + 1) {
1336 semantic_error("Array index must be a"
1337 " number.\n");
1338 return -EINVAL;
1339 }
1340 tmp++;
1341 if (*tmp == '\0')
1342 tmp = NULL;
1343 } else { /* Structure */
1344 if (*tmp == '.') {
1345 str = tmp + 1;
1346 (*fieldp)->ref = false;
1347 } else if (tmp[1] == '>') {
1348 str = tmp + 2;
1349 (*fieldp)->ref = true;
1350 } else {
1351 semantic_error("Argument parse error: %s\n",
1352 str);
1353 return -EINVAL;
1354 }
1355 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001356 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001357 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001358 (*fieldp)->name = strndup(str, tmp - str);
1359 if ((*fieldp)->name == NULL)
1360 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001361 if (*str != '[')
1362 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001363 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1364 fieldp = &(*fieldp)->next;
1365 }
1366 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001367 (*fieldp)->name = strdup(str);
1368 if ((*fieldp)->name == NULL)
1369 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001370 if (*str != '[')
1371 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001372 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001373
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001374 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001375 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001376 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001377 if (arg->name == NULL)
1378 return -ENOMEM;
1379 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001381}
1382
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001383/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001385{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001386 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001388
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001389 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001390 if (!argv) {
1391 pr_debug("Failed to split arguments.\n");
1392 return -ENOMEM;
1393 }
1394 if (argc - 1 > MAX_PROBE_ARGS) {
1395 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1396 ret = -ERANGE;
1397 goto out;
1398 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001399 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 ret = parse_perf_probe_point(argv[0], pev);
1401 if (ret < 0)
1402 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001403
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001404 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001405 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001406 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1407 if (pev->args == NULL) {
1408 ret = -ENOMEM;
1409 goto out;
1410 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1412 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1413 if (ret >= 0 &&
1414 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001415 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416 " kretprobe.\n");
1417 ret = -EINVAL;
1418 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001419 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001421 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001422
1423 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001424}
1425
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426/* Return true if this perf_probe_event requires debuginfo */
1427bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001428{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 int i;
1430
1431 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1432 return true;
1433
1434 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001435 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001436 return true;
1437
1438 return false;
1439}
1440
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301441/* Parse probe_events event into struct probe_point */
1442static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001443 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001444{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301445 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001446 char pr;
1447 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001448 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001449 int ret, i, argc;
1450 char **argv;
1451
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301452 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001454 if (!argv) {
1455 pr_debug("Failed to split arguments.\n");
1456 return -ENOMEM;
1457 }
1458 if (argc < 2) {
1459 semantic_error("Too few probe arguments.\n");
1460 ret = -ERANGE;
1461 goto out;
1462 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001463
1464 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001465 argv0_str = strdup(argv[0]);
1466 if (argv0_str == NULL) {
1467 ret = -ENOMEM;
1468 goto out;
1469 }
1470 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1471 fmt2_str = strtok_r(NULL, "/", &fmt);
1472 fmt3_str = strtok_r(NULL, " \t", &fmt);
1473 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1474 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001475 semantic_error("Failed to parse event name: %s\n", argv[0]);
1476 ret = -EINVAL;
1477 goto out;
1478 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001479 pr = fmt1_str[0];
1480 tev->group = strdup(fmt2_str);
1481 tev->event = strdup(fmt3_str);
1482 if (tev->group == NULL || tev->event == NULL) {
1483 ret = -ENOMEM;
1484 goto out;
1485 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001486 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001487
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001488 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001489
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001490 /* Scan module name(if there), function name and offset */
1491 p = strchr(argv[1], ':');
1492 if (p) {
1493 tp->module = strndup(argv[1], p - argv[1]);
1494 p++;
1495 } else
1496 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001497 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001498 if (fmt1_str[0] == '0') /* only the address started with 0x */
1499 tp->address = strtoul(fmt1_str, NULL, 0);
1500 else {
1501 /* Only the symbol-based probe has offset */
1502 tp->symbol = strdup(fmt1_str);
1503 if (tp->symbol == NULL) {
1504 ret = -ENOMEM;
1505 goto out;
1506 }
1507 fmt2_str = strtok_r(NULL, "", &fmt);
1508 if (fmt2_str == NULL)
1509 tp->offset = 0;
1510 else
1511 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001512 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001513
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001514 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301515 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001516 if (tev->args == NULL) {
1517 ret = -ENOMEM;
1518 goto out;
1519 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001521 p = strchr(argv[i + 2], '=');
1522 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001523 *p++ = '\0';
1524 else
1525 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001526 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001527 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001528 tev->args[i].value = strdup(p);
1529 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1530 ret = -ENOMEM;
1531 goto out;
1532 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001533 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 ret = 0;
1535out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001536 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001537 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001538 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001539}
1540
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001541/* Compose only probe arg */
1542int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1543{
1544 struct perf_probe_arg_field *field = pa->field;
1545 int ret;
1546 char *tmp = buf;
1547
Masami Hiramatsu48481932010-04-12 13:16:53 -04001548 if (pa->name && pa->var)
1549 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1550 else
1551 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001552 if (ret <= 0)
1553 goto error;
1554 tmp += ret;
1555 len -= ret;
1556
1557 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001558 if (field->name[0] == '[')
1559 ret = e_snprintf(tmp, len, "%s", field->name);
1560 else
1561 ret = e_snprintf(tmp, len, "%s%s",
1562 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001563 if (ret <= 0)
1564 goto error;
1565 tmp += ret;
1566 len -= ret;
1567 field = field->next;
1568 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001569
1570 if (pa->type) {
1571 ret = e_snprintf(tmp, len, ":%s", pa->type);
1572 if (ret <= 0)
1573 goto error;
1574 tmp += ret;
1575 len -= ret;
1576 }
1577
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001578 return tmp - buf;
1579error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001580 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001581 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001582}
1583
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001584/* Compose only probe point (not argument) */
1585static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001586{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001587 char *buf, *tmp;
1588 char offs[32] = "", line[32] = "", file[32] = "";
1589 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001590
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001591 buf = zalloc(MAX_CMDLEN);
1592 if (buf == NULL) {
1593 ret = -ENOMEM;
1594 goto error;
1595 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001596 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001597 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001598 if (ret <= 0)
1599 goto error;
1600 }
1601 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001602 ret = e_snprintf(line, 32, ":%d", pp->line);
1603 if (ret <= 0)
1604 goto error;
1605 }
1606 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001607 tmp = pp->file;
1608 len = strlen(tmp);
1609 if (len > 30) {
1610 tmp = strchr(pp->file + len - 30, '/');
1611 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1612 }
1613 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614 if (ret <= 0)
1615 goto error;
1616 }
1617
1618 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001619 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1620 offs, pp->retprobe ? "%return" : "", line,
1621 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001622 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001623 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001624 if (ret <= 0)
1625 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001626
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001627 return buf;
1628error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001629 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001630 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001631 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632}
1633
1634#if 0
1635char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1636{
1637 char *buf;
1638 int i, len, ret;
1639
1640 buf = synthesize_perf_probe_point(&pev->point);
1641 if (!buf)
1642 return NULL;
1643
1644 len = strlen(buf);
1645 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001646 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001647 pev->args[i].name);
1648 if (ret <= 0) {
1649 free(buf);
1650 return NULL;
1651 }
1652 len += ret;
1653 }
1654
1655 return buf;
1656}
1657#endif
1658
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301659static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001660 char **buf, size_t *buflen,
1661 int depth)
1662{
1663 int ret;
1664 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301665 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001666 buflen, depth + 1);
1667 if (depth < 0)
1668 goto out;
1669 }
1670
1671 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1672 if (ret < 0)
1673 depth = ret;
1674 else {
1675 *buf += ret;
1676 *buflen -= ret;
1677 }
1678out:
1679 return depth;
1680
1681}
1682
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301683static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001684 char *buf, size_t buflen)
1685{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301686 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001687 int ret, depth = 0;
1688 char *tmp = buf;
1689
1690 /* Argument name or separator */
1691 if (arg->name)
1692 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1693 else
1694 ret = e_snprintf(buf, buflen, " ");
1695 if (ret < 0)
1696 return ret;
1697 buf += ret;
1698 buflen -= ret;
1699
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001700 /* Special case: @XXX */
1701 if (arg->value[0] == '@' && arg->ref)
1702 ref = ref->next;
1703
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001704 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001705 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301706 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707 &buflen, 1);
1708 if (depth < 0)
1709 return depth;
1710 }
1711
1712 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001713 if (arg->value[0] == '@' && arg->ref)
1714 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1715 arg->ref->offset);
1716 else
1717 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718 if (ret < 0)
1719 return ret;
1720 buf += ret;
1721 buflen -= ret;
1722
1723 /* Closing */
1724 while (depth--) {
1725 ret = e_snprintf(buf, buflen, ")");
1726 if (ret < 0)
1727 return ret;
1728 buf += ret;
1729 buflen -= ret;
1730 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001731 /* Print argument type */
1732 if (arg->type) {
1733 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1734 if (ret <= 0)
1735 return ret;
1736 buf += ret;
1737 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738
1739 return buf - tmp;
1740}
1741
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301742char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001743{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301744 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001745 char *buf;
1746 int i, len, ret;
1747
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001748 buf = zalloc(MAX_CMDLEN);
1749 if (buf == NULL)
1750 return NULL;
1751
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001752 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1753 tev->group, tev->event);
1754 if (len <= 0)
1755 goto error;
1756
1757 /* Uprobes must have tp->address and tp->module */
1758 if (tev->uprobes && (!tp->address || !tp->module))
1759 goto error;
1760
1761 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301762 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001763 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1764 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301765 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001766 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301767 tp->module ?: "", tp->module ? ":" : "",
1768 tp->symbol, tp->offset);
1769
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001770 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001772 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773
1774 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301775 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001776 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001777 if (ret <= 0)
1778 goto error;
1779 len += ret;
1780 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001781
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001782 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001783error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001784 free(buf);
1785 return NULL;
1786}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001787
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001788static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1789 struct perf_probe_point *pp,
1790 bool is_kprobe)
1791{
1792 struct symbol *sym = NULL;
1793 struct map *map;
1794 u64 addr;
1795 int ret = -ENOENT;
1796
1797 if (!is_kprobe) {
1798 map = dso__new_map(tp->module);
1799 if (!map)
1800 goto out;
1801 addr = tp->address;
1802 sym = map__find_symbol(map, addr, NULL);
1803 } else {
1804 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1805 if (addr) {
1806 addr += tp->offset;
1807 sym = __find_kernel_function(addr, &map);
1808 }
1809 }
1810 if (!sym)
1811 goto out;
1812
1813 pp->retprobe = tp->retprobe;
1814 pp->offset = addr - map->unmap_ip(map, sym->start);
1815 pp->function = strdup(sym->name);
1816 ret = pp->function ? 0 : -ENOMEM;
1817
1818out:
1819 if (map && !is_kprobe) {
1820 dso__delete(map->dso);
1821 map__delete(map);
1822 }
1823
1824 return ret;
1825}
1826
1827static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1828 struct perf_probe_point *pp,
1829 bool is_kprobe)
1830{
1831 char buf[128];
1832 int ret;
1833
1834 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1835 if (!ret)
1836 return 0;
1837 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1838 if (!ret)
1839 return 0;
1840
1841 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1842
1843 if (tp->symbol) {
1844 pp->function = strdup(tp->symbol);
1845 pp->offset = tp->offset;
1846 } else if (!tp->module && !is_kprobe) {
1847 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1848 if (ret < 0)
1849 return ret;
1850 pp->function = strdup(buf);
1851 pp->offset = 0;
1852 }
1853 if (pp->function == NULL)
1854 return -ENOMEM;
1855
1856 pp->retprobe = tp->retprobe;
1857
1858 return 0;
1859}
1860
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301861static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301862 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001863{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001864 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001867 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001868 pev->event = strdup(tev->event);
1869 pev->group = strdup(tev->group);
1870 if (pev->event == NULL || pev->group == NULL)
1871 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001872
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001873 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001874 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875 if (ret < 0)
1876 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001877
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001878 /* Convert trace_arg to probe_arg */
1879 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001880 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1881 if (pev->args == NULL)
1882 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001883 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001885 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001886 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301887 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001888 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001889 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001891 if (pev->args[i].name == NULL && ret >= 0)
1892 ret = -ENOMEM;
1893 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001894
1895 if (ret < 0)
1896 clear_perf_probe_event(pev);
1897
1898 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001899}
1900
1901void clear_perf_probe_event(struct perf_probe_event *pev)
1902{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001903 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904 int i;
1905
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001906 free(pev->event);
1907 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001908 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001909
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001910 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001911 free(pev->args[i].name);
1912 free(pev->args[i].var);
1913 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001914 field = pev->args[i].field;
1915 while (field) {
1916 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001917 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001918 free(field);
1919 field = next;
1920 }
1921 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001922 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001923 memset(pev, 0, sizeof(*pev));
1924}
1925
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301926static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301928 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929 int i;
1930
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001931 free(tev->event);
1932 free(tev->group);
1933 free(tev->point.symbol);
1934 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001935 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001936 free(tev->args[i].name);
1937 free(tev->args[i].value);
1938 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001939 ref = tev->args[i].ref;
1940 while (ref) {
1941 next = ref->next;
1942 free(ref);
1943 ref = next;
1944 }
1945 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001946 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001947 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001948}
1949
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001950static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301951{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001952 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301953
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001954 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301955 const char *config;
1956
1957 if (!is_kprobe)
1958 config = "CONFIG_UPROBE_EVENTS";
1959 else
1960 config = "CONFIG_KPROBE_EVENTS";
1961
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001962 pr_warning("%cprobe_events file does not exist"
1963 " - please rebuild kernel with %s.\n",
1964 is_kprobe ? 'k' : 'u', config);
1965 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001966 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001967 else
1968 pr_warning("Failed to open %cprobe_events: %s\n",
1969 is_kprobe ? 'k' : 'u',
1970 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301971}
1972
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001973static void print_both_open_warning(int kerr, int uerr)
1974{
1975 /* Both kprobes and uprobes are disabled, warn it. */
1976 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001977 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001978 else if (kerr == -ENOENT && uerr == -ENOENT)
1979 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1980 "or/and CONFIG_UPROBE_EVENTS.\n");
1981 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001982 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001983 pr_warning("Failed to open kprobe events: %s.\n",
1984 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1985 pr_warning("Failed to open uprobe events: %s.\n",
1986 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1987 }
1988}
1989
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001990static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001991{
1992 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001993 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001994 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001995 int ret;
1996
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001997 __debugfs = tracefs_find_mountpoint();
1998 if (__debugfs == NULL) {
1999 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002000
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002001 __debugfs = debugfs_find_mountpoint();
2002 if (__debugfs == NULL)
2003 return -ENOTSUP;
2004 }
2005
2006 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2007 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002008 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002009 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002010 if (readwrite && !probe_event_dry_run)
2011 ret = open(buf, O_RDWR, O_APPEND);
2012 else
2013 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002014
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302015 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002016 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002017 }
2018 return ret;
2019}
2020
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302021static int open_kprobe_events(bool readwrite)
2022{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002023 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302024}
2025
2026static int open_uprobe_events(bool readwrite)
2027{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002028 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302029}
2030
2031/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302032static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002033{
2034 int ret, idx;
2035 FILE *fp;
2036 char buf[MAX_CMDLEN];
2037 char *p;
2038 struct strlist *sl;
2039
2040 sl = strlist__new(true, NULL);
2041
2042 fp = fdopen(dup(fd), "r");
2043 while (!feof(fp)) {
2044 p = fgets(buf, MAX_CMDLEN, fp);
2045 if (!p)
2046 break;
2047
2048 idx = strlen(p) - 1;
2049 if (p[idx] == '\n')
2050 p[idx] = '\0';
2051 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002052 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002053 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002054 strlist__delete(sl);
2055 return NULL;
2056 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002057 }
2058 fclose(fp);
2059
2060 return sl;
2061}
2062
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002063struct kprobe_blacklist_node {
2064 struct list_head list;
2065 unsigned long start;
2066 unsigned long end;
2067 char *symbol;
2068};
2069
2070static void kprobe_blacklist__delete(struct list_head *blacklist)
2071{
2072 struct kprobe_blacklist_node *node;
2073
2074 while (!list_empty(blacklist)) {
2075 node = list_first_entry(blacklist,
2076 struct kprobe_blacklist_node, list);
2077 list_del(&node->list);
2078 free(node->symbol);
2079 free(node);
2080 }
2081}
2082
2083static int kprobe_blacklist__load(struct list_head *blacklist)
2084{
2085 struct kprobe_blacklist_node *node;
2086 const char *__debugfs = debugfs_find_mountpoint();
2087 char buf[PATH_MAX], *p;
2088 FILE *fp;
2089 int ret;
2090
2091 if (__debugfs == NULL)
2092 return -ENOTSUP;
2093
2094 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2095 if (ret < 0)
2096 return ret;
2097
2098 fp = fopen(buf, "r");
2099 if (!fp)
2100 return -errno;
2101
2102 ret = 0;
2103 while (fgets(buf, PATH_MAX, fp)) {
2104 node = zalloc(sizeof(*node));
2105 if (!node) {
2106 ret = -ENOMEM;
2107 break;
2108 }
2109 INIT_LIST_HEAD(&node->list);
2110 list_add_tail(&node->list, blacklist);
2111 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2112 ret = -EINVAL;
2113 break;
2114 }
2115 p = strchr(buf, '\t');
2116 if (p) {
2117 p++;
2118 if (p[strlen(p) - 1] == '\n')
2119 p[strlen(p) - 1] = '\0';
2120 } else
2121 p = (char *)"unknown";
2122 node->symbol = strdup(p);
2123 if (!node->symbol) {
2124 ret = -ENOMEM;
2125 break;
2126 }
2127 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2128 node->start, node->end, node->symbol);
2129 ret++;
2130 }
2131 if (ret < 0)
2132 kprobe_blacklist__delete(blacklist);
2133 fclose(fp);
2134
2135 return ret;
2136}
2137
2138static struct kprobe_blacklist_node *
2139kprobe_blacklist__find_by_address(struct list_head *blacklist,
2140 unsigned long address)
2141{
2142 struct kprobe_blacklist_node *node;
2143
2144 list_for_each_entry(node, blacklist, list) {
2145 if (node->start <= address && address <= node->end)
2146 return node;
2147 }
2148
2149 return NULL;
2150}
2151
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002152/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002153static int show_perf_probe_event(struct perf_probe_event *pev,
2154 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002155{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002156 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002157 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002158 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002159
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002160 /* Synthesize only event probe point */
2161 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162 if (!place)
2163 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002164
2165 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002166 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002167 return ret;
2168
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002169 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002170 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002171 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002172
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002173 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002174 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002175 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002176 ret = synthesize_perf_probe_arg(&pev->args[i],
2177 buf, 128);
2178 if (ret < 0)
2179 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002180 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002181 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002182 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002183 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002184 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002185 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002186}
2187
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302188static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002189{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302190 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302191 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002192 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002193 struct strlist *rawlist;
2194 struct str_node *ent;
2195
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002196 memset(&tev, 0, sizeof(tev));
2197 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002198
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302199 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002200 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002201 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002202
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002203 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302204 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002205 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302206 ret = convert_to_perf_probe_event(&tev, &pev,
2207 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002208 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002209 ret = show_perf_probe_event(&pev,
2210 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002211 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002212 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302213 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002214 if (ret < 0)
2215 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002216 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002217 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002218
2219 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002220}
2221
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302222/* List up current perf-probe events */
2223int show_perf_probe_events(void)
2224{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002225 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302226
2227 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302228
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002229 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302230 if (ret < 0)
2231 return ret;
2232
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002233 kp_fd = open_kprobe_events(false);
2234 if (kp_fd >= 0) {
2235 ret = __show_perf_probe_events(kp_fd, true);
2236 close(kp_fd);
2237 if (ret < 0)
2238 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302239 }
2240
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002241 up_fd = open_uprobe_events(false);
2242 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002243 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002244 ret = kp_fd;
2245 goto out;
2246 }
2247
2248 if (up_fd >= 0) {
2249 ret = __show_perf_probe_events(up_fd, false);
2250 close(up_fd);
2251 }
2252out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002253 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302254 return ret;
2255}
2256
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002257/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302258static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002259{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002260 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002261 struct strlist *sl, *rawlist;
2262 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302263 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002264 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002265
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002266 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302267 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002268 if (!rawlist)
2269 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002270 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002271 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302272 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002273 if (ret < 0)
2274 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002275 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002276 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2277 tev.event);
2278 if (ret >= 0)
2279 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002280 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002281 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302282 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002283 if (ret < 0)
2284 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002285 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002286 strlist__delete(rawlist);
2287
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288 if (ret < 0) {
2289 strlist__delete(sl);
2290 return NULL;
2291 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002292 return sl;
2293}
2294
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302295static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002296{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002297 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302298 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002299 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002300
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002301 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302302 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002303 return -EINVAL;
2304 }
2305
Masami Hiramatsufa282442009-12-08 17:03:23 -05002306 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002307 if (!probe_event_dry_run) {
2308 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002309 if (ret <= 0) {
2310 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002311 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002312 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002313 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002314 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002315 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002316 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002317}
2318
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319static int get_new_event_name(char *buf, size_t len, const char *base,
2320 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002321{
2322 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002323
2324 /* Try no suffix */
2325 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002327 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002328 return ret;
2329 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002330 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002331 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002332
Masami Hiramatsud761b082009-12-15 10:32:25 -05002333 if (!allow_suffix) {
2334 pr_warning("Error: event \"%s\" already exists. "
2335 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002336 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002337 }
2338
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002339 /* Try to add suffix */
2340 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002341 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002342 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002343 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002344 return ret;
2345 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002346 if (!strlist__has_entry(namelist, buf))
2347 break;
2348 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002349 if (i == MAX_EVENT_INDEX) {
2350 pr_warning("Too many events are on the same function.\n");
2351 ret = -ERANGE;
2352 }
2353
2354 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002355}
2356
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002357/* Warn if the current kernel's uprobe implementation is old */
2358static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2359{
2360 int i;
2361 char *buf = synthesize_probe_trace_command(tev);
2362
2363 /* Old uprobe event doesn't support memory dereference */
2364 if (!tev->uprobes || tev->nargs == 0 || !buf)
2365 goto out;
2366
2367 for (i = 0; i < tev->nargs; i++)
2368 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2369 pr_warning("Please upgrade your kernel to at least "
2370 "3.14 to have access to feature %s\n",
2371 tev->args[i].value);
2372 break;
2373 }
2374out:
2375 free(buf);
2376}
2377
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302378static int __add_probe_trace_events(struct perf_probe_event *pev,
2379 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002380 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002381{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002382 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302383 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002384 char buf[64];
2385 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002386 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002387 LIST_HEAD(blacklist);
2388 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002389
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302390 if (pev->uprobes)
2391 fd = open_uprobe_events(true);
2392 else
2393 fd = open_kprobe_events(true);
2394
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002395 if (fd < 0) {
2396 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002398 }
2399
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002400 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302401 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 if (!namelist) {
2403 pr_debug("Failed to get current event list.\n");
2404 return -EIO;
2405 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002406 /* Get kprobe blacklist if exists */
2407 if (!pev->uprobes) {
2408 ret = kprobe_blacklist__load(&blacklist);
2409 if (ret < 0)
2410 pr_debug("No kprobe blacklist support, ignored\n");
2411 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002412
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002413 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002414 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002415 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002416 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002417 /* Ensure that the address is NOT blacklisted */
2418 node = kprobe_blacklist__find_by_address(&blacklist,
2419 tev->point.address);
2420 if (node) {
2421 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2422 continue;
2423 }
2424
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002425 if (pev->event)
2426 event = pev->event;
2427 else
2428 if (pev->point.function)
2429 event = pev->point.function;
2430 else
2431 event = tev->point.symbol;
2432 if (pev->group)
2433 group = pev->group;
2434 else
2435 group = PERFPROBE_GROUP;
2436
2437 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002438 ret = get_new_event_name(buf, 64, event,
2439 namelist, allow_suffix);
2440 if (ret < 0)
2441 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002442 event = buf;
2443
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002444 tev->event = strdup(event);
2445 tev->group = strdup(group);
2446 if (tev->event == NULL || tev->group == NULL) {
2447 ret = -ENOMEM;
2448 break;
2449 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302450 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002451 if (ret < 0)
2452 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002453 /* Add added event name to namelist */
2454 strlist__add(namelist, event);
2455
2456 /* Trick here - save current event/group */
2457 event = pev->event;
2458 group = pev->group;
2459 pev->event = tev->event;
2460 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002461 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002462 /* Trick here - restore current event/group */
2463 pev->event = (char *)event;
2464 pev->group = (char *)group;
2465
2466 /*
2467 * Probes after the first probe which comes from same
2468 * user input are always allowed to add suffix, because
2469 * there might be several addresses corresponding to
2470 * one code line.
2471 */
2472 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002473 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002474 if (ret == -EINVAL && pev->uprobes)
2475 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002476
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002477 /* Note that it is possible to skip all events because of blacklist */
2478 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002479 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002480 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2481 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002482 tev->event);
2483 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002484
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002485 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002486 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002487 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002488 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002489}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002490
Namhyung Kim564c62a2015-01-14 20:18:07 +09002491static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002492{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002493 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002494 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002495
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002496 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002497 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002498 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002499
2500 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002501}
2502
2503#define strdup_or_goto(str, label) \
2504 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2505
2506/*
2507 * Find probe function addresses from map.
2508 * Return an error or the number of found probe_trace_event
2509 */
2510static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2511 struct probe_trace_event **tevs,
2512 int max_tevs, const char *target)
2513{
2514 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002515 struct ref_reloc_sym *reloc_sym = NULL;
2516 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002517 struct probe_trace_event *tev;
2518 struct perf_probe_point *pp = &pev->point;
2519 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002520 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002521 int ret, i;
2522
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002523 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002524 if (!map) {
2525 ret = -EINVAL;
2526 goto out;
2527 }
2528
2529 /*
2530 * Load matched symbols: Since the different local symbols may have
2531 * same name but different addresses, this lists all the symbols.
2532 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002533 num_matched_functions = find_probe_functions(map, pp->function);
2534 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002535 pr_err("Failed to find symbol %s in %s\n", pp->function,
2536 target ? : "kernel");
2537 ret = -ENOENT;
2538 goto out;
2539 } else if (num_matched_functions > max_tevs) {
2540 pr_err("Too many functions matched in %s\n",
2541 target ? : "kernel");
2542 ret = -E2BIG;
2543 goto out;
2544 }
2545
Namhyung Kim25dd9172015-01-14 20:18:08 +09002546 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002547 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002548 if (!reloc_sym) {
2549 pr_warning("Relocated base symbol is not found!\n");
2550 ret = -EINVAL;
2551 goto out;
2552 }
2553 }
2554
2555 /* Setup result trace-probe-events */
2556 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2557 if (!*tevs) {
2558 ret = -ENOMEM;
2559 goto out;
2560 }
2561
2562 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002563
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002564 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002565 tev = (*tevs) + ret;
2566 tp = &tev->point;
2567 if (ret == num_matched_functions) {
2568 pr_warning("Too many symbols are listed. Skip it.\n");
2569 break;
2570 }
2571 ret++;
2572
2573 if (pp->offset > sym->end - sym->start) {
2574 pr_warning("Offset %ld is bigger than the size of %s\n",
2575 pp->offset, sym->name);
2576 ret = -ENOENT;
2577 goto err_out;
2578 }
2579 /* Add one probe point */
2580 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2581 if (reloc_sym) {
2582 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2583 tp->offset = tp->address - reloc_sym->addr;
2584 } else {
2585 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2586 tp->offset = pp->offset;
2587 }
2588 tp->retprobe = pp->retprobe;
2589 if (target)
2590 tev->point.module = strdup_or_goto(target, nomem_out);
2591 tev->uprobes = pev->uprobes;
2592 tev->nargs = pev->nargs;
2593 if (tev->nargs) {
2594 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2595 tev->nargs);
2596 if (tev->args == NULL)
2597 goto nomem_out;
2598 }
2599 for (i = 0; i < tev->nargs; i++) {
2600 if (pev->args[i].name)
2601 tev->args[i].name =
2602 strdup_or_goto(pev->args[i].name,
2603 nomem_out);
2604
2605 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2606 nomem_out);
2607 if (pev->args[i].type)
2608 tev->args[i].type =
2609 strdup_or_goto(pev->args[i].type,
2610 nomem_out);
2611 }
2612 }
2613
2614out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002615 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002616 return ret;
2617
2618nomem_out:
2619 ret = -ENOMEM;
2620err_out:
2621 clear_probe_trace_events(*tevs, num_matched_functions);
2622 zfree(tevs);
2623 goto out;
2624}
2625
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302626static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2627 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302628 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002629{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002630 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002631
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002632 if (pev->uprobes && !pev->group) {
2633 /* Replace group name if not given */
2634 ret = convert_exec_to_group(target, &pev->group);
2635 if (ret != 0) {
2636 pr_warning("Failed to make a group name.\n");
2637 return ret;
2638 }
2639 }
2640
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002641 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302642 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002643 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002644 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002645
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002646 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002647}
2648
2649struct __event_package {
2650 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302651 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002652 int ntevs;
2653};
2654
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002655int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302656 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002657{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002658 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002659 struct __event_package *pkgs;
2660
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302661 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002662 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302663
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002664 if (pkgs == NULL)
2665 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002666
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002667 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002668 if (ret < 0) {
2669 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002670 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002671 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002672
2673 /* Loop 1: convert all events */
2674 for (i = 0; i < npevs; i++) {
2675 pkgs[i].pev = &pevs[i];
2676 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302677 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002678 &pkgs[i].tevs,
2679 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302680 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002681 if (ret < 0)
2682 goto end;
2683 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002684 }
2685
2686 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002687 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302688 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002690 if (ret < 0)
2691 break;
2692 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002693end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002694 /* Loop 3: cleanup and free trace events */
2695 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302697 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002698 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002699 }
2700 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002701 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002702
2703 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002704}
2705
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302706static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002707{
2708 char *p;
2709 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002710 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002711
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302712 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002713 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2714 if (ret < 0)
2715 goto error;
2716
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002717 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002718 if (!p) {
2719 pr_debug("Internal error: %s should have ':' but not.\n",
2720 ent->s);
2721 ret = -ENOTSUP;
2722 goto error;
2723 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002724 *p = '/';
2725
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002726 pr_debug("Writing event: %s\n", buf);
2727 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002728 if (ret < 0) {
2729 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002730 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002731 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002732
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002733 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002734 return 0;
2735error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002736 pr_warning("Failed to delete event: %s\n",
2737 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002738 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002739}
2740
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302741static int del_trace_probe_event(int fd, const char *buf,
2742 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002743{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002744 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302745 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002746
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002747 if (strpbrk(buf, "*?")) { /* Glob-exp */
2748 strlist__for_each_safe(ent, n, namelist)
2749 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302750 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002751 if (ret < 0)
2752 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002753 strlist__remove(namelist, ent);
2754 }
2755 } else {
2756 ent = strlist__find(namelist, buf);
2757 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302758 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002759 if (ret >= 0)
2760 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002761 }
2762 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002763
2764 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002765}
2766
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002767int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002768{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302769 int ret = -1, ufd = -1, kfd = -1;
2770 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002771 const char *group, *event;
2772 char *p, *str;
2773 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302774 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002775
Masami Hiramatsufa282442009-12-08 17:03:23 -05002776 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302777 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002778 if (kfd >= 0)
2779 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302780
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302781 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002782 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302783 unamelist = get_probe_trace_event_names(ufd, true);
2784
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002785 if (kfd < 0 && ufd < 0) {
2786 print_both_open_warning(kfd, ufd);
2787 goto error;
2788 }
2789
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302790 if (namelist == NULL && unamelist == NULL)
2791 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002792
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002793 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002794 str = strdup(ent->s);
2795 if (str == NULL) {
2796 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302797 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002798 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002799 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002800 p = strchr(str, ':');
2801 if (p) {
2802 group = str;
2803 *p = '\0';
2804 event = p + 1;
2805 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002806 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002807 event = str;
2808 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302809
2810 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2811 if (ret < 0) {
2812 pr_err("Failed to copy event.");
2813 free(str);
2814 goto error;
2815 }
2816
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002817 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302818
2819 if (namelist)
2820 ret = del_trace_probe_event(kfd, buf, namelist);
2821
2822 if (unamelist && ret != 0)
2823 ret = del_trace_probe_event(ufd, buf, unamelist);
2824
2825 if (ret != 0)
2826 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2827
Masami Hiramatsufa282442009-12-08 17:03:23 -05002828 free(str);
2829 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302830
2831error:
2832 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302833 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302834 close(kfd);
2835 }
2836
2837 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302838 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302839 close(ufd);
2840 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002841
2842 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002843}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302844
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002845/* TODO: don't use a global variable for filter ... */
2846static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002847
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002848/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002849 * If a symbol corresponds to a function with global binding and
2850 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002851 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002852static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002853 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002854{
Namhyung Kime578da32015-03-06 16:31:29 +09002855 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002856 return 0;
2857 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002858}
2859
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002860int show_available_funcs(const char *target, struct strfilter *_filter,
2861 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002862{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002863 struct map *map;
2864 int ret;
2865
2866 ret = init_symbol_maps(user);
2867 if (ret < 0)
2868 return ret;
2869
2870 /* Get a symbol map */
2871 if (user)
2872 map = dso__new_map(target);
2873 else
2874 map = kernel_get_module_map(target);
2875 if (!map) {
2876 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002877 return -EINVAL;
2878 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002879
2880 /* Load symbols with given filter */
2881 available_func_filter = _filter;
2882 if (map__load(map, filter_available_functions)) {
2883 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2884 goto end;
2885 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002886 if (!dso__sorted_by_name(map->dso, map->type))
2887 dso__sort_by_name(map->dso, map->type);
2888
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002889 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302890 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002891 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2892end:
2893 if (user) {
2894 dso__delete(map->dso);
2895 map__delete(map);
2896 }
2897 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302898
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002899 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302900}
2901