blob: 4265f2e3bd14ba46441c307661a2d05b405cbacf [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 }
Wang Nanf6c15622015-04-08 02:14:34 +0000325 pr_debug("Symbol %s address found : %" PRIx64 "\n",
326 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900327
328 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
329 result);
330 if (ret <= 0)
331 ret = (!ret) ? -ENOENT : ret;
332 else {
333 result->offset += pp->offset;
334 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800335 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900336 ret = 0;
337 }
338
339out:
340 put_target_map(map, uprobes);
341 return ret;
342
343}
344
345static int get_alternative_probe_event(struct debuginfo *dinfo,
346 struct perf_probe_event *pev,
347 struct perf_probe_point *tmp,
348 const char *target)
349{
350 int ret;
351
352 memcpy(tmp, &pev->point, sizeof(*tmp));
353 memset(&pev->point, 0, sizeof(pev->point));
354 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
355 target, pev->uprobes);
356 if (ret < 0)
357 memcpy(&pev->point, tmp, sizeof(*tmp));
358
359 return ret;
360}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000361
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900362static int get_alternative_line_range(struct debuginfo *dinfo,
363 struct line_range *lr,
364 const char *target, bool user)
365{
David Ahern6d4a4892015-03-11 10:36:20 -0400366 struct perf_probe_point pp = { .function = lr->function,
367 .file = lr->file,
368 .line = lr->start };
369 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900370 int ret, len = 0;
371
David Ahern6d4a4892015-03-11 10:36:20 -0400372 memset(&result, 0, sizeof(result));
373
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900374 if (lr->end != INT_MAX)
375 len = lr->end - lr->start;
376 ret = find_alternative_probe_point(dinfo, &pp, &result,
377 target, user);
378 if (!ret) {
379 lr->function = result.function;
380 lr->file = result.file;
381 lr->start = result.line;
382 if (lr->end != INT_MAX)
383 lr->end = lr->start + len;
384 clear_perf_probe_point(&pp);
385 }
386 return ret;
387}
388
Masami Hiramatsuff741782011-06-27 16:27:39 +0900389/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000390static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900391{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000392 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000393 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900394
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000395 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900396 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900397 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000398 if (!silent)
399 pr_err("Failed to find path of %s module.\n",
400 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900401 return NULL;
402 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900403 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000404 ret = debuginfo__new(path);
405 if (!ret && !silent) {
406 pr_warning("The %s file has no debug information.\n", path);
407 if (!module || !strtailcmp(path, ".ko"))
408 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
409 else
410 pr_warning("Rebuild with -g, ");
411 pr_warning("or install an appropriate debuginfo package.\n");
412 }
413 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400414}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300415
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000416
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000417static int get_text_start_address(const char *exec, unsigned long *address)
418{
419 Elf *elf;
420 GElf_Ehdr ehdr;
421 GElf_Shdr shdr;
422 int fd, ret = -ENOENT;
423
424 fd = open(exec, O_RDONLY);
425 if (fd < 0)
426 return -errno;
427
428 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
429 if (elf == NULL)
430 return -EINVAL;
431
432 if (gelf_getehdr(elf, &ehdr) == NULL)
433 goto out;
434
435 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
436 goto out;
437
438 *address = shdr.sh_addr - shdr.sh_offset;
439 ret = 0;
440out:
441 elf_end(elf);
442 return ret;
443}
444
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000445/*
446 * Convert trace point to probe point with debuginfo
447 */
448static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
449 struct perf_probe_point *pp,
450 bool is_kprobe)
451{
452 struct debuginfo *dinfo = NULL;
453 unsigned long stext = 0;
454 u64 addr = tp->address;
455 int ret = -ENOENT;
456
457 /* convert the address to dwarf address */
458 if (!is_kprobe) {
459 if (!addr) {
460 ret = -EINVAL;
461 goto error;
462 }
463 ret = get_text_start_address(tp->module, &stext);
464 if (ret < 0)
465 goto error;
466 addr += stext;
467 } else {
468 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
469 if (addr == 0)
470 goto error;
471 addr += tp->offset;
472 }
473
474 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
475 tp->module ? : "kernel");
476
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000477 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000478 if (dinfo) {
479 ret = debuginfo__find_probe_point(dinfo,
480 (unsigned long)addr, pp);
481 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000482 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000483 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000484
485 if (ret > 0) {
486 pp->retprobe = tp->retprobe;
487 return 0;
488 }
489error:
490 pr_debug("Failed to find corresponding probes from debuginfo.\n");
491 return ret ? : -ENOENT;
492}
493
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000494static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
495 int ntevs, const char *exec)
496{
497 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000498 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000499
500 if (!exec)
501 return 0;
502
503 ret = get_text_start_address(exec, &stext);
504 if (ret < 0)
505 return ret;
506
507 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000508 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000509 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000510 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000511 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000512 ret = -ENOMEM;
513 break;
514 }
515 tevs[i].uprobes = true;
516 }
517
518 return ret;
519}
520
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900521static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
522 int ntevs, const char *module)
523{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900524 int i, ret = 0;
525 char *tmp;
526
527 if (!module)
528 return 0;
529
530 tmp = strrchr(module, '/');
531 if (tmp) {
532 /* This is a module path -- get the module name */
533 module = strdup(tmp + 1);
534 if (!module)
535 return -ENOMEM;
536 tmp = strchr(module, '.');
537 if (tmp)
538 *tmp = '\0';
539 tmp = (char *)module; /* For free() */
540 }
541
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900542 for (i = 0; i < ntevs; i++) {
543 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900544 if (!tevs[i].point.module) {
545 ret = -ENOMEM;
546 break;
547 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900548 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900549
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300550 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900551 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900552}
553
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000554/* Post processing the probe events */
555static int post_process_probe_trace_events(struct probe_trace_event *tevs,
556 int ntevs, const char *module,
557 bool uprobe)
558{
559 struct ref_reloc_sym *reloc_sym;
560 char *tmp;
561 int i;
562
563 if (uprobe)
564 return add_exec_to_probe_trace_events(tevs, ntevs, module);
565
566 /* Note that currently ref_reloc_sym based probe is not for drivers */
567 if (module)
568 return add_module_to_probe_trace_events(tevs, ntevs, module);
569
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000570 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000571 if (!reloc_sym) {
572 pr_warning("Relocated base symbol is not found!\n");
573 return -EINVAL;
574 }
575
576 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900577 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000578 tmp = strdup(reloc_sym->name);
579 if (!tmp)
580 return -ENOMEM;
581 free(tevs[i].point.symbol);
582 tevs[i].point.symbol = tmp;
583 tevs[i].point.offset = tevs[i].point.address -
584 reloc_sym->unrelocated_addr;
585 }
586 }
587 return 0;
588}
589
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300590/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530591static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900592 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530593 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300594{
595 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900596 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530597 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900598 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300599
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000600 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530601
Masami Hiramatsuff741782011-06-27 16:27:39 +0900602 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000603 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900604 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900605 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300606 return 0;
607 }
608
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000609 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900610 /* Searching trace events corresponding to a probe event */
611 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
612
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900613 if (ntevs == 0) { /* Not found, retry with an alternative */
614 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
615 if (!ret) {
616 ntevs = debuginfo__find_trace_events(dinfo, pev,
617 tevs, max_tevs);
618 /*
619 * Write back to the original probe_event for
620 * setting appropriate (user given) event name
621 */
622 clear_perf_probe_point(&pev->point);
623 memcpy(&pev->point, &tmp, sizeof(tmp));
624 }
625 }
626
Masami Hiramatsuff741782011-06-27 16:27:39 +0900627 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300628
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000630 pr_debug("Found %d probe_trace_events.\n", ntevs);
631 ret = post_process_probe_trace_events(*tevs, ntevs,
632 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000633 if (ret < 0) {
634 clear_probe_trace_events(*tevs, ntevs);
635 zfree(tevs);
636 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900637 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400638 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300639
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400640 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900641 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400642 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900643 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400644 }
645 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400646 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
647 if (ntevs == -EBADF) {
648 pr_warning("Warning: No dwarf info found in the vmlinux - "
649 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
650 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900651 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400652 return 0;
653 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300654 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400655 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300656}
657
658#define LINEBUF_SIZE 256
659#define NR_ADDITIONAL_LINES 2
660
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100661static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300662{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000663 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100664 const char *color = show_num ? "" : PERF_COLOR_BLUE;
665 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300666
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100667 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300668 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
669 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100670 if (skip)
671 continue;
672 if (!prefix) {
673 prefix = show_num ? "%7d " : " ";
674 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300675 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100676 color_fprintf(stdout, color, "%s", buf);
677
678 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400679
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100680 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300681error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100682 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000683 pr_warning("File read error: %s\n",
684 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100685 return -1;
686 }
687 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300688}
689
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100690static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
691{
692 int rv = __show_one_line(fp, l, skip, show_num);
693 if (rv == 0) {
694 pr_warning("Source file is shorter than expected.\n");
695 rv = -1;
696 }
697 return rv;
698}
699
700#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
701#define show_one_line(f,l) _show_one_line(f,l,false,false)
702#define skip_one_line(f,l) _show_one_line(f,l,true,false)
703#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
704
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300705/*
706 * Show line-range always requires debuginfo to find source file and
707 * line number.
708 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900709static int __show_line_range(struct line_range *lr, const char *module,
710 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300711{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400712 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000713 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900714 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300715 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900716 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900717 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000718 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719
720 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000721 dinfo = open_debuginfo(module, false);
722 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900723 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400724
Masami Hiramatsuff741782011-06-27 16:27:39 +0900725 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900726 if (!ret) { /* Not found, retry with an alternative */
727 ret = get_alternative_line_range(dinfo, lr, module, user);
728 if (!ret)
729 ret = debuginfo__find_line_range(dinfo, lr);
730 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900731 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000732 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400733 pr_warning("Specified source line is not found.\n");
734 return -ENOENT;
735 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000736 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400737 return ret;
738 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300739
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900740 /* Convert source file path */
741 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900742 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800743
744 /* Free old path when new path is assigned */
745 if (tmp != lr->path)
746 free(tmp);
747
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900748 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000749 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900750 return ret;
751 }
752
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300753 setup_pager();
754
755 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900756 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300757 lr->start - lr->offset);
758 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100759 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300760
761 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400762 if (fp == NULL) {
763 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000764 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400765 return -errno;
766 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300767 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100768 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100769 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100770 if (ret < 0)
771 goto end;
772 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300773
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000774 intlist__for_each(ln, lr->line_list) {
775 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100776 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100777 if (ret < 0)
778 goto end;
779 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100780 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400781 if (ret < 0)
782 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300783 }
784
785 if (lr->end == INT_MAX)
786 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100787 while (l <= lr->end) {
788 ret = show_one_line_or_eof(fp, l++ - lr->offset);
789 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100790 break;
791 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400792end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300793 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400794 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300795}
796
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000797int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000798{
799 int ret;
800
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000801 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000802 if (ret < 0)
803 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900804 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000805 exit_symbol_maps();
806
807 return ret;
808}
809
Masami Hiramatsuff741782011-06-27 16:27:39 +0900810static int show_available_vars_at(struct debuginfo *dinfo,
811 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900812 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900813 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900814{
815 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900816 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900817 struct str_node *node;
818 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900819 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900820 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900821
822 buf = synthesize_perf_probe_point(&pev->point);
823 if (!buf)
824 return -EINVAL;
825 pr_debug("Searching variables at %s\n", buf);
826
Masami Hiramatsuff741782011-06-27 16:27:39 +0900827 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
828 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900829 if (!ret) { /* Not found, retry with an alternative */
830 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
831 if (!ret) {
832 ret = debuginfo__find_available_vars_at(dinfo, pev,
833 &vls, max_vls, externs);
834 /* Release the old probe_point */
835 clear_perf_probe_point(&tmp);
836 }
837 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900838 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000839 if (ret == 0 || ret == -ENOENT) {
840 pr_err("Failed to find the address of %s\n", buf);
841 ret = -ENOENT;
842 } else
843 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900844 goto end;
845 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000846
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900847 /* Some variables are found */
848 fprintf(stdout, "Available variables at %s\n", buf);
849 for (i = 0; i < ret; i++) {
850 vl = &vls[i];
851 /*
852 * A probe point might be converted to
853 * several trace points.
854 */
855 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
856 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300857 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900858 nvars = 0;
859 if (vl->vars) {
860 strlist__for_each(node, vl->vars) {
861 var = strchr(node->s, '\t') + 1;
862 if (strfilter__compare(_filter, var)) {
863 fprintf(stdout, "\t\t%s\n", node->s);
864 nvars++;
865 }
866 }
867 strlist__delete(vl->vars);
868 }
869 if (nvars == 0)
870 fprintf(stdout, "\t\t(No matched variables)\n");
871 }
872 free(vls);
873end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900874 free(buf);
875 return ret;
876}
877
878/* Show available variables on given probe point */
879int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900880 int max_vls, const char *module,
881 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900882{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900883 int i, ret = 0;
884 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900885
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000886 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900887 if (ret < 0)
888 return ret;
889
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000890 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900891 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000892 ret = -ENOENT;
893 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900894 }
895
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900896 setup_pager();
897
Masami Hiramatsuff741782011-06-27 16:27:39 +0900898 for (i = 0; i < npevs && ret >= 0; i++)
899 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900900 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900901
902 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000903out:
904 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900905 return ret;
906}
907
Ingo Molnar89fe8082013-09-30 12:07:11 +0200908#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300909
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000910static int
911find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
912 struct perf_probe_point *pp __maybe_unused,
913 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300914{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000915 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300916}
917
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530918static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300919 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300920 int max_tevs __maybe_unused,
921 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300922{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400923 if (perf_probe_event_need_dwarf(pev)) {
924 pr_warning("Debuginfo-analysis is not supported.\n");
925 return -ENOSYS;
926 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530927
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300928 return 0;
929}
930
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300931int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000932 const char *module __maybe_unused,
933 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300934{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400935 pr_warning("Debuginfo-analysis is not supported.\n");
936 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300937}
938
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300939int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
940 int npevs __maybe_unused, int max_vls __maybe_unused,
941 const char *module __maybe_unused,
942 struct strfilter *filter __maybe_unused,
943 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944{
945 pr_warning("Debuginfo-analysis is not supported.\n");
946 return -ENOSYS;
947}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400948#endif
949
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000950void line_range__clear(struct line_range *lr)
951{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000952 free(lr->function);
953 free(lr->file);
954 free(lr->path);
955 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000956 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000957 memset(lr, 0, sizeof(*lr));
958}
959
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000960int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000961{
962 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000963 lr->line_list = intlist__new(NULL);
964 if (!lr->line_list)
965 return -ENOMEM;
966 else
967 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000968}
969
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100970static int parse_line_num(char **ptr, int *val, const char *what)
971{
972 const char *start = *ptr;
973
974 errno = 0;
975 *val = strtol(*ptr, ptr, 0);
976 if (errno || *ptr == start) {
977 semantic_error("'%s' is not a valid number.\n", what);
978 return -EINVAL;
979 }
980 return 0;
981}
982
Masami Hiramatsu573709f2015-05-06 21:46:47 +0900983/* Check the name is good for event, group or function */
984static bool is_c_func_name(const char *name)
985{
986 if (!isalpha(*name) && *name != '_')
987 return false;
988 while (*++name != '\0') {
989 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
990 return false;
991 }
992 return true;
993}
994
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100995/*
996 * Stuff 'lr' according to the line range described by 'arg'.
997 * The line range syntax is described by:
998 *
999 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001000 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001001 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001002int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001003{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001004 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001005 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001006
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001007 if (!name)
1008 return -ENOMEM;
1009
1010 lr->start = 0;
1011 lr->end = INT_MAX;
1012
1013 range = strchr(name, ':');
1014 if (range) {
1015 *range++ = '\0';
1016
1017 err = parse_line_num(&range, &lr->start, "start line");
1018 if (err)
1019 goto err;
1020
1021 if (*range == '+' || *range == '-') {
1022 const char c = *range++;
1023
1024 err = parse_line_num(&range, &lr->end, "end line");
1025 if (err)
1026 goto err;
1027
1028 if (c == '+') {
1029 lr->end += lr->start;
1030 /*
1031 * Adjust the number of lines here.
1032 * If the number of lines == 1, the
1033 * the end of line should be equal to
1034 * the start of line.
1035 */
1036 lr->end--;
1037 }
1038 }
1039
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001040 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001041
1042 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001043 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001044 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001045 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001046 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001047 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001048 if (*range != '\0') {
1049 semantic_error("Tailing with invalid str '%s'.\n", range);
1050 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001052 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001053
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001054 file = strchr(name, '@');
1055 if (file) {
1056 *file = '\0';
1057 lr->file = strdup(++file);
1058 if (lr->file == NULL) {
1059 err = -ENOMEM;
1060 goto err;
1061 }
1062 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001063 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001064 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001065 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001066 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001067 else { /* Invalid name */
1068 semantic_error("'%s' is not a valid function name.\n", name);
1069 err = -EINVAL;
1070 goto err;
1071 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001072
1073 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001074err:
1075 free(name);
1076 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001077}
1078
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001079/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001080static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001081{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001083 char *ptr, *tmp;
1084 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301085 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001086 /*
1087 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001088 * perf probe [EVENT=]SRC[:LN|;PTN]
1089 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001090 *
1091 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001092 */
1093
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001094 ptr = strpbrk(arg, ";=@+%");
1095 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001096 *ptr = '\0';
1097 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001098 if (strchr(arg, ':')) {
1099 semantic_error("Group name is not supported yet.\n");
1100 return -ENOTSUP;
1101 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001102 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001103 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001104 "follow C symbol-naming rule.\n", arg);
1105 return -EINVAL;
1106 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001107 pev->event = strdup(arg);
1108 if (pev->event == NULL)
1109 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001110 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001111 arg = tmp;
1112 }
1113
Naveen N. Rao3099c022015-04-28 17:35:34 +05301114 /*
1115 * Check arg is function or file name and copy it.
1116 *
1117 * We consider arg to be a file spec if and only if it satisfies
1118 * all of the below criteria::
1119 * - it does not include any of "+@%",
1120 * - it includes one of ":;", and
1121 * - it has a period '.' in the name.
1122 *
1123 * Otherwise, we consider arg to be a function specification.
1124 */
1125 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1126 /* This is a file spec if it includes a '.' before ; or : */
1127 if (memchr(arg, '.', ptr - arg))
1128 file_spec = true;
1129 }
1130
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001131 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001132 if (ptr) {
1133 nc = *ptr;
1134 *ptr++ = '\0';
1135 }
1136
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001137 tmp = strdup(arg);
1138 if (tmp == NULL)
1139 return -ENOMEM;
1140
Naveen N. Rao3099c022015-04-28 17:35:34 +05301141 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001142 pp->file = tmp;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301143 else
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001144 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001145
1146 /* Parse other options */
1147 while (ptr) {
1148 arg = ptr;
1149 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001150 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001151 pp->lazy_line = strdup(arg);
1152 if (pp->lazy_line == NULL)
1153 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001154 break;
1155 }
1156 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157 if (ptr) {
1158 nc = *ptr;
1159 *ptr++ = '\0';
1160 }
1161 switch (c) {
1162 case ':': /* Line number */
1163 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001164 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001165 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001166 " in line number.\n");
1167 return -EINVAL;
1168 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001169 break;
1170 case '+': /* Byte offset from a symbol */
1171 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001173 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174 " in offset.\n");
1175 return -EINVAL;
1176 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001177 break;
1178 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001179 if (pp->file) {
1180 semantic_error("SRC@SRC is not allowed.\n");
1181 return -EINVAL;
1182 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001183 pp->file = strdup(arg);
1184 if (pp->file == NULL)
1185 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001186 break;
1187 case '%': /* Probe places */
1188 if (strcmp(arg, "return") == 0) {
1189 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 } else { /* Others not supported yet */
1191 semantic_error("%%%s is not supported.\n", arg);
1192 return -ENOTSUP;
1193 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001194 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 default: /* Buggy case */
1196 pr_err("This program has a bug at %s:%d.\n",
1197 __FILE__, __LINE__);
1198 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001199 break;
1200 }
1201 }
1202
1203 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001205 semantic_error("Lazy pattern can't be used with"
1206 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001207 return -EINVAL;
1208 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001209
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001210 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001211 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212 return -EINVAL;
1213 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001214
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001215 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001216 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 return -EINVAL;
1218 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001219
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001221 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001222 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 return -EINVAL;
1224 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001225
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001227 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228 return -EINVAL;
1229 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001230
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001231 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001232 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001233 return -EINVAL;
1234 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001235
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001236 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001237 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001238 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 return -EINVAL;
1240 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001241
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001242 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001243 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1244 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001246}
1247
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001248/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001249static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001250{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001251 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001252 struct perf_probe_arg_field **fieldp;
1253
1254 pr_debug("parsing arg: %s into ", str);
1255
Masami Hiramatsu48481932010-04-12 13:16:53 -04001256 tmp = strchr(str, '=');
1257 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001258 arg->name = strndup(str, tmp - str);
1259 if (arg->name == NULL)
1260 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001261 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001262 str = tmp + 1;
1263 }
1264
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001265 tmp = strchr(str, ':');
1266 if (tmp) { /* Type setting */
1267 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001268 arg->type = strdup(tmp + 1);
1269 if (arg->type == NULL)
1270 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001271 pr_debug("type:%s ", arg->type);
1272 }
1273
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001274 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001275 if (!is_c_varname(str) || !tmp) {
1276 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001277 arg->var = strdup(str);
1278 if (arg->var == NULL)
1279 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001280 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001281 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001282 }
1283
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001284 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001285 arg->var = strndup(str, tmp - str);
1286 if (arg->var == NULL)
1287 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001288 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001289 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001290 fieldp = &arg->field;
1291
1292 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001293 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1294 if (*fieldp == NULL)
1295 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001296 if (*tmp == '[') { /* Array */
1297 str = tmp;
1298 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001299 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001300 if (*tmp != ']' || tmp == str + 1) {
1301 semantic_error("Array index must be a"
1302 " number.\n");
1303 return -EINVAL;
1304 }
1305 tmp++;
1306 if (*tmp == '\0')
1307 tmp = NULL;
1308 } else { /* Structure */
1309 if (*tmp == '.') {
1310 str = tmp + 1;
1311 (*fieldp)->ref = false;
1312 } else if (tmp[1] == '>') {
1313 str = tmp + 2;
1314 (*fieldp)->ref = true;
1315 } else {
1316 semantic_error("Argument parse error: %s\n",
1317 str);
1318 return -EINVAL;
1319 }
1320 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001321 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001322 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001323 (*fieldp)->name = strndup(str, tmp - str);
1324 if ((*fieldp)->name == NULL)
1325 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001326 if (*str != '[')
1327 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001328 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1329 fieldp = &(*fieldp)->next;
1330 }
1331 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001332 (*fieldp)->name = strdup(str);
1333 if ((*fieldp)->name == NULL)
1334 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001335 if (*str != '[')
1336 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001337 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001338
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001339 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001340 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001341 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001342 if (arg->name == NULL)
1343 return -ENOMEM;
1344 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001345 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001346}
1347
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001348/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001349int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001350{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001351 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001352 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001353
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001354 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 if (!argv) {
1356 pr_debug("Failed to split arguments.\n");
1357 return -ENOMEM;
1358 }
1359 if (argc - 1 > MAX_PROBE_ARGS) {
1360 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1361 ret = -ERANGE;
1362 goto out;
1363 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001364 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365 ret = parse_perf_probe_point(argv[0], pev);
1366 if (ret < 0)
1367 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001368
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001369 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001370 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001371 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1372 if (pev->args == NULL) {
1373 ret = -ENOMEM;
1374 goto out;
1375 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1377 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1378 if (ret >= 0 &&
1379 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001380 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 " kretprobe.\n");
1382 ret = -EINVAL;
1383 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001384 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001385out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001386 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387
1388 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001389}
1390
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391/* Return true if this perf_probe_event requires debuginfo */
1392bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001393{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001394 int i;
1395
1396 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1397 return true;
1398
1399 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001400 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001401 return true;
1402
1403 return false;
1404}
1405
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301406/* Parse probe_events event into struct probe_point */
1407static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001408 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001409{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301410 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001411 char pr;
1412 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001413 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001414 int ret, i, argc;
1415 char **argv;
1416
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301417 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001418 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001419 if (!argv) {
1420 pr_debug("Failed to split arguments.\n");
1421 return -ENOMEM;
1422 }
1423 if (argc < 2) {
1424 semantic_error("Too few probe arguments.\n");
1425 ret = -ERANGE;
1426 goto out;
1427 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001428
1429 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001430 argv0_str = strdup(argv[0]);
1431 if (argv0_str == NULL) {
1432 ret = -ENOMEM;
1433 goto out;
1434 }
1435 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1436 fmt2_str = strtok_r(NULL, "/", &fmt);
1437 fmt3_str = strtok_r(NULL, " \t", &fmt);
1438 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1439 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440 semantic_error("Failed to parse event name: %s\n", argv[0]);
1441 ret = -EINVAL;
1442 goto out;
1443 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001444 pr = fmt1_str[0];
1445 tev->group = strdup(fmt2_str);
1446 tev->event = strdup(fmt3_str);
1447 if (tev->group == NULL || tev->event == NULL) {
1448 ret = -ENOMEM;
1449 goto out;
1450 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001451 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001452
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001454
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001455 /* Scan module name(if there), function name and offset */
1456 p = strchr(argv[1], ':');
1457 if (p) {
1458 tp->module = strndup(argv[1], p - argv[1]);
1459 p++;
1460 } else
1461 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001462 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001463 if (fmt1_str[0] == '0') /* only the address started with 0x */
1464 tp->address = strtoul(fmt1_str, NULL, 0);
1465 else {
1466 /* Only the symbol-based probe has offset */
1467 tp->symbol = strdup(fmt1_str);
1468 if (tp->symbol == NULL) {
1469 ret = -ENOMEM;
1470 goto out;
1471 }
1472 fmt2_str = strtok_r(NULL, "", &fmt);
1473 if (fmt2_str == NULL)
1474 tp->offset = 0;
1475 else
1476 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001477 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001479 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301480 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001481 if (tev->args == NULL) {
1482 ret = -ENOMEM;
1483 goto out;
1484 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001485 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001486 p = strchr(argv[i + 2], '=');
1487 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001488 *p++ = '\0';
1489 else
1490 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001491 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001492 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001493 tev->args[i].value = strdup(p);
1494 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1495 ret = -ENOMEM;
1496 goto out;
1497 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001498 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 ret = 0;
1500out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001501 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001504}
1505
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001506/* Compose only probe arg */
1507int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1508{
1509 struct perf_probe_arg_field *field = pa->field;
1510 int ret;
1511 char *tmp = buf;
1512
Masami Hiramatsu48481932010-04-12 13:16:53 -04001513 if (pa->name && pa->var)
1514 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1515 else
1516 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001517 if (ret <= 0)
1518 goto error;
1519 tmp += ret;
1520 len -= ret;
1521
1522 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001523 if (field->name[0] == '[')
1524 ret = e_snprintf(tmp, len, "%s", field->name);
1525 else
1526 ret = e_snprintf(tmp, len, "%s%s",
1527 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001528 if (ret <= 0)
1529 goto error;
1530 tmp += ret;
1531 len -= ret;
1532 field = field->next;
1533 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001534
1535 if (pa->type) {
1536 ret = e_snprintf(tmp, len, ":%s", pa->type);
1537 if (ret <= 0)
1538 goto error;
1539 tmp += ret;
1540 len -= ret;
1541 }
1542
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001543 return tmp - buf;
1544error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001545 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001546 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001547}
1548
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001549/* Compose only probe point (not argument) */
1550static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001551{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001552 char *buf, *tmp;
1553 char offs[32] = "", line[32] = "", file[32] = "";
1554 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001555
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001556 buf = zalloc(MAX_CMDLEN);
1557 if (buf == NULL) {
1558 ret = -ENOMEM;
1559 goto error;
1560 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001561 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001562 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001563 if (ret <= 0)
1564 goto error;
1565 }
1566 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001567 ret = e_snprintf(line, 32, ":%d", pp->line);
1568 if (ret <= 0)
1569 goto error;
1570 }
1571 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001572 tmp = pp->file;
1573 len = strlen(tmp);
1574 if (len > 30) {
1575 tmp = strchr(pp->file + len - 30, '/');
1576 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1577 }
1578 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001579 if (ret <= 0)
1580 goto error;
1581 }
1582
1583 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001584 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1585 offs, pp->retprobe ? "%return" : "", line,
1586 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001588 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589 if (ret <= 0)
1590 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001591
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001592 return buf;
1593error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001594 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001595 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001596 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001597}
1598
1599#if 0
1600char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1601{
1602 char *buf;
1603 int i, len, ret;
1604
1605 buf = synthesize_perf_probe_point(&pev->point);
1606 if (!buf)
1607 return NULL;
1608
1609 len = strlen(buf);
1610 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001611 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001612 pev->args[i].name);
1613 if (ret <= 0) {
1614 free(buf);
1615 return NULL;
1616 }
1617 len += ret;
1618 }
1619
1620 return buf;
1621}
1622#endif
1623
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301624static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625 char **buf, size_t *buflen,
1626 int depth)
1627{
1628 int ret;
1629 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301630 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001631 buflen, depth + 1);
1632 if (depth < 0)
1633 goto out;
1634 }
1635
1636 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1637 if (ret < 0)
1638 depth = ret;
1639 else {
1640 *buf += ret;
1641 *buflen -= ret;
1642 }
1643out:
1644 return depth;
1645
1646}
1647
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301648static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 char *buf, size_t buflen)
1650{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 int ret, depth = 0;
1653 char *tmp = buf;
1654
1655 /* Argument name or separator */
1656 if (arg->name)
1657 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1658 else
1659 ret = e_snprintf(buf, buflen, " ");
1660 if (ret < 0)
1661 return ret;
1662 buf += ret;
1663 buflen -= ret;
1664
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001665 /* Special case: @XXX */
1666 if (arg->value[0] == '@' && arg->ref)
1667 ref = ref->next;
1668
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001669 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001670 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301671 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001672 &buflen, 1);
1673 if (depth < 0)
1674 return depth;
1675 }
1676
1677 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001678 if (arg->value[0] == '@' && arg->ref)
1679 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1680 arg->ref->offset);
1681 else
1682 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001683 if (ret < 0)
1684 return ret;
1685 buf += ret;
1686 buflen -= ret;
1687
1688 /* Closing */
1689 while (depth--) {
1690 ret = e_snprintf(buf, buflen, ")");
1691 if (ret < 0)
1692 return ret;
1693 buf += ret;
1694 buflen -= ret;
1695 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001696 /* Print argument type */
1697 if (arg->type) {
1698 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1699 if (ret <= 0)
1700 return ret;
1701 buf += ret;
1702 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001703
1704 return buf - tmp;
1705}
1706
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301707char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001708{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301709 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001710 char *buf;
1711 int i, len, ret;
1712
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001713 buf = zalloc(MAX_CMDLEN);
1714 if (buf == NULL)
1715 return NULL;
1716
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001717 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1718 tev->group, tev->event);
1719 if (len <= 0)
1720 goto error;
1721
1722 /* Uprobes must have tp->address and tp->module */
1723 if (tev->uprobes && (!tp->address || !tp->module))
1724 goto error;
1725
1726 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301727 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001728 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1729 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301730 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001731 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301732 tp->module ?: "", tp->module ? ":" : "",
1733 tp->symbol, tp->offset);
1734
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001735 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001736 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001737 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738
1739 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301740 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001741 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001742 if (ret <= 0)
1743 goto error;
1744 len += ret;
1745 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001746
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001747 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001748error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001749 free(buf);
1750 return NULL;
1751}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001752
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001753static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1754 struct perf_probe_point *pp,
1755 bool is_kprobe)
1756{
1757 struct symbol *sym = NULL;
1758 struct map *map;
1759 u64 addr;
1760 int ret = -ENOENT;
1761
1762 if (!is_kprobe) {
1763 map = dso__new_map(tp->module);
1764 if (!map)
1765 goto out;
1766 addr = tp->address;
1767 sym = map__find_symbol(map, addr, NULL);
1768 } else {
1769 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1770 if (addr) {
1771 addr += tp->offset;
1772 sym = __find_kernel_function(addr, &map);
1773 }
1774 }
1775 if (!sym)
1776 goto out;
1777
1778 pp->retprobe = tp->retprobe;
1779 pp->offset = addr - map->unmap_ip(map, sym->start);
1780 pp->function = strdup(sym->name);
1781 ret = pp->function ? 0 : -ENOMEM;
1782
1783out:
1784 if (map && !is_kprobe) {
1785 dso__delete(map->dso);
1786 map__delete(map);
1787 }
1788
1789 return ret;
1790}
1791
1792static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1793 struct perf_probe_point *pp,
1794 bool is_kprobe)
1795{
1796 char buf[128];
1797 int ret;
1798
1799 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1800 if (!ret)
1801 return 0;
1802 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1803 if (!ret)
1804 return 0;
1805
1806 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1807
1808 if (tp->symbol) {
1809 pp->function = strdup(tp->symbol);
1810 pp->offset = tp->offset;
1811 } else if (!tp->module && !is_kprobe) {
1812 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1813 if (ret < 0)
1814 return ret;
1815 pp->function = strdup(buf);
1816 pp->offset = 0;
1817 }
1818 if (pp->function == NULL)
1819 return -ENOMEM;
1820
1821 pp->retprobe = tp->retprobe;
1822
1823 return 0;
1824}
1825
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301826static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301827 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001828{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001829 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001830 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001832 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001833 pev->event = strdup(tev->event);
1834 pev->group = strdup(tev->group);
1835 if (pev->event == NULL || pev->group == NULL)
1836 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001837
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001838 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001839 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001840 if (ret < 0)
1841 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001842
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001843 /* Convert trace_arg to probe_arg */
1844 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001845 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1846 if (pev->args == NULL)
1847 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001848 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001849 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001850 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001851 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301852 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001853 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001854 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001856 if (pev->args[i].name == NULL && ret >= 0)
1857 ret = -ENOMEM;
1858 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001859
1860 if (ret < 0)
1861 clear_perf_probe_event(pev);
1862
1863 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001864}
1865
1866void clear_perf_probe_event(struct perf_probe_event *pev)
1867{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001868 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001869 int i;
1870
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001871 free(pev->event);
1872 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001873 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001874 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001875
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001876 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001877 free(pev->args[i].name);
1878 free(pev->args[i].var);
1879 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001880 field = pev->args[i].field;
1881 while (field) {
1882 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001883 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001884 free(field);
1885 field = next;
1886 }
1887 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001888 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889 memset(pev, 0, sizeof(*pev));
1890}
1891
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301892static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301894 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001895 int i;
1896
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001897 free(tev->event);
1898 free(tev->group);
1899 free(tev->point.symbol);
1900 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001901 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001902 free(tev->args[i].name);
1903 free(tev->args[i].value);
1904 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001905 ref = tev->args[i].ref;
1906 while (ref) {
1907 next = ref->next;
1908 free(ref);
1909 ref = next;
1910 }
1911 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001912 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001913 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001914}
1915
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001916static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301917{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001918 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301919
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001920 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301921 const char *config;
1922
1923 if (!is_kprobe)
1924 config = "CONFIG_UPROBE_EVENTS";
1925 else
1926 config = "CONFIG_KPROBE_EVENTS";
1927
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001928 pr_warning("%cprobe_events file does not exist"
1929 " - please rebuild kernel with %s.\n",
1930 is_kprobe ? 'k' : 'u', config);
1931 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001932 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001933 else
1934 pr_warning("Failed to open %cprobe_events: %s\n",
1935 is_kprobe ? 'k' : 'u',
1936 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301937}
1938
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001939static void print_both_open_warning(int kerr, int uerr)
1940{
1941 /* Both kprobes and uprobes are disabled, warn it. */
1942 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001943 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001944 else if (kerr == -ENOENT && uerr == -ENOENT)
1945 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1946 "or/and CONFIG_UPROBE_EVENTS.\n");
1947 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001948 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001949 pr_warning("Failed to open kprobe events: %s.\n",
1950 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1951 pr_warning("Failed to open uprobe events: %s.\n",
1952 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1953 }
1954}
1955
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001956static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001957{
1958 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001959 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001960 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001961 int ret;
1962
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001963 __debugfs = tracefs_find_mountpoint();
1964 if (__debugfs == NULL) {
1965 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001966
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001967 __debugfs = debugfs_find_mountpoint();
1968 if (__debugfs == NULL)
1969 return -ENOTSUP;
1970 }
1971
1972 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1973 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001974 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001975 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001976 if (readwrite && !probe_event_dry_run)
Masami Hiramatsub8dc39842015-05-06 21:46:42 +09001977 ret = open(buf, O_RDWR | O_APPEND, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001978 else
1979 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001980
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301981 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001982 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001983 }
1984 return ret;
1985}
1986
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301987static int open_kprobe_events(bool readwrite)
1988{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001989 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301990}
1991
1992static int open_uprobe_events(bool readwrite)
1993{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001994 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301995}
1996
1997/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301998static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001999{
2000 int ret, idx;
2001 FILE *fp;
2002 char buf[MAX_CMDLEN];
2003 char *p;
2004 struct strlist *sl;
2005
2006 sl = strlist__new(true, NULL);
2007
2008 fp = fdopen(dup(fd), "r");
2009 while (!feof(fp)) {
2010 p = fgets(buf, MAX_CMDLEN, fp);
2011 if (!p)
2012 break;
2013
2014 idx = strlen(p) - 1;
2015 if (p[idx] == '\n')
2016 p[idx] = '\0';
2017 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002018 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002019 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002020 strlist__delete(sl);
2021 return NULL;
2022 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002023 }
2024 fclose(fp);
2025
2026 return sl;
2027}
2028
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002029struct kprobe_blacklist_node {
2030 struct list_head list;
2031 unsigned long start;
2032 unsigned long end;
2033 char *symbol;
2034};
2035
2036static void kprobe_blacklist__delete(struct list_head *blacklist)
2037{
2038 struct kprobe_blacklist_node *node;
2039
2040 while (!list_empty(blacklist)) {
2041 node = list_first_entry(blacklist,
2042 struct kprobe_blacklist_node, list);
2043 list_del(&node->list);
2044 free(node->symbol);
2045 free(node);
2046 }
2047}
2048
2049static int kprobe_blacklist__load(struct list_head *blacklist)
2050{
2051 struct kprobe_blacklist_node *node;
2052 const char *__debugfs = debugfs_find_mountpoint();
2053 char buf[PATH_MAX], *p;
2054 FILE *fp;
2055 int ret;
2056
2057 if (__debugfs == NULL)
2058 return -ENOTSUP;
2059
2060 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2061 if (ret < 0)
2062 return ret;
2063
2064 fp = fopen(buf, "r");
2065 if (!fp)
2066 return -errno;
2067
2068 ret = 0;
2069 while (fgets(buf, PATH_MAX, fp)) {
2070 node = zalloc(sizeof(*node));
2071 if (!node) {
2072 ret = -ENOMEM;
2073 break;
2074 }
2075 INIT_LIST_HEAD(&node->list);
2076 list_add_tail(&node->list, blacklist);
2077 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2078 ret = -EINVAL;
2079 break;
2080 }
2081 p = strchr(buf, '\t');
2082 if (p) {
2083 p++;
2084 if (p[strlen(p) - 1] == '\n')
2085 p[strlen(p) - 1] = '\0';
2086 } else
2087 p = (char *)"unknown";
2088 node->symbol = strdup(p);
2089 if (!node->symbol) {
2090 ret = -ENOMEM;
2091 break;
2092 }
2093 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2094 node->start, node->end, node->symbol);
2095 ret++;
2096 }
2097 if (ret < 0)
2098 kprobe_blacklist__delete(blacklist);
2099 fclose(fp);
2100
2101 return ret;
2102}
2103
2104static struct kprobe_blacklist_node *
2105kprobe_blacklist__find_by_address(struct list_head *blacklist,
2106 unsigned long address)
2107{
2108 struct kprobe_blacklist_node *node;
2109
2110 list_for_each_entry(node, blacklist, list) {
2111 if (node->start <= address && address <= node->end)
2112 return node;
2113 }
2114
2115 return NULL;
2116}
2117
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002118/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002119static int show_perf_probe_event(struct perf_probe_event *pev,
2120 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002121{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002122 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002123 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002124 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002125
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002126 /* Synthesize only event probe point */
2127 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 if (!place)
2129 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002130
2131 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002132 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002133 return ret;
2134
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002135 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002136 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002137 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002138
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002139 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002140 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002141 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002142 ret = synthesize_perf_probe_arg(&pev->args[i],
2143 buf, 128);
2144 if (ret < 0)
2145 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002146 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002147 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002148 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002149 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002151 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002152}
2153
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002154static bool filter_probe_trace_event(struct probe_trace_event *tev,
2155 struct strfilter *filter)
2156{
2157 char tmp[128];
2158
2159 /* At first, check the event name itself */
2160 if (strfilter__compare(filter, tev->event))
2161 return true;
2162
2163 /* Next, check the combination of name and group */
2164 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2165 return false;
2166 return strfilter__compare(filter, tmp);
2167}
2168
2169static int __show_perf_probe_events(int fd, bool is_kprobe,
2170 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002171{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302172 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302173 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002174 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002175 struct strlist *rawlist;
2176 struct str_node *ent;
2177
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002178 memset(&tev, 0, sizeof(tev));
2179 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002180
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302181 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002182 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002183 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002184
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002185 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302186 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002187 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002188 if (!filter_probe_trace_event(&tev, filter))
2189 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302190 ret = convert_to_perf_probe_event(&tev, &pev,
2191 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002192 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002193 ret = show_perf_probe_event(&pev,
2194 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002195 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002196next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002197 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302198 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002199 if (ret < 0)
2200 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002201 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002202 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203
2204 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002205}
2206
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302207/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002208int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302209{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002210 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302211
2212 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302213
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002214 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215 if (ret < 0)
2216 return ret;
2217
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002218 kp_fd = open_kprobe_events(false);
2219 if (kp_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002220 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002221 close(kp_fd);
2222 if (ret < 0)
2223 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302224 }
2225
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002226 up_fd = open_uprobe_events(false);
2227 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002228 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002229 ret = kp_fd;
2230 goto out;
2231 }
2232
2233 if (up_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002234 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002235 close(up_fd);
2236 }
2237out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002238 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302239 return ret;
2240}
2241
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002242/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302243static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002244{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002245 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002246 struct strlist *sl, *rawlist;
2247 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002250
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002251 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302252 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002253 if (!rawlist)
2254 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002255 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002256 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302257 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002258 if (ret < 0)
2259 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002260 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002261 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2262 tev.event);
2263 if (ret >= 0)
2264 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002265 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002266 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302267 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002268 if (ret < 0)
2269 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002270 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002271 strlist__delete(rawlist);
2272
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002273 if (ret < 0) {
2274 strlist__delete(sl);
2275 return NULL;
2276 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002277 return sl;
2278}
2279
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302280static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002281{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002282 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302283 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002284 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002285
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002286 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302287 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288 return -EINVAL;
2289 }
2290
Masami Hiramatsufa282442009-12-08 17:03:23 -05002291 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002292 if (!probe_event_dry_run) {
2293 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002294 if (ret <= 0) {
2295 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002296 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002297 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002298 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002299 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002300 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002301 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002302}
2303
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002304static int get_new_event_name(char *buf, size_t len, const char *base,
2305 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002306{
2307 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002308
Naveen N. Rao3099c022015-04-28 17:35:34 +05302309 if (*base == '.')
2310 base++;
2311
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002312 /* Try no suffix */
2313 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002314 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002315 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002316 return ret;
2317 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002318 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002320
Masami Hiramatsud761b082009-12-15 10:32:25 -05002321 if (!allow_suffix) {
2322 pr_warning("Error: event \"%s\" already exists. "
2323 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002324 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002325 }
2326
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002327 /* Try to add suffix */
2328 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002329 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002330 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002331 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002332 return ret;
2333 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002334 if (!strlist__has_entry(namelist, buf))
2335 break;
2336 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002337 if (i == MAX_EVENT_INDEX) {
2338 pr_warning("Too many events are on the same function.\n");
2339 ret = -ERANGE;
2340 }
2341
2342 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002343}
2344
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002345/* Warn if the current kernel's uprobe implementation is old */
2346static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2347{
2348 int i;
2349 char *buf = synthesize_probe_trace_command(tev);
2350
2351 /* Old uprobe event doesn't support memory dereference */
2352 if (!tev->uprobes || tev->nargs == 0 || !buf)
2353 goto out;
2354
2355 for (i = 0; i < tev->nargs; i++)
2356 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2357 pr_warning("Please upgrade your kernel to at least "
2358 "3.14 to have access to feature %s\n",
2359 tev->args[i].value);
2360 break;
2361 }
2362out:
2363 free(buf);
2364}
2365
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302366static int __add_probe_trace_events(struct perf_probe_event *pev,
2367 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002368 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002369{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002370 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302371 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002372 char buf[64];
2373 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002374 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002375 LIST_HEAD(blacklist);
2376 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002377
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302378 if (pev->uprobes)
2379 fd = open_uprobe_events(true);
2380 else
2381 fd = open_kprobe_events(true);
2382
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002383 if (fd < 0) {
2384 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002385 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002386 }
2387
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002388 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302389 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002390 if (!namelist) {
2391 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002392 ret = -ENOMEM;
2393 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002394 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002395 /* Get kprobe blacklist if exists */
2396 if (!pev->uprobes) {
2397 ret = kprobe_blacklist__load(&blacklist);
2398 if (ret < 0)
2399 pr_debug("No kprobe blacklist support, ignored\n");
2400 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002401
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002403 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002404 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002405 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002406 /* Ensure that the address is NOT blacklisted */
2407 node = kprobe_blacklist__find_by_address(&blacklist,
2408 tev->point.address);
2409 if (node) {
2410 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2411 continue;
2412 }
2413
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002414 if (pev->event)
2415 event = pev->event;
2416 else
2417 if (pev->point.function)
2418 event = pev->point.function;
2419 else
2420 event = tev->point.symbol;
2421 if (pev->group)
2422 group = pev->group;
2423 else
2424 group = PERFPROBE_GROUP;
2425
2426 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 ret = get_new_event_name(buf, 64, event,
2428 namelist, allow_suffix);
2429 if (ret < 0)
2430 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002431 event = buf;
2432
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002433 tev->event = strdup(event);
2434 tev->group = strdup(group);
2435 if (tev->event == NULL || tev->group == NULL) {
2436 ret = -ENOMEM;
2437 break;
2438 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302439 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002440 if (ret < 0)
2441 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002442 /* Add added event name to namelist */
2443 strlist__add(namelist, event);
2444
2445 /* Trick here - save current event/group */
2446 event = pev->event;
2447 group = pev->group;
2448 pev->event = tev->event;
2449 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002450 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002451 /* Trick here - restore current event/group */
2452 pev->event = (char *)event;
2453 pev->group = (char *)group;
2454
2455 /*
2456 * Probes after the first probe which comes from same
2457 * user input are always allowed to add suffix, because
2458 * there might be several addresses corresponding to
2459 * one code line.
2460 */
2461 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002462 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002463 if (ret == -EINVAL && pev->uprobes)
2464 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002465
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002466 /* Note that it is possible to skip all events because of blacklist */
2467 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002468 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002469 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2470 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002471 tev->event);
2472 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002473
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002474 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002475 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002476close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002477 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002478 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002479}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002480
Namhyung Kim564c62a2015-01-14 20:18:07 +09002481static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002482{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002483 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002484 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002485
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002486 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002487 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002488 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002489
2490 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002491}
2492
2493#define strdup_or_goto(str, label) \
2494 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2495
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302496void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2497 struct probe_trace_event *tev __maybe_unused,
2498 struct map *map __maybe_unused) { }
2499
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002500/*
2501 * Find probe function addresses from map.
2502 * Return an error or the number of found probe_trace_event
2503 */
2504static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2505 struct probe_trace_event **tevs,
2506 int max_tevs, const char *target)
2507{
2508 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002509 struct ref_reloc_sym *reloc_sym = NULL;
2510 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002511 struct probe_trace_event *tev;
2512 struct perf_probe_point *pp = &pev->point;
2513 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002514 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002515 int ret, i;
2516
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002517 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002518 if (!map) {
2519 ret = -EINVAL;
2520 goto out;
2521 }
2522
2523 /*
2524 * Load matched symbols: Since the different local symbols may have
2525 * same name but different addresses, this lists all the symbols.
2526 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002527 num_matched_functions = find_probe_functions(map, pp->function);
2528 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002529 pr_err("Failed to find symbol %s in %s\n", pp->function,
2530 target ? : "kernel");
2531 ret = -ENOENT;
2532 goto out;
2533 } else if (num_matched_functions > max_tevs) {
2534 pr_err("Too many functions matched in %s\n",
2535 target ? : "kernel");
2536 ret = -E2BIG;
2537 goto out;
2538 }
2539
Namhyung Kim25dd9172015-01-14 20:18:08 +09002540 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002541 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002542 if (!reloc_sym) {
2543 pr_warning("Relocated base symbol is not found!\n");
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547 }
2548
2549 /* Setup result trace-probe-events */
2550 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2551 if (!*tevs) {
2552 ret = -ENOMEM;
2553 goto out;
2554 }
2555
2556 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002557
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002558 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002559 tev = (*tevs) + ret;
2560 tp = &tev->point;
2561 if (ret == num_matched_functions) {
2562 pr_warning("Too many symbols are listed. Skip it.\n");
2563 break;
2564 }
2565 ret++;
2566
2567 if (pp->offset > sym->end - sym->start) {
2568 pr_warning("Offset %ld is bigger than the size of %s\n",
2569 pp->offset, sym->name);
2570 ret = -ENOENT;
2571 goto err_out;
2572 }
2573 /* Add one probe point */
2574 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2575 if (reloc_sym) {
2576 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2577 tp->offset = tp->address - reloc_sym->addr;
2578 } else {
2579 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2580 tp->offset = pp->offset;
2581 }
2582 tp->retprobe = pp->retprobe;
2583 if (target)
2584 tev->point.module = strdup_or_goto(target, nomem_out);
2585 tev->uprobes = pev->uprobes;
2586 tev->nargs = pev->nargs;
2587 if (tev->nargs) {
2588 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2589 tev->nargs);
2590 if (tev->args == NULL)
2591 goto nomem_out;
2592 }
2593 for (i = 0; i < tev->nargs; i++) {
2594 if (pev->args[i].name)
2595 tev->args[i].name =
2596 strdup_or_goto(pev->args[i].name,
2597 nomem_out);
2598
2599 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2600 nomem_out);
2601 if (pev->args[i].type)
2602 tev->args[i].type =
2603 strdup_or_goto(pev->args[i].type,
2604 nomem_out);
2605 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302606 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002607 }
2608
2609out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002610 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002611 return ret;
2612
2613nomem_out:
2614 ret = -ENOMEM;
2615err_out:
2616 clear_probe_trace_events(*tevs, num_matched_functions);
2617 zfree(tevs);
2618 goto out;
2619}
2620
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302621bool __weak arch__prefers_symtab(void) { return false; }
2622
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302623static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2624 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302625 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002626{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002627 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002628
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002629 if (pev->uprobes && !pev->group) {
2630 /* Replace group name if not given */
2631 ret = convert_exec_to_group(target, &pev->group);
2632 if (ret != 0) {
2633 pr_warning("Failed to make a group name.\n");
2634 return ret;
2635 }
2636 }
2637
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302638 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2639 ret = find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2640 if (ret > 0)
2641 return ret; /* Found in symbol table */
2642 }
2643
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002644 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302645 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002646 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002647 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002648
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002649 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002650}
2651
2652struct __event_package {
2653 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302654 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002655 int ntevs;
2656};
2657
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002658int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002659 int max_tevs, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002660{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002661 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002662 struct __event_package *pkgs;
2663
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302664 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002665 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302666
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002667 if (pkgs == NULL)
2668 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002669
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002670 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002671 if (ret < 0) {
2672 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002673 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002674 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002675
2676 /* Loop 1: convert all events */
2677 for (i = 0; i < npevs; i++) {
2678 pkgs[i].pev = &pevs[i];
2679 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302680 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002681 &pkgs[i].tevs,
2682 max_tevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002683 pkgs[i].pev->target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002684 if (ret < 0)
2685 goto end;
2686 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002687 }
2688
2689 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002690 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302691 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002692 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002693 if (ret < 0)
2694 break;
2695 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002697 /* Loop 3: cleanup and free trace events */
2698 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002699 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302700 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002701 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002702 }
2703 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002704 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002705
2706 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002707}
2708
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302709static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002710{
2711 char *p;
2712 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002713 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002714
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302715 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002716 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2717 if (ret < 0)
2718 goto error;
2719
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002720 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002721 if (!p) {
2722 pr_debug("Internal error: %s should have ':' but not.\n",
2723 ent->s);
2724 ret = -ENOTSUP;
2725 goto error;
2726 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002727 *p = '/';
2728
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002729 pr_debug("Writing event: %s\n", buf);
2730 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002731 if (ret < 0) {
2732 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002733 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002734 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002735
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002736 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002737 return 0;
2738error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002739 pr_warning("Failed to delete event: %s\n",
2740 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002741 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002742}
2743
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002744static int del_trace_probe_events(int fd, struct strfilter *filter,
2745 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002746{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002747 struct str_node *ent;
2748 const char *p;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002749 int ret = -ENOENT;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002750
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002751 if (!namelist)
2752 return -ENOENT;
2753
2754 strlist__for_each(ent, namelist) {
2755 p = strchr(ent->s, ':');
2756 if ((p && strfilter__compare(filter, p + 1)) ||
2757 strfilter__compare(filter, ent->s)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302758 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002759 if (ret < 0)
2760 break;
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 Hiramatsu307a4642015-05-05 11:29:48 +09002767int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002768{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002769 int ret, ret2, ufd = -1, kfd = -1;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302770 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002771 char *str = strfilter__string(filter);
2772
2773 if (!str)
2774 return -EINVAL;
2775
2776 pr_debug("Delete filter: \'%s\'\n", str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002777
Masami Hiramatsufa282442009-12-08 17:03:23 -05002778 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302779 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002780 if (kfd >= 0)
2781 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302782
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302783 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002784 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785 unamelist = get_probe_trace_event_names(ufd, true);
2786
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002787 if (kfd < 0 && ufd < 0) {
2788 print_both_open_warning(kfd, ufd);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002789 ret = kfd;
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002790 goto error;
2791 }
2792
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002793 ret = del_trace_probe_events(kfd, filter, namelist);
2794 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302795 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002796
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002797 ret2 = del_trace_probe_events(ufd, filter, unamelist);
2798 if (ret2 < 0 && ret2 != -ENOENT)
2799 ret = ret2;
2800 else if (ret == -ENOENT && ret2 == -ENOENT) {
2801 pr_debug("\"%s\" does not hit any event.\n", str);
2802 /* Note that this is silently ignored */
2803 ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002804 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302805
2806error:
2807 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302808 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302809 close(kfd);
2810 }
2811
2812 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302813 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302814 close(ufd);
2815 }
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002816 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002817
2818 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002819}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302820
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002821/* TODO: don't use a global variable for filter ... */
2822static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002823
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002824/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002825 * If a symbol corresponds to a function with global binding and
2826 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002827 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002828static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002829 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002830{
Namhyung Kime578da32015-03-06 16:31:29 +09002831 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002832 return 0;
2833 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002834}
2835
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002836int show_available_funcs(const char *target, struct strfilter *_filter,
2837 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002838{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002839 struct map *map;
2840 int ret;
2841
2842 ret = init_symbol_maps(user);
2843 if (ret < 0)
2844 return ret;
2845
2846 /* Get a symbol map */
2847 if (user)
2848 map = dso__new_map(target);
2849 else
2850 map = kernel_get_module_map(target);
2851 if (!map) {
2852 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853 return -EINVAL;
2854 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002855
2856 /* Load symbols with given filter */
2857 available_func_filter = _filter;
2858 if (map__load(map, filter_available_functions)) {
2859 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2860 goto end;
2861 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002862 if (!dso__sorted_by_name(map->dso, map->type))
2863 dso__sort_by_name(map->dso, map->type);
2864
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002865 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302866 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002867 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2868end:
2869 if (user) {
2870 dso__delete(map->dso);
2871 map__delete(map);
2872 }
2873 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302874
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002875 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302876}
2877