blob: f272a711ad15c5182976681b7c99fc0a229a8267 [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{
David Ahern6d4a4892015-03-11 10:36:20 -0400359 struct perf_probe_point pp = { .function = lr->function,
360 .file = lr->file,
361 .line = lr->start };
362 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900363 int ret, len = 0;
364
David Ahern6d4a4892015-03-11 10:36:20 -0400365 memset(&result, 0, sizeof(result));
366
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900367 if (lr->end != INT_MAX)
368 len = lr->end - lr->start;
369 ret = find_alternative_probe_point(dinfo, &pp, &result,
370 target, user);
371 if (!ret) {
372 lr->function = result.function;
373 lr->file = result.file;
374 lr->start = result.line;
375 if (lr->end != INT_MAX)
376 lr->end = lr->start + len;
377 clear_perf_probe_point(&pp);
378 }
379 return ret;
380}
381
Masami Hiramatsuff741782011-06-27 16:27:39 +0900382/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000383static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900384{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000385 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000386 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900387
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000388 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900389 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900390 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000391 if (!silent)
392 pr_err("Failed to find path of %s module.\n",
393 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900394 return NULL;
395 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900396 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000397 ret = debuginfo__new(path);
398 if (!ret && !silent) {
399 pr_warning("The %s file has no debug information.\n", path);
400 if (!module || !strtailcmp(path, ".ko"))
401 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
402 else
403 pr_warning("Rebuild with -g, ");
404 pr_warning("or install an appropriate debuginfo package.\n");
405 }
406 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400407}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300408
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000409
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000410static int get_text_start_address(const char *exec, unsigned long *address)
411{
412 Elf *elf;
413 GElf_Ehdr ehdr;
414 GElf_Shdr shdr;
415 int fd, ret = -ENOENT;
416
417 fd = open(exec, O_RDONLY);
418 if (fd < 0)
419 return -errno;
420
421 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
422 if (elf == NULL)
423 return -EINVAL;
424
425 if (gelf_getehdr(elf, &ehdr) == NULL)
426 goto out;
427
428 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
429 goto out;
430
431 *address = shdr.sh_addr - shdr.sh_offset;
432 ret = 0;
433out:
434 elf_end(elf);
435 return ret;
436}
437
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000438/*
439 * Convert trace point to probe point with debuginfo
440 */
441static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
442 struct perf_probe_point *pp,
443 bool is_kprobe)
444{
445 struct debuginfo *dinfo = NULL;
446 unsigned long stext = 0;
447 u64 addr = tp->address;
448 int ret = -ENOENT;
449
450 /* convert the address to dwarf address */
451 if (!is_kprobe) {
452 if (!addr) {
453 ret = -EINVAL;
454 goto error;
455 }
456 ret = get_text_start_address(tp->module, &stext);
457 if (ret < 0)
458 goto error;
459 addr += stext;
460 } else {
461 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
462 if (addr == 0)
463 goto error;
464 addr += tp->offset;
465 }
466
467 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
468 tp->module ? : "kernel");
469
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000470 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000471 if (dinfo) {
472 ret = debuginfo__find_probe_point(dinfo,
473 (unsigned long)addr, pp);
474 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000475 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000476 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000477
478 if (ret > 0) {
479 pp->retprobe = tp->retprobe;
480 return 0;
481 }
482error:
483 pr_debug("Failed to find corresponding probes from debuginfo.\n");
484 return ret ? : -ENOENT;
485}
486
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000487static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
488 int ntevs, const char *exec)
489{
490 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000491 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000492
493 if (!exec)
494 return 0;
495
496 ret = get_text_start_address(exec, &stext);
497 if (ret < 0)
498 return ret;
499
500 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000501 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000502 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000503 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000504 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000505 ret = -ENOMEM;
506 break;
507 }
508 tevs[i].uprobes = true;
509 }
510
511 return ret;
512}
513
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900514static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
515 int ntevs, const char *module)
516{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900517 int i, ret = 0;
518 char *tmp;
519
520 if (!module)
521 return 0;
522
523 tmp = strrchr(module, '/');
524 if (tmp) {
525 /* This is a module path -- get the module name */
526 module = strdup(tmp + 1);
527 if (!module)
528 return -ENOMEM;
529 tmp = strchr(module, '.');
530 if (tmp)
531 *tmp = '\0';
532 tmp = (char *)module; /* For free() */
533 }
534
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900535 for (i = 0; i < ntevs; i++) {
536 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900537 if (!tevs[i].point.module) {
538 ret = -ENOMEM;
539 break;
540 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900541 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900542
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300543 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900544 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900545}
546
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000547/* Post processing the probe events */
548static int post_process_probe_trace_events(struct probe_trace_event *tevs,
549 int ntevs, const char *module,
550 bool uprobe)
551{
552 struct ref_reloc_sym *reloc_sym;
553 char *tmp;
554 int i;
555
556 if (uprobe)
557 return add_exec_to_probe_trace_events(tevs, ntevs, module);
558
559 /* Note that currently ref_reloc_sym based probe is not for drivers */
560 if (module)
561 return add_module_to_probe_trace_events(tevs, ntevs, module);
562
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000563 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000564 if (!reloc_sym) {
565 pr_warning("Relocated base symbol is not found!\n");
566 return -EINVAL;
567 }
568
569 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900570 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000571 tmp = strdup(reloc_sym->name);
572 if (!tmp)
573 return -ENOMEM;
574 free(tevs[i].point.symbol);
575 tevs[i].point.symbol = tmp;
576 tevs[i].point.offset = tevs[i].point.address -
577 reloc_sym->unrelocated_addr;
578 }
579 }
580 return 0;
581}
582
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300583/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530584static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900585 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530586 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300587{
588 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900589 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530590 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900591 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300592
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000593 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530594
Masami Hiramatsuff741782011-06-27 16:27:39 +0900595 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000596 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900597 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900598 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300599 return 0;
600 }
601
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000602 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900603 /* Searching trace events corresponding to a probe event */
604 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
605
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900606 if (ntevs == 0) { /* Not found, retry with an alternative */
607 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
608 if (!ret) {
609 ntevs = debuginfo__find_trace_events(dinfo, pev,
610 tevs, max_tevs);
611 /*
612 * Write back to the original probe_event for
613 * setting appropriate (user given) event name
614 */
615 clear_perf_probe_point(&pev->point);
616 memcpy(&pev->point, &tmp, sizeof(tmp));
617 }
618 }
619
Masami Hiramatsuff741782011-06-27 16:27:39 +0900620 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300621
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400622 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000623 pr_debug("Found %d probe_trace_events.\n", ntevs);
624 ret = post_process_probe_trace_events(*tevs, ntevs,
625 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000626 if (ret < 0) {
627 clear_probe_trace_events(*tevs, ntevs);
628 zfree(tevs);
629 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900630 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400631 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300632
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400633 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900634 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400635 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900636 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400637 }
638 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400639 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
640 if (ntevs == -EBADF) {
641 pr_warning("Warning: No dwarf info found in the vmlinux - "
642 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
643 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900644 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400645 return 0;
646 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300647 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400648 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300649}
650
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900651/*
652 * Find a src file from a DWARF tag path. Prepend optional source path prefix
653 * and chop off leading directories that do not exist. Result is passed back as
654 * a newly allocated path on success.
655 * Return 0 if file was found and readable, -errno otherwise.
656 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900657static int get_real_path(const char *raw_path, const char *comp_dir,
658 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900659{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900660 const char *prefix = symbol_conf.source_prefix;
661
662 if (!prefix) {
663 if (raw_path[0] != '/' && comp_dir)
664 /* If not an absolute path, try to use comp_dir */
665 prefix = comp_dir;
666 else {
667 if (access(raw_path, R_OK) == 0) {
668 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300669 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900670 } else
671 return -errno;
672 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900673 }
674
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900675 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900676 if (!*new_path)
677 return -ENOMEM;
678
679 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900680 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900681
682 if (access(*new_path, R_OK) == 0)
683 return 0;
684
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900685 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900686 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900687 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900688 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900689 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900690
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900691 switch (errno) {
692 case ENAMETOOLONG:
693 case ENOENT:
694 case EROFS:
695 case EFAULT:
696 raw_path = strchr(++raw_path, '/');
697 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300698 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900699 return -ENOENT;
700 }
701 continue;
702
703 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300704 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900705 return -errno;
706 }
707 }
708}
709
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300710#define LINEBUF_SIZE 256
711#define NR_ADDITIONAL_LINES 2
712
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100713static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300714{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000715 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100716 const char *color = show_num ? "" : PERF_COLOR_BLUE;
717 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300718
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100719 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300720 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
721 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100722 if (skip)
723 continue;
724 if (!prefix) {
725 prefix = show_num ? "%7d " : " ";
726 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300727 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100728 color_fprintf(stdout, color, "%s", buf);
729
730 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400731
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100732 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300733error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100734 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000735 pr_warning("File read error: %s\n",
736 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100737 return -1;
738 }
739 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300740}
741
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100742static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
743{
744 int rv = __show_one_line(fp, l, skip, show_num);
745 if (rv == 0) {
746 pr_warning("Source file is shorter than expected.\n");
747 rv = -1;
748 }
749 return rv;
750}
751
752#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
753#define show_one_line(f,l) _show_one_line(f,l,false,false)
754#define skip_one_line(f,l) _show_one_line(f,l,true,false)
755#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
756
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300757/*
758 * Show line-range always requires debuginfo to find source file and
759 * line number.
760 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900761static int __show_line_range(struct line_range *lr, const char *module,
762 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300763{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400764 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000765 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900766 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300767 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900768 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900769 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000770 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300771
772 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000773 dinfo = open_debuginfo(module, false);
774 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900775 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400776
Masami Hiramatsuff741782011-06-27 16:27:39 +0900777 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900778 if (!ret) { /* Not found, retry with an alternative */
779 ret = get_alternative_line_range(dinfo, lr, module, user);
780 if (!ret)
781 ret = debuginfo__find_line_range(dinfo, lr);
782 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900783 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000784 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400785 pr_warning("Specified source line is not found.\n");
786 return -ENOENT;
787 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000788 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400789 return ret;
790 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300791
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900792 /* Convert source file path */
793 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900794 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800795
796 /* Free old path when new path is assigned */
797 if (tmp != lr->path)
798 free(tmp);
799
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900800 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000801 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900802 return ret;
803 }
804
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300805 setup_pager();
806
807 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900808 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300809 lr->start - lr->offset);
810 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100811 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812
813 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400814 if (fp == NULL) {
815 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000816 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400817 return -errno;
818 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300819 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100820 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100821 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100822 if (ret < 0)
823 goto end;
824 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300825
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000826 intlist__for_each(ln, lr->line_list) {
827 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100828 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100829 if (ret < 0)
830 goto end;
831 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100832 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400833 if (ret < 0)
834 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300835 }
836
837 if (lr->end == INT_MAX)
838 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100839 while (l <= lr->end) {
840 ret = show_one_line_or_eof(fp, l++ - lr->offset);
841 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100842 break;
843 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300845 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300847}
848
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000849int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000850{
851 int ret;
852
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000853 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000854 if (ret < 0)
855 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900856 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000857 exit_symbol_maps();
858
859 return ret;
860}
861
Masami Hiramatsuff741782011-06-27 16:27:39 +0900862static int show_available_vars_at(struct debuginfo *dinfo,
863 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900864 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900865 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900866{
867 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900868 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900869 struct str_node *node;
870 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900871 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900872 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900873
874 buf = synthesize_perf_probe_point(&pev->point);
875 if (!buf)
876 return -EINVAL;
877 pr_debug("Searching variables at %s\n", buf);
878
Masami Hiramatsuff741782011-06-27 16:27:39 +0900879 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
880 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900881 if (!ret) { /* Not found, retry with an alternative */
882 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
883 if (!ret) {
884 ret = debuginfo__find_available_vars_at(dinfo, pev,
885 &vls, max_vls, externs);
886 /* Release the old probe_point */
887 clear_perf_probe_point(&tmp);
888 }
889 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900890 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000891 if (ret == 0 || ret == -ENOENT) {
892 pr_err("Failed to find the address of %s\n", buf);
893 ret = -ENOENT;
894 } else
895 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900896 goto end;
897 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000898
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900899 /* Some variables are found */
900 fprintf(stdout, "Available variables at %s\n", buf);
901 for (i = 0; i < ret; i++) {
902 vl = &vls[i];
903 /*
904 * A probe point might be converted to
905 * several trace points.
906 */
907 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
908 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300909 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900910 nvars = 0;
911 if (vl->vars) {
912 strlist__for_each(node, vl->vars) {
913 var = strchr(node->s, '\t') + 1;
914 if (strfilter__compare(_filter, var)) {
915 fprintf(stdout, "\t\t%s\n", node->s);
916 nvars++;
917 }
918 }
919 strlist__delete(vl->vars);
920 }
921 if (nvars == 0)
922 fprintf(stdout, "\t\t(No matched variables)\n");
923 }
924 free(vls);
925end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900926 free(buf);
927 return ret;
928}
929
930/* Show available variables on given probe point */
931int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900932 int max_vls, const char *module,
933 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900934{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900935 int i, ret = 0;
936 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900937
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000938 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900939 if (ret < 0)
940 return ret;
941
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000942 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900943 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000944 ret = -ENOENT;
945 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900946 }
947
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900948 setup_pager();
949
Masami Hiramatsuff741782011-06-27 16:27:39 +0900950 for (i = 0; i < npevs && ret >= 0; i++)
951 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900952 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900953
954 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000955out:
956 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900957 return ret;
958}
959
Ingo Molnar89fe8082013-09-30 12:07:11 +0200960#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300961
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000962static int
963find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
964 struct perf_probe_point *pp __maybe_unused,
965 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300966{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000967 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300968}
969
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530970static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300971 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300972 int max_tevs __maybe_unused,
973 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300974{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400975 if (perf_probe_event_need_dwarf(pev)) {
976 pr_warning("Debuginfo-analysis is not supported.\n");
977 return -ENOSYS;
978 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530979
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300980 return 0;
981}
982
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300983int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000984 const char *module __maybe_unused,
985 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300986{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400987 pr_warning("Debuginfo-analysis is not supported.\n");
988 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300989}
990
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300991int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
992 int npevs __maybe_unused, int max_vls __maybe_unused,
993 const char *module __maybe_unused,
994 struct strfilter *filter __maybe_unused,
995 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900996{
997 pr_warning("Debuginfo-analysis is not supported.\n");
998 return -ENOSYS;
999}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001000#endif
1001
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001002void line_range__clear(struct line_range *lr)
1003{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001004 free(lr->function);
1005 free(lr->file);
1006 free(lr->path);
1007 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001008 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001009 memset(lr, 0, sizeof(*lr));
1010}
1011
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001012int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001013{
1014 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001015 lr->line_list = intlist__new(NULL);
1016 if (!lr->line_list)
1017 return -ENOMEM;
1018 else
1019 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001020}
1021
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001022static int parse_line_num(char **ptr, int *val, const char *what)
1023{
1024 const char *start = *ptr;
1025
1026 errno = 0;
1027 *val = strtol(*ptr, ptr, 0);
1028 if (errno || *ptr == start) {
1029 semantic_error("'%s' is not a valid number.\n", what);
1030 return -EINVAL;
1031 }
1032 return 0;
1033}
1034
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001035/*
1036 * Stuff 'lr' according to the line range described by 'arg'.
1037 * The line range syntax is described by:
1038 *
1039 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001040 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001041 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001042int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001043{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001044 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001045 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001046
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001047 if (!name)
1048 return -ENOMEM;
1049
1050 lr->start = 0;
1051 lr->end = INT_MAX;
1052
1053 range = strchr(name, ':');
1054 if (range) {
1055 *range++ = '\0';
1056
1057 err = parse_line_num(&range, &lr->start, "start line");
1058 if (err)
1059 goto err;
1060
1061 if (*range == '+' || *range == '-') {
1062 const char c = *range++;
1063
1064 err = parse_line_num(&range, &lr->end, "end line");
1065 if (err)
1066 goto err;
1067
1068 if (c == '+') {
1069 lr->end += lr->start;
1070 /*
1071 * Adjust the number of lines here.
1072 * If the number of lines == 1, the
1073 * the end of line should be equal to
1074 * the start of line.
1075 */
1076 lr->end--;
1077 }
1078 }
1079
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001080 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001081
1082 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001083 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001084 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001086 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001088 if (*range != '\0') {
1089 semantic_error("Tailing with invalid str '%s'.\n", range);
1090 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001091 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001092 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001093
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001094 file = strchr(name, '@');
1095 if (file) {
1096 *file = '\0';
1097 lr->file = strdup(++file);
1098 if (lr->file == NULL) {
1099 err = -ENOMEM;
1100 goto err;
1101 }
1102 lr->function = name;
1103 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001104 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001105 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001106 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001107
1108 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001109err:
1110 free(name);
1111 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001112}
1113
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001114/* Check the name is good for event/group */
1115static bool check_event_name(const char *name)
1116{
1117 if (!isalpha(*name) && *name != '_')
1118 return false;
1119 while (*++name != '\0') {
1120 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1121 return false;
1122 }
1123 return true;
1124}
1125
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001126/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001127static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001128{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001129 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001130 char *ptr, *tmp;
1131 char c, nc = 0;
1132 /*
1133 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001134 * perf probe [EVENT=]SRC[:LN|;PTN]
1135 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001136 *
1137 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001138 */
1139
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001140 ptr = strpbrk(arg, ";=@+%");
1141 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001142 *ptr = '\0';
1143 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144 if (strchr(arg, ':')) {
1145 semantic_error("Group name is not supported yet.\n");
1146 return -ENOTSUP;
1147 }
1148 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001149 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001150 "follow C symbol-naming rule.\n", arg);
1151 return -EINVAL;
1152 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001153 pev->event = strdup(arg);
1154 if (pev->event == NULL)
1155 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001156 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001157 arg = tmp;
1158 }
1159
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001160 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001161 if (ptr) {
1162 nc = *ptr;
1163 *ptr++ = '\0';
1164 }
1165
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001166 tmp = strdup(arg);
1167 if (tmp == NULL)
1168 return -ENOMEM;
1169
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001170 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001171 if (strchr(tmp, '.')) /* File */
1172 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001173 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001174 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001175
1176 /* Parse other options */
1177 while (ptr) {
1178 arg = ptr;
1179 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001180 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001181 pp->lazy_line = strdup(arg);
1182 if (pp->lazy_line == NULL)
1183 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001184 break;
1185 }
1186 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001187 if (ptr) {
1188 nc = *ptr;
1189 *ptr++ = '\0';
1190 }
1191 switch (c) {
1192 case ':': /* Line number */
1193 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001195 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196 " in line number.\n");
1197 return -EINVAL;
1198 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001199 break;
1200 case '+': /* Byte offset from a symbol */
1201 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001202 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001203 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204 " in offset.\n");
1205 return -EINVAL;
1206 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001207 break;
1208 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 if (pp->file) {
1210 semantic_error("SRC@SRC is not allowed.\n");
1211 return -EINVAL;
1212 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001213 pp->file = strdup(arg);
1214 if (pp->file == NULL)
1215 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001216 break;
1217 case '%': /* Probe places */
1218 if (strcmp(arg, "return") == 0) {
1219 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 } else { /* Others not supported yet */
1221 semantic_error("%%%s is not supported.\n", arg);
1222 return -ENOTSUP;
1223 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001224 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225 default: /* Buggy case */
1226 pr_err("This program has a bug at %s:%d.\n",
1227 __FILE__, __LINE__);
1228 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001229 break;
1230 }
1231 }
1232
1233 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001235 semantic_error("Lazy pattern can't be used with"
1236 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 return -EINVAL;
1238 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001239
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001240 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001241 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 return -EINVAL;
1243 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001246 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 return -EINVAL;
1248 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001249
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001251 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001252 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 return -EINVAL;
1254 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001255
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001257 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 return -EINVAL;
1259 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001260
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001262 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 return -EINVAL;
1264 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001265
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001266 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001267 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001268 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001269 return -EINVAL;
1270 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001271
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001272 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001273 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1274 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001275 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001276}
1277
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001278/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001279static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001280{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001281 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001282 struct perf_probe_arg_field **fieldp;
1283
1284 pr_debug("parsing arg: %s into ", str);
1285
Masami Hiramatsu48481932010-04-12 13:16:53 -04001286 tmp = strchr(str, '=');
1287 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001288 arg->name = strndup(str, tmp - str);
1289 if (arg->name == NULL)
1290 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001291 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001292 str = tmp + 1;
1293 }
1294
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001295 tmp = strchr(str, ':');
1296 if (tmp) { /* Type setting */
1297 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001298 arg->type = strdup(tmp + 1);
1299 if (arg->type == NULL)
1300 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001301 pr_debug("type:%s ", arg->type);
1302 }
1303
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001304 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001305 if (!is_c_varname(str) || !tmp) {
1306 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001307 arg->var = strdup(str);
1308 if (arg->var == NULL)
1309 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001310 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001311 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001312 }
1313
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001314 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001315 arg->var = strndup(str, tmp - str);
1316 if (arg->var == NULL)
1317 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001318 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001319 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001320 fieldp = &arg->field;
1321
1322 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001323 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1324 if (*fieldp == NULL)
1325 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001326 if (*tmp == '[') { /* Array */
1327 str = tmp;
1328 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001329 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001330 if (*tmp != ']' || tmp == str + 1) {
1331 semantic_error("Array index must be a"
1332 " number.\n");
1333 return -EINVAL;
1334 }
1335 tmp++;
1336 if (*tmp == '\0')
1337 tmp = NULL;
1338 } else { /* Structure */
1339 if (*tmp == '.') {
1340 str = tmp + 1;
1341 (*fieldp)->ref = false;
1342 } else if (tmp[1] == '>') {
1343 str = tmp + 2;
1344 (*fieldp)->ref = true;
1345 } else {
1346 semantic_error("Argument parse error: %s\n",
1347 str);
1348 return -EINVAL;
1349 }
1350 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001351 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001352 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001353 (*fieldp)->name = strndup(str, tmp - str);
1354 if ((*fieldp)->name == NULL)
1355 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001356 if (*str != '[')
1357 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001358 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1359 fieldp = &(*fieldp)->next;
1360 }
1361 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001362 (*fieldp)->name = strdup(str);
1363 if ((*fieldp)->name == NULL)
1364 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001365 if (*str != '[')
1366 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001367 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001368
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001369 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001370 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001371 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001372 if (arg->name == NULL)
1373 return -ENOMEM;
1374 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001376}
1377
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001378/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001380{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001381 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001382 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001383
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001384 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001385 if (!argv) {
1386 pr_debug("Failed to split arguments.\n");
1387 return -ENOMEM;
1388 }
1389 if (argc - 1 > MAX_PROBE_ARGS) {
1390 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1391 ret = -ERANGE;
1392 goto out;
1393 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395 ret = parse_perf_probe_point(argv[0], pev);
1396 if (ret < 0)
1397 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001398
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001399 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001400 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001401 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1402 if (pev->args == NULL) {
1403 ret = -ENOMEM;
1404 goto out;
1405 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001406 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1407 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1408 if (ret >= 0 &&
1409 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001410 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411 " kretprobe.\n");
1412 ret = -EINVAL;
1413 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001414 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001416 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001417
1418 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001419}
1420
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421/* Return true if this perf_probe_event requires debuginfo */
1422bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001423{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001424 int i;
1425
1426 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1427 return true;
1428
1429 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001430 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001431 return true;
1432
1433 return false;
1434}
1435
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301436/* Parse probe_events event into struct probe_point */
1437static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001438 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001439{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301440 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001441 char pr;
1442 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001443 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001444 int ret, i, argc;
1445 char **argv;
1446
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301447 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001448 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001449 if (!argv) {
1450 pr_debug("Failed to split arguments.\n");
1451 return -ENOMEM;
1452 }
1453 if (argc < 2) {
1454 semantic_error("Too few probe arguments.\n");
1455 ret = -ERANGE;
1456 goto out;
1457 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001458
1459 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001460 argv0_str = strdup(argv[0]);
1461 if (argv0_str == NULL) {
1462 ret = -ENOMEM;
1463 goto out;
1464 }
1465 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1466 fmt2_str = strtok_r(NULL, "/", &fmt);
1467 fmt3_str = strtok_r(NULL, " \t", &fmt);
1468 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1469 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 semantic_error("Failed to parse event name: %s\n", argv[0]);
1471 ret = -EINVAL;
1472 goto out;
1473 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001474 pr = fmt1_str[0];
1475 tev->group = strdup(fmt2_str);
1476 tev->event = strdup(fmt3_str);
1477 if (tev->group == NULL || tev->event == NULL) {
1478 ret = -ENOMEM;
1479 goto out;
1480 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001483 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001484
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001485 /* Scan module name(if there), function name and offset */
1486 p = strchr(argv[1], ':');
1487 if (p) {
1488 tp->module = strndup(argv[1], p - argv[1]);
1489 p++;
1490 } else
1491 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001492 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001493 if (fmt1_str[0] == '0') /* only the address started with 0x */
1494 tp->address = strtoul(fmt1_str, NULL, 0);
1495 else {
1496 /* Only the symbol-based probe has offset */
1497 tp->symbol = strdup(fmt1_str);
1498 if (tp->symbol == NULL) {
1499 ret = -ENOMEM;
1500 goto out;
1501 }
1502 fmt2_str = strtok_r(NULL, "", &fmt);
1503 if (fmt2_str == NULL)
1504 tp->offset = 0;
1505 else
1506 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001507 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001508
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301510 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001511 if (tev->args == NULL) {
1512 ret = -ENOMEM;
1513 goto out;
1514 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001516 p = strchr(argv[i + 2], '=');
1517 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001518 *p++ = '\0';
1519 else
1520 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001521 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001522 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001523 tev->args[i].value = strdup(p);
1524 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1525 ret = -ENOMEM;
1526 goto out;
1527 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001528 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 ret = 0;
1530out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001531 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001532 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001533 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001534}
1535
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001536/* Compose only probe arg */
1537int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1538{
1539 struct perf_probe_arg_field *field = pa->field;
1540 int ret;
1541 char *tmp = buf;
1542
Masami Hiramatsu48481932010-04-12 13:16:53 -04001543 if (pa->name && pa->var)
1544 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1545 else
1546 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001547 if (ret <= 0)
1548 goto error;
1549 tmp += ret;
1550 len -= ret;
1551
1552 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001553 if (field->name[0] == '[')
1554 ret = e_snprintf(tmp, len, "%s", field->name);
1555 else
1556 ret = e_snprintf(tmp, len, "%s%s",
1557 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001558 if (ret <= 0)
1559 goto error;
1560 tmp += ret;
1561 len -= ret;
1562 field = field->next;
1563 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001564
1565 if (pa->type) {
1566 ret = e_snprintf(tmp, len, ":%s", pa->type);
1567 if (ret <= 0)
1568 goto error;
1569 tmp += ret;
1570 len -= ret;
1571 }
1572
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001573 return tmp - buf;
1574error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001575 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001577}
1578
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001579/* Compose only probe point (not argument) */
1580static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001581{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001582 char *buf, *tmp;
1583 char offs[32] = "", line[32] = "", file[32] = "";
1584 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001585
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001586 buf = zalloc(MAX_CMDLEN);
1587 if (buf == NULL) {
1588 ret = -ENOMEM;
1589 goto error;
1590 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001591 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001592 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001593 if (ret <= 0)
1594 goto error;
1595 }
1596 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001597 ret = e_snprintf(line, 32, ":%d", pp->line);
1598 if (ret <= 0)
1599 goto error;
1600 }
1601 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001602 tmp = pp->file;
1603 len = strlen(tmp);
1604 if (len > 30) {
1605 tmp = strchr(pp->file + len - 30, '/');
1606 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1607 }
1608 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001609 if (ret <= 0)
1610 goto error;
1611 }
1612
1613 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001614 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1615 offs, pp->retprobe ? "%return" : "", line,
1616 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001617 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001618 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001619 if (ret <= 0)
1620 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001621
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001622 return buf;
1623error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001624 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001625 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001627}
1628
1629#if 0
1630char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1631{
1632 char *buf;
1633 int i, len, ret;
1634
1635 buf = synthesize_perf_probe_point(&pev->point);
1636 if (!buf)
1637 return NULL;
1638
1639 len = strlen(buf);
1640 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001641 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001642 pev->args[i].name);
1643 if (ret <= 0) {
1644 free(buf);
1645 return NULL;
1646 }
1647 len += ret;
1648 }
1649
1650 return buf;
1651}
1652#endif
1653
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301654static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001655 char **buf, size_t *buflen,
1656 int depth)
1657{
1658 int ret;
1659 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301660 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 buflen, depth + 1);
1662 if (depth < 0)
1663 goto out;
1664 }
1665
1666 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1667 if (ret < 0)
1668 depth = ret;
1669 else {
1670 *buf += ret;
1671 *buflen -= ret;
1672 }
1673out:
1674 return depth;
1675
1676}
1677
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301678static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001679 char *buf, size_t buflen)
1680{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301681 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001682 int ret, depth = 0;
1683 char *tmp = buf;
1684
1685 /* Argument name or separator */
1686 if (arg->name)
1687 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1688 else
1689 ret = e_snprintf(buf, buflen, " ");
1690 if (ret < 0)
1691 return ret;
1692 buf += ret;
1693 buflen -= ret;
1694
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001695 /* Special case: @XXX */
1696 if (arg->value[0] == '@' && arg->ref)
1697 ref = ref->next;
1698
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001699 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001700 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301701 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 &buflen, 1);
1703 if (depth < 0)
1704 return depth;
1705 }
1706
1707 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001708 if (arg->value[0] == '@' && arg->ref)
1709 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1710 arg->ref->offset);
1711 else
1712 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001713 if (ret < 0)
1714 return ret;
1715 buf += ret;
1716 buflen -= ret;
1717
1718 /* Closing */
1719 while (depth--) {
1720 ret = e_snprintf(buf, buflen, ")");
1721 if (ret < 0)
1722 return ret;
1723 buf += ret;
1724 buflen -= ret;
1725 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001726 /* Print argument type */
1727 if (arg->type) {
1728 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1729 if (ret <= 0)
1730 return ret;
1731 buf += ret;
1732 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733
1734 return buf - tmp;
1735}
1736
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301737char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301739 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001740 char *buf;
1741 int i, len, ret;
1742
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001743 buf = zalloc(MAX_CMDLEN);
1744 if (buf == NULL)
1745 return NULL;
1746
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001747 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1748 tev->group, tev->event);
1749 if (len <= 0)
1750 goto error;
1751
1752 /* Uprobes must have tp->address and tp->module */
1753 if (tev->uprobes && (!tp->address || !tp->module))
1754 goto error;
1755
1756 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301757 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001758 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1759 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301760 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001761 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301762 tp->module ?: "", tp->module ? ":" : "",
1763 tp->symbol, tp->offset);
1764
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001765 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001767 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001768
1769 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301770 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001772 if (ret <= 0)
1773 goto error;
1774 len += ret;
1775 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001776
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001777 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001778error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001779 free(buf);
1780 return NULL;
1781}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001782
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001783static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1784 struct perf_probe_point *pp,
1785 bool is_kprobe)
1786{
1787 struct symbol *sym = NULL;
1788 struct map *map;
1789 u64 addr;
1790 int ret = -ENOENT;
1791
1792 if (!is_kprobe) {
1793 map = dso__new_map(tp->module);
1794 if (!map)
1795 goto out;
1796 addr = tp->address;
1797 sym = map__find_symbol(map, addr, NULL);
1798 } else {
1799 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1800 if (addr) {
1801 addr += tp->offset;
1802 sym = __find_kernel_function(addr, &map);
1803 }
1804 }
1805 if (!sym)
1806 goto out;
1807
1808 pp->retprobe = tp->retprobe;
1809 pp->offset = addr - map->unmap_ip(map, sym->start);
1810 pp->function = strdup(sym->name);
1811 ret = pp->function ? 0 : -ENOMEM;
1812
1813out:
1814 if (map && !is_kprobe) {
1815 dso__delete(map->dso);
1816 map__delete(map);
1817 }
1818
1819 return ret;
1820}
1821
1822static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1823 struct perf_probe_point *pp,
1824 bool is_kprobe)
1825{
1826 char buf[128];
1827 int ret;
1828
1829 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1830 if (!ret)
1831 return 0;
1832 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1833 if (!ret)
1834 return 0;
1835
1836 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1837
1838 if (tp->symbol) {
1839 pp->function = strdup(tp->symbol);
1840 pp->offset = tp->offset;
1841 } else if (!tp->module && !is_kprobe) {
1842 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1843 if (ret < 0)
1844 return ret;
1845 pp->function = strdup(buf);
1846 pp->offset = 0;
1847 }
1848 if (pp->function == NULL)
1849 return -ENOMEM;
1850
1851 pp->retprobe = tp->retprobe;
1852
1853 return 0;
1854}
1855
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301856static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301857 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001858{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001859 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001860 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001862 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001863 pev->event = strdup(tev->event);
1864 pev->group = strdup(tev->group);
1865 if (pev->event == NULL || pev->group == NULL)
1866 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001867
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001868 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001869 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001870 if (ret < 0)
1871 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001872
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873 /* Convert trace_arg to probe_arg */
1874 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001875 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1876 if (pev->args == NULL)
1877 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001878 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001880 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301882 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001883 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001884 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001885 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001886 if (pev->args[i].name == NULL && ret >= 0)
1887 ret = -ENOMEM;
1888 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001889
1890 if (ret < 0)
1891 clear_perf_probe_event(pev);
1892
1893 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001894}
1895
1896void clear_perf_probe_event(struct perf_probe_event *pev)
1897{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001898 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001899 int i;
1900
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001901 free(pev->event);
1902 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001903 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001904
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001905 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001906 free(pev->args[i].name);
1907 free(pev->args[i].var);
1908 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001909 field = pev->args[i].field;
1910 while (field) {
1911 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001912 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001913 free(field);
1914 field = next;
1915 }
1916 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001917 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918 memset(pev, 0, sizeof(*pev));
1919}
1920
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301921static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001922{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301923 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001924 int i;
1925
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001926 free(tev->event);
1927 free(tev->group);
1928 free(tev->point.symbol);
1929 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001930 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001931 free(tev->args[i].name);
1932 free(tev->args[i].value);
1933 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001934 ref = tev->args[i].ref;
1935 while (ref) {
1936 next = ref->next;
1937 free(ref);
1938 ref = next;
1939 }
1940 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001941 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001943}
1944
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001945static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301946{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001947 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301948
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001949 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301950 const char *config;
1951
1952 if (!is_kprobe)
1953 config = "CONFIG_UPROBE_EVENTS";
1954 else
1955 config = "CONFIG_KPROBE_EVENTS";
1956
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001957 pr_warning("%cprobe_events file does not exist"
1958 " - please rebuild kernel with %s.\n",
1959 is_kprobe ? 'k' : 'u', config);
1960 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001961 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001962 else
1963 pr_warning("Failed to open %cprobe_events: %s\n",
1964 is_kprobe ? 'k' : 'u',
1965 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301966}
1967
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001968static void print_both_open_warning(int kerr, int uerr)
1969{
1970 /* Both kprobes and uprobes are disabled, warn it. */
1971 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001972 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001973 else if (kerr == -ENOENT && uerr == -ENOENT)
1974 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1975 "or/and CONFIG_UPROBE_EVENTS.\n");
1976 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001977 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001978 pr_warning("Failed to open kprobe events: %s.\n",
1979 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1980 pr_warning("Failed to open uprobe events: %s.\n",
1981 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1982 }
1983}
1984
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001985static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001986{
1987 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001988 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001989 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001990 int ret;
1991
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001992 __debugfs = tracefs_find_mountpoint();
1993 if (__debugfs == NULL) {
1994 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001995
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001996 __debugfs = debugfs_find_mountpoint();
1997 if (__debugfs == NULL)
1998 return -ENOTSUP;
1999 }
2000
2001 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2002 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002003 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002004 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002005 if (readwrite && !probe_event_dry_run)
2006 ret = open(buf, O_RDWR, O_APPEND);
2007 else
2008 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002009
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302010 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002011 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002012 }
2013 return ret;
2014}
2015
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302016static int open_kprobe_events(bool readwrite)
2017{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002018 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302019}
2020
2021static int open_uprobe_events(bool readwrite)
2022{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002023 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302024}
2025
2026/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302027static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002028{
2029 int ret, idx;
2030 FILE *fp;
2031 char buf[MAX_CMDLEN];
2032 char *p;
2033 struct strlist *sl;
2034
2035 sl = strlist__new(true, NULL);
2036
2037 fp = fdopen(dup(fd), "r");
2038 while (!feof(fp)) {
2039 p = fgets(buf, MAX_CMDLEN, fp);
2040 if (!p)
2041 break;
2042
2043 idx = strlen(p) - 1;
2044 if (p[idx] == '\n')
2045 p[idx] = '\0';
2046 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002047 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002048 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002049 strlist__delete(sl);
2050 return NULL;
2051 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002052 }
2053 fclose(fp);
2054
2055 return sl;
2056}
2057
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002058struct kprobe_blacklist_node {
2059 struct list_head list;
2060 unsigned long start;
2061 unsigned long end;
2062 char *symbol;
2063};
2064
2065static void kprobe_blacklist__delete(struct list_head *blacklist)
2066{
2067 struct kprobe_blacklist_node *node;
2068
2069 while (!list_empty(blacklist)) {
2070 node = list_first_entry(blacklist,
2071 struct kprobe_blacklist_node, list);
2072 list_del(&node->list);
2073 free(node->symbol);
2074 free(node);
2075 }
2076}
2077
2078static int kprobe_blacklist__load(struct list_head *blacklist)
2079{
2080 struct kprobe_blacklist_node *node;
2081 const char *__debugfs = debugfs_find_mountpoint();
2082 char buf[PATH_MAX], *p;
2083 FILE *fp;
2084 int ret;
2085
2086 if (__debugfs == NULL)
2087 return -ENOTSUP;
2088
2089 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2090 if (ret < 0)
2091 return ret;
2092
2093 fp = fopen(buf, "r");
2094 if (!fp)
2095 return -errno;
2096
2097 ret = 0;
2098 while (fgets(buf, PATH_MAX, fp)) {
2099 node = zalloc(sizeof(*node));
2100 if (!node) {
2101 ret = -ENOMEM;
2102 break;
2103 }
2104 INIT_LIST_HEAD(&node->list);
2105 list_add_tail(&node->list, blacklist);
2106 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2107 ret = -EINVAL;
2108 break;
2109 }
2110 p = strchr(buf, '\t');
2111 if (p) {
2112 p++;
2113 if (p[strlen(p) - 1] == '\n')
2114 p[strlen(p) - 1] = '\0';
2115 } else
2116 p = (char *)"unknown";
2117 node->symbol = strdup(p);
2118 if (!node->symbol) {
2119 ret = -ENOMEM;
2120 break;
2121 }
2122 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2123 node->start, node->end, node->symbol);
2124 ret++;
2125 }
2126 if (ret < 0)
2127 kprobe_blacklist__delete(blacklist);
2128 fclose(fp);
2129
2130 return ret;
2131}
2132
2133static struct kprobe_blacklist_node *
2134kprobe_blacklist__find_by_address(struct list_head *blacklist,
2135 unsigned long address)
2136{
2137 struct kprobe_blacklist_node *node;
2138
2139 list_for_each_entry(node, blacklist, list) {
2140 if (node->start <= address && address <= node->end)
2141 return node;
2142 }
2143
2144 return NULL;
2145}
2146
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002147/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002148static int show_perf_probe_event(struct perf_probe_event *pev,
2149 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002150{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002151 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002152 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002153 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002154
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002155 /* Synthesize only event probe point */
2156 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002157 if (!place)
2158 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002159
2160 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002161 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162 return ret;
2163
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002164 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002165 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002166 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002167
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002168 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002169 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002170 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002171 ret = synthesize_perf_probe_arg(&pev->args[i],
2172 buf, 128);
2173 if (ret < 0)
2174 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002175 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002176 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002177 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002178 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002179 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002180 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002181}
2182
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302183static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002184{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302185 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302186 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002187 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002188 struct strlist *rawlist;
2189 struct str_node *ent;
2190
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002191 memset(&tev, 0, sizeof(tev));
2192 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002193
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302194 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002195 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002196 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002197
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002198 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302199 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002200 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302201 ret = convert_to_perf_probe_event(&tev, &pev,
2202 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002204 ret = show_perf_probe_event(&pev,
2205 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002206 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002207 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302208 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209 if (ret < 0)
2210 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002211 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002212 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002213
2214 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002215}
2216
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217/* List up current perf-probe events */
2218int show_perf_probe_events(void)
2219{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002220 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302221
2222 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302223
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002224 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302225 if (ret < 0)
2226 return ret;
2227
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002228 kp_fd = open_kprobe_events(false);
2229 if (kp_fd >= 0) {
2230 ret = __show_perf_probe_events(kp_fd, true);
2231 close(kp_fd);
2232 if (ret < 0)
2233 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302234 }
2235
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002236 up_fd = open_uprobe_events(false);
2237 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002238 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002239 ret = kp_fd;
2240 goto out;
2241 }
2242
2243 if (up_fd >= 0) {
2244 ret = __show_perf_probe_events(up_fd, false);
2245 close(up_fd);
2246 }
2247out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002248 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302249 return ret;
2250}
2251
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002252/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302253static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002254{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002255 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002256 struct strlist *sl, *rawlist;
2257 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302258 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002259 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002260
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002261 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302262 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002263 if (!rawlist)
2264 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002265 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002266 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302267 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002268 if (ret < 0)
2269 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002270 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002271 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2272 tev.event);
2273 if (ret >= 0)
2274 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002275 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002276 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302277 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002278 if (ret < 0)
2279 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002280 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002281 strlist__delete(rawlist);
2282
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002283 if (ret < 0) {
2284 strlist__delete(sl);
2285 return NULL;
2286 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002287 return sl;
2288}
2289
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302290static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002291{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002292 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302293 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002294 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002295
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002296 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302297 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002298 return -EINVAL;
2299 }
2300
Masami Hiramatsufa282442009-12-08 17:03:23 -05002301 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002302 if (!probe_event_dry_run) {
2303 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002304 if (ret <= 0) {
2305 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002306 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002307 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002308 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002309 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002310 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002311 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002312}
2313
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002314static int get_new_event_name(char *buf, size_t len, const char *base,
2315 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002316{
2317 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002318
2319 /* Try no suffix */
2320 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002321 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002322 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002323 return ret;
2324 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002325 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002327
Masami Hiramatsud761b082009-12-15 10:32:25 -05002328 if (!allow_suffix) {
2329 pr_warning("Error: event \"%s\" already exists. "
2330 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002331 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002332 }
2333
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002334 /* Try to add suffix */
2335 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002336 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002337 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002338 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002339 return ret;
2340 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002341 if (!strlist__has_entry(namelist, buf))
2342 break;
2343 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002344 if (i == MAX_EVENT_INDEX) {
2345 pr_warning("Too many events are on the same function.\n");
2346 ret = -ERANGE;
2347 }
2348
2349 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002350}
2351
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002352/* Warn if the current kernel's uprobe implementation is old */
2353static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2354{
2355 int i;
2356 char *buf = synthesize_probe_trace_command(tev);
2357
2358 /* Old uprobe event doesn't support memory dereference */
2359 if (!tev->uprobes || tev->nargs == 0 || !buf)
2360 goto out;
2361
2362 for (i = 0; i < tev->nargs; i++)
2363 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2364 pr_warning("Please upgrade your kernel to at least "
2365 "3.14 to have access to feature %s\n",
2366 tev->args[i].value);
2367 break;
2368 }
2369out:
2370 free(buf);
2371}
2372
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302373static int __add_probe_trace_events(struct perf_probe_event *pev,
2374 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002375 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002376{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002377 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302378 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002379 char buf[64];
2380 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002381 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002382 LIST_HEAD(blacklist);
2383 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002384
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302385 if (pev->uprobes)
2386 fd = open_uprobe_events(true);
2387 else
2388 fd = open_kprobe_events(true);
2389
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002390 if (fd < 0) {
2391 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002392 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002393 }
2394
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002395 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302396 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 if (!namelist) {
2398 pr_debug("Failed to get current event list.\n");
2399 return -EIO;
2400 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002401 /* Get kprobe blacklist if exists */
2402 if (!pev->uprobes) {
2403 ret = kprobe_blacklist__load(&blacklist);
2404 if (ret < 0)
2405 pr_debug("No kprobe blacklist support, ignored\n");
2406 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002407
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002408 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002409 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002410 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002411 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002412 /* Ensure that the address is NOT blacklisted */
2413 node = kprobe_blacklist__find_by_address(&blacklist,
2414 tev->point.address);
2415 if (node) {
2416 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2417 continue;
2418 }
2419
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002420 if (pev->event)
2421 event = pev->event;
2422 else
2423 if (pev->point.function)
2424 event = pev->point.function;
2425 else
2426 event = tev->point.symbol;
2427 if (pev->group)
2428 group = pev->group;
2429 else
2430 group = PERFPROBE_GROUP;
2431
2432 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002433 ret = get_new_event_name(buf, 64, event,
2434 namelist, allow_suffix);
2435 if (ret < 0)
2436 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002437 event = buf;
2438
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002439 tev->event = strdup(event);
2440 tev->group = strdup(group);
2441 if (tev->event == NULL || tev->group == NULL) {
2442 ret = -ENOMEM;
2443 break;
2444 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302445 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002446 if (ret < 0)
2447 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002448 /* Add added event name to namelist */
2449 strlist__add(namelist, event);
2450
2451 /* Trick here - save current event/group */
2452 event = pev->event;
2453 group = pev->group;
2454 pev->event = tev->event;
2455 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002456 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002457 /* Trick here - restore current event/group */
2458 pev->event = (char *)event;
2459 pev->group = (char *)group;
2460
2461 /*
2462 * Probes after the first probe which comes from same
2463 * user input are always allowed to add suffix, because
2464 * there might be several addresses corresponding to
2465 * one code line.
2466 */
2467 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002468 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002469 if (ret == -EINVAL && pev->uprobes)
2470 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002471
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002472 /* Note that it is possible to skip all events because of blacklist */
2473 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002474 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002475 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2476 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002477 tev->event);
2478 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002479
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002480 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002481 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002482 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002483 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002484}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002485
Namhyung Kim564c62a2015-01-14 20:18:07 +09002486static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002487{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002488 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002489 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002490
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002491 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kime578da32015-03-06 16:31:29 +09002492 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002493 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002494
2495 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002496}
2497
2498#define strdup_or_goto(str, label) \
2499 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2500
2501/*
2502 * Find probe function addresses from map.
2503 * Return an error or the number of found probe_trace_event
2504 */
2505static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2506 struct probe_trace_event **tevs,
2507 int max_tevs, const char *target)
2508{
2509 struct map *map = NULL;
2510 struct kmap *kmap = NULL;
2511 struct ref_reloc_sym *reloc_sym = NULL;
2512 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002513 struct probe_trace_event *tev;
2514 struct perf_probe_point *pp = &pev->point;
2515 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002516 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002517 int ret, i;
2518
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002519 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002520 if (!map) {
2521 ret = -EINVAL;
2522 goto out;
2523 }
2524
2525 /*
2526 * Load matched symbols: Since the different local symbols may have
2527 * same name but different addresses, this lists all the symbols.
2528 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002529 num_matched_functions = find_probe_functions(map, pp->function);
2530 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002531 pr_err("Failed to find symbol %s in %s\n", pp->function,
2532 target ? : "kernel");
2533 ret = -ENOENT;
2534 goto out;
2535 } else if (num_matched_functions > max_tevs) {
2536 pr_err("Too many functions matched in %s\n",
2537 target ? : "kernel");
2538 ret = -E2BIG;
2539 goto out;
2540 }
2541
Namhyung Kim25dd9172015-01-14 20:18:08 +09002542 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002543 kmap = map__kmap(map);
2544 reloc_sym = kmap->ref_reloc_sym;
2545 if (!reloc_sym) {
2546 pr_warning("Relocated base symbol is not found!\n");
2547 ret = -EINVAL;
2548 goto out;
2549 }
2550 }
2551
2552 /* Setup result trace-probe-events */
2553 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2554 if (!*tevs) {
2555 ret = -ENOMEM;
2556 goto out;
2557 }
2558
2559 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002560
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002561 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002562 tev = (*tevs) + ret;
2563 tp = &tev->point;
2564 if (ret == num_matched_functions) {
2565 pr_warning("Too many symbols are listed. Skip it.\n");
2566 break;
2567 }
2568 ret++;
2569
2570 if (pp->offset > sym->end - sym->start) {
2571 pr_warning("Offset %ld is bigger than the size of %s\n",
2572 pp->offset, sym->name);
2573 ret = -ENOENT;
2574 goto err_out;
2575 }
2576 /* Add one probe point */
2577 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2578 if (reloc_sym) {
2579 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2580 tp->offset = tp->address - reloc_sym->addr;
2581 } else {
2582 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2583 tp->offset = pp->offset;
2584 }
2585 tp->retprobe = pp->retprobe;
2586 if (target)
2587 tev->point.module = strdup_or_goto(target, nomem_out);
2588 tev->uprobes = pev->uprobes;
2589 tev->nargs = pev->nargs;
2590 if (tev->nargs) {
2591 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2592 tev->nargs);
2593 if (tev->args == NULL)
2594 goto nomem_out;
2595 }
2596 for (i = 0; i < tev->nargs; i++) {
2597 if (pev->args[i].name)
2598 tev->args[i].name =
2599 strdup_or_goto(pev->args[i].name,
2600 nomem_out);
2601
2602 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2603 nomem_out);
2604 if (pev->args[i].type)
2605 tev->args[i].type =
2606 strdup_or_goto(pev->args[i].type,
2607 nomem_out);
2608 }
2609 }
2610
2611out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002612 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002613 return ret;
2614
2615nomem_out:
2616 ret = -ENOMEM;
2617err_out:
2618 clear_probe_trace_events(*tevs, num_matched_functions);
2619 zfree(tevs);
2620 goto out;
2621}
2622
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302623static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2624 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302625 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002626{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002627 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002628
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002629 if (pev->uprobes && !pev->group) {
2630 /* Replace group name if not given */
2631 ret = convert_exec_to_group(target, &pev->group);
2632 if (ret != 0) {
2633 pr_warning("Failed to make a group name.\n");
2634 return ret;
2635 }
2636 }
2637
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002638 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302639 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002640 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002641 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002642
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002643 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002644}
2645
2646struct __event_package {
2647 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302648 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002649 int ntevs;
2650};
2651
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002652int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302653 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002654{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002655 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002656 struct __event_package *pkgs;
2657
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302658 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002659 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302660
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002661 if (pkgs == NULL)
2662 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002663
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002664 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002665 if (ret < 0) {
2666 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002667 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002668 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002669
2670 /* Loop 1: convert all events */
2671 for (i = 0; i < npevs; i++) {
2672 pkgs[i].pev = &pevs[i];
2673 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302674 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002675 &pkgs[i].tevs,
2676 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302677 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002678 if (ret < 0)
2679 goto end;
2680 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002681 }
2682
2683 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002684 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302685 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002686 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002687 if (ret < 0)
2688 break;
2689 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002690end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002691 /* Loop 3: cleanup and free trace events */
2692 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002693 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302694 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002695 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002696 }
2697 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002698 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002699
2700 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002701}
2702
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302703static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002704{
2705 char *p;
2706 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002707 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002708
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302709 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002710 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2711 if (ret < 0)
2712 goto error;
2713
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002714 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002715 if (!p) {
2716 pr_debug("Internal error: %s should have ':' but not.\n",
2717 ent->s);
2718 ret = -ENOTSUP;
2719 goto error;
2720 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002721 *p = '/';
2722
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002723 pr_debug("Writing event: %s\n", buf);
2724 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002725 if (ret < 0) {
2726 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002727 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002728 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002729
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002730 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002731 return 0;
2732error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002733 pr_warning("Failed to delete event: %s\n",
2734 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002735 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002736}
2737
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302738static int del_trace_probe_event(int fd, const char *buf,
2739 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002740{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002741 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302742 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002743
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002744 if (strpbrk(buf, "*?")) { /* Glob-exp */
2745 strlist__for_each_safe(ent, n, namelist)
2746 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302747 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002748 if (ret < 0)
2749 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002750 strlist__remove(namelist, ent);
2751 }
2752 } else {
2753 ent = strlist__find(namelist, buf);
2754 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302755 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002756 if (ret >= 0)
2757 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002758 }
2759 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002760
2761 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002762}
2763
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002764int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002765{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302766 int ret = -1, ufd = -1, kfd = -1;
2767 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002768 const char *group, *event;
2769 char *p, *str;
2770 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302771 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002772
Masami Hiramatsufa282442009-12-08 17:03:23 -05002773 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302774 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002775 if (kfd >= 0)
2776 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302777
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302778 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002779 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302780 unamelist = get_probe_trace_event_names(ufd, true);
2781
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002782 if (kfd < 0 && ufd < 0) {
2783 print_both_open_warning(kfd, ufd);
2784 goto error;
2785 }
2786
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302787 if (namelist == NULL && unamelist == NULL)
2788 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002789
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002790 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002791 str = strdup(ent->s);
2792 if (str == NULL) {
2793 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302794 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002795 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002796 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002797 p = strchr(str, ':');
2798 if (p) {
2799 group = str;
2800 *p = '\0';
2801 event = p + 1;
2802 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002803 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002804 event = str;
2805 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302806
2807 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2808 if (ret < 0) {
2809 pr_err("Failed to copy event.");
2810 free(str);
2811 goto error;
2812 }
2813
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002814 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302815
2816 if (namelist)
2817 ret = del_trace_probe_event(kfd, buf, namelist);
2818
2819 if (unamelist && ret != 0)
2820 ret = del_trace_probe_event(ufd, buf, unamelist);
2821
2822 if (ret != 0)
2823 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2824
Masami Hiramatsufa282442009-12-08 17:03:23 -05002825 free(str);
2826 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302827
2828error:
2829 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302830 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302831 close(kfd);
2832 }
2833
2834 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302835 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302836 close(ufd);
2837 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002838
2839 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002840}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302841
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002842/* TODO: don't use a global variable for filter ... */
2843static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002844
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002845/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002846 * If a symbol corresponds to a function with global binding and
2847 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002848 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002849static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002850 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002851{
Namhyung Kime578da32015-03-06 16:31:29 +09002852 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002853 return 0;
2854 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002855}
2856
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002857int show_available_funcs(const char *target, struct strfilter *_filter,
2858 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002859{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002860 struct map *map;
2861 int ret;
2862
2863 ret = init_symbol_maps(user);
2864 if (ret < 0)
2865 return ret;
2866
2867 /* Get a symbol map */
2868 if (user)
2869 map = dso__new_map(target);
2870 else
2871 map = kernel_get_module_map(target);
2872 if (!map) {
2873 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002874 return -EINVAL;
2875 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002876
2877 /* Load symbols with given filter */
2878 available_func_filter = _filter;
2879 if (map__load(map, filter_available_functions)) {
2880 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2881 goto end;
2882 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002883 if (!dso__sorted_by_name(map->dso, map->type))
2884 dso__sort_by_name(map->dso, map->type);
2885
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002886 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302887 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002888 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2889end:
2890 if (user) {
2891 dso__delete(map->dso);
2892 map__delete(map);
2893 }
2894 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302895
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002896 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302897}
2898