blob: e2bf620f98cb29b6d5cc89010fa0fbdc8ce589bc [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);
He Kuanga78604d2015-03-04 18:01:42 +0800793
794 /* Free old path when new path is assigned */
795 if (tmp != lr->path)
796 free(tmp);
797
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900798 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000799 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900800 return ret;
801 }
802
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300803 setup_pager();
804
805 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900806 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300807 lr->start - lr->offset);
808 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100809 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810
811 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400812 if (fp == NULL) {
813 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000814 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400815 return -errno;
816 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300817 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100818 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100819 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100820 if (ret < 0)
821 goto end;
822 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300823
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000824 intlist__for_each(ln, lr->line_list) {
825 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100826 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100827 if (ret < 0)
828 goto end;
829 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100830 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 if (ret < 0)
832 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300833 }
834
835 if (lr->end == INT_MAX)
836 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100837 while (l <= lr->end) {
838 ret = show_one_line_or_eof(fp, l++ - lr->offset);
839 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100840 break;
841 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300843 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300845}
846
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000847int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000848{
849 int ret;
850
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000851 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000852 if (ret < 0)
853 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900854 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000855 exit_symbol_maps();
856
857 return ret;
858}
859
Masami Hiramatsuff741782011-06-27 16:27:39 +0900860static int show_available_vars_at(struct debuginfo *dinfo,
861 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900862 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900863 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900864{
865 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900866 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900867 struct str_node *node;
868 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900869 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900870 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900871
872 buf = synthesize_perf_probe_point(&pev->point);
873 if (!buf)
874 return -EINVAL;
875 pr_debug("Searching variables at %s\n", buf);
876
Masami Hiramatsuff741782011-06-27 16:27:39 +0900877 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
878 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900879 if (!ret) { /* Not found, retry with an alternative */
880 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
881 if (!ret) {
882 ret = debuginfo__find_available_vars_at(dinfo, pev,
883 &vls, max_vls, externs);
884 /* Release the old probe_point */
885 clear_perf_probe_point(&tmp);
886 }
887 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900888 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000889 if (ret == 0 || ret == -ENOENT) {
890 pr_err("Failed to find the address of %s\n", buf);
891 ret = -ENOENT;
892 } else
893 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900894 goto end;
895 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000896
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900897 /* Some variables are found */
898 fprintf(stdout, "Available variables at %s\n", buf);
899 for (i = 0; i < ret; i++) {
900 vl = &vls[i];
901 /*
902 * A probe point might be converted to
903 * several trace points.
904 */
905 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
906 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300907 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900908 nvars = 0;
909 if (vl->vars) {
910 strlist__for_each(node, vl->vars) {
911 var = strchr(node->s, '\t') + 1;
912 if (strfilter__compare(_filter, var)) {
913 fprintf(stdout, "\t\t%s\n", node->s);
914 nvars++;
915 }
916 }
917 strlist__delete(vl->vars);
918 }
919 if (nvars == 0)
920 fprintf(stdout, "\t\t(No matched variables)\n");
921 }
922 free(vls);
923end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900924 free(buf);
925 return ret;
926}
927
928/* Show available variables on given probe point */
929int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900930 int max_vls, const char *module,
931 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900932{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900933 int i, ret = 0;
934 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900935
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000936 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900937 if (ret < 0)
938 return ret;
939
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000940 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900941 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000942 ret = -ENOENT;
943 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900944 }
945
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900946 setup_pager();
947
Masami Hiramatsuff741782011-06-27 16:27:39 +0900948 for (i = 0; i < npevs && ret >= 0; i++)
949 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900950 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900951
952 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000953out:
954 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900955 return ret;
956}
957
Ingo Molnar89fe8082013-09-30 12:07:11 +0200958#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300959
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000960static int
961find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
962 struct perf_probe_point *pp __maybe_unused,
963 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300964{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000965 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300966}
967
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530968static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300969 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300970 int max_tevs __maybe_unused,
971 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300972{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 if (perf_probe_event_need_dwarf(pev)) {
974 pr_warning("Debuginfo-analysis is not supported.\n");
975 return -ENOSYS;
976 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530977
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300978 return 0;
979}
980
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300981int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000982 const char *module __maybe_unused,
983 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300984{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400985 pr_warning("Debuginfo-analysis is not supported.\n");
986 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300987}
988
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300989int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
990 int npevs __maybe_unused, int max_vls __maybe_unused,
991 const char *module __maybe_unused,
992 struct strfilter *filter __maybe_unused,
993 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900994{
995 pr_warning("Debuginfo-analysis is not supported.\n");
996 return -ENOSYS;
997}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400998#endif
999
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001000void line_range__clear(struct line_range *lr)
1001{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001002 free(lr->function);
1003 free(lr->file);
1004 free(lr->path);
1005 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001006 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001007 memset(lr, 0, sizeof(*lr));
1008}
1009
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001010int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001011{
1012 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001013 lr->line_list = intlist__new(NULL);
1014 if (!lr->line_list)
1015 return -ENOMEM;
1016 else
1017 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001018}
1019
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001020static int parse_line_num(char **ptr, int *val, const char *what)
1021{
1022 const char *start = *ptr;
1023
1024 errno = 0;
1025 *val = strtol(*ptr, ptr, 0);
1026 if (errno || *ptr == start) {
1027 semantic_error("'%s' is not a valid number.\n", what);
1028 return -EINVAL;
1029 }
1030 return 0;
1031}
1032
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001033/*
1034 * Stuff 'lr' according to the line range described by 'arg'.
1035 * The line range syntax is described by:
1036 *
1037 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001038 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001039 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001041{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001042 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001043 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001044
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001045 if (!name)
1046 return -ENOMEM;
1047
1048 lr->start = 0;
1049 lr->end = INT_MAX;
1050
1051 range = strchr(name, ':');
1052 if (range) {
1053 *range++ = '\0';
1054
1055 err = parse_line_num(&range, &lr->start, "start line");
1056 if (err)
1057 goto err;
1058
1059 if (*range == '+' || *range == '-') {
1060 const char c = *range++;
1061
1062 err = parse_line_num(&range, &lr->end, "end line");
1063 if (err)
1064 goto err;
1065
1066 if (c == '+') {
1067 lr->end += lr->start;
1068 /*
1069 * Adjust the number of lines here.
1070 * If the number of lines == 1, the
1071 * the end of line should be equal to
1072 * the start of line.
1073 */
1074 lr->end--;
1075 }
1076 }
1077
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001078 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001079
1080 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001081 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001082 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001083 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001084 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001086 if (*range != '\0') {
1087 semantic_error("Tailing with invalid str '%s'.\n", range);
1088 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001090 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001091
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001092 file = strchr(name, '@');
1093 if (file) {
1094 *file = '\0';
1095 lr->file = strdup(++file);
1096 if (lr->file == NULL) {
1097 err = -ENOMEM;
1098 goto err;
1099 }
1100 lr->function = name;
1101 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001102 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001103 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001104 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105
1106 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001107err:
1108 free(name);
1109 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001110}
1111
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001112/* Check the name is good for event/group */
1113static bool check_event_name(const char *name)
1114{
1115 if (!isalpha(*name) && *name != '_')
1116 return false;
1117 while (*++name != '\0') {
1118 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1119 return false;
1120 }
1121 return true;
1122}
1123
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001124/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001125static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001126{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001127 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001128 char *ptr, *tmp;
1129 char c, nc = 0;
1130 /*
1131 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001132 * perf probe [EVENT=]SRC[:LN|;PTN]
1133 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001134 *
1135 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001136 */
1137
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001138 ptr = strpbrk(arg, ";=@+%");
1139 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001140 *ptr = '\0';
1141 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001142 if (strchr(arg, ':')) {
1143 semantic_error("Group name is not supported yet.\n");
1144 return -ENOTSUP;
1145 }
1146 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001147 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001148 "follow C symbol-naming rule.\n", arg);
1149 return -EINVAL;
1150 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001151 pev->event = strdup(arg);
1152 if (pev->event == NULL)
1153 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001154 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001155 arg = tmp;
1156 }
1157
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001158 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001159 if (ptr) {
1160 nc = *ptr;
1161 *ptr++ = '\0';
1162 }
1163
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001164 tmp = strdup(arg);
1165 if (tmp == NULL)
1166 return -ENOMEM;
1167
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001168 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001169 if (strchr(tmp, '.')) /* File */
1170 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001171 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001172 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001173
1174 /* Parse other options */
1175 while (ptr) {
1176 arg = ptr;
1177 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001178 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001179 pp->lazy_line = strdup(arg);
1180 if (pp->lazy_line == NULL)
1181 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001182 break;
1183 }
1184 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001185 if (ptr) {
1186 nc = *ptr;
1187 *ptr++ = '\0';
1188 }
1189 switch (c) {
1190 case ':': /* Line number */
1191 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001193 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194 " in line number.\n");
1195 return -EINVAL;
1196 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001197 break;
1198 case '+': /* Byte offset from a symbol */
1199 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001200 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001201 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001202 " in offset.\n");
1203 return -EINVAL;
1204 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205 break;
1206 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001207 if (pp->file) {
1208 semantic_error("SRC@SRC is not allowed.\n");
1209 return -EINVAL;
1210 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001211 pp->file = strdup(arg);
1212 if (pp->file == NULL)
1213 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001214 break;
1215 case '%': /* Probe places */
1216 if (strcmp(arg, "return") == 0) {
1217 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218 } else { /* Others not supported yet */
1219 semantic_error("%%%s is not supported.\n", arg);
1220 return -ENOTSUP;
1221 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001222 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 default: /* Buggy case */
1224 pr_err("This program has a bug at %s:%d.\n",
1225 __FILE__, __LINE__);
1226 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001227 break;
1228 }
1229 }
1230
1231 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001232 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001233 semantic_error("Lazy pattern can't be used with"
1234 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 return -EINVAL;
1236 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001237
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001238 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001239 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001240 return -EINVAL;
1241 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001242
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001243 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001244 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 return -EINVAL;
1246 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001247
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001249 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001250 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001251 return -EINVAL;
1252 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001253
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001255 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 return -EINVAL;
1257 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001260 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 return -EINVAL;
1262 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001263
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001264 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001265 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001266 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001267 return -EINVAL;
1268 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001269
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001270 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001271 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1272 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001274}
1275
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001276/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001278{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001279 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001280 struct perf_probe_arg_field **fieldp;
1281
1282 pr_debug("parsing arg: %s into ", str);
1283
Masami Hiramatsu48481932010-04-12 13:16:53 -04001284 tmp = strchr(str, '=');
1285 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001286 arg->name = strndup(str, tmp - str);
1287 if (arg->name == NULL)
1288 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001289 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001290 str = tmp + 1;
1291 }
1292
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001293 tmp = strchr(str, ':');
1294 if (tmp) { /* Type setting */
1295 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001296 arg->type = strdup(tmp + 1);
1297 if (arg->type == NULL)
1298 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001299 pr_debug("type:%s ", arg->type);
1300 }
1301
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001302 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001303 if (!is_c_varname(str) || !tmp) {
1304 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001305 arg->var = strdup(str);
1306 if (arg->var == NULL)
1307 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001308 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001309 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001310 }
1311
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001312 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001313 arg->var = strndup(str, tmp - str);
1314 if (arg->var == NULL)
1315 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001316 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001317 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001318 fieldp = &arg->field;
1319
1320 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001321 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1322 if (*fieldp == NULL)
1323 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001324 if (*tmp == '[') { /* Array */
1325 str = tmp;
1326 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001327 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001328 if (*tmp != ']' || tmp == str + 1) {
1329 semantic_error("Array index must be a"
1330 " number.\n");
1331 return -EINVAL;
1332 }
1333 tmp++;
1334 if (*tmp == '\0')
1335 tmp = NULL;
1336 } else { /* Structure */
1337 if (*tmp == '.') {
1338 str = tmp + 1;
1339 (*fieldp)->ref = false;
1340 } else if (tmp[1] == '>') {
1341 str = tmp + 2;
1342 (*fieldp)->ref = true;
1343 } else {
1344 semantic_error("Argument parse error: %s\n",
1345 str);
1346 return -EINVAL;
1347 }
1348 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001349 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001350 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001351 (*fieldp)->name = strndup(str, tmp - str);
1352 if ((*fieldp)->name == NULL)
1353 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001354 if (*str != '[')
1355 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001356 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1357 fieldp = &(*fieldp)->next;
1358 }
1359 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001360 (*fieldp)->name = strdup(str);
1361 if ((*fieldp)->name == NULL)
1362 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001363 if (*str != '[')
1364 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001365 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001366
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001367 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001368 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001369 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001370 if (arg->name == NULL)
1371 return -ENOMEM;
1372 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001373 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001374}
1375
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001376/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001378{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001379 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001381
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001382 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001383 if (!argv) {
1384 pr_debug("Failed to split arguments.\n");
1385 return -ENOMEM;
1386 }
1387 if (argc - 1 > MAX_PROBE_ARGS) {
1388 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1389 ret = -ERANGE;
1390 goto out;
1391 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001392 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 ret = parse_perf_probe_point(argv[0], pev);
1394 if (ret < 0)
1395 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001396
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001397 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001399 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1400 if (pev->args == NULL) {
1401 ret = -ENOMEM;
1402 goto out;
1403 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1405 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1406 if (ret >= 0 &&
1407 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001408 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001409 " kretprobe.\n");
1410 ret = -EINVAL;
1411 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001412 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001413out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001414 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415
1416 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001417}
1418
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001419/* Return true if this perf_probe_event requires debuginfo */
1420bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001421{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001422 int i;
1423
1424 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1425 return true;
1426
1427 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001428 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 return true;
1430
1431 return false;
1432}
1433
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301434/* Parse probe_events event into struct probe_point */
1435static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001436 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301438 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001439 char pr;
1440 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001441 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001442 int ret, i, argc;
1443 char **argv;
1444
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301445 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001446 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001447 if (!argv) {
1448 pr_debug("Failed to split arguments.\n");
1449 return -ENOMEM;
1450 }
1451 if (argc < 2) {
1452 semantic_error("Too few probe arguments.\n");
1453 ret = -ERANGE;
1454 goto out;
1455 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001456
1457 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001458 argv0_str = strdup(argv[0]);
1459 if (argv0_str == NULL) {
1460 ret = -ENOMEM;
1461 goto out;
1462 }
1463 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1464 fmt2_str = strtok_r(NULL, "/", &fmt);
1465 fmt3_str = strtok_r(NULL, " \t", &fmt);
1466 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1467 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468 semantic_error("Failed to parse event name: %s\n", argv[0]);
1469 ret = -EINVAL;
1470 goto out;
1471 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001472 pr = fmt1_str[0];
1473 tev->group = strdup(fmt2_str);
1474 tev->event = strdup(fmt3_str);
1475 if (tev->group == NULL || tev->event == NULL) {
1476 ret = -ENOMEM;
1477 goto out;
1478 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001479 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001480
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001483 /* Scan module name(if there), function name and offset */
1484 p = strchr(argv[1], ':');
1485 if (p) {
1486 tp->module = strndup(argv[1], p - argv[1]);
1487 p++;
1488 } else
1489 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001490 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001491 if (fmt1_str[0] == '0') /* only the address started with 0x */
1492 tp->address = strtoul(fmt1_str, NULL, 0);
1493 else {
1494 /* Only the symbol-based probe has offset */
1495 tp->symbol = strdup(fmt1_str);
1496 if (tp->symbol == NULL) {
1497 ret = -ENOMEM;
1498 goto out;
1499 }
1500 fmt2_str = strtok_r(NULL, "", &fmt);
1501 if (fmt2_str == NULL)
1502 tp->offset = 0;
1503 else
1504 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001505 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001506
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001507 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301508 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001509 if (tev->args == NULL) {
1510 ret = -ENOMEM;
1511 goto out;
1512 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001514 p = strchr(argv[i + 2], '=');
1515 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001516 *p++ = '\0';
1517 else
1518 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001519 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001521 tev->args[i].value = strdup(p);
1522 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1523 ret = -ENOMEM;
1524 goto out;
1525 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001526 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 ret = 0;
1528out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001529 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001530 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001531 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001532}
1533
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001534/* Compose only probe arg */
1535int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1536{
1537 struct perf_probe_arg_field *field = pa->field;
1538 int ret;
1539 char *tmp = buf;
1540
Masami Hiramatsu48481932010-04-12 13:16:53 -04001541 if (pa->name && pa->var)
1542 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1543 else
1544 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001545 if (ret <= 0)
1546 goto error;
1547 tmp += ret;
1548 len -= ret;
1549
1550 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001551 if (field->name[0] == '[')
1552 ret = e_snprintf(tmp, len, "%s", field->name);
1553 else
1554 ret = e_snprintf(tmp, len, "%s%s",
1555 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001556 if (ret <= 0)
1557 goto error;
1558 tmp += ret;
1559 len -= ret;
1560 field = field->next;
1561 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001562
1563 if (pa->type) {
1564 ret = e_snprintf(tmp, len, ":%s", pa->type);
1565 if (ret <= 0)
1566 goto error;
1567 tmp += ret;
1568 len -= ret;
1569 }
1570
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001571 return tmp - buf;
1572error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001573 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001575}
1576
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577/* Compose only probe point (not argument) */
1578static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001579{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001580 char *buf, *tmp;
1581 char offs[32] = "", line[32] = "", file[32] = "";
1582 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001583
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001584 buf = zalloc(MAX_CMDLEN);
1585 if (buf == NULL) {
1586 ret = -ENOMEM;
1587 goto error;
1588 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001590 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001591 if (ret <= 0)
1592 goto error;
1593 }
1594 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001595 ret = e_snprintf(line, 32, ":%d", pp->line);
1596 if (ret <= 0)
1597 goto error;
1598 }
1599 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001600 tmp = pp->file;
1601 len = strlen(tmp);
1602 if (len > 30) {
1603 tmp = strchr(pp->file + len - 30, '/');
1604 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1605 }
1606 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001607 if (ret <= 0)
1608 goto error;
1609 }
1610
1611 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001612 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1613 offs, pp->retprobe ? "%return" : "", line,
1614 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001615 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001616 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001617 if (ret <= 0)
1618 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001619
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001620 return buf;
1621error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001622 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001623 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001624 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625}
1626
1627#if 0
1628char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1629{
1630 char *buf;
1631 int i, len, ret;
1632
1633 buf = synthesize_perf_probe_point(&pev->point);
1634 if (!buf)
1635 return NULL;
1636
1637 len = strlen(buf);
1638 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001639 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001640 pev->args[i].name);
1641 if (ret <= 0) {
1642 free(buf);
1643 return NULL;
1644 }
1645 len += ret;
1646 }
1647
1648 return buf;
1649}
1650#endif
1651
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301652static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001653 char **buf, size_t *buflen,
1654 int depth)
1655{
1656 int ret;
1657 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301658 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001659 buflen, depth + 1);
1660 if (depth < 0)
1661 goto out;
1662 }
1663
1664 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1665 if (ret < 0)
1666 depth = ret;
1667 else {
1668 *buf += ret;
1669 *buflen -= ret;
1670 }
1671out:
1672 return depth;
1673
1674}
1675
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301676static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001677 char *buf, size_t buflen)
1678{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301679 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001680 int ret, depth = 0;
1681 char *tmp = buf;
1682
1683 /* Argument name or separator */
1684 if (arg->name)
1685 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1686 else
1687 ret = e_snprintf(buf, buflen, " ");
1688 if (ret < 0)
1689 return ret;
1690 buf += ret;
1691 buflen -= ret;
1692
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001693 /* Special case: @XXX */
1694 if (arg->value[0] == '@' && arg->ref)
1695 ref = ref->next;
1696
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001697 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001698 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301699 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700 &buflen, 1);
1701 if (depth < 0)
1702 return depth;
1703 }
1704
1705 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001706 if (arg->value[0] == '@' && arg->ref)
1707 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1708 arg->ref->offset);
1709 else
1710 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001711 if (ret < 0)
1712 return ret;
1713 buf += ret;
1714 buflen -= ret;
1715
1716 /* Closing */
1717 while (depth--) {
1718 ret = e_snprintf(buf, buflen, ")");
1719 if (ret < 0)
1720 return ret;
1721 buf += ret;
1722 buflen -= ret;
1723 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001724 /* Print argument type */
1725 if (arg->type) {
1726 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1727 if (ret <= 0)
1728 return ret;
1729 buf += ret;
1730 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001731
1732 return buf - tmp;
1733}
1734
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301735char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001736{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301737 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738 char *buf;
1739 int i, len, ret;
1740
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001741 buf = zalloc(MAX_CMDLEN);
1742 if (buf == NULL)
1743 return NULL;
1744
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001745 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1746 tev->group, tev->event);
1747 if (len <= 0)
1748 goto error;
1749
1750 /* Uprobes must have tp->address and tp->module */
1751 if (tev->uprobes && (!tp->address || !tp->module))
1752 goto error;
1753
1754 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301755 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001756 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1757 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301758 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001759 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301760 tp->module ?: "", tp->module ? ":" : "",
1761 tp->symbol, tp->offset);
1762
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001763 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001764 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001765 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766
1767 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301768 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001770 if (ret <= 0)
1771 goto error;
1772 len += ret;
1773 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001774
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001775 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001776error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001777 free(buf);
1778 return NULL;
1779}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001780
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001781static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1782 struct perf_probe_point *pp,
1783 bool is_kprobe)
1784{
1785 struct symbol *sym = NULL;
1786 struct map *map;
1787 u64 addr;
1788 int ret = -ENOENT;
1789
1790 if (!is_kprobe) {
1791 map = dso__new_map(tp->module);
1792 if (!map)
1793 goto out;
1794 addr = tp->address;
1795 sym = map__find_symbol(map, addr, NULL);
1796 } else {
1797 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1798 if (addr) {
1799 addr += tp->offset;
1800 sym = __find_kernel_function(addr, &map);
1801 }
1802 }
1803 if (!sym)
1804 goto out;
1805
1806 pp->retprobe = tp->retprobe;
1807 pp->offset = addr - map->unmap_ip(map, sym->start);
1808 pp->function = strdup(sym->name);
1809 ret = pp->function ? 0 : -ENOMEM;
1810
1811out:
1812 if (map && !is_kprobe) {
1813 dso__delete(map->dso);
1814 map__delete(map);
1815 }
1816
1817 return ret;
1818}
1819
1820static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1821 struct perf_probe_point *pp,
1822 bool is_kprobe)
1823{
1824 char buf[128];
1825 int ret;
1826
1827 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1828 if (!ret)
1829 return 0;
1830 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1831 if (!ret)
1832 return 0;
1833
1834 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1835
1836 if (tp->symbol) {
1837 pp->function = strdup(tp->symbol);
1838 pp->offset = tp->offset;
1839 } else if (!tp->module && !is_kprobe) {
1840 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1841 if (ret < 0)
1842 return ret;
1843 pp->function = strdup(buf);
1844 pp->offset = 0;
1845 }
1846 if (pp->function == NULL)
1847 return -ENOMEM;
1848
1849 pp->retprobe = tp->retprobe;
1850
1851 return 0;
1852}
1853
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301854static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301855 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001856{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001857 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001858 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001860 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001861 pev->event = strdup(tev->event);
1862 pev->group = strdup(tev->group);
1863 if (pev->event == NULL || pev->group == NULL)
1864 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001865
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001866 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001867 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001868 if (ret < 0)
1869 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001870
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001871 /* Convert trace_arg to probe_arg */
1872 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001873 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1874 if (pev->args == NULL)
1875 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001876 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001877 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001878 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301880 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001881 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001882 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001884 if (pev->args[i].name == NULL && ret >= 0)
1885 ret = -ENOMEM;
1886 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001887
1888 if (ret < 0)
1889 clear_perf_probe_event(pev);
1890
1891 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001892}
1893
1894void clear_perf_probe_event(struct perf_probe_event *pev)
1895{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001896 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897 int i;
1898
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001899 free(pev->event);
1900 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001901 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001902
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001903 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001904 free(pev->args[i].name);
1905 free(pev->args[i].var);
1906 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001907 field = pev->args[i].field;
1908 while (field) {
1909 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001910 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001911 free(field);
1912 field = next;
1913 }
1914 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001915 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001916 memset(pev, 0, sizeof(*pev));
1917}
1918
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301919static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001920{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301921 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001922 int i;
1923
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001924 free(tev->event);
1925 free(tev->group);
1926 free(tev->point.symbol);
1927 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001929 free(tev->args[i].name);
1930 free(tev->args[i].value);
1931 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001932 ref = tev->args[i].ref;
1933 while (ref) {
1934 next = ref->next;
1935 free(ref);
1936 ref = next;
1937 }
1938 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001939 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001940 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001941}
1942
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001943static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301944{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001945 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301946
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001947 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301948 const char *config;
1949
1950 if (!is_kprobe)
1951 config = "CONFIG_UPROBE_EVENTS";
1952 else
1953 config = "CONFIG_KPROBE_EVENTS";
1954
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001955 pr_warning("%cprobe_events file does not exist"
1956 " - please rebuild kernel with %s.\n",
1957 is_kprobe ? 'k' : 'u', config);
1958 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001959 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001960 else
1961 pr_warning("Failed to open %cprobe_events: %s\n",
1962 is_kprobe ? 'k' : 'u',
1963 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301964}
1965
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001966static void print_both_open_warning(int kerr, int uerr)
1967{
1968 /* Both kprobes and uprobes are disabled, warn it. */
1969 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001970 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001971 else if (kerr == -ENOENT && uerr == -ENOENT)
1972 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1973 "or/and CONFIG_UPROBE_EVENTS.\n");
1974 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001975 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001976 pr_warning("Failed to open kprobe events: %s.\n",
1977 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1978 pr_warning("Failed to open uprobe events: %s.\n",
1979 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1980 }
1981}
1982
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001983static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001984{
1985 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001986 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001987 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001988 int ret;
1989
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001990 __debugfs = tracefs_find_mountpoint();
1991 if (__debugfs == NULL) {
1992 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001993
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001994 __debugfs = debugfs_find_mountpoint();
1995 if (__debugfs == NULL)
1996 return -ENOTSUP;
1997 }
1998
1999 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2000 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002001 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002002 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002003 if (readwrite && !probe_event_dry_run)
2004 ret = open(buf, O_RDWR, O_APPEND);
2005 else
2006 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002007
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302008 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002009 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002010 }
2011 return ret;
2012}
2013
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302014static int open_kprobe_events(bool readwrite)
2015{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002016 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302017}
2018
2019static int open_uprobe_events(bool readwrite)
2020{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002021 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302022}
2023
2024/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302025static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002026{
2027 int ret, idx;
2028 FILE *fp;
2029 char buf[MAX_CMDLEN];
2030 char *p;
2031 struct strlist *sl;
2032
2033 sl = strlist__new(true, NULL);
2034
2035 fp = fdopen(dup(fd), "r");
2036 while (!feof(fp)) {
2037 p = fgets(buf, MAX_CMDLEN, fp);
2038 if (!p)
2039 break;
2040
2041 idx = strlen(p) - 1;
2042 if (p[idx] == '\n')
2043 p[idx] = '\0';
2044 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002045 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002046 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002047 strlist__delete(sl);
2048 return NULL;
2049 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002050 }
2051 fclose(fp);
2052
2053 return sl;
2054}
2055
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002056struct kprobe_blacklist_node {
2057 struct list_head list;
2058 unsigned long start;
2059 unsigned long end;
2060 char *symbol;
2061};
2062
2063static void kprobe_blacklist__delete(struct list_head *blacklist)
2064{
2065 struct kprobe_blacklist_node *node;
2066
2067 while (!list_empty(blacklist)) {
2068 node = list_first_entry(blacklist,
2069 struct kprobe_blacklist_node, list);
2070 list_del(&node->list);
2071 free(node->symbol);
2072 free(node);
2073 }
2074}
2075
2076static int kprobe_blacklist__load(struct list_head *blacklist)
2077{
2078 struct kprobe_blacklist_node *node;
2079 const char *__debugfs = debugfs_find_mountpoint();
2080 char buf[PATH_MAX], *p;
2081 FILE *fp;
2082 int ret;
2083
2084 if (__debugfs == NULL)
2085 return -ENOTSUP;
2086
2087 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2088 if (ret < 0)
2089 return ret;
2090
2091 fp = fopen(buf, "r");
2092 if (!fp)
2093 return -errno;
2094
2095 ret = 0;
2096 while (fgets(buf, PATH_MAX, fp)) {
2097 node = zalloc(sizeof(*node));
2098 if (!node) {
2099 ret = -ENOMEM;
2100 break;
2101 }
2102 INIT_LIST_HEAD(&node->list);
2103 list_add_tail(&node->list, blacklist);
2104 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2105 ret = -EINVAL;
2106 break;
2107 }
2108 p = strchr(buf, '\t');
2109 if (p) {
2110 p++;
2111 if (p[strlen(p) - 1] == '\n')
2112 p[strlen(p) - 1] = '\0';
2113 } else
2114 p = (char *)"unknown";
2115 node->symbol = strdup(p);
2116 if (!node->symbol) {
2117 ret = -ENOMEM;
2118 break;
2119 }
2120 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2121 node->start, node->end, node->symbol);
2122 ret++;
2123 }
2124 if (ret < 0)
2125 kprobe_blacklist__delete(blacklist);
2126 fclose(fp);
2127
2128 return ret;
2129}
2130
2131static struct kprobe_blacklist_node *
2132kprobe_blacklist__find_by_address(struct list_head *blacklist,
2133 unsigned long address)
2134{
2135 struct kprobe_blacklist_node *node;
2136
2137 list_for_each_entry(node, blacklist, list) {
2138 if (node->start <= address && address <= node->end)
2139 return node;
2140 }
2141
2142 return NULL;
2143}
2144
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002145/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002146static int show_perf_probe_event(struct perf_probe_event *pev,
2147 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002148{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002149 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002150 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002151 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002152
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002153 /* Synthesize only event probe point */
2154 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002155 if (!place)
2156 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002157
2158 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002159 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002160 return ret;
2161
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002162 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002163 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002164 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002165
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002166 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002167 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002168 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169 ret = synthesize_perf_probe_arg(&pev->args[i],
2170 buf, 128);
2171 if (ret < 0)
2172 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002173 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002174 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002175 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002176 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002177 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002178 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002179}
2180
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302181static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002182{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302183 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302184 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002186 struct strlist *rawlist;
2187 struct str_node *ent;
2188
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189 memset(&tev, 0, sizeof(tev));
2190 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002191
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302192 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002193 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002194 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002195
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002196 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302197 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002198 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302199 ret = convert_to_perf_probe_event(&tev, &pev,
2200 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002201 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002202 ret = show_perf_probe_event(&pev,
2203 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002204 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002205 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302206 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207 if (ret < 0)
2208 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002209 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002210 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002211
2212 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002213}
2214
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215/* List up current perf-probe events */
2216int show_perf_probe_events(void)
2217{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002218 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219
2220 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302221
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002222 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302223 if (ret < 0)
2224 return ret;
2225
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002226 kp_fd = open_kprobe_events(false);
2227 if (kp_fd >= 0) {
2228 ret = __show_perf_probe_events(kp_fd, true);
2229 close(kp_fd);
2230 if (ret < 0)
2231 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302232 }
2233
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002234 up_fd = open_uprobe_events(false);
2235 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002236 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002237 ret = kp_fd;
2238 goto out;
2239 }
2240
2241 if (up_fd >= 0) {
2242 ret = __show_perf_probe_events(up_fd, false);
2243 close(up_fd);
2244 }
2245out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002246 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302247 return ret;
2248}
2249
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002250/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302251static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002252{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002253 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002254 struct strlist *sl, *rawlist;
2255 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302256 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002257 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002258
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002259 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302260 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002261 if (!rawlist)
2262 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002263 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002264 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302265 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002266 if (ret < 0)
2267 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002268 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002269 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2270 tev.event);
2271 if (ret >= 0)
2272 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002273 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002274 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302275 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002276 if (ret < 0)
2277 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002278 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002279 strlist__delete(rawlist);
2280
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002281 if (ret < 0) {
2282 strlist__delete(sl);
2283 return NULL;
2284 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002285 return sl;
2286}
2287
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302288static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002289{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002290 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302291 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002292 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002293
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002294 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302295 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002296 return -EINVAL;
2297 }
2298
Masami Hiramatsufa282442009-12-08 17:03:23 -05002299 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002300 if (!probe_event_dry_run) {
2301 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002302 if (ret <= 0) {
2303 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002304 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002305 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002306 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002307 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002308 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002309 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002310}
2311
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002312static int get_new_event_name(char *buf, size_t len, const char *base,
2313 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002314{
2315 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002316
2317 /* Try no suffix */
2318 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002320 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002321 return ret;
2322 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002323 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002324 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002325
Masami Hiramatsud761b082009-12-15 10:32:25 -05002326 if (!allow_suffix) {
2327 pr_warning("Error: event \"%s\" already exists. "
2328 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002329 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002330 }
2331
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002332 /* Try to add suffix */
2333 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002334 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002335 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002336 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002337 return ret;
2338 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002339 if (!strlist__has_entry(namelist, buf))
2340 break;
2341 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002342 if (i == MAX_EVENT_INDEX) {
2343 pr_warning("Too many events are on the same function.\n");
2344 ret = -ERANGE;
2345 }
2346
2347 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002348}
2349
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002350/* Warn if the current kernel's uprobe implementation is old */
2351static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2352{
2353 int i;
2354 char *buf = synthesize_probe_trace_command(tev);
2355
2356 /* Old uprobe event doesn't support memory dereference */
2357 if (!tev->uprobes || tev->nargs == 0 || !buf)
2358 goto out;
2359
2360 for (i = 0; i < tev->nargs; i++)
2361 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2362 pr_warning("Please upgrade your kernel to at least "
2363 "3.14 to have access to feature %s\n",
2364 tev->args[i].value);
2365 break;
2366 }
2367out:
2368 free(buf);
2369}
2370
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302371static int __add_probe_trace_events(struct perf_probe_event *pev,
2372 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002373 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002374{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002375 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302376 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002377 char buf[64];
2378 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002379 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002380 LIST_HEAD(blacklist);
2381 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002382
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302383 if (pev->uprobes)
2384 fd = open_uprobe_events(true);
2385 else
2386 fd = open_kprobe_events(true);
2387
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002388 if (fd < 0) {
2389 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002390 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002391 }
2392
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002393 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302394 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002395 if (!namelist) {
2396 pr_debug("Failed to get current event list.\n");
2397 return -EIO;
2398 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002399 /* Get kprobe blacklist if exists */
2400 if (!pev->uprobes) {
2401 ret = kprobe_blacklist__load(&blacklist);
2402 if (ret < 0)
2403 pr_debug("No kprobe blacklist support, ignored\n");
2404 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002405
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002406 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002407 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002408 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002409 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002410 /* Ensure that the address is NOT blacklisted */
2411 node = kprobe_blacklist__find_by_address(&blacklist,
2412 tev->point.address);
2413 if (node) {
2414 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2415 continue;
2416 }
2417
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002418 if (pev->event)
2419 event = pev->event;
2420 else
2421 if (pev->point.function)
2422 event = pev->point.function;
2423 else
2424 event = tev->point.symbol;
2425 if (pev->group)
2426 group = pev->group;
2427 else
2428 group = PERFPROBE_GROUP;
2429
2430 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002431 ret = get_new_event_name(buf, 64, event,
2432 namelist, allow_suffix);
2433 if (ret < 0)
2434 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002435 event = buf;
2436
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002437 tev->event = strdup(event);
2438 tev->group = strdup(group);
2439 if (tev->event == NULL || tev->group == NULL) {
2440 ret = -ENOMEM;
2441 break;
2442 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302443 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002444 if (ret < 0)
2445 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002446 /* Add added event name to namelist */
2447 strlist__add(namelist, event);
2448
2449 /* Trick here - save current event/group */
2450 event = pev->event;
2451 group = pev->group;
2452 pev->event = tev->event;
2453 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002454 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002455 /* Trick here - restore current event/group */
2456 pev->event = (char *)event;
2457 pev->group = (char *)group;
2458
2459 /*
2460 * Probes after the first probe which comes from same
2461 * user input are always allowed to add suffix, because
2462 * there might be several addresses corresponding to
2463 * one code line.
2464 */
2465 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002466 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002467 if (ret == -EINVAL && pev->uprobes)
2468 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002469
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002470 /* Note that it is possible to skip all events because of blacklist */
2471 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002472 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002473 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2474 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002475 tev->event);
2476 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002477
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002478 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002479 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002480 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002482}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002483
Namhyung Kim564c62a2015-01-14 20:18:07 +09002484static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002485{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002486 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002487 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002488
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002489 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002490 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002491 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002492
2493 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002494}
2495
2496#define strdup_or_goto(str, label) \
2497 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2498
2499/*
2500 * Find probe function addresses from map.
2501 * Return an error or the number of found probe_trace_event
2502 */
2503static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2504 struct probe_trace_event **tevs,
2505 int max_tevs, const char *target)
2506{
2507 struct map *map = NULL;
2508 struct kmap *kmap = NULL;
2509 struct ref_reloc_sym *reloc_sym = NULL;
2510 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002511 struct probe_trace_event *tev;
2512 struct perf_probe_point *pp = &pev->point;
2513 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002514 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002515 int ret, i;
2516
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002517 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002518 if (!map) {
2519 ret = -EINVAL;
2520 goto out;
2521 }
2522
2523 /*
2524 * Load matched symbols: Since the different local symbols may have
2525 * same name but different addresses, this lists all the symbols.
2526 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002527 num_matched_functions = find_probe_functions(map, pp->function);
2528 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002529 pr_err("Failed to find symbol %s in %s\n", pp->function,
2530 target ? : "kernel");
2531 ret = -ENOENT;
2532 goto out;
2533 } else if (num_matched_functions > max_tevs) {
2534 pr_err("Too many functions matched in %s\n",
2535 target ? : "kernel");
2536 ret = -E2BIG;
2537 goto out;
2538 }
2539
Namhyung Kim25dd9172015-01-14 20:18:08 +09002540 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002541 kmap = map__kmap(map);
2542 reloc_sym = kmap->ref_reloc_sym;
2543 if (!reloc_sym) {
2544 pr_warning("Relocated base symbol is not found!\n");
2545 ret = -EINVAL;
2546 goto out;
2547 }
2548 }
2549
2550 /* Setup result trace-probe-events */
2551 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2552 if (!*tevs) {
2553 ret = -ENOMEM;
2554 goto out;
2555 }
2556
2557 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002558
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002559 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002560 tev = (*tevs) + ret;
2561 tp = &tev->point;
2562 if (ret == num_matched_functions) {
2563 pr_warning("Too many symbols are listed. Skip it.\n");
2564 break;
2565 }
2566 ret++;
2567
2568 if (pp->offset > sym->end - sym->start) {
2569 pr_warning("Offset %ld is bigger than the size of %s\n",
2570 pp->offset, sym->name);
2571 ret = -ENOENT;
2572 goto err_out;
2573 }
2574 /* Add one probe point */
2575 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2576 if (reloc_sym) {
2577 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2578 tp->offset = tp->address - reloc_sym->addr;
2579 } else {
2580 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2581 tp->offset = pp->offset;
2582 }
2583 tp->retprobe = pp->retprobe;
2584 if (target)
2585 tev->point.module = strdup_or_goto(target, nomem_out);
2586 tev->uprobes = pev->uprobes;
2587 tev->nargs = pev->nargs;
2588 if (tev->nargs) {
2589 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2590 tev->nargs);
2591 if (tev->args == NULL)
2592 goto nomem_out;
2593 }
2594 for (i = 0; i < tev->nargs; i++) {
2595 if (pev->args[i].name)
2596 tev->args[i].name =
2597 strdup_or_goto(pev->args[i].name,
2598 nomem_out);
2599
2600 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2601 nomem_out);
2602 if (pev->args[i].type)
2603 tev->args[i].type =
2604 strdup_or_goto(pev->args[i].type,
2605 nomem_out);
2606 }
2607 }
2608
2609out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002610 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002611 return ret;
2612
2613nomem_out:
2614 ret = -ENOMEM;
2615err_out:
2616 clear_probe_trace_events(*tevs, num_matched_functions);
2617 zfree(tevs);
2618 goto out;
2619}
2620
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302621static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2622 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302623 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002624{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002625 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002626
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002627 if (pev->uprobes && !pev->group) {
2628 /* Replace group name if not given */
2629 ret = convert_exec_to_group(target, &pev->group);
2630 if (ret != 0) {
2631 pr_warning("Failed to make a group name.\n");
2632 return ret;
2633 }
2634 }
2635
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002636 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302637 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002638 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002639 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002640
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002641 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002642}
2643
2644struct __event_package {
2645 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302646 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002647 int ntevs;
2648};
2649
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002650int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302651 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002652{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002653 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002654 struct __event_package *pkgs;
2655
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302656 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002657 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302658
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002659 if (pkgs == NULL)
2660 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002661
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002662 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002663 if (ret < 0) {
2664 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002665 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002666 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002667
2668 /* Loop 1: convert all events */
2669 for (i = 0; i < npevs; i++) {
2670 pkgs[i].pev = &pevs[i];
2671 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302672 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002673 &pkgs[i].tevs,
2674 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302675 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002676 if (ret < 0)
2677 goto end;
2678 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002679 }
2680
2681 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002682 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302683 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002684 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002685 if (ret < 0)
2686 break;
2687 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002688end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002689 /* Loop 3: cleanup and free trace events */
2690 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002691 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302692 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002693 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002694 }
2695 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002696 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002697
2698 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002699}
2700
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302701static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002702{
2703 char *p;
2704 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002705 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002706
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302707 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002708 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2709 if (ret < 0)
2710 goto error;
2711
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002712 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002713 if (!p) {
2714 pr_debug("Internal error: %s should have ':' but not.\n",
2715 ent->s);
2716 ret = -ENOTSUP;
2717 goto error;
2718 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002719 *p = '/';
2720
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002721 pr_debug("Writing event: %s\n", buf);
2722 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002723 if (ret < 0) {
2724 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002725 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002726 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002727
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002728 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002729 return 0;
2730error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002731 pr_warning("Failed to delete event: %s\n",
2732 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002733 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002734}
2735
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302736static int del_trace_probe_event(int fd, const char *buf,
2737 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002738{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002739 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302740 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002741
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002742 if (strpbrk(buf, "*?")) { /* Glob-exp */
2743 strlist__for_each_safe(ent, n, namelist)
2744 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302745 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002746 if (ret < 0)
2747 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002748 strlist__remove(namelist, ent);
2749 }
2750 } else {
2751 ent = strlist__find(namelist, buf);
2752 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302753 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002754 if (ret >= 0)
2755 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002756 }
2757 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002758
2759 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002760}
2761
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002762int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002763{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302764 int ret = -1, ufd = -1, kfd = -1;
2765 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002766 const char *group, *event;
2767 char *p, *str;
2768 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302769 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002770
Masami Hiramatsufa282442009-12-08 17:03:23 -05002771 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302772 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002773 if (kfd >= 0)
2774 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302775
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302776 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002777 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302778 unamelist = get_probe_trace_event_names(ufd, true);
2779
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002780 if (kfd < 0 && ufd < 0) {
2781 print_both_open_warning(kfd, ufd);
2782 goto error;
2783 }
2784
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785 if (namelist == NULL && unamelist == NULL)
2786 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002787
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002788 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002789 str = strdup(ent->s);
2790 if (str == NULL) {
2791 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302792 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002793 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002794 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002795 p = strchr(str, ':');
2796 if (p) {
2797 group = str;
2798 *p = '\0';
2799 event = p + 1;
2800 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002801 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002802 event = str;
2803 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302804
2805 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2806 if (ret < 0) {
2807 pr_err("Failed to copy event.");
2808 free(str);
2809 goto error;
2810 }
2811
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002812 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302813
2814 if (namelist)
2815 ret = del_trace_probe_event(kfd, buf, namelist);
2816
2817 if (unamelist && ret != 0)
2818 ret = del_trace_probe_event(ufd, buf, unamelist);
2819
2820 if (ret != 0)
2821 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2822
Masami Hiramatsufa282442009-12-08 17:03:23 -05002823 free(str);
2824 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302825
2826error:
2827 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302828 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302829 close(kfd);
2830 }
2831
2832 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302833 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302834 close(ufd);
2835 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002836
2837 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002838}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302839
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002840/* TODO: don't use a global variable for filter ... */
2841static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002842
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002843/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002844 * If a symbol corresponds to a function with global binding and
2845 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002846 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002847static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002848 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002849{
Namhyung Kime578da32015-03-06 16:31:29 +09002850 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002851 return 0;
2852 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853}
2854
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002855int show_available_funcs(const char *target, struct strfilter *_filter,
2856 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002857{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002858 struct map *map;
2859 int ret;
2860
2861 ret = init_symbol_maps(user);
2862 if (ret < 0)
2863 return ret;
2864
2865 /* Get a symbol map */
2866 if (user)
2867 map = dso__new_map(target);
2868 else
2869 map = kernel_get_module_map(target);
2870 if (!map) {
2871 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002872 return -EINVAL;
2873 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002874
2875 /* Load symbols with given filter */
2876 available_func_filter = _filter;
2877 if (map__load(map, filter_available_functions)) {
2878 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2879 goto end;
2880 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002881 if (!dso__sorted_by_name(map->dso, map->type))
2882 dso__sort_by_name(map->dso, map->type);
2883
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002884 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302885 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002886 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2887end:
2888 if (user) {
2889 dso__delete(map->dso);
2890 map__delete(map);
2891 }
2892 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302893
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002894 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302895}
2896