blob: eb75a5efcfd7691317a57955d55a00fe1028ad33 [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
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100983/*
984 * Stuff 'lr' according to the line range described by 'arg'.
985 * The line range syntax is described by:
986 *
987 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900988 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100989 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400990int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500991{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900992 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100993 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100994
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100995 if (!name)
996 return -ENOMEM;
997
998 lr->start = 0;
999 lr->end = INT_MAX;
1000
1001 range = strchr(name, ':');
1002 if (range) {
1003 *range++ = '\0';
1004
1005 err = parse_line_num(&range, &lr->start, "start line");
1006 if (err)
1007 goto err;
1008
1009 if (*range == '+' || *range == '-') {
1010 const char c = *range++;
1011
1012 err = parse_line_num(&range, &lr->end, "end line");
1013 if (err)
1014 goto err;
1015
1016 if (c == '+') {
1017 lr->end += lr->start;
1018 /*
1019 * Adjust the number of lines here.
1020 * If the number of lines == 1, the
1021 * the end of line should be equal to
1022 * the start of line.
1023 */
1024 lr->end--;
1025 }
1026 }
1027
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001028 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001029
1030 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001031 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001032 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001034 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001035 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001036 if (*range != '\0') {
1037 semantic_error("Tailing with invalid str '%s'.\n", range);
1038 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001039 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001040 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001041
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001042 file = strchr(name, '@');
1043 if (file) {
1044 *file = '\0';
1045 lr->file = strdup(++file);
1046 if (lr->file == NULL) {
1047 err = -ENOMEM;
1048 goto err;
1049 }
1050 lr->function = name;
1051 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001052 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001053 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001054 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001055
1056 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001057err:
1058 free(name);
1059 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001060}
1061
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001062/* Check the name is good for event/group */
1063static bool check_event_name(const char *name)
1064{
1065 if (!isalpha(*name) && *name != '_')
1066 return false;
1067 while (*++name != '\0') {
1068 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1069 return false;
1070 }
1071 return true;
1072}
1073
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001074/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001075static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001076{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001077 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001078 char *ptr, *tmp;
1079 char c, nc = 0;
1080 /*
1081 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001082 * perf probe [EVENT=]SRC[:LN|;PTN]
1083 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001084 *
1085 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001086 */
1087
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001088 ptr = strpbrk(arg, ";=@+%");
1089 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001090 *ptr = '\0';
1091 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 if (strchr(arg, ':')) {
1093 semantic_error("Group name is not supported yet.\n");
1094 return -ENOTSUP;
1095 }
1096 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001097 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001098 "follow C symbol-naming rule.\n", arg);
1099 return -EINVAL;
1100 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001101 pev->event = strdup(arg);
1102 if (pev->event == NULL)
1103 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001104 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001105 arg = tmp;
1106 }
1107
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001108 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001109 if (ptr) {
1110 nc = *ptr;
1111 *ptr++ = '\0';
1112 }
1113
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001114 tmp = strdup(arg);
1115 if (tmp == NULL)
1116 return -ENOMEM;
1117
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001118 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001119 if (strchr(tmp, '.')) /* File */
1120 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001121 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001122 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001123
1124 /* Parse other options */
1125 while (ptr) {
1126 arg = ptr;
1127 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001128 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001129 pp->lazy_line = strdup(arg);
1130 if (pp->lazy_line == NULL)
1131 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001132 break;
1133 }
1134 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001135 if (ptr) {
1136 nc = *ptr;
1137 *ptr++ = '\0';
1138 }
1139 switch (c) {
1140 case ':': /* Line number */
1141 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001142 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001143 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144 " in line number.\n");
1145 return -EINVAL;
1146 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001147 break;
1148 case '+': /* Byte offset from a symbol */
1149 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001150 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001151 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001152 " in offset.\n");
1153 return -EINVAL;
1154 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001155 break;
1156 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001157 if (pp->file) {
1158 semantic_error("SRC@SRC is not allowed.\n");
1159 return -EINVAL;
1160 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001161 pp->file = strdup(arg);
1162 if (pp->file == NULL)
1163 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001164 break;
1165 case '%': /* Probe places */
1166 if (strcmp(arg, "return") == 0) {
1167 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001168 } else { /* Others not supported yet */
1169 semantic_error("%%%s is not supported.\n", arg);
1170 return -ENOTSUP;
1171 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001172 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001173 default: /* Buggy case */
1174 pr_err("This program has a bug at %s:%d.\n",
1175 __FILE__, __LINE__);
1176 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001177 break;
1178 }
1179 }
1180
1181 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001182 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001183 semantic_error("Lazy pattern can't be used with"
1184 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001185 return -EINVAL;
1186 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001187
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001188 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001189 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 return -EINVAL;
1191 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001192
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001194 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 return -EINVAL;
1196 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001197
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001199 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001200 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 return -EINVAL;
1202 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001205 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 return -EINVAL;
1207 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001208
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001210 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 return -EINVAL;
1212 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001213
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001215 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001216 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 return -EINVAL;
1218 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001219
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001220 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001221 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1222 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001224}
1225
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001226/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001228{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001229 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001230 struct perf_probe_arg_field **fieldp;
1231
1232 pr_debug("parsing arg: %s into ", str);
1233
Masami Hiramatsu48481932010-04-12 13:16:53 -04001234 tmp = strchr(str, '=');
1235 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001236 arg->name = strndup(str, tmp - str);
1237 if (arg->name == NULL)
1238 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001239 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001240 str = tmp + 1;
1241 }
1242
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001243 tmp = strchr(str, ':');
1244 if (tmp) { /* Type setting */
1245 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001246 arg->type = strdup(tmp + 1);
1247 if (arg->type == NULL)
1248 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001249 pr_debug("type:%s ", arg->type);
1250 }
1251
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001252 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001253 if (!is_c_varname(str) || !tmp) {
1254 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001255 arg->var = strdup(str);
1256 if (arg->var == NULL)
1257 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001258 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001260 }
1261
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001262 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001263 arg->var = strndup(str, tmp - str);
1264 if (arg->var == NULL)
1265 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001266 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001267 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001268 fieldp = &arg->field;
1269
1270 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001271 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1272 if (*fieldp == NULL)
1273 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001274 if (*tmp == '[') { /* Array */
1275 str = tmp;
1276 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001277 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001278 if (*tmp != ']' || tmp == str + 1) {
1279 semantic_error("Array index must be a"
1280 " number.\n");
1281 return -EINVAL;
1282 }
1283 tmp++;
1284 if (*tmp == '\0')
1285 tmp = NULL;
1286 } else { /* Structure */
1287 if (*tmp == '.') {
1288 str = tmp + 1;
1289 (*fieldp)->ref = false;
1290 } else if (tmp[1] == '>') {
1291 str = tmp + 2;
1292 (*fieldp)->ref = true;
1293 } else {
1294 semantic_error("Argument parse error: %s\n",
1295 str);
1296 return -EINVAL;
1297 }
1298 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001299 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001300 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001301 (*fieldp)->name = strndup(str, tmp - str);
1302 if ((*fieldp)->name == NULL)
1303 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001304 if (*str != '[')
1305 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001306 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1307 fieldp = &(*fieldp)->next;
1308 }
1309 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001310 (*fieldp)->name = strdup(str);
1311 if ((*fieldp)->name == NULL)
1312 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001313 if (*str != '[')
1314 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001315 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001316
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001317 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001318 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001319 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 if (arg->name == NULL)
1321 return -ENOMEM;
1322 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001323 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001324}
1325
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001328{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001329 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001330 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001331
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001332 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001333 if (!argv) {
1334 pr_debug("Failed to split arguments.\n");
1335 return -ENOMEM;
1336 }
1337 if (argc - 1 > MAX_PROBE_ARGS) {
1338 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1339 ret = -ERANGE;
1340 goto out;
1341 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001342 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001343 ret = parse_perf_probe_point(argv[0], pev);
1344 if (ret < 0)
1345 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001346
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001347 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001348 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001349 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1350 if (pev->args == NULL) {
1351 ret = -ENOMEM;
1352 goto out;
1353 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001354 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1355 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1356 if (ret >= 0 &&
1357 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001358 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001359 " kretprobe.\n");
1360 ret = -EINVAL;
1361 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001362 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001364 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365
1366 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001367}
1368
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001369/* Return true if this perf_probe_event requires debuginfo */
1370bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001371{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001372 int i;
1373
1374 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1375 return true;
1376
1377 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001378 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001379 return true;
1380
1381 return false;
1382}
1383
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301384/* Parse probe_events event into struct probe_point */
1385static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001386 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001387{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301388 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001389 char pr;
1390 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001391 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001392 int ret, i, argc;
1393 char **argv;
1394
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301395 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001396 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397 if (!argv) {
1398 pr_debug("Failed to split arguments.\n");
1399 return -ENOMEM;
1400 }
1401 if (argc < 2) {
1402 semantic_error("Too few probe arguments.\n");
1403 ret = -ERANGE;
1404 goto out;
1405 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001406
1407 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001408 argv0_str = strdup(argv[0]);
1409 if (argv0_str == NULL) {
1410 ret = -ENOMEM;
1411 goto out;
1412 }
1413 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1414 fmt2_str = strtok_r(NULL, "/", &fmt);
1415 fmt3_str = strtok_r(NULL, " \t", &fmt);
1416 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1417 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 semantic_error("Failed to parse event name: %s\n", argv[0]);
1419 ret = -EINVAL;
1420 goto out;
1421 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001422 pr = fmt1_str[0];
1423 tev->group = strdup(fmt2_str);
1424 tev->event = strdup(fmt3_str);
1425 if (tev->group == NULL || tev->event == NULL) {
1426 ret = -ENOMEM;
1427 goto out;
1428 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001430
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001431 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001432
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001433 /* Scan module name(if there), function name and offset */
1434 p = strchr(argv[1], ':');
1435 if (p) {
1436 tp->module = strndup(argv[1], p - argv[1]);
1437 p++;
1438 } else
1439 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001440 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001441 if (fmt1_str[0] == '0') /* only the address started with 0x */
1442 tp->address = strtoul(fmt1_str, NULL, 0);
1443 else {
1444 /* Only the symbol-based probe has offset */
1445 tp->symbol = strdup(fmt1_str);
1446 if (tp->symbol == NULL) {
1447 ret = -ENOMEM;
1448 goto out;
1449 }
1450 fmt2_str = strtok_r(NULL, "", &fmt);
1451 if (fmt2_str == NULL)
1452 tp->offset = 0;
1453 else
1454 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001455 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001456
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001457 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301458 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001459 if (tev->args == NULL) {
1460 ret = -ENOMEM;
1461 goto out;
1462 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001463 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001464 p = strchr(argv[i + 2], '=');
1465 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001466 *p++ = '\0';
1467 else
1468 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001469 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001470 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001471 tev->args[i].value = strdup(p);
1472 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1473 ret = -ENOMEM;
1474 goto out;
1475 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001476 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001477 ret = 0;
1478out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001479 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001480 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001481 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482}
1483
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001484/* Compose only probe arg */
1485int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1486{
1487 struct perf_probe_arg_field *field = pa->field;
1488 int ret;
1489 char *tmp = buf;
1490
Masami Hiramatsu48481932010-04-12 13:16:53 -04001491 if (pa->name && pa->var)
1492 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1493 else
1494 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001495 if (ret <= 0)
1496 goto error;
1497 tmp += ret;
1498 len -= ret;
1499
1500 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001501 if (field->name[0] == '[')
1502 ret = e_snprintf(tmp, len, "%s", field->name);
1503 else
1504 ret = e_snprintf(tmp, len, "%s%s",
1505 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001506 if (ret <= 0)
1507 goto error;
1508 tmp += ret;
1509 len -= ret;
1510 field = field->next;
1511 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001512
1513 if (pa->type) {
1514 ret = e_snprintf(tmp, len, ":%s", pa->type);
1515 if (ret <= 0)
1516 goto error;
1517 tmp += ret;
1518 len -= ret;
1519 }
1520
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001521 return tmp - buf;
1522error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001523 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001525}
1526
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001527/* Compose only probe point (not argument) */
1528static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001529{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001530 char *buf, *tmp;
1531 char offs[32] = "", line[32] = "", file[32] = "";
1532 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001533
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001534 buf = zalloc(MAX_CMDLEN);
1535 if (buf == NULL) {
1536 ret = -ENOMEM;
1537 goto error;
1538 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001539 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001540 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541 if (ret <= 0)
1542 goto error;
1543 }
1544 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001545 ret = e_snprintf(line, 32, ":%d", pp->line);
1546 if (ret <= 0)
1547 goto error;
1548 }
1549 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001550 tmp = pp->file;
1551 len = strlen(tmp);
1552 if (len > 30) {
1553 tmp = strchr(pp->file + len - 30, '/');
1554 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1555 }
1556 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001557 if (ret <= 0)
1558 goto error;
1559 }
1560
1561 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001562 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1563 offs, pp->retprobe ? "%return" : "", line,
1564 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001565 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001566 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001567 if (ret <= 0)
1568 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001569
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001570 return buf;
1571error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001572 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001573 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001575}
1576
1577#if 0
1578char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1579{
1580 char *buf;
1581 int i, len, ret;
1582
1583 buf = synthesize_perf_probe_point(&pev->point);
1584 if (!buf)
1585 return NULL;
1586
1587 len = strlen(buf);
1588 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001590 pev->args[i].name);
1591 if (ret <= 0) {
1592 free(buf);
1593 return NULL;
1594 }
1595 len += ret;
1596 }
1597
1598 return buf;
1599}
1600#endif
1601
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301602static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001603 char **buf, size_t *buflen,
1604 int depth)
1605{
1606 int ret;
1607 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301608 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001609 buflen, depth + 1);
1610 if (depth < 0)
1611 goto out;
1612 }
1613
1614 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1615 if (ret < 0)
1616 depth = ret;
1617 else {
1618 *buf += ret;
1619 *buflen -= ret;
1620 }
1621out:
1622 return depth;
1623
1624}
1625
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301626static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001627 char *buf, size_t buflen)
1628{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301629 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001630 int ret, depth = 0;
1631 char *tmp = buf;
1632
1633 /* Argument name or separator */
1634 if (arg->name)
1635 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1636 else
1637 ret = e_snprintf(buf, buflen, " ");
1638 if (ret < 0)
1639 return ret;
1640 buf += ret;
1641 buflen -= ret;
1642
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001643 /* Special case: @XXX */
1644 if (arg->value[0] == '@' && arg->ref)
1645 ref = ref->next;
1646
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001647 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001648 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301649 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001650 &buflen, 1);
1651 if (depth < 0)
1652 return depth;
1653 }
1654
1655 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001656 if (arg->value[0] == '@' && arg->ref)
1657 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1658 arg->ref->offset);
1659 else
1660 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 if (ret < 0)
1662 return ret;
1663 buf += ret;
1664 buflen -= ret;
1665
1666 /* Closing */
1667 while (depth--) {
1668 ret = e_snprintf(buf, buflen, ")");
1669 if (ret < 0)
1670 return ret;
1671 buf += ret;
1672 buflen -= ret;
1673 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001674 /* Print argument type */
1675 if (arg->type) {
1676 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1677 if (ret <= 0)
1678 return ret;
1679 buf += ret;
1680 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001681
1682 return buf - tmp;
1683}
1684
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301685char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001686{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301687 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001688 char *buf;
1689 int i, len, ret;
1690
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001691 buf = zalloc(MAX_CMDLEN);
1692 if (buf == NULL)
1693 return NULL;
1694
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001695 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1696 tev->group, tev->event);
1697 if (len <= 0)
1698 goto error;
1699
1700 /* Uprobes must have tp->address and tp->module */
1701 if (tev->uprobes && (!tp->address || !tp->module))
1702 goto error;
1703
1704 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301705 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001706 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1707 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301708 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001709 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301710 tp->module ?: "", tp->module ? ":" : "",
1711 tp->symbol, tp->offset);
1712
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001713 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001715 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716
1717 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301718 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001720 if (ret <= 0)
1721 goto error;
1722 len += ret;
1723 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001724
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001725 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001726error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727 free(buf);
1728 return NULL;
1729}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001730
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001731static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1732 struct perf_probe_point *pp,
1733 bool is_kprobe)
1734{
1735 struct symbol *sym = NULL;
1736 struct map *map;
1737 u64 addr;
1738 int ret = -ENOENT;
1739
1740 if (!is_kprobe) {
1741 map = dso__new_map(tp->module);
1742 if (!map)
1743 goto out;
1744 addr = tp->address;
1745 sym = map__find_symbol(map, addr, NULL);
1746 } else {
1747 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1748 if (addr) {
1749 addr += tp->offset;
1750 sym = __find_kernel_function(addr, &map);
1751 }
1752 }
1753 if (!sym)
1754 goto out;
1755
1756 pp->retprobe = tp->retprobe;
1757 pp->offset = addr - map->unmap_ip(map, sym->start);
1758 pp->function = strdup(sym->name);
1759 ret = pp->function ? 0 : -ENOMEM;
1760
1761out:
1762 if (map && !is_kprobe) {
1763 dso__delete(map->dso);
1764 map__delete(map);
1765 }
1766
1767 return ret;
1768}
1769
1770static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1771 struct perf_probe_point *pp,
1772 bool is_kprobe)
1773{
1774 char buf[128];
1775 int ret;
1776
1777 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1778 if (!ret)
1779 return 0;
1780 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1781 if (!ret)
1782 return 0;
1783
1784 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1785
1786 if (tp->symbol) {
1787 pp->function = strdup(tp->symbol);
1788 pp->offset = tp->offset;
1789 } else if (!tp->module && !is_kprobe) {
1790 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1791 if (ret < 0)
1792 return ret;
1793 pp->function = strdup(buf);
1794 pp->offset = 0;
1795 }
1796 if (pp->function == NULL)
1797 return -ENOMEM;
1798
1799 pp->retprobe = tp->retprobe;
1800
1801 return 0;
1802}
1803
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301804static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301805 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001807 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001808 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001809
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001810 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001811 pev->event = strdup(tev->event);
1812 pev->group = strdup(tev->group);
1813 if (pev->event == NULL || pev->group == NULL)
1814 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001815
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001816 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001817 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001818 if (ret < 0)
1819 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001820
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001821 /* Convert trace_arg to probe_arg */
1822 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001823 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1824 if (pev->args == NULL)
1825 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001826 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001828 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001829 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301830 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001831 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001832 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001833 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001834 if (pev->args[i].name == NULL && ret >= 0)
1835 ret = -ENOMEM;
1836 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001837
1838 if (ret < 0)
1839 clear_perf_probe_event(pev);
1840
1841 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001842}
1843
1844void clear_perf_probe_event(struct perf_probe_event *pev)
1845{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001846 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001847 int i;
1848
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001849 free(pev->event);
1850 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001851 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001852 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001853
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001854 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001855 free(pev->args[i].name);
1856 free(pev->args[i].var);
1857 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001858 field = pev->args[i].field;
1859 while (field) {
1860 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001861 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001862 free(field);
1863 field = next;
1864 }
1865 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001866 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001867 memset(pev, 0, sizeof(*pev));
1868}
1869
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301870static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001871{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301872 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873 int i;
1874
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001875 free(tev->event);
1876 free(tev->group);
1877 free(tev->point.symbol);
1878 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001880 free(tev->args[i].name);
1881 free(tev->args[i].value);
1882 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883 ref = tev->args[i].ref;
1884 while (ref) {
1885 next = ref->next;
1886 free(ref);
1887 ref = next;
1888 }
1889 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001890 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001891 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001892}
1893
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001894static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301895{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001896 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301897
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001898 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301899 const char *config;
1900
1901 if (!is_kprobe)
1902 config = "CONFIG_UPROBE_EVENTS";
1903 else
1904 config = "CONFIG_KPROBE_EVENTS";
1905
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001906 pr_warning("%cprobe_events file does not exist"
1907 " - please rebuild kernel with %s.\n",
1908 is_kprobe ? 'k' : 'u', config);
1909 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001910 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001911 else
1912 pr_warning("Failed to open %cprobe_events: %s\n",
1913 is_kprobe ? 'k' : 'u',
1914 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301915}
1916
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001917static void print_both_open_warning(int kerr, int uerr)
1918{
1919 /* Both kprobes and uprobes are disabled, warn it. */
1920 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001921 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001922 else if (kerr == -ENOENT && uerr == -ENOENT)
1923 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1924 "or/and CONFIG_UPROBE_EVENTS.\n");
1925 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001926 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001927 pr_warning("Failed to open kprobe events: %s.\n",
1928 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1929 pr_warning("Failed to open uprobe events: %s.\n",
1930 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1931 }
1932}
1933
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001934static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001935{
1936 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001937 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001938 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001939 int ret;
1940
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001941 __debugfs = tracefs_find_mountpoint();
1942 if (__debugfs == NULL) {
1943 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001944
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001945 __debugfs = debugfs_find_mountpoint();
1946 if (__debugfs == NULL)
1947 return -ENOTSUP;
1948 }
1949
1950 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1951 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001952 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001953 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001954 if (readwrite && !probe_event_dry_run)
1955 ret = open(buf, O_RDWR, O_APPEND);
1956 else
1957 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001958
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301959 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001960 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001961 }
1962 return ret;
1963}
1964
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301965static int open_kprobe_events(bool readwrite)
1966{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001967 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301968}
1969
1970static int open_uprobe_events(bool readwrite)
1971{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001972 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301973}
1974
1975/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301976static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001977{
1978 int ret, idx;
1979 FILE *fp;
1980 char buf[MAX_CMDLEN];
1981 char *p;
1982 struct strlist *sl;
1983
1984 sl = strlist__new(true, NULL);
1985
1986 fp = fdopen(dup(fd), "r");
1987 while (!feof(fp)) {
1988 p = fgets(buf, MAX_CMDLEN, fp);
1989 if (!p)
1990 break;
1991
1992 idx = strlen(p) - 1;
1993 if (p[idx] == '\n')
1994 p[idx] = '\0';
1995 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001996 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001997 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001998 strlist__delete(sl);
1999 return NULL;
2000 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002001 }
2002 fclose(fp);
2003
2004 return sl;
2005}
2006
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002007struct kprobe_blacklist_node {
2008 struct list_head list;
2009 unsigned long start;
2010 unsigned long end;
2011 char *symbol;
2012};
2013
2014static void kprobe_blacklist__delete(struct list_head *blacklist)
2015{
2016 struct kprobe_blacklist_node *node;
2017
2018 while (!list_empty(blacklist)) {
2019 node = list_first_entry(blacklist,
2020 struct kprobe_blacklist_node, list);
2021 list_del(&node->list);
2022 free(node->symbol);
2023 free(node);
2024 }
2025}
2026
2027static int kprobe_blacklist__load(struct list_head *blacklist)
2028{
2029 struct kprobe_blacklist_node *node;
2030 const char *__debugfs = debugfs_find_mountpoint();
2031 char buf[PATH_MAX], *p;
2032 FILE *fp;
2033 int ret;
2034
2035 if (__debugfs == NULL)
2036 return -ENOTSUP;
2037
2038 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2039 if (ret < 0)
2040 return ret;
2041
2042 fp = fopen(buf, "r");
2043 if (!fp)
2044 return -errno;
2045
2046 ret = 0;
2047 while (fgets(buf, PATH_MAX, fp)) {
2048 node = zalloc(sizeof(*node));
2049 if (!node) {
2050 ret = -ENOMEM;
2051 break;
2052 }
2053 INIT_LIST_HEAD(&node->list);
2054 list_add_tail(&node->list, blacklist);
2055 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2056 ret = -EINVAL;
2057 break;
2058 }
2059 p = strchr(buf, '\t');
2060 if (p) {
2061 p++;
2062 if (p[strlen(p) - 1] == '\n')
2063 p[strlen(p) - 1] = '\0';
2064 } else
2065 p = (char *)"unknown";
2066 node->symbol = strdup(p);
2067 if (!node->symbol) {
2068 ret = -ENOMEM;
2069 break;
2070 }
2071 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2072 node->start, node->end, node->symbol);
2073 ret++;
2074 }
2075 if (ret < 0)
2076 kprobe_blacklist__delete(blacklist);
2077 fclose(fp);
2078
2079 return ret;
2080}
2081
2082static struct kprobe_blacklist_node *
2083kprobe_blacklist__find_by_address(struct list_head *blacklist,
2084 unsigned long address)
2085{
2086 struct kprobe_blacklist_node *node;
2087
2088 list_for_each_entry(node, blacklist, list) {
2089 if (node->start <= address && address <= node->end)
2090 return node;
2091 }
2092
2093 return NULL;
2094}
2095
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002096/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002097static int show_perf_probe_event(struct perf_probe_event *pev,
2098 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002099{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002100 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002101 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002102 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002103
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002104 /* Synthesize only event probe point */
2105 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002106 if (!place)
2107 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002108
2109 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002110 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002111 return ret;
2112
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002113 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002114 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002115 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002116
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002117 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002118 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002119 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002120 ret = synthesize_perf_probe_arg(&pev->args[i],
2121 buf, 128);
2122 if (ret < 0)
2123 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002124 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002125 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002126 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002127 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002128 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002129 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002130}
2131
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302132static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002133{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302134 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302135 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002136 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002137 struct strlist *rawlist;
2138 struct str_node *ent;
2139
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002140 memset(&tev, 0, sizeof(tev));
2141 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002142
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302143 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002144 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002145 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002146
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002147 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302148 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002149 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302150 ret = convert_to_perf_probe_event(&tev, &pev,
2151 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002152 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002153 ret = show_perf_probe_event(&pev,
2154 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002155 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002156 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302157 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002158 if (ret < 0)
2159 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002160 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002161 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162
2163 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002164}
2165
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302166/* List up current perf-probe events */
2167int show_perf_probe_events(void)
2168{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002169 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302170
2171 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302172
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002173 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302174 if (ret < 0)
2175 return ret;
2176
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002177 kp_fd = open_kprobe_events(false);
2178 if (kp_fd >= 0) {
2179 ret = __show_perf_probe_events(kp_fd, true);
2180 close(kp_fd);
2181 if (ret < 0)
2182 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302183 }
2184
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002185 up_fd = open_uprobe_events(false);
2186 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002187 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002188 ret = kp_fd;
2189 goto out;
2190 }
2191
2192 if (up_fd >= 0) {
2193 ret = __show_perf_probe_events(up_fd, false);
2194 close(up_fd);
2195 }
2196out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002197 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302198 return ret;
2199}
2200
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002201/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302202static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002203{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002204 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002205 struct strlist *sl, *rawlist;
2206 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302207 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002208 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002209
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002210 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302211 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002212 if (!rawlist)
2213 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002214 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002215 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302216 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002217 if (ret < 0)
2218 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002219 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002220 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2221 tev.event);
2222 if (ret >= 0)
2223 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002224 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002225 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302226 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002227 if (ret < 0)
2228 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002229 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002230 strlist__delete(rawlist);
2231
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002232 if (ret < 0) {
2233 strlist__delete(sl);
2234 return NULL;
2235 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002236 return sl;
2237}
2238
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302239static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002240{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002241 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302242 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002243 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002245 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302246 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002247 return -EINVAL;
2248 }
2249
Masami Hiramatsufa282442009-12-08 17:03:23 -05002250 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002251 if (!probe_event_dry_run) {
2252 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002253 if (ret <= 0) {
2254 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002255 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002256 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002257 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002258 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002259 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002260 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002261}
2262
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002263static int get_new_event_name(char *buf, size_t len, const char *base,
2264 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002265{
2266 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002267
2268 /* Try no suffix */
2269 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002270 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002271 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 return ret;
2273 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002274 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002275 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002276
Masami Hiramatsud761b082009-12-15 10:32:25 -05002277 if (!allow_suffix) {
2278 pr_warning("Error: event \"%s\" already exists. "
2279 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002280 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002281 }
2282
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002283 /* Try to add suffix */
2284 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002285 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002286 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002287 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288 return ret;
2289 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002290 if (!strlist__has_entry(namelist, buf))
2291 break;
2292 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002293 if (i == MAX_EVENT_INDEX) {
2294 pr_warning("Too many events are on the same function.\n");
2295 ret = -ERANGE;
2296 }
2297
2298 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002299}
2300
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002301/* Warn if the current kernel's uprobe implementation is old */
2302static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2303{
2304 int i;
2305 char *buf = synthesize_probe_trace_command(tev);
2306
2307 /* Old uprobe event doesn't support memory dereference */
2308 if (!tev->uprobes || tev->nargs == 0 || !buf)
2309 goto out;
2310
2311 for (i = 0; i < tev->nargs; i++)
2312 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2313 pr_warning("Please upgrade your kernel to at least "
2314 "3.14 to have access to feature %s\n",
2315 tev->args[i].value);
2316 break;
2317 }
2318out:
2319 free(buf);
2320}
2321
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302322static int __add_probe_trace_events(struct perf_probe_event *pev,
2323 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002324 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002325{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302327 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002328 char buf[64];
2329 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002330 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002331 LIST_HEAD(blacklist);
2332 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002333
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302334 if (pev->uprobes)
2335 fd = open_uprobe_events(true);
2336 else
2337 fd = open_kprobe_events(true);
2338
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002339 if (fd < 0) {
2340 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002341 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002342 }
2343
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002344 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302345 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002346 if (!namelist) {
2347 pr_debug("Failed to get current event list.\n");
2348 return -EIO;
2349 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002350 /* Get kprobe blacklist if exists */
2351 if (!pev->uprobes) {
2352 ret = kprobe_blacklist__load(&blacklist);
2353 if (ret < 0)
2354 pr_debug("No kprobe blacklist support, ignored\n");
2355 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002356
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002357 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002358 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002359 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002360 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002361 /* Ensure that the address is NOT blacklisted */
2362 node = kprobe_blacklist__find_by_address(&blacklist,
2363 tev->point.address);
2364 if (node) {
2365 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2366 continue;
2367 }
2368
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002369 if (pev->event)
2370 event = pev->event;
2371 else
2372 if (pev->point.function)
2373 event = pev->point.function;
2374 else
2375 event = tev->point.symbol;
2376 if (pev->group)
2377 group = pev->group;
2378 else
2379 group = PERFPROBE_GROUP;
2380
2381 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002382 ret = get_new_event_name(buf, 64, event,
2383 namelist, allow_suffix);
2384 if (ret < 0)
2385 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002386 event = buf;
2387
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002388 tev->event = strdup(event);
2389 tev->group = strdup(group);
2390 if (tev->event == NULL || tev->group == NULL) {
2391 ret = -ENOMEM;
2392 break;
2393 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302394 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002395 if (ret < 0)
2396 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002397 /* Add added event name to namelist */
2398 strlist__add(namelist, event);
2399
2400 /* Trick here - save current event/group */
2401 event = pev->event;
2402 group = pev->group;
2403 pev->event = tev->event;
2404 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002405 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002406 /* Trick here - restore current event/group */
2407 pev->event = (char *)event;
2408 pev->group = (char *)group;
2409
2410 /*
2411 * Probes after the first probe which comes from same
2412 * user input are always allowed to add suffix, because
2413 * there might be several addresses corresponding to
2414 * one code line.
2415 */
2416 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002417 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002418 if (ret == -EINVAL && pev->uprobes)
2419 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002420
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002421 /* Note that it is possible to skip all events because of blacklist */
2422 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002423 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002424 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2425 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002426 tev->event);
2427 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002428
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002429 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002430 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002431 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002432 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002433}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002434
Namhyung Kim564c62a2015-01-14 20:18:07 +09002435static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002436{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002437 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002438 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002439
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002440 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002441 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002442 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002443
2444 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002445}
2446
2447#define strdup_or_goto(str, label) \
2448 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2449
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302450void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2451 struct probe_trace_event *tev __maybe_unused,
2452 struct map *map __maybe_unused) { }
2453
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002454/*
2455 * Find probe function addresses from map.
2456 * Return an error or the number of found probe_trace_event
2457 */
2458static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2459 struct probe_trace_event **tevs,
2460 int max_tevs, const char *target)
2461{
2462 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002463 struct ref_reloc_sym *reloc_sym = NULL;
2464 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002465 struct probe_trace_event *tev;
2466 struct perf_probe_point *pp = &pev->point;
2467 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002468 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002469 int ret, i;
2470
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002471 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002472 if (!map) {
2473 ret = -EINVAL;
2474 goto out;
2475 }
2476
2477 /*
2478 * Load matched symbols: Since the different local symbols may have
2479 * same name but different addresses, this lists all the symbols.
2480 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002481 num_matched_functions = find_probe_functions(map, pp->function);
2482 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002483 pr_err("Failed to find symbol %s in %s\n", pp->function,
2484 target ? : "kernel");
2485 ret = -ENOENT;
2486 goto out;
2487 } else if (num_matched_functions > max_tevs) {
2488 pr_err("Too many functions matched in %s\n",
2489 target ? : "kernel");
2490 ret = -E2BIG;
2491 goto out;
2492 }
2493
Namhyung Kim25dd9172015-01-14 20:18:08 +09002494 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002495 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002496 if (!reloc_sym) {
2497 pr_warning("Relocated base symbol is not found!\n");
2498 ret = -EINVAL;
2499 goto out;
2500 }
2501 }
2502
2503 /* Setup result trace-probe-events */
2504 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2505 if (!*tevs) {
2506 ret = -ENOMEM;
2507 goto out;
2508 }
2509
2510 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002511
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002512 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002513 tev = (*tevs) + ret;
2514 tp = &tev->point;
2515 if (ret == num_matched_functions) {
2516 pr_warning("Too many symbols are listed. Skip it.\n");
2517 break;
2518 }
2519 ret++;
2520
2521 if (pp->offset > sym->end - sym->start) {
2522 pr_warning("Offset %ld is bigger than the size of %s\n",
2523 pp->offset, sym->name);
2524 ret = -ENOENT;
2525 goto err_out;
2526 }
2527 /* Add one probe point */
2528 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2529 if (reloc_sym) {
2530 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2531 tp->offset = tp->address - reloc_sym->addr;
2532 } else {
2533 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2534 tp->offset = pp->offset;
2535 }
2536 tp->retprobe = pp->retprobe;
2537 if (target)
2538 tev->point.module = strdup_or_goto(target, nomem_out);
2539 tev->uprobes = pev->uprobes;
2540 tev->nargs = pev->nargs;
2541 if (tev->nargs) {
2542 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2543 tev->nargs);
2544 if (tev->args == NULL)
2545 goto nomem_out;
2546 }
2547 for (i = 0; i < tev->nargs; i++) {
2548 if (pev->args[i].name)
2549 tev->args[i].name =
2550 strdup_or_goto(pev->args[i].name,
2551 nomem_out);
2552
2553 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2554 nomem_out);
2555 if (pev->args[i].type)
2556 tev->args[i].type =
2557 strdup_or_goto(pev->args[i].type,
2558 nomem_out);
2559 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302560 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002561 }
2562
2563out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002564 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002565 return ret;
2566
2567nomem_out:
2568 ret = -ENOMEM;
2569err_out:
2570 clear_probe_trace_events(*tevs, num_matched_functions);
2571 zfree(tevs);
2572 goto out;
2573}
2574
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302575bool __weak arch__prefers_symtab(void) { return false; }
2576
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302577static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2578 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302579 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002580{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002581 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002582
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002583 if (pev->uprobes && !pev->group) {
2584 /* Replace group name if not given */
2585 ret = convert_exec_to_group(target, &pev->group);
2586 if (ret != 0) {
2587 pr_warning("Failed to make a group name.\n");
2588 return ret;
2589 }
2590 }
2591
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302592 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2593 ret = find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2594 if (ret > 0)
2595 return ret; /* Found in symbol table */
2596 }
2597
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002598 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302599 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002600 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002601 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002602
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002603 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002604}
2605
2606struct __event_package {
2607 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302608 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002609 int ntevs;
2610};
2611
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002612int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002613 int max_tevs, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002614{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002615 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002616 struct __event_package *pkgs;
2617
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302618 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002619 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302620
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002621 if (pkgs == NULL)
2622 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002623
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002624 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002625 if (ret < 0) {
2626 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002627 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002628 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002629
2630 /* Loop 1: convert all events */
2631 for (i = 0; i < npevs; i++) {
2632 pkgs[i].pev = &pevs[i];
2633 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302634 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002635 &pkgs[i].tevs,
2636 max_tevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002637 pkgs[i].pev->target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002638 if (ret < 0)
2639 goto end;
2640 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002641 }
2642
2643 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002644 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302645 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002646 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002647 if (ret < 0)
2648 break;
2649 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002650end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002651 /* Loop 3: cleanup and free trace events */
2652 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002653 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302654 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002655 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002656 }
2657 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002658 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002659
2660 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002661}
2662
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302663static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002664{
2665 char *p;
2666 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002667 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002668
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302669 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002670 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2671 if (ret < 0)
2672 goto error;
2673
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002674 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002675 if (!p) {
2676 pr_debug("Internal error: %s should have ':' but not.\n",
2677 ent->s);
2678 ret = -ENOTSUP;
2679 goto error;
2680 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002681 *p = '/';
2682
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002683 pr_debug("Writing event: %s\n", buf);
2684 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002685 if (ret < 0) {
2686 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002687 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002688 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002690 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002691 return 0;
2692error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002693 pr_warning("Failed to delete event: %s\n",
2694 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002695 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002696}
2697
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302698static int del_trace_probe_event(int fd, const char *buf,
2699 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002700{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002701 struct str_node *ent, *n;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002702 int ret = -ENOENT;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002703
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002704 if (strpbrk(buf, "*?")) { /* Glob-exp */
2705 strlist__for_each_safe(ent, n, namelist)
2706 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302707 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002708 if (ret < 0)
2709 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002710 strlist__remove(namelist, ent);
2711 }
2712 } else {
2713 ent = strlist__find(namelist, buf);
2714 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302715 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002716 if (ret >= 0)
2717 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002718 }
2719 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002720
2721 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002722}
2723
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002724int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002725{
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002726 int ret = -1, ret2, ufd = -1, kfd = -1;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302727 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002728 const char *group, *event;
2729 char *p, *str;
2730 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302731 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002732
Masami Hiramatsufa282442009-12-08 17:03:23 -05002733 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302734 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002735 if (kfd >= 0)
2736 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302737
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302738 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002739 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302740 unamelist = get_probe_trace_event_names(ufd, true);
2741
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002742 if (kfd < 0 && ufd < 0) {
2743 print_both_open_warning(kfd, ufd);
2744 goto error;
2745 }
2746
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002747 if (namelist == NULL && unamelist == NULL) {
2748 ret = -ENOENT;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302749 goto error;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002750 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05002751
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002752 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002753 str = strdup(ent->s);
2754 if (str == NULL) {
2755 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302756 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002757 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002758 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002759 p = strchr(str, ':');
2760 if (p) {
2761 group = str;
2762 *p = '\0';
2763 event = p + 1;
2764 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002765 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002766 event = str;
2767 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302768
2769 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2770 if (ret < 0) {
2771 pr_err("Failed to copy event.");
2772 free(str);
2773 goto error;
2774 }
2775
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002776 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002777 free(str);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302778
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002779 ret = ret2 = -ENOENT;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302780 if (namelist)
2781 ret = del_trace_probe_event(kfd, buf, namelist);
2782
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002783 if ((ret >= 0 || ret == -ENOENT) && unamelist)
2784 ret2 = del_trace_probe_event(ufd, buf, unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002786 /* Since we can remove probes which already removed, don't check it */
2787 if (ret == -ENOENT && ret2 == -ENOENT)
2788 pr_debug("Event \"%s\" does not exist.\n", buf);
2789 else if (ret < 0 || ret2 < 0) {
2790 if (ret >= 0)
2791 ret = ret2;
2792 break;
2793 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05002794 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302795
2796error:
2797 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302798 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302799 close(kfd);
2800 }
2801
2802 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302803 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302804 close(ufd);
2805 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002806
2807 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002808}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302809
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002810/* TODO: don't use a global variable for filter ... */
2811static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002812
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002813/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002814 * If a symbol corresponds to a function with global binding and
2815 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002816 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002817static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002818 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002819{
Namhyung Kime578da32015-03-06 16:31:29 +09002820 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002821 return 0;
2822 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002823}
2824
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002825int show_available_funcs(const char *target, struct strfilter *_filter,
2826 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002827{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002828 struct map *map;
2829 int ret;
2830
2831 ret = init_symbol_maps(user);
2832 if (ret < 0)
2833 return ret;
2834
2835 /* Get a symbol map */
2836 if (user)
2837 map = dso__new_map(target);
2838 else
2839 map = kernel_get_module_map(target);
2840 if (!map) {
2841 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002842 return -EINVAL;
2843 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002844
2845 /* Load symbols with given filter */
2846 available_func_filter = _filter;
2847 if (map__load(map, filter_available_functions)) {
2848 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2849 goto end;
2850 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002851 if (!dso__sorted_by_name(map->dso, map->type))
2852 dso__sort_by_name(map->dso, map->type);
2853
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002854 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302855 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002856 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2857end:
2858 if (user) {
2859 dso__delete(map->dso);
2860 map__delete(map);
2861 }
2862 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302863
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002864 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302865}
2866