blob: 9feba0e3343e4ed0a0fd7a0a15f182cc143750b8 [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) {
313 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) {
314 address = sym->start;
315 break;
316 }
317 }
318 if (!address) {
319 ret = -ENOENT;
320 goto out;
321 }
322 pr_debug("Symbol %s address found : %lx\n", pp->function, address);
323
324 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
325 result);
326 if (ret <= 0)
327 ret = (!ret) ? -ENOENT : ret;
328 else {
329 result->offset += pp->offset;
330 result->line += pp->line;
331 ret = 0;
332 }
333
334out:
335 put_target_map(map, uprobes);
336 return ret;
337
338}
339
340static int get_alternative_probe_event(struct debuginfo *dinfo,
341 struct perf_probe_event *pev,
342 struct perf_probe_point *tmp,
343 const char *target)
344{
345 int ret;
346
347 memcpy(tmp, &pev->point, sizeof(*tmp));
348 memset(&pev->point, 0, sizeof(pev->point));
349 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
350 target, pev->uprobes);
351 if (ret < 0)
352 memcpy(&pev->point, tmp, sizeof(*tmp));
353
354 return ret;
355}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000356
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900357static int get_alternative_line_range(struct debuginfo *dinfo,
358 struct line_range *lr,
359 const char *target, bool user)
360{
361 struct perf_probe_point pp = { 0 }, result = { 0 };
362 int ret, len = 0;
363
364 pp.function = lr->function;
365 pp.file = lr->file;
366 pp.line = lr->start;
367 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);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900795 free(tmp); /* Free old path */
796 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000797 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900798 return ret;
799 }
800
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300801 setup_pager();
802
803 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900804 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300805 lr->start - lr->offset);
806 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100807 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808
809 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400810 if (fp == NULL) {
811 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000812 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400813 return -errno;
814 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300815 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100816 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100817 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100818 if (ret < 0)
819 goto end;
820 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300821
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000822 intlist__for_each(ln, lr->line_list) {
823 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100824 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100825 if (ret < 0)
826 goto end;
827 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100828 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400829 if (ret < 0)
830 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831 }
832
833 if (lr->end == INT_MAX)
834 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100835 while (l <= lr->end) {
836 ret = show_one_line_or_eof(fp, l++ - lr->offset);
837 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100838 break;
839 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400840end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300841 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300843}
844
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000845int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000846{
847 int ret;
848
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000849 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000850 if (ret < 0)
851 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900852 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000853 exit_symbol_maps();
854
855 return ret;
856}
857
Masami Hiramatsuff741782011-06-27 16:27:39 +0900858static int show_available_vars_at(struct debuginfo *dinfo,
859 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900860 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900861 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900862{
863 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900864 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900865 struct str_node *node;
866 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900867 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900868 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900869
870 buf = synthesize_perf_probe_point(&pev->point);
871 if (!buf)
872 return -EINVAL;
873 pr_debug("Searching variables at %s\n", buf);
874
Masami Hiramatsuff741782011-06-27 16:27:39 +0900875 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
876 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900877 if (!ret) { /* Not found, retry with an alternative */
878 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
879 if (!ret) {
880 ret = debuginfo__find_available_vars_at(dinfo, pev,
881 &vls, max_vls, externs);
882 /* Release the old probe_point */
883 clear_perf_probe_point(&tmp);
884 }
885 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900886 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000887 if (ret == 0 || ret == -ENOENT) {
888 pr_err("Failed to find the address of %s\n", buf);
889 ret = -ENOENT;
890 } else
891 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900892 goto end;
893 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000894
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900895 /* Some variables are found */
896 fprintf(stdout, "Available variables at %s\n", buf);
897 for (i = 0; i < ret; i++) {
898 vl = &vls[i];
899 /*
900 * A probe point might be converted to
901 * several trace points.
902 */
903 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
904 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300905 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900906 nvars = 0;
907 if (vl->vars) {
908 strlist__for_each(node, vl->vars) {
909 var = strchr(node->s, '\t') + 1;
910 if (strfilter__compare(_filter, var)) {
911 fprintf(stdout, "\t\t%s\n", node->s);
912 nvars++;
913 }
914 }
915 strlist__delete(vl->vars);
916 }
917 if (nvars == 0)
918 fprintf(stdout, "\t\t(No matched variables)\n");
919 }
920 free(vls);
921end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900922 free(buf);
923 return ret;
924}
925
926/* Show available variables on given probe point */
927int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900928 int max_vls, const char *module,
929 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900930{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900931 int i, ret = 0;
932 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900933
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000934 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900935 if (ret < 0)
936 return ret;
937
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000938 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900939 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000940 ret = -ENOENT;
941 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900942 }
943
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944 setup_pager();
945
Masami Hiramatsuff741782011-06-27 16:27:39 +0900946 for (i = 0; i < npevs && ret >= 0; i++)
947 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900948 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900949
950 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000951out:
952 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900953 return ret;
954}
955
Ingo Molnar89fe8082013-09-30 12:07:11 +0200956#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300957
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000958static int
959find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
960 struct perf_probe_point *pp __maybe_unused,
961 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300962{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000963 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300964}
965
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530966static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300967 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300968 int max_tevs __maybe_unused,
969 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300970{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400971 if (perf_probe_event_need_dwarf(pev)) {
972 pr_warning("Debuginfo-analysis is not supported.\n");
973 return -ENOSYS;
974 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530975
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300976 return 0;
977}
978
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300979int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000980 const char *module __maybe_unused,
981 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300982{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400983 pr_warning("Debuginfo-analysis is not supported.\n");
984 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985}
986
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300987int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
988 int npevs __maybe_unused, int max_vls __maybe_unused,
989 const char *module __maybe_unused,
990 struct strfilter *filter __maybe_unused,
991 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900992{
993 pr_warning("Debuginfo-analysis is not supported.\n");
994 return -ENOSYS;
995}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400996#endif
997
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000998void line_range__clear(struct line_range *lr)
999{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001000 free(lr->function);
1001 free(lr->file);
1002 free(lr->path);
1003 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001004 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001005 memset(lr, 0, sizeof(*lr));
1006}
1007
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001008int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001009{
1010 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001011 lr->line_list = intlist__new(NULL);
1012 if (!lr->line_list)
1013 return -ENOMEM;
1014 else
1015 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001016}
1017
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001018static int parse_line_num(char **ptr, int *val, const char *what)
1019{
1020 const char *start = *ptr;
1021
1022 errno = 0;
1023 *val = strtol(*ptr, ptr, 0);
1024 if (errno || *ptr == start) {
1025 semantic_error("'%s' is not a valid number.\n", what);
1026 return -EINVAL;
1027 }
1028 return 0;
1029}
1030
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001031/*
1032 * Stuff 'lr' according to the line range described by 'arg'.
1033 * The line range syntax is described by:
1034 *
1035 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001036 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001037 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001039{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001040 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001041 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001042
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001043 if (!name)
1044 return -ENOMEM;
1045
1046 lr->start = 0;
1047 lr->end = INT_MAX;
1048
1049 range = strchr(name, ':');
1050 if (range) {
1051 *range++ = '\0';
1052
1053 err = parse_line_num(&range, &lr->start, "start line");
1054 if (err)
1055 goto err;
1056
1057 if (*range == '+' || *range == '-') {
1058 const char c = *range++;
1059
1060 err = parse_line_num(&range, &lr->end, "end line");
1061 if (err)
1062 goto err;
1063
1064 if (c == '+') {
1065 lr->end += lr->start;
1066 /*
1067 * Adjust the number of lines here.
1068 * If the number of lines == 1, the
1069 * the end of line should be equal to
1070 * the start of line.
1071 */
1072 lr->end--;
1073 }
1074 }
1075
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001076 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001077
1078 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001079 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001080 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001082 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001083 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001084 if (*range != '\0') {
1085 semantic_error("Tailing with invalid str '%s'.\n", range);
1086 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001088 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001089
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001090 file = strchr(name, '@');
1091 if (file) {
1092 *file = '\0';
1093 lr->file = strdup(++file);
1094 if (lr->file == NULL) {
1095 err = -ENOMEM;
1096 goto err;
1097 }
1098 lr->function = name;
1099 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001100 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001101 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001102 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103
1104 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001105err:
1106 free(name);
1107 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001108}
1109
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001110/* Check the name is good for event/group */
1111static bool check_event_name(const char *name)
1112{
1113 if (!isalpha(*name) && *name != '_')
1114 return false;
1115 while (*++name != '\0') {
1116 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1117 return false;
1118 }
1119 return true;
1120}
1121
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001122/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001123static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001124{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001125 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001126 char *ptr, *tmp;
1127 char c, nc = 0;
1128 /*
1129 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001130 * perf probe [EVENT=]SRC[:LN|;PTN]
1131 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001132 *
1133 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001134 */
1135
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001136 ptr = strpbrk(arg, ";=@+%");
1137 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001138 *ptr = '\0';
1139 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001140 if (strchr(arg, ':')) {
1141 semantic_error("Group name is not supported yet.\n");
1142 return -ENOTSUP;
1143 }
1144 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001145 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001146 "follow C symbol-naming rule.\n", arg);
1147 return -EINVAL;
1148 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001149 pev->event = strdup(arg);
1150 if (pev->event == NULL)
1151 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001152 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001153 arg = tmp;
1154 }
1155
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001156 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157 if (ptr) {
1158 nc = *ptr;
1159 *ptr++ = '\0';
1160 }
1161
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001162 tmp = strdup(arg);
1163 if (tmp == NULL)
1164 return -ENOMEM;
1165
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001166 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001167 if (strchr(tmp, '.')) /* File */
1168 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001169 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001170 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001171
1172 /* Parse other options */
1173 while (ptr) {
1174 arg = ptr;
1175 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001176 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001177 pp->lazy_line = strdup(arg);
1178 if (pp->lazy_line == NULL)
1179 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001180 break;
1181 }
1182 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001183 if (ptr) {
1184 nc = *ptr;
1185 *ptr++ = '\0';
1186 }
1187 switch (c) {
1188 case ':': /* Line number */
1189 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001191 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 " in line number.\n");
1193 return -EINVAL;
1194 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001195 break;
1196 case '+': /* Byte offset from a symbol */
1197 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001199 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001200 " in offset.\n");
1201 return -EINVAL;
1202 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203 break;
1204 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001205 if (pp->file) {
1206 semantic_error("SRC@SRC is not allowed.\n");
1207 return -EINVAL;
1208 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001209 pp->file = strdup(arg);
1210 if (pp->file == NULL)
1211 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001212 break;
1213 case '%': /* Probe places */
1214 if (strcmp(arg, "return") == 0) {
1215 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001216 } else { /* Others not supported yet */
1217 semantic_error("%%%s is not supported.\n", arg);
1218 return -ENOTSUP;
1219 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001220 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001221 default: /* Buggy case */
1222 pr_err("This program has a bug at %s:%d.\n",
1223 __FILE__, __LINE__);
1224 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001225 break;
1226 }
1227 }
1228
1229 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001231 semantic_error("Lazy pattern can't be used with"
1232 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001233 return -EINVAL;
1234 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001235
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001236 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001237 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001238 return -EINVAL;
1239 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001240
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001242 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001243 return -EINVAL;
1244 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001245
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001246 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001247 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001248 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001249 return -EINVAL;
1250 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001251
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001253 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254 return -EINVAL;
1255 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001256
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001258 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259 return -EINVAL;
1260 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001261
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001262 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001263 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001264 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001265 return -EINVAL;
1266 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001267
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001269 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1270 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001271 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001272}
1273
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001274/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001275static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001276{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001277 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001278 struct perf_probe_arg_field **fieldp;
1279
1280 pr_debug("parsing arg: %s into ", str);
1281
Masami Hiramatsu48481932010-04-12 13:16:53 -04001282 tmp = strchr(str, '=');
1283 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001284 arg->name = strndup(str, tmp - str);
1285 if (arg->name == NULL)
1286 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001287 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001288 str = tmp + 1;
1289 }
1290
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001291 tmp = strchr(str, ':');
1292 if (tmp) { /* Type setting */
1293 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001294 arg->type = strdup(tmp + 1);
1295 if (arg->type == NULL)
1296 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001297 pr_debug("type:%s ", arg->type);
1298 }
1299
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001300 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001301 if (!is_c_varname(str) || !tmp) {
1302 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001303 arg->var = strdup(str);
1304 if (arg->var == NULL)
1305 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001306 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001307 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001308 }
1309
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001310 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001311 arg->var = strndup(str, tmp - str);
1312 if (arg->var == NULL)
1313 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001314 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001315 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001316 fieldp = &arg->field;
1317
1318 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001319 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1320 if (*fieldp == NULL)
1321 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001322 if (*tmp == '[') { /* Array */
1323 str = tmp;
1324 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001325 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001326 if (*tmp != ']' || tmp == str + 1) {
1327 semantic_error("Array index must be a"
1328 " number.\n");
1329 return -EINVAL;
1330 }
1331 tmp++;
1332 if (*tmp == '\0')
1333 tmp = NULL;
1334 } else { /* Structure */
1335 if (*tmp == '.') {
1336 str = tmp + 1;
1337 (*fieldp)->ref = false;
1338 } else if (tmp[1] == '>') {
1339 str = tmp + 2;
1340 (*fieldp)->ref = true;
1341 } else {
1342 semantic_error("Argument parse error: %s\n",
1343 str);
1344 return -EINVAL;
1345 }
1346 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001347 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001348 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001349 (*fieldp)->name = strndup(str, tmp - str);
1350 if ((*fieldp)->name == NULL)
1351 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001352 if (*str != '[')
1353 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001354 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1355 fieldp = &(*fieldp)->next;
1356 }
1357 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001358 (*fieldp)->name = strdup(str);
1359 if ((*fieldp)->name == NULL)
1360 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001361 if (*str != '[')
1362 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001363 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001364
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001365 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001367 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001368 if (arg->name == NULL)
1369 return -ENOMEM;
1370 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001372}
1373
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001374/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001376{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001377 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001378 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001379
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001380 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 if (!argv) {
1382 pr_debug("Failed to split arguments.\n");
1383 return -ENOMEM;
1384 }
1385 if (argc - 1 > MAX_PROBE_ARGS) {
1386 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1387 ret = -ERANGE;
1388 goto out;
1389 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001390 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 ret = parse_perf_probe_point(argv[0], pev);
1392 if (ret < 0)
1393 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001395 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001396 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001397 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1398 if (pev->args == NULL) {
1399 ret = -ENOMEM;
1400 goto out;
1401 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001402 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1403 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1404 if (ret >= 0 &&
1405 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001406 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001407 " kretprobe.\n");
1408 ret = -EINVAL;
1409 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001410 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001412 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001413
1414 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001415}
1416
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001417/* Return true if this perf_probe_event requires debuginfo */
1418bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001419{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001420 int i;
1421
1422 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1423 return true;
1424
1425 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001426 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001427 return true;
1428
1429 return false;
1430}
1431
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301432/* Parse probe_events event into struct probe_point */
1433static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001434 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001435{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301436 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001437 char pr;
1438 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001439 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001440 int ret, i, argc;
1441 char **argv;
1442
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301443 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001444 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445 if (!argv) {
1446 pr_debug("Failed to split arguments.\n");
1447 return -ENOMEM;
1448 }
1449 if (argc < 2) {
1450 semantic_error("Too few probe arguments.\n");
1451 ret = -ERANGE;
1452 goto out;
1453 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001454
1455 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001456 argv0_str = strdup(argv[0]);
1457 if (argv0_str == NULL) {
1458 ret = -ENOMEM;
1459 goto out;
1460 }
1461 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1462 fmt2_str = strtok_r(NULL, "/", &fmt);
1463 fmt3_str = strtok_r(NULL, " \t", &fmt);
1464 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1465 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 semantic_error("Failed to parse event name: %s\n", argv[0]);
1467 ret = -EINVAL;
1468 goto out;
1469 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001470 pr = fmt1_str[0];
1471 tev->group = strdup(fmt2_str);
1472 tev->event = strdup(fmt3_str);
1473 if (tev->group == NULL || tev->event == NULL) {
1474 ret = -ENOMEM;
1475 goto out;
1476 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001477 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001479 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001480
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001481 /* Scan module name(if there), function name and offset */
1482 p = strchr(argv[1], ':');
1483 if (p) {
1484 tp->module = strndup(argv[1], p - argv[1]);
1485 p++;
1486 } else
1487 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001488 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001489 if (fmt1_str[0] == '0') /* only the address started with 0x */
1490 tp->address = strtoul(fmt1_str, NULL, 0);
1491 else {
1492 /* Only the symbol-based probe has offset */
1493 tp->symbol = strdup(fmt1_str);
1494 if (tp->symbol == NULL) {
1495 ret = -ENOMEM;
1496 goto out;
1497 }
1498 fmt2_str = strtok_r(NULL, "", &fmt);
1499 if (fmt2_str == NULL)
1500 tp->offset = 0;
1501 else
1502 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001503 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001504
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001505 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301506 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001507 if (tev->args == NULL) {
1508 ret = -ENOMEM;
1509 goto out;
1510 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001511 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001512 p = strchr(argv[i + 2], '=');
1513 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001514 *p++ = '\0';
1515 else
1516 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001517 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001518 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001519 tev->args[i].value = strdup(p);
1520 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1521 ret = -ENOMEM;
1522 goto out;
1523 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001524 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001525 ret = 0;
1526out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001527 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001528 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001530}
1531
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001532/* Compose only probe arg */
1533int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1534{
1535 struct perf_probe_arg_field *field = pa->field;
1536 int ret;
1537 char *tmp = buf;
1538
Masami Hiramatsu48481932010-04-12 13:16:53 -04001539 if (pa->name && pa->var)
1540 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1541 else
1542 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001543 if (ret <= 0)
1544 goto error;
1545 tmp += ret;
1546 len -= ret;
1547
1548 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001549 if (field->name[0] == '[')
1550 ret = e_snprintf(tmp, len, "%s", field->name);
1551 else
1552 ret = e_snprintf(tmp, len, "%s%s",
1553 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001554 if (ret <= 0)
1555 goto error;
1556 tmp += ret;
1557 len -= ret;
1558 field = field->next;
1559 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001560
1561 if (pa->type) {
1562 ret = e_snprintf(tmp, len, ":%s", pa->type);
1563 if (ret <= 0)
1564 goto error;
1565 tmp += ret;
1566 len -= ret;
1567 }
1568
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001569 return tmp - buf;
1570error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001571 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001572 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001573}
1574
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001575/* Compose only probe point (not argument) */
1576static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001577{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001578 char *buf, *tmp;
1579 char offs[32] = "", line[32] = "", file[32] = "";
1580 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001581
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001582 buf = zalloc(MAX_CMDLEN);
1583 if (buf == NULL) {
1584 ret = -ENOMEM;
1585 goto error;
1586 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001588 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589 if (ret <= 0)
1590 goto error;
1591 }
1592 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001593 ret = e_snprintf(line, 32, ":%d", pp->line);
1594 if (ret <= 0)
1595 goto error;
1596 }
1597 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001598 tmp = pp->file;
1599 len = strlen(tmp);
1600 if (len > 30) {
1601 tmp = strchr(pp->file + len - 30, '/');
1602 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1603 }
1604 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001605 if (ret <= 0)
1606 goto error;
1607 }
1608
1609 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001610 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1611 offs, pp->retprobe ? "%return" : "", line,
1612 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001613 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001614 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001615 if (ret <= 0)
1616 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001617
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001618 return buf;
1619error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001620 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001621 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001622 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001623}
1624
1625#if 0
1626char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1627{
1628 char *buf;
1629 int i, len, ret;
1630
1631 buf = synthesize_perf_probe_point(&pev->point);
1632 if (!buf)
1633 return NULL;
1634
1635 len = strlen(buf);
1636 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001637 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 pev->args[i].name);
1639 if (ret <= 0) {
1640 free(buf);
1641 return NULL;
1642 }
1643 len += ret;
1644 }
1645
1646 return buf;
1647}
1648#endif
1649
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301650static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651 char **buf, size_t *buflen,
1652 int depth)
1653{
1654 int ret;
1655 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301656 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001657 buflen, depth + 1);
1658 if (depth < 0)
1659 goto out;
1660 }
1661
1662 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1663 if (ret < 0)
1664 depth = ret;
1665 else {
1666 *buf += ret;
1667 *buflen -= ret;
1668 }
1669out:
1670 return depth;
1671
1672}
1673
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301674static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001675 char *buf, size_t buflen)
1676{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301677 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001678 int ret, depth = 0;
1679 char *tmp = buf;
1680
1681 /* Argument name or separator */
1682 if (arg->name)
1683 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1684 else
1685 ret = e_snprintf(buf, buflen, " ");
1686 if (ret < 0)
1687 return ret;
1688 buf += ret;
1689 buflen -= ret;
1690
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001691 /* Special case: @XXX */
1692 if (arg->value[0] == '@' && arg->ref)
1693 ref = ref->next;
1694
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001696 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301697 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001698 &buflen, 1);
1699 if (depth < 0)
1700 return depth;
1701 }
1702
1703 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001704 if (arg->value[0] == '@' && arg->ref)
1705 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1706 arg->ref->offset);
1707 else
1708 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001709 if (ret < 0)
1710 return ret;
1711 buf += ret;
1712 buflen -= ret;
1713
1714 /* Closing */
1715 while (depth--) {
1716 ret = e_snprintf(buf, buflen, ")");
1717 if (ret < 0)
1718 return ret;
1719 buf += ret;
1720 buflen -= ret;
1721 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001722 /* Print argument type */
1723 if (arg->type) {
1724 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1725 if (ret <= 0)
1726 return ret;
1727 buf += ret;
1728 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001729
1730 return buf - tmp;
1731}
1732
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301733char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301735 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001736 char *buf;
1737 int i, len, ret;
1738
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001739 buf = zalloc(MAX_CMDLEN);
1740 if (buf == NULL)
1741 return NULL;
1742
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001743 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1744 tev->group, tev->event);
1745 if (len <= 0)
1746 goto error;
1747
1748 /* Uprobes must have tp->address and tp->module */
1749 if (tev->uprobes && (!tp->address || !tp->module))
1750 goto error;
1751
1752 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301753 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001754 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1755 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301756 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001757 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301758 tp->module ?: "", tp->module ? ":" : "",
1759 tp->symbol, tp->offset);
1760
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001761 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001763 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001764
1765 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301766 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001767 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001768 if (ret <= 0)
1769 goto error;
1770 len += ret;
1771 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001772
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001774error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001775 free(buf);
1776 return NULL;
1777}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001778
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001779static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1780 struct perf_probe_point *pp,
1781 bool is_kprobe)
1782{
1783 struct symbol *sym = NULL;
1784 struct map *map;
1785 u64 addr;
1786 int ret = -ENOENT;
1787
1788 if (!is_kprobe) {
1789 map = dso__new_map(tp->module);
1790 if (!map)
1791 goto out;
1792 addr = tp->address;
1793 sym = map__find_symbol(map, addr, NULL);
1794 } else {
1795 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1796 if (addr) {
1797 addr += tp->offset;
1798 sym = __find_kernel_function(addr, &map);
1799 }
1800 }
1801 if (!sym)
1802 goto out;
1803
1804 pp->retprobe = tp->retprobe;
1805 pp->offset = addr - map->unmap_ip(map, sym->start);
1806 pp->function = strdup(sym->name);
1807 ret = pp->function ? 0 : -ENOMEM;
1808
1809out:
1810 if (map && !is_kprobe) {
1811 dso__delete(map->dso);
1812 map__delete(map);
1813 }
1814
1815 return ret;
1816}
1817
1818static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1819 struct perf_probe_point *pp,
1820 bool is_kprobe)
1821{
1822 char buf[128];
1823 int ret;
1824
1825 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1826 if (!ret)
1827 return 0;
1828 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1829 if (!ret)
1830 return 0;
1831
1832 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1833
1834 if (tp->symbol) {
1835 pp->function = strdup(tp->symbol);
1836 pp->offset = tp->offset;
1837 } else if (!tp->module && !is_kprobe) {
1838 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1839 if (ret < 0)
1840 return ret;
1841 pp->function = strdup(buf);
1842 pp->offset = 0;
1843 }
1844 if (pp->function == NULL)
1845 return -ENOMEM;
1846
1847 pp->retprobe = tp->retprobe;
1848
1849 return 0;
1850}
1851
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301852static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301853 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001854{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001855 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001856 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001857
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001858 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001859 pev->event = strdup(tev->event);
1860 pev->group = strdup(tev->group);
1861 if (pev->event == NULL || pev->group == NULL)
1862 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001863
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001864 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001865 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001866 if (ret < 0)
1867 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001868
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001869 /* Convert trace_arg to probe_arg */
1870 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001871 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1872 if (pev->args == NULL)
1873 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001874 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001875 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001876 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001877 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301878 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001879 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001880 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001882 if (pev->args[i].name == NULL && ret >= 0)
1883 ret = -ENOMEM;
1884 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001885
1886 if (ret < 0)
1887 clear_perf_probe_event(pev);
1888
1889 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890}
1891
1892void clear_perf_probe_event(struct perf_probe_event *pev)
1893{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001894 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001895 int i;
1896
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001897 free(pev->event);
1898 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001899 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001900
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001901 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001902 free(pev->args[i].name);
1903 free(pev->args[i].var);
1904 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001905 field = pev->args[i].field;
1906 while (field) {
1907 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001908 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001909 free(field);
1910 field = next;
1911 }
1912 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001913 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001914 memset(pev, 0, sizeof(*pev));
1915}
1916
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301917static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301919 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001920 int i;
1921
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001922 free(tev->event);
1923 free(tev->group);
1924 free(tev->point.symbol);
1925 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001926 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001927 free(tev->args[i].name);
1928 free(tev->args[i].value);
1929 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001930 ref = tev->args[i].ref;
1931 while (ref) {
1932 next = ref->next;
1933 free(ref);
1934 ref = next;
1935 }
1936 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001937 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001938 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001939}
1940
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001941static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301942{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001943 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301944
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001945 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301946 const char *config;
1947
1948 if (!is_kprobe)
1949 config = "CONFIG_UPROBE_EVENTS";
1950 else
1951 config = "CONFIG_KPROBE_EVENTS";
1952
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001953 pr_warning("%cprobe_events file does not exist"
1954 " - please rebuild kernel with %s.\n",
1955 is_kprobe ? 'k' : 'u', config);
1956 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001957 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001958 else
1959 pr_warning("Failed to open %cprobe_events: %s\n",
1960 is_kprobe ? 'k' : 'u',
1961 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301962}
1963
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001964static void print_both_open_warning(int kerr, int uerr)
1965{
1966 /* Both kprobes and uprobes are disabled, warn it. */
1967 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001968 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001969 else if (kerr == -ENOENT && uerr == -ENOENT)
1970 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1971 "or/and CONFIG_UPROBE_EVENTS.\n");
1972 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001973 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001974 pr_warning("Failed to open kprobe events: %s.\n",
1975 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1976 pr_warning("Failed to open uprobe events: %s.\n",
1977 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1978 }
1979}
1980
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001981static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001982{
1983 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001984 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001985 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001986 int ret;
1987
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001988 __debugfs = tracefs_find_mountpoint();
1989 if (__debugfs == NULL) {
1990 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001991
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001992 __debugfs = debugfs_find_mountpoint();
1993 if (__debugfs == NULL)
1994 return -ENOTSUP;
1995 }
1996
1997 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1998 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001999 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002000 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002001 if (readwrite && !probe_event_dry_run)
2002 ret = open(buf, O_RDWR, O_APPEND);
2003 else
2004 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002005
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302006 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002007 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002008 }
2009 return ret;
2010}
2011
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302012static int open_kprobe_events(bool readwrite)
2013{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002014 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302015}
2016
2017static int open_uprobe_events(bool readwrite)
2018{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002019 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302020}
2021
2022/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302023static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002024{
2025 int ret, idx;
2026 FILE *fp;
2027 char buf[MAX_CMDLEN];
2028 char *p;
2029 struct strlist *sl;
2030
2031 sl = strlist__new(true, NULL);
2032
2033 fp = fdopen(dup(fd), "r");
2034 while (!feof(fp)) {
2035 p = fgets(buf, MAX_CMDLEN, fp);
2036 if (!p)
2037 break;
2038
2039 idx = strlen(p) - 1;
2040 if (p[idx] == '\n')
2041 p[idx] = '\0';
2042 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002043 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002044 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002045 strlist__delete(sl);
2046 return NULL;
2047 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002048 }
2049 fclose(fp);
2050
2051 return sl;
2052}
2053
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002054struct kprobe_blacklist_node {
2055 struct list_head list;
2056 unsigned long start;
2057 unsigned long end;
2058 char *symbol;
2059};
2060
2061static void kprobe_blacklist__delete(struct list_head *blacklist)
2062{
2063 struct kprobe_blacklist_node *node;
2064
2065 while (!list_empty(blacklist)) {
2066 node = list_first_entry(blacklist,
2067 struct kprobe_blacklist_node, list);
2068 list_del(&node->list);
2069 free(node->symbol);
2070 free(node);
2071 }
2072}
2073
2074static int kprobe_blacklist__load(struct list_head *blacklist)
2075{
2076 struct kprobe_blacklist_node *node;
2077 const char *__debugfs = debugfs_find_mountpoint();
2078 char buf[PATH_MAX], *p;
2079 FILE *fp;
2080 int ret;
2081
2082 if (__debugfs == NULL)
2083 return -ENOTSUP;
2084
2085 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2086 if (ret < 0)
2087 return ret;
2088
2089 fp = fopen(buf, "r");
2090 if (!fp)
2091 return -errno;
2092
2093 ret = 0;
2094 while (fgets(buf, PATH_MAX, fp)) {
2095 node = zalloc(sizeof(*node));
2096 if (!node) {
2097 ret = -ENOMEM;
2098 break;
2099 }
2100 INIT_LIST_HEAD(&node->list);
2101 list_add_tail(&node->list, blacklist);
2102 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2103 ret = -EINVAL;
2104 break;
2105 }
2106 p = strchr(buf, '\t');
2107 if (p) {
2108 p++;
2109 if (p[strlen(p) - 1] == '\n')
2110 p[strlen(p) - 1] = '\0';
2111 } else
2112 p = (char *)"unknown";
2113 node->symbol = strdup(p);
2114 if (!node->symbol) {
2115 ret = -ENOMEM;
2116 break;
2117 }
2118 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2119 node->start, node->end, node->symbol);
2120 ret++;
2121 }
2122 if (ret < 0)
2123 kprobe_blacklist__delete(blacklist);
2124 fclose(fp);
2125
2126 return ret;
2127}
2128
2129static struct kprobe_blacklist_node *
2130kprobe_blacklist__find_by_address(struct list_head *blacklist,
2131 unsigned long address)
2132{
2133 struct kprobe_blacklist_node *node;
2134
2135 list_for_each_entry(node, blacklist, list) {
2136 if (node->start <= address && address <= node->end)
2137 return node;
2138 }
2139
2140 return NULL;
2141}
2142
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002143/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002144static int show_perf_probe_event(struct perf_probe_event *pev,
2145 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002146{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002147 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002148 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002149 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002150
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002151 /* Synthesize only event probe point */
2152 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002153 if (!place)
2154 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002155
2156 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002157 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002158 return ret;
2159
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002160 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002161 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002162 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002163
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002164 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002165 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002166 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002167 ret = synthesize_perf_probe_arg(&pev->args[i],
2168 buf, 128);
2169 if (ret < 0)
2170 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002171 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002172 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002173 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002174 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002175 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002176 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002177}
2178
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302179static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002180{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302181 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302182 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002183 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002184 struct strlist *rawlist;
2185 struct str_node *ent;
2186
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002187 memset(&tev, 0, sizeof(tev));
2188 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002189
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302190 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002191 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002192 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002193
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002194 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302195 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002196 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302197 ret = convert_to_perf_probe_event(&tev, &pev,
2198 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002199 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002200 ret = show_perf_probe_event(&pev,
2201 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002202 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002203 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302204 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002205 if (ret < 0)
2206 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002207 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002208 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209
2210 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002211}
2212
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302213/* List up current perf-probe events */
2214int show_perf_probe_events(void)
2215{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002216 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217
2218 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002220 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302221 if (ret < 0)
2222 return ret;
2223
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002224 kp_fd = open_kprobe_events(false);
2225 if (kp_fd >= 0) {
2226 ret = __show_perf_probe_events(kp_fd, true);
2227 close(kp_fd);
2228 if (ret < 0)
2229 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302230 }
2231
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002232 up_fd = open_uprobe_events(false);
2233 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002234 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002235 ret = kp_fd;
2236 goto out;
2237 }
2238
2239 if (up_fd >= 0) {
2240 ret = __show_perf_probe_events(up_fd, false);
2241 close(up_fd);
2242 }
2243out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002244 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302245 return ret;
2246}
2247
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002248/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302249static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002250{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002251 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002252 struct strlist *sl, *rawlist;
2253 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302254 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002255 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002256
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002257 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302258 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002259 if (!rawlist)
2260 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002261 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002262 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302263 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002264 if (ret < 0)
2265 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002266 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002267 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2268 tev.event);
2269 if (ret >= 0)
2270 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002271 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302273 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002274 if (ret < 0)
2275 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002276 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002277 strlist__delete(rawlist);
2278
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002279 if (ret < 0) {
2280 strlist__delete(sl);
2281 return NULL;
2282 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002283 return sl;
2284}
2285
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302286static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002287{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002288 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302289 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002290 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002291
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002292 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302293 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002294 return -EINVAL;
2295 }
2296
Masami Hiramatsufa282442009-12-08 17:03:23 -05002297 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002298 if (!probe_event_dry_run) {
2299 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002300 if (ret <= 0) {
2301 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002302 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002303 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002304 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002305 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002306 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002307 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002308}
2309
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002310static int get_new_event_name(char *buf, size_t len, const char *base,
2311 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002312{
2313 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002314
2315 /* Try no suffix */
2316 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002317 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002318 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319 return ret;
2320 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002321 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002322 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002323
Masami Hiramatsud761b082009-12-15 10:32:25 -05002324 if (!allow_suffix) {
2325 pr_warning("Error: event \"%s\" already exists. "
2326 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002327 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002328 }
2329
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002330 /* Try to add suffix */
2331 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002332 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002333 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002334 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002335 return ret;
2336 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002337 if (!strlist__has_entry(namelist, buf))
2338 break;
2339 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002340 if (i == MAX_EVENT_INDEX) {
2341 pr_warning("Too many events are on the same function.\n");
2342 ret = -ERANGE;
2343 }
2344
2345 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002346}
2347
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002348/* Warn if the current kernel's uprobe implementation is old */
2349static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2350{
2351 int i;
2352 char *buf = synthesize_probe_trace_command(tev);
2353
2354 /* Old uprobe event doesn't support memory dereference */
2355 if (!tev->uprobes || tev->nargs == 0 || !buf)
2356 goto out;
2357
2358 for (i = 0; i < tev->nargs; i++)
2359 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2360 pr_warning("Please upgrade your kernel to at least "
2361 "3.14 to have access to feature %s\n",
2362 tev->args[i].value);
2363 break;
2364 }
2365out:
2366 free(buf);
2367}
2368
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302369static int __add_probe_trace_events(struct perf_probe_event *pev,
2370 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002371 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002372{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002373 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302374 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002375 char buf[64];
2376 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002377 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002378 LIST_HEAD(blacklist);
2379 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002380
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302381 if (pev->uprobes)
2382 fd = open_uprobe_events(true);
2383 else
2384 fd = open_kprobe_events(true);
2385
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002386 if (fd < 0) {
2387 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002388 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002389 }
2390
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002391 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302392 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002393 if (!namelist) {
2394 pr_debug("Failed to get current event list.\n");
2395 return -EIO;
2396 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002397 /* Get kprobe blacklist if exists */
2398 if (!pev->uprobes) {
2399 ret = kprobe_blacklist__load(&blacklist);
2400 if (ret < 0)
2401 pr_debug("No kprobe blacklist support, ignored\n");
2402 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002403
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002404 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002405 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002406 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002407 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002408 /* Ensure that the address is NOT blacklisted */
2409 node = kprobe_blacklist__find_by_address(&blacklist,
2410 tev->point.address);
2411 if (node) {
2412 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2413 continue;
2414 }
2415
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002416 if (pev->event)
2417 event = pev->event;
2418 else
2419 if (pev->point.function)
2420 event = pev->point.function;
2421 else
2422 event = tev->point.symbol;
2423 if (pev->group)
2424 group = pev->group;
2425 else
2426 group = PERFPROBE_GROUP;
2427
2428 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002429 ret = get_new_event_name(buf, 64, event,
2430 namelist, allow_suffix);
2431 if (ret < 0)
2432 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002433 event = buf;
2434
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002435 tev->event = strdup(event);
2436 tev->group = strdup(group);
2437 if (tev->event == NULL || tev->group == NULL) {
2438 ret = -ENOMEM;
2439 break;
2440 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302441 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002442 if (ret < 0)
2443 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002444 /* Add added event name to namelist */
2445 strlist__add(namelist, event);
2446
2447 /* Trick here - save current event/group */
2448 event = pev->event;
2449 group = pev->group;
2450 pev->event = tev->event;
2451 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002452 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002453 /* Trick here - restore current event/group */
2454 pev->event = (char *)event;
2455 pev->group = (char *)group;
2456
2457 /*
2458 * Probes after the first probe which comes from same
2459 * user input are always allowed to add suffix, because
2460 * there might be several addresses corresponding to
2461 * one code line.
2462 */
2463 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002464 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002465 if (ret == -EINVAL && pev->uprobes)
2466 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002467
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002468 /* Note that it is possible to skip all events because of blacklist */
2469 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002470 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002471 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2472 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002473 tev->event);
2474 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002475
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002476 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002477 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002478 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002479 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002480}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002481
Namhyung Kim564c62a2015-01-14 20:18:07 +09002482static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002483{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002484 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002485 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002486
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002487 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002488 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2489 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002490 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002491
2492 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002493}
2494
2495#define strdup_or_goto(str, label) \
2496 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2497
2498/*
2499 * Find probe function addresses from map.
2500 * Return an error or the number of found probe_trace_event
2501 */
2502static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2503 struct probe_trace_event **tevs,
2504 int max_tevs, const char *target)
2505{
2506 struct map *map = NULL;
2507 struct kmap *kmap = NULL;
2508 struct ref_reloc_sym *reloc_sym = NULL;
2509 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002510 struct probe_trace_event *tev;
2511 struct perf_probe_point *pp = &pev->point;
2512 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002513 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002514 int ret, i;
2515
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002516 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002517 if (!map) {
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521
2522 /*
2523 * Load matched symbols: Since the different local symbols may have
2524 * same name but different addresses, this lists all the symbols.
2525 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002526 num_matched_functions = find_probe_functions(map, pp->function);
2527 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002528 pr_err("Failed to find symbol %s in %s\n", pp->function,
2529 target ? : "kernel");
2530 ret = -ENOENT;
2531 goto out;
2532 } else if (num_matched_functions > max_tevs) {
2533 pr_err("Too many functions matched in %s\n",
2534 target ? : "kernel");
2535 ret = -E2BIG;
2536 goto out;
2537 }
2538
Namhyung Kim25dd9172015-01-14 20:18:08 +09002539 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002540 kmap = map__kmap(map);
2541 reloc_sym = kmap->ref_reloc_sym;
2542 if (!reloc_sym) {
2543 pr_warning("Relocated base symbol is not found!\n");
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547 }
2548
2549 /* Setup result trace-probe-events */
2550 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2551 if (!*tevs) {
2552 ret = -ENOMEM;
2553 goto out;
2554 }
2555
2556 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002557
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002558 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002559 tev = (*tevs) + ret;
2560 tp = &tev->point;
2561 if (ret == num_matched_functions) {
2562 pr_warning("Too many symbols are listed. Skip it.\n");
2563 break;
2564 }
2565 ret++;
2566
2567 if (pp->offset > sym->end - sym->start) {
2568 pr_warning("Offset %ld is bigger than the size of %s\n",
2569 pp->offset, sym->name);
2570 ret = -ENOENT;
2571 goto err_out;
2572 }
2573 /* Add one probe point */
2574 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2575 if (reloc_sym) {
2576 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2577 tp->offset = tp->address - reloc_sym->addr;
2578 } else {
2579 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2580 tp->offset = pp->offset;
2581 }
2582 tp->retprobe = pp->retprobe;
2583 if (target)
2584 tev->point.module = strdup_or_goto(target, nomem_out);
2585 tev->uprobes = pev->uprobes;
2586 tev->nargs = pev->nargs;
2587 if (tev->nargs) {
2588 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2589 tev->nargs);
2590 if (tev->args == NULL)
2591 goto nomem_out;
2592 }
2593 for (i = 0; i < tev->nargs; i++) {
2594 if (pev->args[i].name)
2595 tev->args[i].name =
2596 strdup_or_goto(pev->args[i].name,
2597 nomem_out);
2598
2599 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2600 nomem_out);
2601 if (pev->args[i].type)
2602 tev->args[i].type =
2603 strdup_or_goto(pev->args[i].type,
2604 nomem_out);
2605 }
2606 }
2607
2608out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002609 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002610 return ret;
2611
2612nomem_out:
2613 ret = -ENOMEM;
2614err_out:
2615 clear_probe_trace_events(*tevs, num_matched_functions);
2616 zfree(tevs);
2617 goto out;
2618}
2619
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302620static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2621 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302622 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002623{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002624 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002625
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002626 if (pev->uprobes && !pev->group) {
2627 /* Replace group name if not given */
2628 ret = convert_exec_to_group(target, &pev->group);
2629 if (ret != 0) {
2630 pr_warning("Failed to make a group name.\n");
2631 return ret;
2632 }
2633 }
2634
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002635 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302636 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002637 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002638 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002639
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002640 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002641}
2642
2643struct __event_package {
2644 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302645 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002646 int ntevs;
2647};
2648
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002649int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302650 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002651{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002652 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002653 struct __event_package *pkgs;
2654
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302655 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002656 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302657
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002658 if (pkgs == NULL)
2659 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002660
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002661 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002662 if (ret < 0) {
2663 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002664 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002665 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002666
2667 /* Loop 1: convert all events */
2668 for (i = 0; i < npevs; i++) {
2669 pkgs[i].pev = &pevs[i];
2670 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302671 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002672 &pkgs[i].tevs,
2673 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302674 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002675 if (ret < 0)
2676 goto end;
2677 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002678 }
2679
2680 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002681 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302682 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002683 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002684 if (ret < 0)
2685 break;
2686 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002687end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002688 /* Loop 3: cleanup and free trace events */
2689 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002690 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302691 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002692 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002693 }
2694 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002695 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696
2697 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002698}
2699
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302700static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002701{
2702 char *p;
2703 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002704 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002705
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302706 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002707 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2708 if (ret < 0)
2709 goto error;
2710
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002711 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002712 if (!p) {
2713 pr_debug("Internal error: %s should have ':' but not.\n",
2714 ent->s);
2715 ret = -ENOTSUP;
2716 goto error;
2717 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002718 *p = '/';
2719
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002720 pr_debug("Writing event: %s\n", buf);
2721 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002722 if (ret < 0) {
2723 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002724 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002725 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002726
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002727 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002728 return 0;
2729error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002730 pr_warning("Failed to delete event: %s\n",
2731 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002732 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002733}
2734
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302735static int del_trace_probe_event(int fd, const char *buf,
2736 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002737{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002738 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302739 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002740
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002741 if (strpbrk(buf, "*?")) { /* Glob-exp */
2742 strlist__for_each_safe(ent, n, namelist)
2743 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302744 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002745 if (ret < 0)
2746 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002747 strlist__remove(namelist, ent);
2748 }
2749 } else {
2750 ent = strlist__find(namelist, buf);
2751 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302752 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002753 if (ret >= 0)
2754 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002755 }
2756 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002757
2758 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002759}
2760
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002761int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002762{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302763 int ret = -1, ufd = -1, kfd = -1;
2764 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002765 const char *group, *event;
2766 char *p, *str;
2767 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302768 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002769
Masami Hiramatsufa282442009-12-08 17:03:23 -05002770 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302771 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002772 if (kfd >= 0)
2773 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302774
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302775 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002776 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302777 unamelist = get_probe_trace_event_names(ufd, true);
2778
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002779 if (kfd < 0 && ufd < 0) {
2780 print_both_open_warning(kfd, ufd);
2781 goto error;
2782 }
2783
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302784 if (namelist == NULL && unamelist == NULL)
2785 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002786
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002787 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002788 str = strdup(ent->s);
2789 if (str == NULL) {
2790 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302791 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002792 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002793 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002794 p = strchr(str, ':');
2795 if (p) {
2796 group = str;
2797 *p = '\0';
2798 event = p + 1;
2799 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002800 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002801 event = str;
2802 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302803
2804 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2805 if (ret < 0) {
2806 pr_err("Failed to copy event.");
2807 free(str);
2808 goto error;
2809 }
2810
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002811 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302812
2813 if (namelist)
2814 ret = del_trace_probe_event(kfd, buf, namelist);
2815
2816 if (unamelist && ret != 0)
2817 ret = del_trace_probe_event(ufd, buf, unamelist);
2818
2819 if (ret != 0)
2820 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2821
Masami Hiramatsufa282442009-12-08 17:03:23 -05002822 free(str);
2823 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302824
2825error:
2826 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302827 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302828 close(kfd);
2829 }
2830
2831 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302832 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302833 close(ufd);
2834 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002835
2836 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002837}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302838
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002839/* TODO: don't use a global variable for filter ... */
2840static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002841
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002842/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002843 * If a symbol corresponds to a function with global binding and
2844 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002845 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002846static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002847 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002848{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002849 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002850 strfilter__compare(available_func_filter, sym->name))
2851 return 0;
2852 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853}
2854
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002855int show_available_funcs(const char *target, struct strfilter *_filter,
2856 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002857{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002858 struct map *map;
2859 int ret;
2860
2861 ret = init_symbol_maps(user);
2862 if (ret < 0)
2863 return ret;
2864
2865 /* Get a symbol map */
2866 if (user)
2867 map = dso__new_map(target);
2868 else
2869 map = kernel_get_module_map(target);
2870 if (!map) {
2871 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002872 return -EINVAL;
2873 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002874
2875 /* Load symbols with given filter */
2876 available_func_filter = _filter;
2877 if (map__load(map, filter_available_functions)) {
2878 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2879 goto end;
2880 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002881 if (!dso__sorted_by_name(map->dso, map->type))
2882 dso__sort_by_name(map->dso, map->type);
2883
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002884 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302885 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002886 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2887end:
2888 if (user) {
2889 dso__delete(map->dso);
2890 map__delete(map);
2891 }
2892 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302893
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002894 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302895}
2896