blob: d05b77cf35f77051354b9d08acc035cf4575dd5b [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 */
Wang Nane59d29e2015-04-28 08:46:09 +00001087 if (!arg)
1088 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001089
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001090 ptr = strpbrk(arg, ";=@+%");
1091 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001092 *ptr = '\0';
1093 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001094 if (strchr(arg, ':')) {
1095 semantic_error("Group name is not supported yet.\n");
1096 return -ENOTSUP;
1097 }
1098 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001099 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 "follow C symbol-naming rule.\n", arg);
1101 return -EINVAL;
1102 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001103 pev->event = strdup(arg);
1104 if (pev->event == NULL)
1105 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001106 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001107 arg = tmp;
1108 }
1109
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001110 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001111 if (ptr) {
1112 nc = *ptr;
1113 *ptr++ = '\0';
1114 }
1115
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001116 tmp = strdup(arg);
1117 if (tmp == NULL)
1118 return -ENOMEM;
1119
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001120 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001121 if (strchr(tmp, '.')) /* File */
1122 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001123 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001124 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001125
1126 /* Parse other options */
1127 while (ptr) {
1128 arg = ptr;
1129 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001130 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001131 pp->lazy_line = strdup(arg);
1132 if (pp->lazy_line == NULL)
1133 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001134 break;
1135 }
1136 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001137 if (ptr) {
1138 nc = *ptr;
1139 *ptr++ = '\0';
1140 }
1141 switch (c) {
1142 case ':': /* Line number */
1143 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001145 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001146 " in line number.\n");
1147 return -EINVAL;
1148 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001149 break;
1150 case '+': /* Byte offset from a symbol */
1151 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001152 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001153 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001154 " in offset.\n");
1155 return -EINVAL;
1156 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157 break;
1158 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 if (pp->file) {
1160 semantic_error("SRC@SRC is not allowed.\n");
1161 return -EINVAL;
1162 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001163 pp->file = strdup(arg);
1164 if (pp->file == NULL)
1165 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001166 break;
1167 case '%': /* Probe places */
1168 if (strcmp(arg, "return") == 0) {
1169 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 } else { /* Others not supported yet */
1171 semantic_error("%%%s is not supported.\n", arg);
1172 return -ENOTSUP;
1173 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001174 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 default: /* Buggy case */
1176 pr_err("This program has a bug at %s:%d.\n",
1177 __FILE__, __LINE__);
1178 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001179 break;
1180 }
1181 }
1182
1183 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001184 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001185 semantic_error("Lazy pattern can't be used with"
1186 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 return -EINVAL;
1188 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001189
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001191 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 return -EINVAL;
1193 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001194
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001196 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 return -EINVAL;
1198 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001199
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001200 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001201 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001202 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001203 return -EINVAL;
1204 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001207 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001208 return -EINVAL;
1209 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001212 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001213 return -EINVAL;
1214 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001215
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001216 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001217 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001218 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 return -EINVAL;
1220 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001221
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001223 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1224 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001226}
1227
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001228/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001230{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001231 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001232 struct perf_probe_arg_field **fieldp;
1233
1234 pr_debug("parsing arg: %s into ", str);
1235
Masami Hiramatsu48481932010-04-12 13:16:53 -04001236 tmp = strchr(str, '=');
1237 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001238 arg->name = strndup(str, tmp - str);
1239 if (arg->name == NULL)
1240 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001241 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001242 str = tmp + 1;
1243 }
1244
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001245 tmp = strchr(str, ':');
1246 if (tmp) { /* Type setting */
1247 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001248 arg->type = strdup(tmp + 1);
1249 if (arg->type == NULL)
1250 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001251 pr_debug("type:%s ", arg->type);
1252 }
1253
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001254 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001255 if (!is_c_varname(str) || !tmp) {
1256 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001257 arg->var = strdup(str);
1258 if (arg->var == NULL)
1259 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001260 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001262 }
1263
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001264 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001265 arg->var = strndup(str, tmp - str);
1266 if (arg->var == NULL)
1267 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001268 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001269 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001270 fieldp = &arg->field;
1271
1272 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001273 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1274 if (*fieldp == NULL)
1275 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001276 if (*tmp == '[') { /* Array */
1277 str = tmp;
1278 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001279 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001280 if (*tmp != ']' || tmp == str + 1) {
1281 semantic_error("Array index must be a"
1282 " number.\n");
1283 return -EINVAL;
1284 }
1285 tmp++;
1286 if (*tmp == '\0')
1287 tmp = NULL;
1288 } else { /* Structure */
1289 if (*tmp == '.') {
1290 str = tmp + 1;
1291 (*fieldp)->ref = false;
1292 } else if (tmp[1] == '>') {
1293 str = tmp + 2;
1294 (*fieldp)->ref = true;
1295 } else {
1296 semantic_error("Argument parse error: %s\n",
1297 str);
1298 return -EINVAL;
1299 }
1300 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001301 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001302 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001303 (*fieldp)->name = strndup(str, tmp - str);
1304 if ((*fieldp)->name == NULL)
1305 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001306 if (*str != '[')
1307 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001308 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1309 fieldp = &(*fieldp)->next;
1310 }
1311 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001312 (*fieldp)->name = strdup(str);
1313 if ((*fieldp)->name == NULL)
1314 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001315 if (*str != '[')
1316 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001317 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001318
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001319 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001321 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001322 if (arg->name == NULL)
1323 return -ENOMEM;
1324 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001325 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001326}
1327
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001328/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001329int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001330{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001331 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001333
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001334 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001335 if (!argv) {
1336 pr_debug("Failed to split arguments.\n");
1337 return -ENOMEM;
1338 }
1339 if (argc - 1 > MAX_PROBE_ARGS) {
1340 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1341 ret = -ERANGE;
1342 goto out;
1343 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001344 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001345 ret = parse_perf_probe_point(argv[0], pev);
1346 if (ret < 0)
1347 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001348
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001349 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001350 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001351 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1352 if (pev->args == NULL) {
1353 ret = -ENOMEM;
1354 goto out;
1355 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001356 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1357 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1358 if (ret >= 0 &&
1359 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001360 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001361 " kretprobe.\n");
1362 ret = -EINVAL;
1363 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001364 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001366 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001367
1368 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001369}
1370
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001371/* Return true if this perf_probe_event requires debuginfo */
1372bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001373{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001374 int i;
1375
1376 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1377 return true;
1378
1379 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001380 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001381 return true;
1382
1383 return false;
1384}
1385
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301386/* Parse probe_events event into struct probe_point */
1387static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001388 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001389{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301390 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001391 char pr;
1392 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001393 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001394 int ret, i, argc;
1395 char **argv;
1396
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301397 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399 if (!argv) {
1400 pr_debug("Failed to split arguments.\n");
1401 return -ENOMEM;
1402 }
1403 if (argc < 2) {
1404 semantic_error("Too few probe arguments.\n");
1405 ret = -ERANGE;
1406 goto out;
1407 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001408
1409 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001410 argv0_str = strdup(argv[0]);
1411 if (argv0_str == NULL) {
1412 ret = -ENOMEM;
1413 goto out;
1414 }
1415 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1416 fmt2_str = strtok_r(NULL, "/", &fmt);
1417 fmt3_str = strtok_r(NULL, " \t", &fmt);
1418 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1419 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420 semantic_error("Failed to parse event name: %s\n", argv[0]);
1421 ret = -EINVAL;
1422 goto out;
1423 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001424 pr = fmt1_str[0];
1425 tev->group = strdup(fmt2_str);
1426 tev->event = strdup(fmt3_str);
1427 if (tev->group == NULL || tev->event == NULL) {
1428 ret = -ENOMEM;
1429 goto out;
1430 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001431 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001432
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001433 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001434
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001435 /* Scan module name(if there), function name and offset */
1436 p = strchr(argv[1], ':');
1437 if (p) {
1438 tp->module = strndup(argv[1], p - argv[1]);
1439 p++;
1440 } else
1441 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001442 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001443 if (fmt1_str[0] == '0') /* only the address started with 0x */
1444 tp->address = strtoul(fmt1_str, NULL, 0);
1445 else {
1446 /* Only the symbol-based probe has offset */
1447 tp->symbol = strdup(fmt1_str);
1448 if (tp->symbol == NULL) {
1449 ret = -ENOMEM;
1450 goto out;
1451 }
1452 fmt2_str = strtok_r(NULL, "", &fmt);
1453 if (fmt2_str == NULL)
1454 tp->offset = 0;
1455 else
1456 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001457 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001458
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001459 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301460 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001461 if (tev->args == NULL) {
1462 ret = -ENOMEM;
1463 goto out;
1464 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001465 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001466 p = strchr(argv[i + 2], '=');
1467 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001468 *p++ = '\0';
1469 else
1470 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001471 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001473 tev->args[i].value = strdup(p);
1474 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1475 ret = -ENOMEM;
1476 goto out;
1477 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001479 ret = 0;
1480out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001481 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001483 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001484}
1485
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001486/* Compose only probe arg */
1487int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1488{
1489 struct perf_probe_arg_field *field = pa->field;
1490 int ret;
1491 char *tmp = buf;
1492
Masami Hiramatsu48481932010-04-12 13:16:53 -04001493 if (pa->name && pa->var)
1494 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1495 else
1496 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001497 if (ret <= 0)
1498 goto error;
1499 tmp += ret;
1500 len -= ret;
1501
1502 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001503 if (field->name[0] == '[')
1504 ret = e_snprintf(tmp, len, "%s", field->name);
1505 else
1506 ret = e_snprintf(tmp, len, "%s%s",
1507 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001508 if (ret <= 0)
1509 goto error;
1510 tmp += ret;
1511 len -= ret;
1512 field = field->next;
1513 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001514
1515 if (pa->type) {
1516 ret = e_snprintf(tmp, len, ":%s", pa->type);
1517 if (ret <= 0)
1518 goto error;
1519 tmp += ret;
1520 len -= ret;
1521 }
1522
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001523 return tmp - buf;
1524error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001525 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001526 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001527}
1528
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001529/* Compose only probe point (not argument) */
1530static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001531{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001532 char *buf, *tmp;
1533 char offs[32] = "", line[32] = "", file[32] = "";
1534 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001535
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001536 buf = zalloc(MAX_CMDLEN);
1537 if (buf == NULL) {
1538 ret = -ENOMEM;
1539 goto error;
1540 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001542 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001543 if (ret <= 0)
1544 goto error;
1545 }
1546 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001547 ret = e_snprintf(line, 32, ":%d", pp->line);
1548 if (ret <= 0)
1549 goto error;
1550 }
1551 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001552 tmp = pp->file;
1553 len = strlen(tmp);
1554 if (len > 30) {
1555 tmp = strchr(pp->file + len - 30, '/');
1556 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1557 }
1558 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001559 if (ret <= 0)
1560 goto error;
1561 }
1562
1563 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001564 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1565 offs, pp->retprobe ? "%return" : "", line,
1566 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001567 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001568 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001569 if (ret <= 0)
1570 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001571
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001572 return buf;
1573error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001574 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001575 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577}
1578
1579#if 0
1580char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1581{
1582 char *buf;
1583 int i, len, ret;
1584
1585 buf = synthesize_perf_probe_point(&pev->point);
1586 if (!buf)
1587 return NULL;
1588
1589 len = strlen(buf);
1590 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001591 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001592 pev->args[i].name);
1593 if (ret <= 0) {
1594 free(buf);
1595 return NULL;
1596 }
1597 len += ret;
1598 }
1599
1600 return buf;
1601}
1602#endif
1603
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301604static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605 char **buf, size_t *buflen,
1606 int depth)
1607{
1608 int ret;
1609 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301610 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001611 buflen, depth + 1);
1612 if (depth < 0)
1613 goto out;
1614 }
1615
1616 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1617 if (ret < 0)
1618 depth = ret;
1619 else {
1620 *buf += ret;
1621 *buflen -= ret;
1622 }
1623out:
1624 return depth;
1625
1626}
1627
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301628static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001629 char *buf, size_t buflen)
1630{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301631 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632 int ret, depth = 0;
1633 char *tmp = buf;
1634
1635 /* Argument name or separator */
1636 if (arg->name)
1637 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1638 else
1639 ret = e_snprintf(buf, buflen, " ");
1640 if (ret < 0)
1641 return ret;
1642 buf += ret;
1643 buflen -= ret;
1644
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001645 /* Special case: @XXX */
1646 if (arg->value[0] == '@' && arg->ref)
1647 ref = ref->next;
1648
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001650 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 &buflen, 1);
1653 if (depth < 0)
1654 return depth;
1655 }
1656
1657 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001658 if (arg->value[0] == '@' && arg->ref)
1659 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1660 arg->ref->offset);
1661 else
1662 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001663 if (ret < 0)
1664 return ret;
1665 buf += ret;
1666 buflen -= ret;
1667
1668 /* Closing */
1669 while (depth--) {
1670 ret = e_snprintf(buf, buflen, ")");
1671 if (ret < 0)
1672 return ret;
1673 buf += ret;
1674 buflen -= ret;
1675 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001676 /* Print argument type */
1677 if (arg->type) {
1678 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1679 if (ret <= 0)
1680 return ret;
1681 buf += ret;
1682 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001683
1684 return buf - tmp;
1685}
1686
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301687char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001688{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301689 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001690 char *buf;
1691 int i, len, ret;
1692
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001693 buf = zalloc(MAX_CMDLEN);
1694 if (buf == NULL)
1695 return NULL;
1696
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001697 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1698 tev->group, tev->event);
1699 if (len <= 0)
1700 goto error;
1701
1702 /* Uprobes must have tp->address and tp->module */
1703 if (tev->uprobes && (!tp->address || !tp->module))
1704 goto error;
1705
1706 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301707 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001708 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1709 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301710 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001711 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301712 tp->module ?: "", tp->module ? ":" : "",
1713 tp->symbol, tp->offset);
1714
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001715 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001717 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718
1719 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301720 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001721 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001722 if (ret <= 0)
1723 goto error;
1724 len += ret;
1725 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001726
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001728error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001729 free(buf);
1730 return NULL;
1731}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001732
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001733static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1734 struct perf_probe_point *pp,
1735 bool is_kprobe)
1736{
1737 struct symbol *sym = NULL;
1738 struct map *map;
1739 u64 addr;
1740 int ret = -ENOENT;
1741
1742 if (!is_kprobe) {
1743 map = dso__new_map(tp->module);
1744 if (!map)
1745 goto out;
1746 addr = tp->address;
1747 sym = map__find_symbol(map, addr, NULL);
1748 } else {
1749 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1750 if (addr) {
1751 addr += tp->offset;
1752 sym = __find_kernel_function(addr, &map);
1753 }
1754 }
1755 if (!sym)
1756 goto out;
1757
1758 pp->retprobe = tp->retprobe;
1759 pp->offset = addr - map->unmap_ip(map, sym->start);
1760 pp->function = strdup(sym->name);
1761 ret = pp->function ? 0 : -ENOMEM;
1762
1763out:
1764 if (map && !is_kprobe) {
1765 dso__delete(map->dso);
1766 map__delete(map);
1767 }
1768
1769 return ret;
1770}
1771
1772static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1773 struct perf_probe_point *pp,
1774 bool is_kprobe)
1775{
1776 char buf[128];
1777 int ret;
1778
1779 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1780 if (!ret)
1781 return 0;
1782 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1783 if (!ret)
1784 return 0;
1785
1786 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1787
1788 if (tp->symbol) {
1789 pp->function = strdup(tp->symbol);
1790 pp->offset = tp->offset;
1791 } else if (!tp->module && !is_kprobe) {
1792 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1793 if (ret < 0)
1794 return ret;
1795 pp->function = strdup(buf);
1796 pp->offset = 0;
1797 }
1798 if (pp->function == NULL)
1799 return -ENOMEM;
1800
1801 pp->retprobe = tp->retprobe;
1802
1803 return 0;
1804}
1805
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301806static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301807 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001808{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001809 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001810 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001811
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001812 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001813 pev->event = strdup(tev->event);
1814 pev->group = strdup(tev->group);
1815 if (pev->event == NULL || pev->group == NULL)
1816 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001817
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001818 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001819 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001820 if (ret < 0)
1821 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001822
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001823 /* Convert trace_arg to probe_arg */
1824 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001825 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1826 if (pev->args == NULL)
1827 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001828 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001829 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001830 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301832 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001833 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001834 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001835 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 if (pev->args[i].name == NULL && ret >= 0)
1837 ret = -ENOMEM;
1838 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001839
1840 if (ret < 0)
1841 clear_perf_probe_event(pev);
1842
1843 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001844}
1845
1846void clear_perf_probe_event(struct perf_probe_event *pev)
1847{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001848 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001849 int i;
1850
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001851 free(pev->event);
1852 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001853 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001854 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001855
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001856 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001857 free(pev->args[i].name);
1858 free(pev->args[i].var);
1859 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001860 field = pev->args[i].field;
1861 while (field) {
1862 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001863 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001864 free(field);
1865 field = next;
1866 }
1867 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001868 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001869 memset(pev, 0, sizeof(*pev));
1870}
1871
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301872static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301874 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001875 int i;
1876
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001877 free(tev->event);
1878 free(tev->group);
1879 free(tev->point.symbol);
1880 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001882 free(tev->args[i].name);
1883 free(tev->args[i].value);
1884 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001885 ref = tev->args[i].ref;
1886 while (ref) {
1887 next = ref->next;
1888 free(ref);
1889 ref = next;
1890 }
1891 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001892 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001894}
1895
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001896static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301897{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001898 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301899
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001900 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301901 const char *config;
1902
1903 if (!is_kprobe)
1904 config = "CONFIG_UPROBE_EVENTS";
1905 else
1906 config = "CONFIG_KPROBE_EVENTS";
1907
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001908 pr_warning("%cprobe_events file does not exist"
1909 " - please rebuild kernel with %s.\n",
1910 is_kprobe ? 'k' : 'u', config);
1911 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001912 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001913 else
1914 pr_warning("Failed to open %cprobe_events: %s\n",
1915 is_kprobe ? 'k' : 'u',
1916 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301917}
1918
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001919static void print_both_open_warning(int kerr, int uerr)
1920{
1921 /* Both kprobes and uprobes are disabled, warn it. */
1922 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001923 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001924 else if (kerr == -ENOENT && uerr == -ENOENT)
1925 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1926 "or/and CONFIG_UPROBE_EVENTS.\n");
1927 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001928 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001929 pr_warning("Failed to open kprobe events: %s.\n",
1930 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1931 pr_warning("Failed to open uprobe events: %s.\n",
1932 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1933 }
1934}
1935
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001936static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001937{
1938 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001939 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001940 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001941 int ret;
1942
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001943 __debugfs = tracefs_find_mountpoint();
1944 if (__debugfs == NULL) {
1945 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001946
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001947 __debugfs = debugfs_find_mountpoint();
1948 if (__debugfs == NULL)
1949 return -ENOTSUP;
1950 }
1951
1952 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1953 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001954 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001955 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001956 if (readwrite && !probe_event_dry_run)
1957 ret = open(buf, O_RDWR, O_APPEND);
1958 else
1959 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001960
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301961 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001962 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001963 }
1964 return ret;
1965}
1966
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301967static int open_kprobe_events(bool readwrite)
1968{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001969 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301970}
1971
1972static int open_uprobe_events(bool readwrite)
1973{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001974 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301975}
1976
1977/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301978static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001979{
1980 int ret, idx;
1981 FILE *fp;
1982 char buf[MAX_CMDLEN];
1983 char *p;
1984 struct strlist *sl;
1985
1986 sl = strlist__new(true, NULL);
1987
1988 fp = fdopen(dup(fd), "r");
1989 while (!feof(fp)) {
1990 p = fgets(buf, MAX_CMDLEN, fp);
1991 if (!p)
1992 break;
1993
1994 idx = strlen(p) - 1;
1995 if (p[idx] == '\n')
1996 p[idx] = '\0';
1997 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001998 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001999 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002000 strlist__delete(sl);
2001 return NULL;
2002 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002003 }
2004 fclose(fp);
2005
2006 return sl;
2007}
2008
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002009struct kprobe_blacklist_node {
2010 struct list_head list;
2011 unsigned long start;
2012 unsigned long end;
2013 char *symbol;
2014};
2015
2016static void kprobe_blacklist__delete(struct list_head *blacklist)
2017{
2018 struct kprobe_blacklist_node *node;
2019
2020 while (!list_empty(blacklist)) {
2021 node = list_first_entry(blacklist,
2022 struct kprobe_blacklist_node, list);
2023 list_del(&node->list);
2024 free(node->symbol);
2025 free(node);
2026 }
2027}
2028
2029static int kprobe_blacklist__load(struct list_head *blacklist)
2030{
2031 struct kprobe_blacklist_node *node;
2032 const char *__debugfs = debugfs_find_mountpoint();
2033 char buf[PATH_MAX], *p;
2034 FILE *fp;
2035 int ret;
2036
2037 if (__debugfs == NULL)
2038 return -ENOTSUP;
2039
2040 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2041 if (ret < 0)
2042 return ret;
2043
2044 fp = fopen(buf, "r");
2045 if (!fp)
2046 return -errno;
2047
2048 ret = 0;
2049 while (fgets(buf, PATH_MAX, fp)) {
2050 node = zalloc(sizeof(*node));
2051 if (!node) {
2052 ret = -ENOMEM;
2053 break;
2054 }
2055 INIT_LIST_HEAD(&node->list);
2056 list_add_tail(&node->list, blacklist);
2057 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2058 ret = -EINVAL;
2059 break;
2060 }
2061 p = strchr(buf, '\t');
2062 if (p) {
2063 p++;
2064 if (p[strlen(p) - 1] == '\n')
2065 p[strlen(p) - 1] = '\0';
2066 } else
2067 p = (char *)"unknown";
2068 node->symbol = strdup(p);
2069 if (!node->symbol) {
2070 ret = -ENOMEM;
2071 break;
2072 }
2073 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2074 node->start, node->end, node->symbol);
2075 ret++;
2076 }
2077 if (ret < 0)
2078 kprobe_blacklist__delete(blacklist);
2079 fclose(fp);
2080
2081 return ret;
2082}
2083
2084static struct kprobe_blacklist_node *
2085kprobe_blacklist__find_by_address(struct list_head *blacklist,
2086 unsigned long address)
2087{
2088 struct kprobe_blacklist_node *node;
2089
2090 list_for_each_entry(node, blacklist, list) {
2091 if (node->start <= address && address <= node->end)
2092 return node;
2093 }
2094
2095 return NULL;
2096}
2097
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002098/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002099static int show_perf_probe_event(struct perf_probe_event *pev,
2100 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002101{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002102 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002103 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002104 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002105
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002106 /* Synthesize only event probe point */
2107 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002108 if (!place)
2109 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002110
2111 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002112 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002113 return ret;
2114
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002115 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002116 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002117 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002118
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002119 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002120 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002121 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002122 ret = synthesize_perf_probe_arg(&pev->args[i],
2123 buf, 128);
2124 if (ret < 0)
2125 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002126 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002127 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002128 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002129 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002130 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002131 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002132}
2133
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302134static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002135{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302136 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302137 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002138 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002139 struct strlist *rawlist;
2140 struct str_node *ent;
2141
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002142 memset(&tev, 0, sizeof(tev));
2143 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002144
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302145 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002146 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002147 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002148
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002149 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302150 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002151 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302152 ret = convert_to_perf_probe_event(&tev, &pev,
2153 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002154 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002155 ret = show_perf_probe_event(&pev,
2156 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002157 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002158 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302159 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002160 if (ret < 0)
2161 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002162 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002163 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002164
2165 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002166}
2167
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302168/* List up current perf-probe events */
2169int show_perf_probe_events(void)
2170{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002171 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302172
2173 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302174
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002175 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302176 if (ret < 0)
2177 return ret;
2178
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002179 kp_fd = open_kprobe_events(false);
2180 if (kp_fd >= 0) {
2181 ret = __show_perf_probe_events(kp_fd, true);
2182 close(kp_fd);
2183 if (ret < 0)
2184 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302185 }
2186
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002187 up_fd = open_uprobe_events(false);
2188 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002189 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002190 ret = kp_fd;
2191 goto out;
2192 }
2193
2194 if (up_fd >= 0) {
2195 ret = __show_perf_probe_events(up_fd, false);
2196 close(up_fd);
2197 }
2198out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002199 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302200 return ret;
2201}
2202
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002203/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302204static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002205{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002206 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002207 struct strlist *sl, *rawlist;
2208 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302209 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002210 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002211
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002212 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302213 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002214 if (!rawlist)
2215 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002216 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002217 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302218 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002219 if (ret < 0)
2220 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002221 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002222 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2223 tev.event);
2224 if (ret >= 0)
2225 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002226 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002227 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302228 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002229 if (ret < 0)
2230 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002231 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002232 strlist__delete(rawlist);
2233
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002234 if (ret < 0) {
2235 strlist__delete(sl);
2236 return NULL;
2237 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002238 return sl;
2239}
2240
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302241static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002242{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002243 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302244 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002245 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002246
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002247 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249 return -EINVAL;
2250 }
2251
Masami Hiramatsufa282442009-12-08 17:03:23 -05002252 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002253 if (!probe_event_dry_run) {
2254 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002255 if (ret <= 0) {
2256 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002257 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002258 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002259 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002260 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002261 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002262 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002263}
2264
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002265static int get_new_event_name(char *buf, size_t len, const char *base,
2266 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002267{
2268 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002269
2270 /* Try no suffix */
2271 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002273 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002274 return ret;
2275 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002276 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002277 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002278
Masami Hiramatsud761b082009-12-15 10:32:25 -05002279 if (!allow_suffix) {
2280 pr_warning("Error: event \"%s\" already exists. "
2281 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002282 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002283 }
2284
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002285 /* Try to add suffix */
2286 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002287 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002289 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002290 return ret;
2291 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002292 if (!strlist__has_entry(namelist, buf))
2293 break;
2294 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002295 if (i == MAX_EVENT_INDEX) {
2296 pr_warning("Too many events are on the same function.\n");
2297 ret = -ERANGE;
2298 }
2299
2300 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002301}
2302
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002303/* Warn if the current kernel's uprobe implementation is old */
2304static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2305{
2306 int i;
2307 char *buf = synthesize_probe_trace_command(tev);
2308
2309 /* Old uprobe event doesn't support memory dereference */
2310 if (!tev->uprobes || tev->nargs == 0 || !buf)
2311 goto out;
2312
2313 for (i = 0; i < tev->nargs; i++)
2314 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2315 pr_warning("Please upgrade your kernel to at least "
2316 "3.14 to have access to feature %s\n",
2317 tev->args[i].value);
2318 break;
2319 }
2320out:
2321 free(buf);
2322}
2323
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302324static int __add_probe_trace_events(struct perf_probe_event *pev,
2325 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002327{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002328 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302329 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002330 char buf[64];
2331 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002332 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002333 LIST_HEAD(blacklist);
2334 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002335
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302336 if (pev->uprobes)
2337 fd = open_uprobe_events(true);
2338 else
2339 fd = open_kprobe_events(true);
2340
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002341 if (fd < 0) {
2342 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002343 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002344 }
2345
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002346 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302347 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002348 if (!namelist) {
2349 pr_debug("Failed to get current event list.\n");
2350 return -EIO;
2351 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002352 /* Get kprobe blacklist if exists */
2353 if (!pev->uprobes) {
2354 ret = kprobe_blacklist__load(&blacklist);
2355 if (ret < 0)
2356 pr_debug("No kprobe blacklist support, ignored\n");
2357 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002358
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002359 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002360 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002361 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002362 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002363 /* Ensure that the address is NOT blacklisted */
2364 node = kprobe_blacklist__find_by_address(&blacklist,
2365 tev->point.address);
2366 if (node) {
2367 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2368 continue;
2369 }
2370
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002371 if (pev->event)
2372 event = pev->event;
2373 else
2374 if (pev->point.function)
2375 event = pev->point.function;
2376 else
2377 event = tev->point.symbol;
2378 if (pev->group)
2379 group = pev->group;
2380 else
2381 group = PERFPROBE_GROUP;
2382
2383 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002384 ret = get_new_event_name(buf, 64, event,
2385 namelist, allow_suffix);
2386 if (ret < 0)
2387 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002388 event = buf;
2389
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002390 tev->event = strdup(event);
2391 tev->group = strdup(group);
2392 if (tev->event == NULL || tev->group == NULL) {
2393 ret = -ENOMEM;
2394 break;
2395 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302396 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 if (ret < 0)
2398 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002399 /* Add added event name to namelist */
2400 strlist__add(namelist, event);
2401
2402 /* Trick here - save current event/group */
2403 event = pev->event;
2404 group = pev->group;
2405 pev->event = tev->event;
2406 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002407 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002408 /* Trick here - restore current event/group */
2409 pev->event = (char *)event;
2410 pev->group = (char *)group;
2411
2412 /*
2413 * Probes after the first probe which comes from same
2414 * user input are always allowed to add suffix, because
2415 * there might be several addresses corresponding to
2416 * one code line.
2417 */
2418 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002419 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002420 if (ret == -EINVAL && pev->uprobes)
2421 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002422
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002423 /* Note that it is possible to skip all events because of blacklist */
2424 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002425 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002426 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2427 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002428 tev->event);
2429 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002430
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002431 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002432 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002433 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002434 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002435}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002436
Namhyung Kim564c62a2015-01-14 20:18:07 +09002437static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002438{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002439 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002440 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002441
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002442 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002443 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002444 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002445
2446 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002447}
2448
2449#define strdup_or_goto(str, label) \
2450 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2451
2452/*
2453 * Find probe function addresses from map.
2454 * Return an error or the number of found probe_trace_event
2455 */
2456static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2457 struct probe_trace_event **tevs,
2458 int max_tevs, const char *target)
2459{
2460 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002461 struct ref_reloc_sym *reloc_sym = NULL;
2462 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002463 struct probe_trace_event *tev;
2464 struct perf_probe_point *pp = &pev->point;
2465 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002466 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002467 int ret, i;
2468
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002469 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002470 if (!map) {
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474
2475 /*
2476 * Load matched symbols: Since the different local symbols may have
2477 * same name but different addresses, this lists all the symbols.
2478 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002479 num_matched_functions = find_probe_functions(map, pp->function);
2480 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002481 pr_err("Failed to find symbol %s in %s\n", pp->function,
2482 target ? : "kernel");
2483 ret = -ENOENT;
2484 goto out;
2485 } else if (num_matched_functions > max_tevs) {
2486 pr_err("Too many functions matched in %s\n",
2487 target ? : "kernel");
2488 ret = -E2BIG;
2489 goto out;
2490 }
2491
Namhyung Kim25dd9172015-01-14 20:18:08 +09002492 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002493 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002494 if (!reloc_sym) {
2495 pr_warning("Relocated base symbol is not found!\n");
2496 ret = -EINVAL;
2497 goto out;
2498 }
2499 }
2500
2501 /* Setup result trace-probe-events */
2502 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2503 if (!*tevs) {
2504 ret = -ENOMEM;
2505 goto out;
2506 }
2507
2508 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002509
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002510 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002511 tev = (*tevs) + ret;
2512 tp = &tev->point;
2513 if (ret == num_matched_functions) {
2514 pr_warning("Too many symbols are listed. Skip it.\n");
2515 break;
2516 }
2517 ret++;
2518
2519 if (pp->offset > sym->end - sym->start) {
2520 pr_warning("Offset %ld is bigger than the size of %s\n",
2521 pp->offset, sym->name);
2522 ret = -ENOENT;
2523 goto err_out;
2524 }
2525 /* Add one probe point */
2526 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2527 if (reloc_sym) {
2528 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2529 tp->offset = tp->address - reloc_sym->addr;
2530 } else {
2531 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2532 tp->offset = pp->offset;
2533 }
2534 tp->retprobe = pp->retprobe;
2535 if (target)
2536 tev->point.module = strdup_or_goto(target, nomem_out);
2537 tev->uprobes = pev->uprobes;
2538 tev->nargs = pev->nargs;
2539 if (tev->nargs) {
2540 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2541 tev->nargs);
2542 if (tev->args == NULL)
2543 goto nomem_out;
2544 }
2545 for (i = 0; i < tev->nargs; i++) {
2546 if (pev->args[i].name)
2547 tev->args[i].name =
2548 strdup_or_goto(pev->args[i].name,
2549 nomem_out);
2550
2551 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2552 nomem_out);
2553 if (pev->args[i].type)
2554 tev->args[i].type =
2555 strdup_or_goto(pev->args[i].type,
2556 nomem_out);
2557 }
2558 }
2559
2560out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002561 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002562 return ret;
2563
2564nomem_out:
2565 ret = -ENOMEM;
2566err_out:
2567 clear_probe_trace_events(*tevs, num_matched_functions);
2568 zfree(tevs);
2569 goto out;
2570}
2571
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302572static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2573 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302574 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002575{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002576 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002577
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002578 if (pev->uprobes && !pev->group) {
2579 /* Replace group name if not given */
2580 ret = convert_exec_to_group(target, &pev->group);
2581 if (ret != 0) {
2582 pr_warning("Failed to make a group name.\n");
2583 return ret;
2584 }
2585 }
2586
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002587 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302588 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002589 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002590 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002591
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002592 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002593}
2594
2595struct __event_package {
2596 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302597 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002598 int ntevs;
2599};
2600
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002601int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002602 int max_tevs, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002603{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002604 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002605 struct __event_package *pkgs;
2606
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302607 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002608 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302609
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002610 if (pkgs == NULL)
2611 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002612
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002613 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002614 if (ret < 0) {
2615 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002616 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002617 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002618
2619 /* Loop 1: convert all events */
2620 for (i = 0; i < npevs; i++) {
2621 pkgs[i].pev = &pevs[i];
2622 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302623 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002624 &pkgs[i].tevs,
2625 max_tevs,
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002626 pkgs[i].pev->target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002627 if (ret < 0)
2628 goto end;
2629 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002630 }
2631
2632 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002633 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302634 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002635 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002636 if (ret < 0)
2637 break;
2638 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002639end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002640 /* Loop 3: cleanup and free trace events */
2641 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002642 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302643 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002644 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002645 }
2646 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002647 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002648
2649 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002650}
2651
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302652static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002653{
2654 char *p;
2655 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002656 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002657
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302658 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002659 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2660 if (ret < 0)
2661 goto error;
2662
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002663 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002664 if (!p) {
2665 pr_debug("Internal error: %s should have ':' but not.\n",
2666 ent->s);
2667 ret = -ENOTSUP;
2668 goto error;
2669 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002670 *p = '/';
2671
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002672 pr_debug("Writing event: %s\n", buf);
2673 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002674 if (ret < 0) {
2675 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002676 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002677 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002678
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002679 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002680 return 0;
2681error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002682 pr_warning("Failed to delete event: %s\n",
2683 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002684 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002685}
2686
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302687static int del_trace_probe_event(int fd, const char *buf,
2688 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002689{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002690 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302691 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002692
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002693 if (strpbrk(buf, "*?")) { /* Glob-exp */
2694 strlist__for_each_safe(ent, n, namelist)
2695 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302696 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002697 if (ret < 0)
2698 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002699 strlist__remove(namelist, ent);
2700 }
2701 } else {
2702 ent = strlist__find(namelist, buf);
2703 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302704 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002705 if (ret >= 0)
2706 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002707 }
2708 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002709
2710 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002711}
2712
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002713int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002714{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302715 int ret = -1, ufd = -1, kfd = -1;
2716 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002717 const char *group, *event;
2718 char *p, *str;
2719 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302720 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002721
Masami Hiramatsufa282442009-12-08 17:03:23 -05002722 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302723 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002724 if (kfd >= 0)
2725 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302726
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302727 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002728 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302729 unamelist = get_probe_trace_event_names(ufd, true);
2730
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002731 if (kfd < 0 && ufd < 0) {
2732 print_both_open_warning(kfd, ufd);
2733 goto error;
2734 }
2735
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302736 if (namelist == NULL && unamelist == NULL)
2737 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002738
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002739 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002740 str = strdup(ent->s);
2741 if (str == NULL) {
2742 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302743 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002744 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002745 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002746 p = strchr(str, ':');
2747 if (p) {
2748 group = str;
2749 *p = '\0';
2750 event = p + 1;
2751 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002752 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002753 event = str;
2754 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302755
2756 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2757 if (ret < 0) {
2758 pr_err("Failed to copy event.");
2759 free(str);
2760 goto error;
2761 }
2762
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002763 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302764
2765 if (namelist)
2766 ret = del_trace_probe_event(kfd, buf, namelist);
2767
2768 if (unamelist && ret != 0)
2769 ret = del_trace_probe_event(ufd, buf, unamelist);
2770
2771 if (ret != 0)
2772 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2773
Masami Hiramatsufa282442009-12-08 17:03:23 -05002774 free(str);
2775 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302776
2777error:
2778 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302779 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302780 close(kfd);
2781 }
2782
2783 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302784 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785 close(ufd);
2786 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002787
2788 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002789}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302790
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002791/* TODO: don't use a global variable for filter ... */
2792static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002793
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002794/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002795 * If a symbol corresponds to a function with global binding and
2796 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002797 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002798static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002799 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002800{
Namhyung Kime578da32015-03-06 16:31:29 +09002801 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002802 return 0;
2803 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002804}
2805
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002806int show_available_funcs(const char *target, struct strfilter *_filter,
2807 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002808{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002809 struct map *map;
2810 int ret;
2811
2812 ret = init_symbol_maps(user);
2813 if (ret < 0)
2814 return ret;
2815
2816 /* Get a symbol map */
2817 if (user)
2818 map = dso__new_map(target);
2819 else
2820 map = kernel_get_module_map(target);
2821 if (!map) {
2822 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002823 return -EINVAL;
2824 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002825
2826 /* Load symbols with given filter */
2827 available_func_filter = _filter;
2828 if (map__load(map, filter_available_functions)) {
2829 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2830 goto end;
2831 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002832 if (!dso__sorted_by_name(map->dso, map->type))
2833 dso__sort_by_name(map->dso, map->type);
2834
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002835 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302836 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002837 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2838end:
2839 if (user) {
2840 dso__delete(map->dso);
2841 map__delete(map);
2842 }
2843 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302844
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002845 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302846}
2847