blob: 8af8e7f55254aadb3a2829039ccdf5d64a384a4f [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]);
138 return kmap->ref_reloc_sym;
139}
140
141static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
142{
143 struct ref_reloc_sym *reloc_sym;
144 struct symbol *sym;
145 struct map *map;
146
147 /* ref_reloc_sym is just a label. Need a special fix*/
148 reloc_sym = kernel_get_ref_reloc_sym();
149 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
150 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
151 else {
152 sym = __find_kernel_function_by_name(name, &map);
153 if (sym)
154 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800155 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000156 }
157 return 0;
158}
159
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900160static struct map *kernel_get_module_map(const char *module)
161{
162 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000163 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900164
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000167 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900169 if (!module)
170 module = "kernel";
171
172 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
173 struct map *pos = rb_entry(nd, struct map, rb_node);
174 if (strncmp(pos->dso->short_name + 1, module,
175 pos->dso->short_name_len - 2) == 0) {
176 return pos;
177 }
178 }
179 return NULL;
180}
181
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900182static struct map *get_target_map(const char *target, bool user)
183{
184 /* Init maps of given executable or kernel */
185 if (user)
186 return dso__new_map(target);
187 else
188 return kernel_get_module_map(target);
189}
190
191static void put_target_map(struct map *map, bool user)
192{
193 if (map && user) {
194 /* Only the user map needs to be released */
195 dso__delete(map->dso);
196 map__delete(map);
197 }
198}
199
200
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900201static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900202{
203 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100204 struct map *map;
205 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900206
207 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400208 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
209 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900210 if (strncmp(dso->short_name + 1, module,
211 dso->short_name_len - 2) == 0)
212 goto found;
213 }
214 pr_debug("Failed to find module %s.\n", module);
215 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100216 }
217
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000218 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100219 dso = map->dso;
220
221 vmlinux_name = symbol_conf.vmlinux_name;
222 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300223 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100224 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900225 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100226 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900227 pr_debug("Failed to load kernel map.\n");
228 return NULL;
229 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400230 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900231found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900232 return dso;
233}
234
235const char *kernel_get_module_path(const char *module)
236{
237 struct dso *dso = kernel_get_module_dso(module);
238 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900239}
240
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000241static int convert_exec_to_group(const char *exec, char **result)
242{
243 char *ptr1, *ptr2, *exec_copy;
244 char buf[64];
245 int ret;
246
247 exec_copy = strdup(exec);
248 if (!exec_copy)
249 return -ENOMEM;
250
251 ptr1 = basename(exec_copy);
252 if (!ptr1) {
253 ret = -EINVAL;
254 goto out;
255 }
256
257 ptr2 = strpbrk(ptr1, "-._");
258 if (ptr2)
259 *ptr2 = '\0';
260 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
261 if (ret < 0)
262 goto out;
263
264 *result = strdup(buf);
265 ret = *result ? 0 : -ENOMEM;
266
267out:
268 free(exec_copy);
269 return ret;
270}
271
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900272static void clear_perf_probe_point(struct perf_probe_point *pp)
273{
274 free(pp->file);
275 free(pp->function);
276 free(pp->lazy_line);
277}
278
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000279static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
280{
281 int i;
282
283 for (i = 0; i < ntevs; i++)
284 clear_probe_trace_event(tevs + i);
285}
286
Ingo Molnar89fe8082013-09-30 12:07:11 +0200287#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900288/*
289 * Some binaries like glibc have special symbols which are on the symbol
290 * table, but not in the debuginfo. If we can find the address of the
291 * symbol from map, we can translate the address back to the probe point.
292 */
293static int find_alternative_probe_point(struct debuginfo *dinfo,
294 struct perf_probe_point *pp,
295 struct perf_probe_point *result,
296 const char *target, bool uprobes)
297{
298 struct map *map = NULL;
299 struct symbol *sym;
300 u64 address = 0;
301 int ret = -ENOENT;
302
303 /* This can work only for function-name based one */
304 if (!pp->function || pp->file)
305 return -ENOTSUP;
306
307 map = get_target_map(target, uprobes);
308 if (!map)
309 return -EINVAL;
310
311 /* Find the address of given function */
312 map__for_each_symbol_by_name(map, pp->function, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +0900313 address = sym->start;
314 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900315 }
316 if (!address) {
317 ret = -ENOENT;
318 goto out;
319 }
320 pr_debug("Symbol %s address found : %lx\n", pp->function, address);
321
322 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
323 result);
324 if (ret <= 0)
325 ret = (!ret) ? -ENOENT : ret;
326 else {
327 result->offset += pp->offset;
328 result->line += pp->line;
329 ret = 0;
330 }
331
332out:
333 put_target_map(map, uprobes);
334 return ret;
335
336}
337
338static int get_alternative_probe_event(struct debuginfo *dinfo,
339 struct perf_probe_event *pev,
340 struct perf_probe_point *tmp,
341 const char *target)
342{
343 int ret;
344
345 memcpy(tmp, &pev->point, sizeof(*tmp));
346 memset(&pev->point, 0, sizeof(pev->point));
347 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
348 target, pev->uprobes);
349 if (ret < 0)
350 memcpy(&pev->point, tmp, sizeof(*tmp));
351
352 return ret;
353}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000354
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900355static int get_alternative_line_range(struct debuginfo *dinfo,
356 struct line_range *lr,
357 const char *target, bool user)
358{
359 struct perf_probe_point pp = { 0 }, result = { 0 };
360 int ret, len = 0;
361
362 pp.function = lr->function;
363 pp.file = lr->file;
364 pp.line = lr->start;
365 if (lr->end != INT_MAX)
366 len = lr->end - lr->start;
367 ret = find_alternative_probe_point(dinfo, &pp, &result,
368 target, user);
369 if (!ret) {
370 lr->function = result.function;
371 lr->file = result.file;
372 lr->start = result.line;
373 if (lr->end != INT_MAX)
374 lr->end = lr->start + len;
375 clear_perf_probe_point(&pp);
376 }
377 return ret;
378}
379
Masami Hiramatsuff741782011-06-27 16:27:39 +0900380/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000381static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900382{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000383 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000384 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900385
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000386 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900387 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900388 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000389 if (!silent)
390 pr_err("Failed to find path of %s module.\n",
391 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900392 return NULL;
393 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900394 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000395 ret = debuginfo__new(path);
396 if (!ret && !silent) {
397 pr_warning("The %s file has no debug information.\n", path);
398 if (!module || !strtailcmp(path, ".ko"))
399 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
400 else
401 pr_warning("Rebuild with -g, ");
402 pr_warning("or install an appropriate debuginfo package.\n");
403 }
404 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400405}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300406
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000407
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000408static int get_text_start_address(const char *exec, unsigned long *address)
409{
410 Elf *elf;
411 GElf_Ehdr ehdr;
412 GElf_Shdr shdr;
413 int fd, ret = -ENOENT;
414
415 fd = open(exec, O_RDONLY);
416 if (fd < 0)
417 return -errno;
418
419 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
420 if (elf == NULL)
421 return -EINVAL;
422
423 if (gelf_getehdr(elf, &ehdr) == NULL)
424 goto out;
425
426 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
427 goto out;
428
429 *address = shdr.sh_addr - shdr.sh_offset;
430 ret = 0;
431out:
432 elf_end(elf);
433 return ret;
434}
435
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000436/*
437 * Convert trace point to probe point with debuginfo
438 */
439static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
440 struct perf_probe_point *pp,
441 bool is_kprobe)
442{
443 struct debuginfo *dinfo = NULL;
444 unsigned long stext = 0;
445 u64 addr = tp->address;
446 int ret = -ENOENT;
447
448 /* convert the address to dwarf address */
449 if (!is_kprobe) {
450 if (!addr) {
451 ret = -EINVAL;
452 goto error;
453 }
454 ret = get_text_start_address(tp->module, &stext);
455 if (ret < 0)
456 goto error;
457 addr += stext;
458 } else {
459 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
460 if (addr == 0)
461 goto error;
462 addr += tp->offset;
463 }
464
465 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
466 tp->module ? : "kernel");
467
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000468 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000469 if (dinfo) {
470 ret = debuginfo__find_probe_point(dinfo,
471 (unsigned long)addr, pp);
472 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000473 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000474 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000475
476 if (ret > 0) {
477 pp->retprobe = tp->retprobe;
478 return 0;
479 }
480error:
481 pr_debug("Failed to find corresponding probes from debuginfo.\n");
482 return ret ? : -ENOENT;
483}
484
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000485static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
486 int ntevs, const char *exec)
487{
488 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000489 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000490
491 if (!exec)
492 return 0;
493
494 ret = get_text_start_address(exec, &stext);
495 if (ret < 0)
496 return ret;
497
498 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000499 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000500 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000501 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000502 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000503 ret = -ENOMEM;
504 break;
505 }
506 tevs[i].uprobes = true;
507 }
508
509 return ret;
510}
511
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900512static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
513 int ntevs, const char *module)
514{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900515 int i, ret = 0;
516 char *tmp;
517
518 if (!module)
519 return 0;
520
521 tmp = strrchr(module, '/');
522 if (tmp) {
523 /* This is a module path -- get the module name */
524 module = strdup(tmp + 1);
525 if (!module)
526 return -ENOMEM;
527 tmp = strchr(module, '.');
528 if (tmp)
529 *tmp = '\0';
530 tmp = (char *)module; /* For free() */
531 }
532
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900533 for (i = 0; i < ntevs; i++) {
534 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900535 if (!tevs[i].point.module) {
536 ret = -ENOMEM;
537 break;
538 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900539 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900540
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300541 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900542 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900543}
544
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000545/* Post processing the probe events */
546static int post_process_probe_trace_events(struct probe_trace_event *tevs,
547 int ntevs, const char *module,
548 bool uprobe)
549{
550 struct ref_reloc_sym *reloc_sym;
551 char *tmp;
552 int i;
553
554 if (uprobe)
555 return add_exec_to_probe_trace_events(tevs, ntevs, module);
556
557 /* Note that currently ref_reloc_sym based probe is not for drivers */
558 if (module)
559 return add_module_to_probe_trace_events(tevs, ntevs, module);
560
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000561 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000562 if (!reloc_sym) {
563 pr_warning("Relocated base symbol is not found!\n");
564 return -EINVAL;
565 }
566
567 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900568 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000569 tmp = strdup(reloc_sym->name);
570 if (!tmp)
571 return -ENOMEM;
572 free(tevs[i].point.symbol);
573 tevs[i].point.symbol = tmp;
574 tevs[i].point.offset = tevs[i].point.address -
575 reloc_sym->unrelocated_addr;
576 }
577 }
578 return 0;
579}
580
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300581/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530582static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900583 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530584 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300585{
586 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900587 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530588 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900589 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300590
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000591 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530592
Masami Hiramatsuff741782011-06-27 16:27:39 +0900593 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000594 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900595 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900596 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300597 return 0;
598 }
599
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000600 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900601 /* Searching trace events corresponding to a probe event */
602 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
603
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900604 if (ntevs == 0) { /* Not found, retry with an alternative */
605 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
606 if (!ret) {
607 ntevs = debuginfo__find_trace_events(dinfo, pev,
608 tevs, max_tevs);
609 /*
610 * Write back to the original probe_event for
611 * setting appropriate (user given) event name
612 */
613 clear_perf_probe_point(&pev->point);
614 memcpy(&pev->point, &tmp, sizeof(tmp));
615 }
616 }
617
Masami Hiramatsuff741782011-06-27 16:27:39 +0900618 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300619
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400620 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000621 pr_debug("Found %d probe_trace_events.\n", ntevs);
622 ret = post_process_probe_trace_events(*tevs, ntevs,
623 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000624 if (ret < 0) {
625 clear_probe_trace_events(*tevs, ntevs);
626 zfree(tevs);
627 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900628 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300630
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400631 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900632 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400633 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900634 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400635 }
636 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400637 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
638 if (ntevs == -EBADF) {
639 pr_warning("Warning: No dwarf info found in the vmlinux - "
640 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
641 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900642 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400643 return 0;
644 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300645 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400646 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300647}
648
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900649/*
650 * Find a src file from a DWARF tag path. Prepend optional source path prefix
651 * and chop off leading directories that do not exist. Result is passed back as
652 * a newly allocated path on success.
653 * Return 0 if file was found and readable, -errno otherwise.
654 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900655static int get_real_path(const char *raw_path, const char *comp_dir,
656 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900657{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900658 const char *prefix = symbol_conf.source_prefix;
659
660 if (!prefix) {
661 if (raw_path[0] != '/' && comp_dir)
662 /* If not an absolute path, try to use comp_dir */
663 prefix = comp_dir;
664 else {
665 if (access(raw_path, R_OK) == 0) {
666 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300667 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900668 } else
669 return -errno;
670 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900671 }
672
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900673 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900674 if (!*new_path)
675 return -ENOMEM;
676
677 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900678 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900679
680 if (access(*new_path, R_OK) == 0)
681 return 0;
682
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900683 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900684 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900685 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900686 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900687 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900688
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900689 switch (errno) {
690 case ENAMETOOLONG:
691 case ENOENT:
692 case EROFS:
693 case EFAULT:
694 raw_path = strchr(++raw_path, '/');
695 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300696 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900697 return -ENOENT;
698 }
699 continue;
700
701 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300702 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900703 return -errno;
704 }
705 }
706}
707
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300708#define LINEBUF_SIZE 256
709#define NR_ADDITIONAL_LINES 2
710
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100711static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300712{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000713 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100714 const char *color = show_num ? "" : PERF_COLOR_BLUE;
715 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300716
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100717 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300718 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
719 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100720 if (skip)
721 continue;
722 if (!prefix) {
723 prefix = show_num ? "%7d " : " ";
724 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100726 color_fprintf(stdout, color, "%s", buf);
727
728 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400729
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100730 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100732 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000733 pr_warning("File read error: %s\n",
734 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100735 return -1;
736 }
737 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738}
739
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100740static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
741{
742 int rv = __show_one_line(fp, l, skip, show_num);
743 if (rv == 0) {
744 pr_warning("Source file is shorter than expected.\n");
745 rv = -1;
746 }
747 return rv;
748}
749
750#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
751#define show_one_line(f,l) _show_one_line(f,l,false,false)
752#define skip_one_line(f,l) _show_one_line(f,l,true,false)
753#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
754
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300755/*
756 * Show line-range always requires debuginfo to find source file and
757 * line number.
758 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900759static int __show_line_range(struct line_range *lr, const char *module,
760 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300761{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400762 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000763 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900764 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300765 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900766 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900767 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000768 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300769
770 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000771 dinfo = open_debuginfo(module, false);
772 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900773 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400774
Masami Hiramatsuff741782011-06-27 16:27:39 +0900775 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900776 if (!ret) { /* Not found, retry with an alternative */
777 ret = get_alternative_line_range(dinfo, lr, module, user);
778 if (!ret)
779 ret = debuginfo__find_line_range(dinfo, lr);
780 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900781 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000782 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400783 pr_warning("Specified source line is not found.\n");
784 return -ENOENT;
785 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000786 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400787 return ret;
788 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300789
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900790 /* Convert source file path */
791 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900792 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900793 free(tmp); /* Free old path */
794 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000795 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900796 return ret;
797 }
798
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300799 setup_pager();
800
801 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900802 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300803 lr->start - lr->offset);
804 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100805 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806
807 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400808 if (fp == NULL) {
809 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000810 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400811 return -errno;
812 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100814 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100815 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100816 if (ret < 0)
817 goto end;
818 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300819
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000820 intlist__for_each(ln, lr->line_list) {
821 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100822 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100823 if (ret < 0)
824 goto end;
825 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100826 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 if (ret < 0)
828 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300829 }
830
831 if (lr->end == INT_MAX)
832 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100833 while (l <= lr->end) {
834 ret = show_one_line_or_eof(fp, l++ - lr->offset);
835 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100836 break;
837 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300839 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400840 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300841}
842
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000843int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000844{
845 int ret;
846
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000847 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000848 if (ret < 0)
849 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900850 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000851 exit_symbol_maps();
852
853 return ret;
854}
855
Masami Hiramatsuff741782011-06-27 16:27:39 +0900856static int show_available_vars_at(struct debuginfo *dinfo,
857 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900858 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900859 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900860{
861 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900862 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900863 struct str_node *node;
864 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900865 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900866 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900867
868 buf = synthesize_perf_probe_point(&pev->point);
869 if (!buf)
870 return -EINVAL;
871 pr_debug("Searching variables at %s\n", buf);
872
Masami Hiramatsuff741782011-06-27 16:27:39 +0900873 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
874 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900875 if (!ret) { /* Not found, retry with an alternative */
876 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
877 if (!ret) {
878 ret = debuginfo__find_available_vars_at(dinfo, pev,
879 &vls, max_vls, externs);
880 /* Release the old probe_point */
881 clear_perf_probe_point(&tmp);
882 }
883 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900884 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000885 if (ret == 0 || ret == -ENOENT) {
886 pr_err("Failed to find the address of %s\n", buf);
887 ret = -ENOENT;
888 } else
889 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900890 goto end;
891 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000892
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900893 /* Some variables are found */
894 fprintf(stdout, "Available variables at %s\n", buf);
895 for (i = 0; i < ret; i++) {
896 vl = &vls[i];
897 /*
898 * A probe point might be converted to
899 * several trace points.
900 */
901 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
902 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300903 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900904 nvars = 0;
905 if (vl->vars) {
906 strlist__for_each(node, vl->vars) {
907 var = strchr(node->s, '\t') + 1;
908 if (strfilter__compare(_filter, var)) {
909 fprintf(stdout, "\t\t%s\n", node->s);
910 nvars++;
911 }
912 }
913 strlist__delete(vl->vars);
914 }
915 if (nvars == 0)
916 fprintf(stdout, "\t\t(No matched variables)\n");
917 }
918 free(vls);
919end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900920 free(buf);
921 return ret;
922}
923
924/* Show available variables on given probe point */
925int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900926 int max_vls, const char *module,
927 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900928{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900929 int i, ret = 0;
930 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900931
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000932 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900933 if (ret < 0)
934 return ret;
935
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000936 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900937 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000938 ret = -ENOENT;
939 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900940 }
941
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900942 setup_pager();
943
Masami Hiramatsuff741782011-06-27 16:27:39 +0900944 for (i = 0; i < npevs && ret >= 0; i++)
945 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900946 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900947
948 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000949out:
950 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900951 return ret;
952}
953
Ingo Molnar89fe8082013-09-30 12:07:11 +0200954#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300955
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000956static int
957find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
958 struct perf_probe_point *pp __maybe_unused,
959 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300960{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000961 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300962}
963
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530964static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300965 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300966 int max_tevs __maybe_unused,
967 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300968{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400969 if (perf_probe_event_need_dwarf(pev)) {
970 pr_warning("Debuginfo-analysis is not supported.\n");
971 return -ENOSYS;
972 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530973
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300974 return 0;
975}
976
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300977int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000978 const char *module __maybe_unused,
979 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300980{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400981 pr_warning("Debuginfo-analysis is not supported.\n");
982 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300983}
984
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300985int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
986 int npevs __maybe_unused, int max_vls __maybe_unused,
987 const char *module __maybe_unused,
988 struct strfilter *filter __maybe_unused,
989 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900990{
991 pr_warning("Debuginfo-analysis is not supported.\n");
992 return -ENOSYS;
993}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400994#endif
995
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000996void line_range__clear(struct line_range *lr)
997{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000998 free(lr->function);
999 free(lr->file);
1000 free(lr->path);
1001 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001002 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001003 memset(lr, 0, sizeof(*lr));
1004}
1005
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001006int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001007{
1008 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001009 lr->line_list = intlist__new(NULL);
1010 if (!lr->line_list)
1011 return -ENOMEM;
1012 else
1013 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001014}
1015
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001016static int parse_line_num(char **ptr, int *val, const char *what)
1017{
1018 const char *start = *ptr;
1019
1020 errno = 0;
1021 *val = strtol(*ptr, ptr, 0);
1022 if (errno || *ptr == start) {
1023 semantic_error("'%s' is not a valid number.\n", what);
1024 return -EINVAL;
1025 }
1026 return 0;
1027}
1028
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001029/*
1030 * Stuff 'lr' according to the line range described by 'arg'.
1031 * The line range syntax is described by:
1032 *
1033 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001034 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001035 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001036int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001037{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001038 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001039 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001040
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001041 if (!name)
1042 return -ENOMEM;
1043
1044 lr->start = 0;
1045 lr->end = INT_MAX;
1046
1047 range = strchr(name, ':');
1048 if (range) {
1049 *range++ = '\0';
1050
1051 err = parse_line_num(&range, &lr->start, "start line");
1052 if (err)
1053 goto err;
1054
1055 if (*range == '+' || *range == '-') {
1056 const char c = *range++;
1057
1058 err = parse_line_num(&range, &lr->end, "end line");
1059 if (err)
1060 goto err;
1061
1062 if (c == '+') {
1063 lr->end += lr->start;
1064 /*
1065 * Adjust the number of lines here.
1066 * If the number of lines == 1, the
1067 * the end of line should be equal to
1068 * the start of line.
1069 */
1070 lr->end--;
1071 }
1072 }
1073
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001074 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001075
1076 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001077 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001078 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001079 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001080 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001082 if (*range != '\0') {
1083 semantic_error("Tailing with invalid str '%s'.\n", range);
1084 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001086 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001087
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001088 file = strchr(name, '@');
1089 if (file) {
1090 *file = '\0';
1091 lr->file = strdup(++file);
1092 if (lr->file == NULL) {
1093 err = -ENOMEM;
1094 goto err;
1095 }
1096 lr->function = name;
1097 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001098 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001099 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001100 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001101
1102 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001103err:
1104 free(name);
1105 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001106}
1107
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001108/* Check the name is good for event/group */
1109static bool check_event_name(const char *name)
1110{
1111 if (!isalpha(*name) && *name != '_')
1112 return false;
1113 while (*++name != '\0') {
1114 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1115 return false;
1116 }
1117 return true;
1118}
1119
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001120/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001121static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001122{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001123 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001124 char *ptr, *tmp;
1125 char c, nc = 0;
1126 /*
1127 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001128 * perf probe [EVENT=]SRC[:LN|;PTN]
1129 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001130 *
1131 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001132 */
1133
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001134 ptr = strpbrk(arg, ";=@+%");
1135 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001136 *ptr = '\0';
1137 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001138 if (strchr(arg, ':')) {
1139 semantic_error("Group name is not supported yet.\n");
1140 return -ENOTSUP;
1141 }
1142 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001143 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144 "follow C symbol-naming rule.\n", arg);
1145 return -EINVAL;
1146 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001147 pev->event = strdup(arg);
1148 if (pev->event == NULL)
1149 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001150 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001151 arg = tmp;
1152 }
1153
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001154 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001155 if (ptr) {
1156 nc = *ptr;
1157 *ptr++ = '\0';
1158 }
1159
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001160 tmp = strdup(arg);
1161 if (tmp == NULL)
1162 return -ENOMEM;
1163
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001164 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001165 if (strchr(tmp, '.')) /* File */
1166 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001167 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001168 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001169
1170 /* Parse other options */
1171 while (ptr) {
1172 arg = ptr;
1173 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001174 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001175 pp->lazy_line = strdup(arg);
1176 if (pp->lazy_line == NULL)
1177 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001178 break;
1179 }
1180 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001181 if (ptr) {
1182 nc = *ptr;
1183 *ptr++ = '\0';
1184 }
1185 switch (c) {
1186 case ':': /* Line number */
1187 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001188 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001189 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 " in line number.\n");
1191 return -EINVAL;
1192 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001193 break;
1194 case '+': /* Byte offset from a symbol */
1195 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001197 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 " in offset.\n");
1199 return -EINVAL;
1200 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001201 break;
1202 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001203 if (pp->file) {
1204 semantic_error("SRC@SRC is not allowed.\n");
1205 return -EINVAL;
1206 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001207 pp->file = strdup(arg);
1208 if (pp->file == NULL)
1209 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210 break;
1211 case '%': /* Probe places */
1212 if (strcmp(arg, "return") == 0) {
1213 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214 } else { /* Others not supported yet */
1215 semantic_error("%%%s is not supported.\n", arg);
1216 return -ENOTSUP;
1217 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001218 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 default: /* Buggy case */
1220 pr_err("This program has a bug at %s:%d.\n",
1221 __FILE__, __LINE__);
1222 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001223 break;
1224 }
1225 }
1226
1227 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001229 semantic_error("Lazy pattern can't be used with"
1230 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001231 return -EINVAL;
1232 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001233
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001235 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001236 return -EINVAL;
1237 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001238
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001240 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 return -EINVAL;
1242 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001243
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001245 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001246 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 return -EINVAL;
1248 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001249
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001251 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 return -EINVAL;
1253 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001254
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001256 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257 return -EINVAL;
1258 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001259
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001261 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001262 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 return -EINVAL;
1264 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001265
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001266 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001267 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1268 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001269 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001270}
1271
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001272/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001274{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001275 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001276 struct perf_probe_arg_field **fieldp;
1277
1278 pr_debug("parsing arg: %s into ", str);
1279
Masami Hiramatsu48481932010-04-12 13:16:53 -04001280 tmp = strchr(str, '=');
1281 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001282 arg->name = strndup(str, tmp - str);
1283 if (arg->name == NULL)
1284 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001285 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001286 str = tmp + 1;
1287 }
1288
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001289 tmp = strchr(str, ':');
1290 if (tmp) { /* Type setting */
1291 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001292 arg->type = strdup(tmp + 1);
1293 if (arg->type == NULL)
1294 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001295 pr_debug("type:%s ", arg->type);
1296 }
1297
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001298 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001299 if (!is_c_varname(str) || !tmp) {
1300 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001301 arg->var = strdup(str);
1302 if (arg->var == NULL)
1303 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001304 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001305 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001306 }
1307
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001308 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001309 arg->var = strndup(str, tmp - str);
1310 if (arg->var == NULL)
1311 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001312 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001313 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001314 fieldp = &arg->field;
1315
1316 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001317 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1318 if (*fieldp == NULL)
1319 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001320 if (*tmp == '[') { /* Array */
1321 str = tmp;
1322 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001323 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001324 if (*tmp != ']' || tmp == str + 1) {
1325 semantic_error("Array index must be a"
1326 " number.\n");
1327 return -EINVAL;
1328 }
1329 tmp++;
1330 if (*tmp == '\0')
1331 tmp = NULL;
1332 } else { /* Structure */
1333 if (*tmp == '.') {
1334 str = tmp + 1;
1335 (*fieldp)->ref = false;
1336 } else if (tmp[1] == '>') {
1337 str = tmp + 2;
1338 (*fieldp)->ref = true;
1339 } else {
1340 semantic_error("Argument parse error: %s\n",
1341 str);
1342 return -EINVAL;
1343 }
1344 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001345 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001346 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001347 (*fieldp)->name = strndup(str, tmp - str);
1348 if ((*fieldp)->name == NULL)
1349 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001350 if (*str != '[')
1351 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001352 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1353 fieldp = &(*fieldp)->next;
1354 }
1355 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001356 (*fieldp)->name = strdup(str);
1357 if ((*fieldp)->name == NULL)
1358 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001359 if (*str != '[')
1360 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001361 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001362
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001363 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001364 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001365 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 if (arg->name == NULL)
1367 return -ENOMEM;
1368 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001369 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001370}
1371
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001372/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001373int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001374{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001375 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001377
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001378 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379 if (!argv) {
1380 pr_debug("Failed to split arguments.\n");
1381 return -ENOMEM;
1382 }
1383 if (argc - 1 > MAX_PROBE_ARGS) {
1384 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1385 ret = -ERANGE;
1386 goto out;
1387 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001388 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001389 ret = parse_perf_probe_point(argv[0], pev);
1390 if (ret < 0)
1391 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001392
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001393 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001394 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001395 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1396 if (pev->args == NULL) {
1397 ret = -ENOMEM;
1398 goto out;
1399 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1401 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1402 if (ret >= 0 &&
1403 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405 " kretprobe.\n");
1406 ret = -EINVAL;
1407 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001408 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001409out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001410 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411
1412 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001413}
1414
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001415/* Return true if this perf_probe_event requires debuginfo */
1416bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001417{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001418 int i;
1419
1420 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1421 return true;
1422
1423 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001424 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001425 return true;
1426
1427 return false;
1428}
1429
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301430/* Parse probe_events event into struct probe_point */
1431static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001432 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001433{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301434 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001435 char pr;
1436 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001437 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 int ret, i, argc;
1439 char **argv;
1440
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301441 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001442 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443 if (!argv) {
1444 pr_debug("Failed to split arguments.\n");
1445 return -ENOMEM;
1446 }
1447 if (argc < 2) {
1448 semantic_error("Too few probe arguments.\n");
1449 ret = -ERANGE;
1450 goto out;
1451 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001452
1453 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001454 argv0_str = strdup(argv[0]);
1455 if (argv0_str == NULL) {
1456 ret = -ENOMEM;
1457 goto out;
1458 }
1459 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1460 fmt2_str = strtok_r(NULL, "/", &fmt);
1461 fmt3_str = strtok_r(NULL, " \t", &fmt);
1462 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1463 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 semantic_error("Failed to parse event name: %s\n", argv[0]);
1465 ret = -EINVAL;
1466 goto out;
1467 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001468 pr = fmt1_str[0];
1469 tev->group = strdup(fmt2_str);
1470 tev->event = strdup(fmt3_str);
1471 if (tev->group == NULL || tev->event == NULL) {
1472 ret = -ENOMEM;
1473 goto out;
1474 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001476
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001477 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001479 /* Scan module name(if there), function name and offset */
1480 p = strchr(argv[1], ':');
1481 if (p) {
1482 tp->module = strndup(argv[1], p - argv[1]);
1483 p++;
1484 } else
1485 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001486 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001487 if (fmt1_str[0] == '0') /* only the address started with 0x */
1488 tp->address = strtoul(fmt1_str, NULL, 0);
1489 else {
1490 /* Only the symbol-based probe has offset */
1491 tp->symbol = strdup(fmt1_str);
1492 if (tp->symbol == NULL) {
1493 ret = -ENOMEM;
1494 goto out;
1495 }
1496 fmt2_str = strtok_r(NULL, "", &fmt);
1497 if (fmt2_str == NULL)
1498 tp->offset = 0;
1499 else
1500 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001501 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001503 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301504 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001505 if (tev->args == NULL) {
1506 ret = -ENOMEM;
1507 goto out;
1508 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001510 p = strchr(argv[i + 2], '=');
1511 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001512 *p++ = '\0';
1513 else
1514 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001515 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001516 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001517 tev->args[i].value = strdup(p);
1518 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1519 ret = -ENOMEM;
1520 goto out;
1521 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001522 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 ret = 0;
1524out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001525 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001526 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001528}
1529
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001530/* Compose only probe arg */
1531int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1532{
1533 struct perf_probe_arg_field *field = pa->field;
1534 int ret;
1535 char *tmp = buf;
1536
Masami Hiramatsu48481932010-04-12 13:16:53 -04001537 if (pa->name && pa->var)
1538 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1539 else
1540 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001541 if (ret <= 0)
1542 goto error;
1543 tmp += ret;
1544 len -= ret;
1545
1546 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001547 if (field->name[0] == '[')
1548 ret = e_snprintf(tmp, len, "%s", field->name);
1549 else
1550 ret = e_snprintf(tmp, len, "%s%s",
1551 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001552 if (ret <= 0)
1553 goto error;
1554 tmp += ret;
1555 len -= ret;
1556 field = field->next;
1557 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001558
1559 if (pa->type) {
1560 ret = e_snprintf(tmp, len, ":%s", pa->type);
1561 if (ret <= 0)
1562 goto error;
1563 tmp += ret;
1564 len -= ret;
1565 }
1566
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001567 return tmp - buf;
1568error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001569 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001570 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001571}
1572
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573/* Compose only probe point (not argument) */
1574static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001575{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001576 char *buf, *tmp;
1577 char offs[32] = "", line[32] = "", file[32] = "";
1578 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001579
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001580 buf = zalloc(MAX_CMDLEN);
1581 if (buf == NULL) {
1582 ret = -ENOMEM;
1583 goto error;
1584 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001585 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001586 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587 if (ret <= 0)
1588 goto error;
1589 }
1590 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001591 ret = e_snprintf(line, 32, ":%d", pp->line);
1592 if (ret <= 0)
1593 goto error;
1594 }
1595 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001596 tmp = pp->file;
1597 len = strlen(tmp);
1598 if (len > 30) {
1599 tmp = strchr(pp->file + len - 30, '/');
1600 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1601 }
1602 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001603 if (ret <= 0)
1604 goto error;
1605 }
1606
1607 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001608 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1609 offs, pp->retprobe ? "%return" : "", line,
1610 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001611 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001612 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001613 if (ret <= 0)
1614 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001615
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001616 return buf;
1617error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001618 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001619 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001620 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621}
1622
1623#if 0
1624char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1625{
1626 char *buf;
1627 int i, len, ret;
1628
1629 buf = synthesize_perf_probe_point(&pev->point);
1630 if (!buf)
1631 return NULL;
1632
1633 len = strlen(buf);
1634 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001635 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 pev->args[i].name);
1637 if (ret <= 0) {
1638 free(buf);
1639 return NULL;
1640 }
1641 len += ret;
1642 }
1643
1644 return buf;
1645}
1646#endif
1647
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301648static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 char **buf, size_t *buflen,
1650 int depth)
1651{
1652 int ret;
1653 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301654 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001655 buflen, depth + 1);
1656 if (depth < 0)
1657 goto out;
1658 }
1659
1660 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1661 if (ret < 0)
1662 depth = ret;
1663 else {
1664 *buf += ret;
1665 *buflen -= ret;
1666 }
1667out:
1668 return depth;
1669
1670}
1671
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301672static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001673 char *buf, size_t buflen)
1674{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301675 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001676 int ret, depth = 0;
1677 char *tmp = buf;
1678
1679 /* Argument name or separator */
1680 if (arg->name)
1681 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1682 else
1683 ret = e_snprintf(buf, buflen, " ");
1684 if (ret < 0)
1685 return ret;
1686 buf += ret;
1687 buflen -= ret;
1688
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001689 /* Special case: @XXX */
1690 if (arg->value[0] == '@' && arg->ref)
1691 ref = ref->next;
1692
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001693 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001694 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301695 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 &buflen, 1);
1697 if (depth < 0)
1698 return depth;
1699 }
1700
1701 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001702 if (arg->value[0] == '@' && arg->ref)
1703 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1704 arg->ref->offset);
1705 else
1706 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707 if (ret < 0)
1708 return ret;
1709 buf += ret;
1710 buflen -= ret;
1711
1712 /* Closing */
1713 while (depth--) {
1714 ret = e_snprintf(buf, buflen, ")");
1715 if (ret < 0)
1716 return ret;
1717 buf += ret;
1718 buflen -= ret;
1719 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001720 /* Print argument type */
1721 if (arg->type) {
1722 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1723 if (ret <= 0)
1724 return ret;
1725 buf += ret;
1726 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727
1728 return buf - tmp;
1729}
1730
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301731char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001732{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301733 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734 char *buf;
1735 int i, len, ret;
1736
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001737 buf = zalloc(MAX_CMDLEN);
1738 if (buf == NULL)
1739 return NULL;
1740
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001741 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1742 tev->group, tev->event);
1743 if (len <= 0)
1744 goto error;
1745
1746 /* Uprobes must have tp->address and tp->module */
1747 if (tev->uprobes && (!tp->address || !tp->module))
1748 goto error;
1749
1750 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301751 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001752 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1753 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301754 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001755 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301756 tp->module ?: "", tp->module ? ":" : "",
1757 tp->symbol, tp->offset);
1758
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001759 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001760 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001761 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762
1763 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301764 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001766 if (ret <= 0)
1767 goto error;
1768 len += ret;
1769 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001770
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001772error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773 free(buf);
1774 return NULL;
1775}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001776
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001777static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1778 struct perf_probe_point *pp,
1779 bool is_kprobe)
1780{
1781 struct symbol *sym = NULL;
1782 struct map *map;
1783 u64 addr;
1784 int ret = -ENOENT;
1785
1786 if (!is_kprobe) {
1787 map = dso__new_map(tp->module);
1788 if (!map)
1789 goto out;
1790 addr = tp->address;
1791 sym = map__find_symbol(map, addr, NULL);
1792 } else {
1793 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1794 if (addr) {
1795 addr += tp->offset;
1796 sym = __find_kernel_function(addr, &map);
1797 }
1798 }
1799 if (!sym)
1800 goto out;
1801
1802 pp->retprobe = tp->retprobe;
1803 pp->offset = addr - map->unmap_ip(map, sym->start);
1804 pp->function = strdup(sym->name);
1805 ret = pp->function ? 0 : -ENOMEM;
1806
1807out:
1808 if (map && !is_kprobe) {
1809 dso__delete(map->dso);
1810 map__delete(map);
1811 }
1812
1813 return ret;
1814}
1815
1816static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1817 struct perf_probe_point *pp,
1818 bool is_kprobe)
1819{
1820 char buf[128];
1821 int ret;
1822
1823 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1824 if (!ret)
1825 return 0;
1826 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1827 if (!ret)
1828 return 0;
1829
1830 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1831
1832 if (tp->symbol) {
1833 pp->function = strdup(tp->symbol);
1834 pp->offset = tp->offset;
1835 } else if (!tp->module && !is_kprobe) {
1836 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1837 if (ret < 0)
1838 return ret;
1839 pp->function = strdup(buf);
1840 pp->offset = 0;
1841 }
1842 if (pp->function == NULL)
1843 return -ENOMEM;
1844
1845 pp->retprobe = tp->retprobe;
1846
1847 return 0;
1848}
1849
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301850static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301851 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001852{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001853 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001854 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001856 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001857 pev->event = strdup(tev->event);
1858 pev->group = strdup(tev->group);
1859 if (pev->event == NULL || pev->group == NULL)
1860 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001861
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001862 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001863 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001864 if (ret < 0)
1865 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001866
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001867 /* Convert trace_arg to probe_arg */
1868 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001869 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1870 if (pev->args == NULL)
1871 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001872 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001874 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001875 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301876 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001877 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001878 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001880 if (pev->args[i].name == NULL && ret >= 0)
1881 ret = -ENOMEM;
1882 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001883
1884 if (ret < 0)
1885 clear_perf_probe_event(pev);
1886
1887 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888}
1889
1890void clear_perf_probe_event(struct perf_probe_event *pev)
1891{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001892 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893 int i;
1894
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001895 free(pev->event);
1896 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001897 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001898
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001899 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001900 free(pev->args[i].name);
1901 free(pev->args[i].var);
1902 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001903 field = pev->args[i].field;
1904 while (field) {
1905 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001906 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001907 free(field);
1908 field = next;
1909 }
1910 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001911 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912 memset(pev, 0, sizeof(*pev));
1913}
1914
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301915static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001916{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301917 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918 int i;
1919
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001920 free(tev->event);
1921 free(tev->group);
1922 free(tev->point.symbol);
1923 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001924 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001925 free(tev->args[i].name);
1926 free(tev->args[i].value);
1927 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928 ref = tev->args[i].ref;
1929 while (ref) {
1930 next = ref->next;
1931 free(ref);
1932 ref = next;
1933 }
1934 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001935 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001936 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001937}
1938
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001939static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301940{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001941 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301942
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001943 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301944 const char *config;
1945
1946 if (!is_kprobe)
1947 config = "CONFIG_UPROBE_EVENTS";
1948 else
1949 config = "CONFIG_KPROBE_EVENTS";
1950
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001951 pr_warning("%cprobe_events file does not exist"
1952 " - please rebuild kernel with %s.\n",
1953 is_kprobe ? 'k' : 'u', config);
1954 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001955 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001956 else
1957 pr_warning("Failed to open %cprobe_events: %s\n",
1958 is_kprobe ? 'k' : 'u',
1959 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301960}
1961
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001962static void print_both_open_warning(int kerr, int uerr)
1963{
1964 /* Both kprobes and uprobes are disabled, warn it. */
1965 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001966 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001967 else if (kerr == -ENOENT && uerr == -ENOENT)
1968 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1969 "or/and CONFIG_UPROBE_EVENTS.\n");
1970 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001971 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001972 pr_warning("Failed to open kprobe events: %s.\n",
1973 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1974 pr_warning("Failed to open uprobe events: %s.\n",
1975 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1976 }
1977}
1978
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001979static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001980{
1981 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001982 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001983 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001984 int ret;
1985
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001986 __debugfs = tracefs_find_mountpoint();
1987 if (__debugfs == NULL) {
1988 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001989
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001990 __debugfs = debugfs_find_mountpoint();
1991 if (__debugfs == NULL)
1992 return -ENOTSUP;
1993 }
1994
1995 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1996 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001997 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001998 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001999 if (readwrite && !probe_event_dry_run)
2000 ret = open(buf, O_RDWR, O_APPEND);
2001 else
2002 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002003
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302004 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002005 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002006 }
2007 return ret;
2008}
2009
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302010static int open_kprobe_events(bool readwrite)
2011{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002012 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302013}
2014
2015static int open_uprobe_events(bool readwrite)
2016{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002017 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302018}
2019
2020/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302021static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002022{
2023 int ret, idx;
2024 FILE *fp;
2025 char buf[MAX_CMDLEN];
2026 char *p;
2027 struct strlist *sl;
2028
2029 sl = strlist__new(true, NULL);
2030
2031 fp = fdopen(dup(fd), "r");
2032 while (!feof(fp)) {
2033 p = fgets(buf, MAX_CMDLEN, fp);
2034 if (!p)
2035 break;
2036
2037 idx = strlen(p) - 1;
2038 if (p[idx] == '\n')
2039 p[idx] = '\0';
2040 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002041 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002042 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002043 strlist__delete(sl);
2044 return NULL;
2045 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002046 }
2047 fclose(fp);
2048
2049 return sl;
2050}
2051
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002052struct kprobe_blacklist_node {
2053 struct list_head list;
2054 unsigned long start;
2055 unsigned long end;
2056 char *symbol;
2057};
2058
2059static void kprobe_blacklist__delete(struct list_head *blacklist)
2060{
2061 struct kprobe_blacklist_node *node;
2062
2063 while (!list_empty(blacklist)) {
2064 node = list_first_entry(blacklist,
2065 struct kprobe_blacklist_node, list);
2066 list_del(&node->list);
2067 free(node->symbol);
2068 free(node);
2069 }
2070}
2071
2072static int kprobe_blacklist__load(struct list_head *blacklist)
2073{
2074 struct kprobe_blacklist_node *node;
2075 const char *__debugfs = debugfs_find_mountpoint();
2076 char buf[PATH_MAX], *p;
2077 FILE *fp;
2078 int ret;
2079
2080 if (__debugfs == NULL)
2081 return -ENOTSUP;
2082
2083 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2084 if (ret < 0)
2085 return ret;
2086
2087 fp = fopen(buf, "r");
2088 if (!fp)
2089 return -errno;
2090
2091 ret = 0;
2092 while (fgets(buf, PATH_MAX, fp)) {
2093 node = zalloc(sizeof(*node));
2094 if (!node) {
2095 ret = -ENOMEM;
2096 break;
2097 }
2098 INIT_LIST_HEAD(&node->list);
2099 list_add_tail(&node->list, blacklist);
2100 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2101 ret = -EINVAL;
2102 break;
2103 }
2104 p = strchr(buf, '\t');
2105 if (p) {
2106 p++;
2107 if (p[strlen(p) - 1] == '\n')
2108 p[strlen(p) - 1] = '\0';
2109 } else
2110 p = (char *)"unknown";
2111 node->symbol = strdup(p);
2112 if (!node->symbol) {
2113 ret = -ENOMEM;
2114 break;
2115 }
2116 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2117 node->start, node->end, node->symbol);
2118 ret++;
2119 }
2120 if (ret < 0)
2121 kprobe_blacklist__delete(blacklist);
2122 fclose(fp);
2123
2124 return ret;
2125}
2126
2127static struct kprobe_blacklist_node *
2128kprobe_blacklist__find_by_address(struct list_head *blacklist,
2129 unsigned long address)
2130{
2131 struct kprobe_blacklist_node *node;
2132
2133 list_for_each_entry(node, blacklist, list) {
2134 if (node->start <= address && address <= node->end)
2135 return node;
2136 }
2137
2138 return NULL;
2139}
2140
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002141/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002142static int show_perf_probe_event(struct perf_probe_event *pev,
2143 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002144{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002145 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002146 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002147 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002148
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002149 /* Synthesize only event probe point */
2150 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002151 if (!place)
2152 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002153
2154 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002155 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002156 return ret;
2157
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002158 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002159 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002160 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002161
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002162 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002163 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002164 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002165 ret = synthesize_perf_probe_arg(&pev->args[i],
2166 buf, 128);
2167 if (ret < 0)
2168 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002169 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002170 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002171 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002172 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002173 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002174 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002175}
2176
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302177static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002178{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302179 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302180 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002181 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002182 struct strlist *rawlist;
2183 struct str_node *ent;
2184
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 memset(&tev, 0, sizeof(tev));
2186 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002187
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302188 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002189 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002190 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002191
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002192 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302193 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002194 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302195 ret = convert_to_perf_probe_event(&tev, &pev,
2196 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002197 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002198 ret = show_perf_probe_event(&pev,
2199 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002200 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002201 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302202 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 if (ret < 0)
2204 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002205 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002206 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207
2208 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002209}
2210
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302211/* List up current perf-probe events */
2212int show_perf_probe_events(void)
2213{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002214 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215
2216 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002218 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219 if (ret < 0)
2220 return ret;
2221
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002222 kp_fd = open_kprobe_events(false);
2223 if (kp_fd >= 0) {
2224 ret = __show_perf_probe_events(kp_fd, true);
2225 close(kp_fd);
2226 if (ret < 0)
2227 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302228 }
2229
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002230 up_fd = open_uprobe_events(false);
2231 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002232 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002233 ret = kp_fd;
2234 goto out;
2235 }
2236
2237 if (up_fd >= 0) {
2238 ret = __show_perf_probe_events(up_fd, false);
2239 close(up_fd);
2240 }
2241out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002242 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302243 return ret;
2244}
2245
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002246/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302247static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002248{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002249 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002250 struct strlist *sl, *rawlist;
2251 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302252 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002253 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002254
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002255 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302256 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002257 if (!rawlist)
2258 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002259 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002260 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302261 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002262 if (ret < 0)
2263 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002264 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002265 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2266 tev.event);
2267 if (ret >= 0)
2268 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002269 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002270 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302271 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 if (ret < 0)
2273 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002274 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002275 strlist__delete(rawlist);
2276
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002277 if (ret < 0) {
2278 strlist__delete(sl);
2279 return NULL;
2280 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002281 return sl;
2282}
2283
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302284static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002285{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002286 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302287 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002288 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002289
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002290 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302291 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002292 return -EINVAL;
2293 }
2294
Masami Hiramatsufa282442009-12-08 17:03:23 -05002295 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002296 if (!probe_event_dry_run) {
2297 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002298 if (ret <= 0) {
2299 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002300 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002301 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002302 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002303 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002304 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002305 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002306}
2307
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002308static int get_new_event_name(char *buf, size_t len, const char *base,
2309 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002310{
2311 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002312
2313 /* Try no suffix */
2314 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002315 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002316 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002317 return ret;
2318 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002319 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002320 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002321
Masami Hiramatsud761b082009-12-15 10:32:25 -05002322 if (!allow_suffix) {
2323 pr_warning("Error: event \"%s\" already exists. "
2324 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002325 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002326 }
2327
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002328 /* Try to add suffix */
2329 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002330 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002331 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002332 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002333 return ret;
2334 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002335 if (!strlist__has_entry(namelist, buf))
2336 break;
2337 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002338 if (i == MAX_EVENT_INDEX) {
2339 pr_warning("Too many events are on the same function.\n");
2340 ret = -ERANGE;
2341 }
2342
2343 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002344}
2345
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002346/* Warn if the current kernel's uprobe implementation is old */
2347static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2348{
2349 int i;
2350 char *buf = synthesize_probe_trace_command(tev);
2351
2352 /* Old uprobe event doesn't support memory dereference */
2353 if (!tev->uprobes || tev->nargs == 0 || !buf)
2354 goto out;
2355
2356 for (i = 0; i < tev->nargs; i++)
2357 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2358 pr_warning("Please upgrade your kernel to at least "
2359 "3.14 to have access to feature %s\n",
2360 tev->args[i].value);
2361 break;
2362 }
2363out:
2364 free(buf);
2365}
2366
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302367static int __add_probe_trace_events(struct perf_probe_event *pev,
2368 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002369 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002370{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002371 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302372 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002373 char buf[64];
2374 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002375 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002376 LIST_HEAD(blacklist);
2377 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002378
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302379 if (pev->uprobes)
2380 fd = open_uprobe_events(true);
2381 else
2382 fd = open_kprobe_events(true);
2383
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002384 if (fd < 0) {
2385 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002386 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002387 }
2388
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002389 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302390 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002391 if (!namelist) {
2392 pr_debug("Failed to get current event list.\n");
2393 return -EIO;
2394 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002395 /* Get kprobe blacklist if exists */
2396 if (!pev->uprobes) {
2397 ret = kprobe_blacklist__load(&blacklist);
2398 if (ret < 0)
2399 pr_debug("No kprobe blacklist support, ignored\n");
2400 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002401
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002403 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002404 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002405 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002406 /* Ensure that the address is NOT blacklisted */
2407 node = kprobe_blacklist__find_by_address(&blacklist,
2408 tev->point.address);
2409 if (node) {
2410 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2411 continue;
2412 }
2413
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002414 if (pev->event)
2415 event = pev->event;
2416 else
2417 if (pev->point.function)
2418 event = pev->point.function;
2419 else
2420 event = tev->point.symbol;
2421 if (pev->group)
2422 group = pev->group;
2423 else
2424 group = PERFPROBE_GROUP;
2425
2426 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 ret = get_new_event_name(buf, 64, event,
2428 namelist, allow_suffix);
2429 if (ret < 0)
2430 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002431 event = buf;
2432
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002433 tev->event = strdup(event);
2434 tev->group = strdup(group);
2435 if (tev->event == NULL || tev->group == NULL) {
2436 ret = -ENOMEM;
2437 break;
2438 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302439 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002440 if (ret < 0)
2441 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002442 /* Add added event name to namelist */
2443 strlist__add(namelist, event);
2444
2445 /* Trick here - save current event/group */
2446 event = pev->event;
2447 group = pev->group;
2448 pev->event = tev->event;
2449 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002450 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002451 /* Trick here - restore current event/group */
2452 pev->event = (char *)event;
2453 pev->group = (char *)group;
2454
2455 /*
2456 * Probes after the first probe which comes from same
2457 * user input are always allowed to add suffix, because
2458 * there might be several addresses corresponding to
2459 * one code line.
2460 */
2461 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002462 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002463 if (ret == -EINVAL && pev->uprobes)
2464 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002465
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002466 /* Note that it is possible to skip all events because of blacklist */
2467 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002468 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002469 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2470 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002471 tev->event);
2472 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002473
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002474 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002475 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002476 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002477 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002478}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002479
Namhyung Kim564c62a2015-01-14 20:18:07 +09002480static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002481{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002482 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002483 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002484
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002485 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002486 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002487 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002488
2489 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002490}
2491
2492#define strdup_or_goto(str, label) \
2493 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2494
2495/*
2496 * Find probe function addresses from map.
2497 * Return an error or the number of found probe_trace_event
2498 */
2499static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2500 struct probe_trace_event **tevs,
2501 int max_tevs, const char *target)
2502{
2503 struct map *map = NULL;
2504 struct kmap *kmap = NULL;
2505 struct ref_reloc_sym *reloc_sym = NULL;
2506 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002507 struct probe_trace_event *tev;
2508 struct perf_probe_point *pp = &pev->point;
2509 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002510 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002511 int ret, i;
2512
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002513 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002514 if (!map) {
2515 ret = -EINVAL;
2516 goto out;
2517 }
2518
2519 /*
2520 * Load matched symbols: Since the different local symbols may have
2521 * same name but different addresses, this lists all the symbols.
2522 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002523 num_matched_functions = find_probe_functions(map, pp->function);
2524 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002525 pr_err("Failed to find symbol %s in %s\n", pp->function,
2526 target ? : "kernel");
2527 ret = -ENOENT;
2528 goto out;
2529 } else if (num_matched_functions > max_tevs) {
2530 pr_err("Too many functions matched in %s\n",
2531 target ? : "kernel");
2532 ret = -E2BIG;
2533 goto out;
2534 }
2535
Namhyung Kim25dd9172015-01-14 20:18:08 +09002536 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002537 kmap = map__kmap(map);
2538 reloc_sym = kmap->ref_reloc_sym;
2539 if (!reloc_sym) {
2540 pr_warning("Relocated base symbol is not found!\n");
2541 ret = -EINVAL;
2542 goto out;
2543 }
2544 }
2545
2546 /* Setup result trace-probe-events */
2547 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2548 if (!*tevs) {
2549 ret = -ENOMEM;
2550 goto out;
2551 }
2552
2553 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002554
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002555 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002556 tev = (*tevs) + ret;
2557 tp = &tev->point;
2558 if (ret == num_matched_functions) {
2559 pr_warning("Too many symbols are listed. Skip it.\n");
2560 break;
2561 }
2562 ret++;
2563
2564 if (pp->offset > sym->end - sym->start) {
2565 pr_warning("Offset %ld is bigger than the size of %s\n",
2566 pp->offset, sym->name);
2567 ret = -ENOENT;
2568 goto err_out;
2569 }
2570 /* Add one probe point */
2571 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2572 if (reloc_sym) {
2573 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2574 tp->offset = tp->address - reloc_sym->addr;
2575 } else {
2576 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2577 tp->offset = pp->offset;
2578 }
2579 tp->retprobe = pp->retprobe;
2580 if (target)
2581 tev->point.module = strdup_or_goto(target, nomem_out);
2582 tev->uprobes = pev->uprobes;
2583 tev->nargs = pev->nargs;
2584 if (tev->nargs) {
2585 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2586 tev->nargs);
2587 if (tev->args == NULL)
2588 goto nomem_out;
2589 }
2590 for (i = 0; i < tev->nargs; i++) {
2591 if (pev->args[i].name)
2592 tev->args[i].name =
2593 strdup_or_goto(pev->args[i].name,
2594 nomem_out);
2595
2596 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2597 nomem_out);
2598 if (pev->args[i].type)
2599 tev->args[i].type =
2600 strdup_or_goto(pev->args[i].type,
2601 nomem_out);
2602 }
2603 }
2604
2605out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002606 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002607 return ret;
2608
2609nomem_out:
2610 ret = -ENOMEM;
2611err_out:
2612 clear_probe_trace_events(*tevs, num_matched_functions);
2613 zfree(tevs);
2614 goto out;
2615}
2616
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302617static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2618 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302619 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002620{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002621 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002622
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002623 if (pev->uprobes && !pev->group) {
2624 /* Replace group name if not given */
2625 ret = convert_exec_to_group(target, &pev->group);
2626 if (ret != 0) {
2627 pr_warning("Failed to make a group name.\n");
2628 return ret;
2629 }
2630 }
2631
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002632 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302633 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002634 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002635 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002636
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002637 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002638}
2639
2640struct __event_package {
2641 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302642 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002643 int ntevs;
2644};
2645
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002646int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302647 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002648{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002649 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002650 struct __event_package *pkgs;
2651
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302652 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002653 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302654
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002655 if (pkgs == NULL)
2656 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002657
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002658 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002659 if (ret < 0) {
2660 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002661 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002662 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002663
2664 /* Loop 1: convert all events */
2665 for (i = 0; i < npevs; i++) {
2666 pkgs[i].pev = &pevs[i];
2667 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302668 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002669 &pkgs[i].tevs,
2670 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302671 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002672 if (ret < 0)
2673 goto end;
2674 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002675 }
2676
2677 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002678 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302679 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002680 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002681 if (ret < 0)
2682 break;
2683 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002684end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002685 /* Loop 3: cleanup and free trace events */
2686 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002687 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302688 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002689 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002690 }
2691 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002692 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002693
2694 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002695}
2696
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302697static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002698{
2699 char *p;
2700 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002701 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002702
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302703 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002704 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2705 if (ret < 0)
2706 goto error;
2707
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002708 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002709 if (!p) {
2710 pr_debug("Internal error: %s should have ':' but not.\n",
2711 ent->s);
2712 ret = -ENOTSUP;
2713 goto error;
2714 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002715 *p = '/';
2716
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002717 pr_debug("Writing event: %s\n", buf);
2718 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002719 if (ret < 0) {
2720 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002721 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002722 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002723
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002724 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002725 return 0;
2726error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002727 pr_warning("Failed to delete event: %s\n",
2728 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002729 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002730}
2731
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302732static int del_trace_probe_event(int fd, const char *buf,
2733 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002734{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002735 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302736 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002737
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002738 if (strpbrk(buf, "*?")) { /* Glob-exp */
2739 strlist__for_each_safe(ent, n, namelist)
2740 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302741 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002742 if (ret < 0)
2743 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002744 strlist__remove(namelist, ent);
2745 }
2746 } else {
2747 ent = strlist__find(namelist, buf);
2748 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302749 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002750 if (ret >= 0)
2751 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002752 }
2753 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002754
2755 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002756}
2757
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002758int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002759{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302760 int ret = -1, ufd = -1, kfd = -1;
2761 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002762 const char *group, *event;
2763 char *p, *str;
2764 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302765 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002766
Masami Hiramatsufa282442009-12-08 17:03:23 -05002767 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302768 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002769 if (kfd >= 0)
2770 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302771
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302772 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002773 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302774 unamelist = get_probe_trace_event_names(ufd, true);
2775
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002776 if (kfd < 0 && ufd < 0) {
2777 print_both_open_warning(kfd, ufd);
2778 goto error;
2779 }
2780
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302781 if (namelist == NULL && unamelist == NULL)
2782 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002783
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002784 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002785 str = strdup(ent->s);
2786 if (str == NULL) {
2787 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302788 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002789 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002790 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002791 p = strchr(str, ':');
2792 if (p) {
2793 group = str;
2794 *p = '\0';
2795 event = p + 1;
2796 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002797 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002798 event = str;
2799 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302800
2801 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2802 if (ret < 0) {
2803 pr_err("Failed to copy event.");
2804 free(str);
2805 goto error;
2806 }
2807
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002808 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302809
2810 if (namelist)
2811 ret = del_trace_probe_event(kfd, buf, namelist);
2812
2813 if (unamelist && ret != 0)
2814 ret = del_trace_probe_event(ufd, buf, unamelist);
2815
2816 if (ret != 0)
2817 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2818
Masami Hiramatsufa282442009-12-08 17:03:23 -05002819 free(str);
2820 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302821
2822error:
2823 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302824 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302825 close(kfd);
2826 }
2827
2828 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302829 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302830 close(ufd);
2831 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002832
2833 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002834}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302835
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002836/* TODO: don't use a global variable for filter ... */
2837static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002838
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002839/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002840 * If a symbol corresponds to a function with global binding and
2841 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002842 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002843static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002844 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002845{
Namhyung Kime578da32015-03-06 16:31:29 +09002846 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002847 return 0;
2848 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002849}
2850
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002851int show_available_funcs(const char *target, struct strfilter *_filter,
2852 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002854 struct map *map;
2855 int ret;
2856
2857 ret = init_symbol_maps(user);
2858 if (ret < 0)
2859 return ret;
2860
2861 /* Get a symbol map */
2862 if (user)
2863 map = dso__new_map(target);
2864 else
2865 map = kernel_get_module_map(target);
2866 if (!map) {
2867 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002868 return -EINVAL;
2869 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002870
2871 /* Load symbols with given filter */
2872 available_func_filter = _filter;
2873 if (map__load(map, filter_available_functions)) {
2874 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2875 goto end;
2876 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002877 if (!dso__sorted_by_name(map->dso, map->type))
2878 dso__sort_by_name(map->dso, map->type);
2879
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002880 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302881 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002882 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2883end:
2884 if (user) {
2885 dso__delete(map->dso);
2886 map__delete(map);
2887 }
2888 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302889
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002890 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302891}
2892