blob: c379ea0edfd53e10e84a036ac33a9fef4e024838 [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 Kim0a7e6d12014-08-12 15:40:45 +090083 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040084 if (ret < 0) {
85 pr_debug("Failed to init symbol map.\n");
86 goto out;
87 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (host_machine || user_only) /* already initialized */
90 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030091
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000092 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94
95 host_machine = machine__new_host();
96 if (!host_machine) {
97 pr_debug("machine__new_host() failed.\n");
98 symbol__exit();
99 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107static void exit_symbol_maps(void)
108{
109 if (host_machine) {
110 machine__delete(host_machine);
111 host_machine = NULL;
112 }
113 symbol__exit();
114}
115
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900116static struct symbol *__find_kernel_function_by_name(const char *name,
117 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400118{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000119 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120 NULL);
121}
122
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000123static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
124{
125 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
126}
127
128static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
129{
130 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 struct kmap *kmap;
132
133 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
134 return NULL;
135
136 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800154 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
161 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000162 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000166 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
171 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
172 struct map *pos = rb_entry(nd, struct map, rb_node);
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900181static struct map *get_target_map(const char *target, bool user)
182{
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193 /* Only the user map needs to be released */
194 dso__delete(map->dso);
195 map__delete(map);
196 }
197}
198
199
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900200static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900201{
202 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100203 struct map *map;
204 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900205
206 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400207 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
208 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900209 if (strncmp(dso->short_name + 1, module,
210 dso->short_name_len - 2) == 0)
211 goto found;
212 }
213 pr_debug("Failed to find module %s.\n", module);
214 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100215 }
216
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000217 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100218 dso = map->dso;
219
220 vmlinux_name = symbol_conf.vmlinux_name;
221 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300222 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100223 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900224 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100225 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900226 pr_debug("Failed to load kernel map.\n");
227 return NULL;
228 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400229 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900230found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900231 return dso;
232}
233
234const char *kernel_get_module_path(const char *module)
235{
236 struct dso *dso = kernel_get_module_dso(module);
237 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900238}
239
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000240static int convert_exec_to_group(const char *exec, char **result)
241{
242 char *ptr1, *ptr2, *exec_copy;
243 char buf[64];
244 int ret;
245
246 exec_copy = strdup(exec);
247 if (!exec_copy)
248 return -ENOMEM;
249
250 ptr1 = basename(exec_copy);
251 if (!ptr1) {
252 ret = -EINVAL;
253 goto out;
254 }
255
256 ptr2 = strpbrk(ptr1, "-._");
257 if (ptr2)
258 *ptr2 = '\0';
259 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
260 if (ret < 0)
261 goto out;
262
263 *result = strdup(buf);
264 ret = *result ? 0 : -ENOMEM;
265
266out:
267 free(exec_copy);
268 return ret;
269}
270
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900271static void clear_perf_probe_point(struct perf_probe_point *pp)
272{
273 free(pp->file);
274 free(pp->function);
275 free(pp->lazy_line);
276}
277
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000278static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
279{
280 int i;
281
282 for (i = 0; i < ntevs; i++)
283 clear_probe_trace_event(tevs + i);
284}
285
Ingo Molnar89fe8082013-09-30 12:07:11 +0200286#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900287/*
288 * Some binaries like glibc have special symbols which are on the symbol
289 * table, but not in the debuginfo. If we can find the address of the
290 * symbol from map, we can translate the address back to the probe point.
291 */
292static int find_alternative_probe_point(struct debuginfo *dinfo,
293 struct perf_probe_point *pp,
294 struct perf_probe_point *result,
295 const char *target, bool uprobes)
296{
297 struct map *map = NULL;
298 struct symbol *sym;
299 u64 address = 0;
300 int ret = -ENOENT;
301
302 /* This can work only for function-name based one */
303 if (!pp->function || pp->file)
304 return -ENOTSUP;
305
306 map = get_target_map(target, uprobes);
307 if (!map)
308 return -EINVAL;
309
310 /* Find the address of given function */
311 map__for_each_symbol_by_name(map, pp->function, sym) {
312 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) {
313 address = sym->start;
314 break;
315 }
316 }
317 if (!address) {
318 ret = -ENOENT;
319 goto out;
320 }
321 pr_debug("Symbol %s address found : %lx\n", pp->function, address);
322
323 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
324 result);
325 if (ret <= 0)
326 ret = (!ret) ? -ENOENT : ret;
327 else {
328 result->offset += pp->offset;
329 result->line += pp->line;
330 ret = 0;
331 }
332
333out:
334 put_target_map(map, uprobes);
335 return ret;
336
337}
338
339static int get_alternative_probe_event(struct debuginfo *dinfo,
340 struct perf_probe_event *pev,
341 struct perf_probe_point *tmp,
342 const char *target)
343{
344 int ret;
345
346 memcpy(tmp, &pev->point, sizeof(*tmp));
347 memset(&pev->point, 0, sizeof(pev->point));
348 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
349 target, pev->uprobes);
350 if (ret < 0)
351 memcpy(&pev->point, tmp, sizeof(*tmp));
352
353 return ret;
354}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000355
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900356static int get_alternative_line_range(struct debuginfo *dinfo,
357 struct line_range *lr,
358 const char *target, bool user)
359{
360 struct perf_probe_point pp = { 0 }, result = { 0 };
361 int ret, len = 0;
362
363 pp.function = lr->function;
364 pp.file = lr->file;
365 pp.line = lr->start;
366 if (lr->end != INT_MAX)
367 len = lr->end - lr->start;
368 ret = find_alternative_probe_point(dinfo, &pp, &result,
369 target, user);
370 if (!ret) {
371 lr->function = result.function;
372 lr->file = result.file;
373 lr->start = result.line;
374 if (lr->end != INT_MAX)
375 lr->end = lr->start + len;
376 clear_perf_probe_point(&pp);
377 }
378 return ret;
379}
380
Masami Hiramatsuff741782011-06-27 16:27:39 +0900381/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000382static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900383{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000384 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000385 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900386
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000387 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900388 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900389 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000390 if (!silent)
391 pr_err("Failed to find path of %s module.\n",
392 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900393 return NULL;
394 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900395 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000396 ret = debuginfo__new(path);
397 if (!ret && !silent) {
398 pr_warning("The %s file has no debug information.\n", path);
399 if (!module || !strtailcmp(path, ".ko"))
400 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
401 else
402 pr_warning("Rebuild with -g, ");
403 pr_warning("or install an appropriate debuginfo package.\n");
404 }
405 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400406}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300407
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000408
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000409static int get_text_start_address(const char *exec, unsigned long *address)
410{
411 Elf *elf;
412 GElf_Ehdr ehdr;
413 GElf_Shdr shdr;
414 int fd, ret = -ENOENT;
415
416 fd = open(exec, O_RDONLY);
417 if (fd < 0)
418 return -errno;
419
420 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
421 if (elf == NULL)
422 return -EINVAL;
423
424 if (gelf_getehdr(elf, &ehdr) == NULL)
425 goto out;
426
427 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
428 goto out;
429
430 *address = shdr.sh_addr - shdr.sh_offset;
431 ret = 0;
432out:
433 elf_end(elf);
434 return ret;
435}
436
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000437/*
438 * Convert trace point to probe point with debuginfo
439 */
440static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
441 struct perf_probe_point *pp,
442 bool is_kprobe)
443{
444 struct debuginfo *dinfo = NULL;
445 unsigned long stext = 0;
446 u64 addr = tp->address;
447 int ret = -ENOENT;
448
449 /* convert the address to dwarf address */
450 if (!is_kprobe) {
451 if (!addr) {
452 ret = -EINVAL;
453 goto error;
454 }
455 ret = get_text_start_address(tp->module, &stext);
456 if (ret < 0)
457 goto error;
458 addr += stext;
459 } else {
460 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
461 if (addr == 0)
462 goto error;
463 addr += tp->offset;
464 }
465
466 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
467 tp->module ? : "kernel");
468
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000469 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000470 if (dinfo) {
471 ret = debuginfo__find_probe_point(dinfo,
472 (unsigned long)addr, pp);
473 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000474 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000475 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000476
477 if (ret > 0) {
478 pp->retprobe = tp->retprobe;
479 return 0;
480 }
481error:
482 pr_debug("Failed to find corresponding probes from debuginfo.\n");
483 return ret ? : -ENOENT;
484}
485
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000486static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
487 int ntevs, const char *exec)
488{
489 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000490 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000491
492 if (!exec)
493 return 0;
494
495 ret = get_text_start_address(exec, &stext);
496 if (ret < 0)
497 return ret;
498
499 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000500 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000501 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000502 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000503 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000504 ret = -ENOMEM;
505 break;
506 }
507 tevs[i].uprobes = true;
508 }
509
510 return ret;
511}
512
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900513static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
514 int ntevs, const char *module)
515{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900516 int i, ret = 0;
517 char *tmp;
518
519 if (!module)
520 return 0;
521
522 tmp = strrchr(module, '/');
523 if (tmp) {
524 /* This is a module path -- get the module name */
525 module = strdup(tmp + 1);
526 if (!module)
527 return -ENOMEM;
528 tmp = strchr(module, '.');
529 if (tmp)
530 *tmp = '\0';
531 tmp = (char *)module; /* For free() */
532 }
533
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900534 for (i = 0; i < ntevs; i++) {
535 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900536 if (!tevs[i].point.module) {
537 ret = -ENOMEM;
538 break;
539 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900540 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900541
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300542 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900543 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900544}
545
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000546/* Post processing the probe events */
547static int post_process_probe_trace_events(struct probe_trace_event *tevs,
548 int ntevs, const char *module,
549 bool uprobe)
550{
551 struct ref_reloc_sym *reloc_sym;
552 char *tmp;
553 int i;
554
555 if (uprobe)
556 return add_exec_to_probe_trace_events(tevs, ntevs, module);
557
558 /* Note that currently ref_reloc_sym based probe is not for drivers */
559 if (module)
560 return add_module_to_probe_trace_events(tevs, ntevs, module);
561
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000562 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000563 if (!reloc_sym) {
564 pr_warning("Relocated base symbol is not found!\n");
565 return -EINVAL;
566 }
567
568 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900569 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000570 tmp = strdup(reloc_sym->name);
571 if (!tmp)
572 return -ENOMEM;
573 free(tevs[i].point.symbol);
574 tevs[i].point.symbol = tmp;
575 tevs[i].point.offset = tevs[i].point.address -
576 reloc_sym->unrelocated_addr;
577 }
578 }
579 return 0;
580}
581
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300582/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530583static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900584 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530585 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300586{
587 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900588 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530589 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900590 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300591
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000592 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530593
Masami Hiramatsuff741782011-06-27 16:27:39 +0900594 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000595 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900596 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900597 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300598 return 0;
599 }
600
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000601 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900602 /* Searching trace events corresponding to a probe event */
603 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
604
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900605 if (ntevs == 0) { /* Not found, retry with an alternative */
606 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
607 if (!ret) {
608 ntevs = debuginfo__find_trace_events(dinfo, pev,
609 tevs, max_tevs);
610 /*
611 * Write back to the original probe_event for
612 * setting appropriate (user given) event name
613 */
614 clear_perf_probe_point(&pev->point);
615 memcpy(&pev->point, &tmp, sizeof(tmp));
616 }
617 }
618
Masami Hiramatsuff741782011-06-27 16:27:39 +0900619 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300620
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400621 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000622 pr_debug("Found %d probe_trace_events.\n", ntevs);
623 ret = post_process_probe_trace_events(*tevs, ntevs,
624 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000625 if (ret < 0) {
626 clear_probe_trace_events(*tevs, ntevs);
627 zfree(tevs);
628 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900629 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300631
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400632 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900633 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400634 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900635 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400636 }
637 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400638 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
639 if (ntevs == -EBADF) {
640 pr_warning("Warning: No dwarf info found in the vmlinux - "
641 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
642 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900643 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400644 return 0;
645 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300646 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400647 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300648}
649
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900650/*
651 * Find a src file from a DWARF tag path. Prepend optional source path prefix
652 * and chop off leading directories that do not exist. Result is passed back as
653 * a newly allocated path on success.
654 * Return 0 if file was found and readable, -errno otherwise.
655 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900656static int get_real_path(const char *raw_path, const char *comp_dir,
657 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900658{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900659 const char *prefix = symbol_conf.source_prefix;
660
661 if (!prefix) {
662 if (raw_path[0] != '/' && comp_dir)
663 /* If not an absolute path, try to use comp_dir */
664 prefix = comp_dir;
665 else {
666 if (access(raw_path, R_OK) == 0) {
667 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300668 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900669 } else
670 return -errno;
671 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900672 }
673
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900674 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900675 if (!*new_path)
676 return -ENOMEM;
677
678 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900679 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900680
681 if (access(*new_path, R_OK) == 0)
682 return 0;
683
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900684 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900685 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900686 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900687 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900688 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900689
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900690 switch (errno) {
691 case ENAMETOOLONG:
692 case ENOENT:
693 case EROFS:
694 case EFAULT:
695 raw_path = strchr(++raw_path, '/');
696 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300697 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900698 return -ENOENT;
699 }
700 continue;
701
702 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300703 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900704 return -errno;
705 }
706 }
707}
708
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300709#define LINEBUF_SIZE 256
710#define NR_ADDITIONAL_LINES 2
711
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100712static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300713{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000714 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100715 const char *color = show_num ? "" : PERF_COLOR_BLUE;
716 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100718 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
720 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100721 if (skip)
722 continue;
723 if (!prefix) {
724 prefix = show_num ? "%7d " : " ";
725 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300726 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100727 color_fprintf(stdout, color, "%s", buf);
728
729 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400730
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100731 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300732error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100733 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000734 pr_warning("File read error: %s\n",
735 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100736 return -1;
737 }
738 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300739}
740
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100741static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
742{
743 int rv = __show_one_line(fp, l, skip, show_num);
744 if (rv == 0) {
745 pr_warning("Source file is shorter than expected.\n");
746 rv = -1;
747 }
748 return rv;
749}
750
751#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
752#define show_one_line(f,l) _show_one_line(f,l,false,false)
753#define skip_one_line(f,l) _show_one_line(f,l,true,false)
754#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
755
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300756/*
757 * Show line-range always requires debuginfo to find source file and
758 * line number.
759 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900760static int __show_line_range(struct line_range *lr, const char *module,
761 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300762{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400763 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000764 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900765 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300766 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900767 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900768 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000769 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300770
771 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000772 dinfo = open_debuginfo(module, false);
773 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900774 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400775
Masami Hiramatsuff741782011-06-27 16:27:39 +0900776 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900777 if (!ret) { /* Not found, retry with an alternative */
778 ret = get_alternative_line_range(dinfo, lr, module, user);
779 if (!ret)
780 ret = debuginfo__find_line_range(dinfo, lr);
781 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900782 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000783 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400784 pr_warning("Specified source line is not found.\n");
785 return -ENOENT;
786 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000787 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400788 return ret;
789 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300790
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900791 /* Convert source file path */
792 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900793 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900794 free(tmp); /* Free old path */
795 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000796 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900797 return ret;
798 }
799
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800 setup_pager();
801
802 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900803 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300804 lr->start - lr->offset);
805 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100806 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300807
808 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400809 if (fp == NULL) {
810 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000811 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400812 return -errno;
813 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300814 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100815 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100816 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100817 if (ret < 0)
818 goto end;
819 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300820
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000821 intlist__for_each(ln, lr->line_list) {
822 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100823 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100824 if (ret < 0)
825 goto end;
826 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100827 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400828 if (ret < 0)
829 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830 }
831
832 if (lr->end == INT_MAX)
833 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100834 while (l <= lr->end) {
835 ret = show_one_line_or_eof(fp, l++ - lr->offset);
836 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100837 break;
838 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400839end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300840 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400841 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300842}
843
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000844int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000845{
846 int ret;
847
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000848 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000849 if (ret < 0)
850 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900851 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000852 exit_symbol_maps();
853
854 return ret;
855}
856
Masami Hiramatsuff741782011-06-27 16:27:39 +0900857static int show_available_vars_at(struct debuginfo *dinfo,
858 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900859 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900860 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900861{
862 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900863 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900864 struct str_node *node;
865 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900866 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900867 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900868
869 buf = synthesize_perf_probe_point(&pev->point);
870 if (!buf)
871 return -EINVAL;
872 pr_debug("Searching variables at %s\n", buf);
873
Masami Hiramatsuff741782011-06-27 16:27:39 +0900874 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
875 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900876 if (!ret) { /* Not found, retry with an alternative */
877 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
878 if (!ret) {
879 ret = debuginfo__find_available_vars_at(dinfo, pev,
880 &vls, max_vls, externs);
881 /* Release the old probe_point */
882 clear_perf_probe_point(&tmp);
883 }
884 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900885 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000886 if (ret == 0 || ret == -ENOENT) {
887 pr_err("Failed to find the address of %s\n", buf);
888 ret = -ENOENT;
889 } else
890 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900891 goto end;
892 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000893
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900894 /* Some variables are found */
895 fprintf(stdout, "Available variables at %s\n", buf);
896 for (i = 0; i < ret; i++) {
897 vl = &vls[i];
898 /*
899 * A probe point might be converted to
900 * several trace points.
901 */
902 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
903 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300904 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900905 nvars = 0;
906 if (vl->vars) {
907 strlist__for_each(node, vl->vars) {
908 var = strchr(node->s, '\t') + 1;
909 if (strfilter__compare(_filter, var)) {
910 fprintf(stdout, "\t\t%s\n", node->s);
911 nvars++;
912 }
913 }
914 strlist__delete(vl->vars);
915 }
916 if (nvars == 0)
917 fprintf(stdout, "\t\t(No matched variables)\n");
918 }
919 free(vls);
920end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900921 free(buf);
922 return ret;
923}
924
925/* Show available variables on given probe point */
926int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900927 int max_vls, const char *module,
928 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900929{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900930 int i, ret = 0;
931 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900932
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000933 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900934 if (ret < 0)
935 return ret;
936
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000937 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900938 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000939 ret = -ENOENT;
940 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900941 }
942
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900943 setup_pager();
944
Masami Hiramatsuff741782011-06-27 16:27:39 +0900945 for (i = 0; i < npevs && ret >= 0; i++)
946 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900947 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900948
949 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000950out:
951 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900952 return ret;
953}
954
Ingo Molnar89fe8082013-09-30 12:07:11 +0200955#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300956
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000957static int
958find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
959 struct perf_probe_point *pp __maybe_unused,
960 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300961{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000962 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300963}
964
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530965static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300966 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300967 int max_tevs __maybe_unused,
968 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300969{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400970 if (perf_probe_event_need_dwarf(pev)) {
971 pr_warning("Debuginfo-analysis is not supported.\n");
972 return -ENOSYS;
973 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530974
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300975 return 0;
976}
977
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300978int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000979 const char *module __maybe_unused,
980 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300981{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400982 pr_warning("Debuginfo-analysis is not supported.\n");
983 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300984}
985
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300986int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
987 int npevs __maybe_unused, int max_vls __maybe_unused,
988 const char *module __maybe_unused,
989 struct strfilter *filter __maybe_unused,
990 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900991{
992 pr_warning("Debuginfo-analysis is not supported.\n");
993 return -ENOSYS;
994}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400995#endif
996
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000997void line_range__clear(struct line_range *lr)
998{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000999 free(lr->function);
1000 free(lr->file);
1001 free(lr->path);
1002 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001003 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001004 memset(lr, 0, sizeof(*lr));
1005}
1006
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001007int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001008{
1009 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001010 lr->line_list = intlist__new(NULL);
1011 if (!lr->line_list)
1012 return -ENOMEM;
1013 else
1014 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001015}
1016
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001017static int parse_line_num(char **ptr, int *val, const char *what)
1018{
1019 const char *start = *ptr;
1020
1021 errno = 0;
1022 *val = strtol(*ptr, ptr, 0);
1023 if (errno || *ptr == start) {
1024 semantic_error("'%s' is not a valid number.\n", what);
1025 return -EINVAL;
1026 }
1027 return 0;
1028}
1029
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001030/*
1031 * Stuff 'lr' according to the line range described by 'arg'.
1032 * The line range syntax is described by:
1033 *
1034 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001035 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001036 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001037int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001038{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001039 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001040 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001041
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001042 if (!name)
1043 return -ENOMEM;
1044
1045 lr->start = 0;
1046 lr->end = INT_MAX;
1047
1048 range = strchr(name, ':');
1049 if (range) {
1050 *range++ = '\0';
1051
1052 err = parse_line_num(&range, &lr->start, "start line");
1053 if (err)
1054 goto err;
1055
1056 if (*range == '+' || *range == '-') {
1057 const char c = *range++;
1058
1059 err = parse_line_num(&range, &lr->end, "end line");
1060 if (err)
1061 goto err;
1062
1063 if (c == '+') {
1064 lr->end += lr->start;
1065 /*
1066 * Adjust the number of lines here.
1067 * If the number of lines == 1, the
1068 * the end of line should be equal to
1069 * the start of line.
1070 */
1071 lr->end--;
1072 }
1073 }
1074
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001075 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001076
1077 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001078 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001079 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001080 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001081 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001082 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001083 if (*range != '\0') {
1084 semantic_error("Tailing with invalid str '%s'.\n", range);
1085 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001086 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001087 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001088
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001089 file = strchr(name, '@');
1090 if (file) {
1091 *file = '\0';
1092 lr->file = strdup(++file);
1093 if (lr->file == NULL) {
1094 err = -ENOMEM;
1095 goto err;
1096 }
1097 lr->function = name;
1098 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001099 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001100 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001101 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001102
1103 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001104err:
1105 free(name);
1106 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001107}
1108
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001109/* Check the name is good for event/group */
1110static bool check_event_name(const char *name)
1111{
1112 if (!isalpha(*name) && *name != '_')
1113 return false;
1114 while (*++name != '\0') {
1115 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1116 return false;
1117 }
1118 return true;
1119}
1120
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001121/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001122static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001123{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001124 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001125 char *ptr, *tmp;
1126 char c, nc = 0;
1127 /*
1128 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001129 * perf probe [EVENT=]SRC[:LN|;PTN]
1130 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001131 *
1132 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001133 */
1134
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001135 ptr = strpbrk(arg, ";=@+%");
1136 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001137 *ptr = '\0';
1138 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001139 if (strchr(arg, ':')) {
1140 semantic_error("Group name is not supported yet.\n");
1141 return -ENOTSUP;
1142 }
1143 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001144 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001145 "follow C symbol-naming rule.\n", arg);
1146 return -EINVAL;
1147 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001148 pev->event = strdup(arg);
1149 if (pev->event == NULL)
1150 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001151 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001152 arg = tmp;
1153 }
1154
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001155 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001156 if (ptr) {
1157 nc = *ptr;
1158 *ptr++ = '\0';
1159 }
1160
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001161 tmp = strdup(arg);
1162 if (tmp == NULL)
1163 return -ENOMEM;
1164
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001165 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001166 if (strchr(tmp, '.')) /* File */
1167 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001168 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001169 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001170
1171 /* Parse other options */
1172 while (ptr) {
1173 arg = ptr;
1174 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001175 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001176 pp->lazy_line = strdup(arg);
1177 if (pp->lazy_line == NULL)
1178 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001179 break;
1180 }
1181 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001182 if (ptr) {
1183 nc = *ptr;
1184 *ptr++ = '\0';
1185 }
1186 switch (c) {
1187 case ':': /* Line number */
1188 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001190 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191 " in line number.\n");
1192 return -EINVAL;
1193 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001194 break;
1195 case '+': /* Byte offset from a symbol */
1196 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001198 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199 " in offset.\n");
1200 return -EINVAL;
1201 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001202 break;
1203 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204 if (pp->file) {
1205 semantic_error("SRC@SRC is not allowed.\n");
1206 return -EINVAL;
1207 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001208 pp->file = strdup(arg);
1209 if (pp->file == NULL)
1210 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001211 break;
1212 case '%': /* Probe places */
1213 if (strcmp(arg, "return") == 0) {
1214 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001215 } else { /* Others not supported yet */
1216 semantic_error("%%%s is not supported.\n", arg);
1217 return -ENOTSUP;
1218 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001219 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 default: /* Buggy case */
1221 pr_err("This program has a bug at %s:%d.\n",
1222 __FILE__, __LINE__);
1223 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001224 break;
1225 }
1226 }
1227
1228 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001230 semantic_error("Lazy pattern can't be used with"
1231 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001232 return -EINVAL;
1233 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001234
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001236 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 return -EINVAL;
1238 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001239
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001240 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001241 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 return -EINVAL;
1243 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001246 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001247 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248 return -EINVAL;
1249 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001250
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001251 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001252 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 return -EINVAL;
1254 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001255
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001257 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 return -EINVAL;
1259 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001260
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001262 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001263 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001264 return -EINVAL;
1265 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001268 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1269 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001270 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001271}
1272
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001273/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001274static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001275{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001276 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001277 struct perf_probe_arg_field **fieldp;
1278
1279 pr_debug("parsing arg: %s into ", str);
1280
Masami Hiramatsu48481932010-04-12 13:16:53 -04001281 tmp = strchr(str, '=');
1282 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001283 arg->name = strndup(str, tmp - str);
1284 if (arg->name == NULL)
1285 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001286 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001287 str = tmp + 1;
1288 }
1289
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001290 tmp = strchr(str, ':');
1291 if (tmp) { /* Type setting */
1292 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001293 arg->type = strdup(tmp + 1);
1294 if (arg->type == NULL)
1295 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001296 pr_debug("type:%s ", arg->type);
1297 }
1298
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001299 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001300 if (!is_c_varname(str) || !tmp) {
1301 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001302 arg->var = strdup(str);
1303 if (arg->var == NULL)
1304 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001305 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001306 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001307 }
1308
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001309 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001310 arg->var = strndup(str, tmp - str);
1311 if (arg->var == NULL)
1312 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001313 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001314 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001315 fieldp = &arg->field;
1316
1317 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001318 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1319 if (*fieldp == NULL)
1320 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001321 if (*tmp == '[') { /* Array */
1322 str = tmp;
1323 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001324 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001325 if (*tmp != ']' || tmp == str + 1) {
1326 semantic_error("Array index must be a"
1327 " number.\n");
1328 return -EINVAL;
1329 }
1330 tmp++;
1331 if (*tmp == '\0')
1332 tmp = NULL;
1333 } else { /* Structure */
1334 if (*tmp == '.') {
1335 str = tmp + 1;
1336 (*fieldp)->ref = false;
1337 } else if (tmp[1] == '>') {
1338 str = tmp + 2;
1339 (*fieldp)->ref = true;
1340 } else {
1341 semantic_error("Argument parse error: %s\n",
1342 str);
1343 return -EINVAL;
1344 }
1345 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001346 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001347 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001348 (*fieldp)->name = strndup(str, tmp - str);
1349 if ((*fieldp)->name == NULL)
1350 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001351 if (*str != '[')
1352 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001353 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1354 fieldp = &(*fieldp)->next;
1355 }
1356 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001357 (*fieldp)->name = strdup(str);
1358 if ((*fieldp)->name == NULL)
1359 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001360 if (*str != '[')
1361 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001362 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001363
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001364 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001365 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001366 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001367 if (arg->name == NULL)
1368 return -ENOMEM;
1369 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001371}
1372
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001374int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001375{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001376 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001378
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001379 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 if (!argv) {
1381 pr_debug("Failed to split arguments.\n");
1382 return -ENOMEM;
1383 }
1384 if (argc - 1 > MAX_PROBE_ARGS) {
1385 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1386 ret = -ERANGE;
1387 goto out;
1388 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001389 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001390 ret = parse_perf_probe_point(argv[0], pev);
1391 if (ret < 0)
1392 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001393
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001394 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001395 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001396 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1397 if (pev->args == NULL) {
1398 ret = -ENOMEM;
1399 goto out;
1400 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001401 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1402 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1403 if (ret >= 0 &&
1404 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001405 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001406 " kretprobe.\n");
1407 ret = -EINVAL;
1408 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001409 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001410out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001411 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412
1413 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001414}
1415
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001416/* Return true if this perf_probe_event requires debuginfo */
1417bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001418{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001419 int i;
1420
1421 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1422 return true;
1423
1424 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001425 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426 return true;
1427
1428 return false;
1429}
1430
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301431/* Parse probe_events event into struct probe_point */
1432static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001433 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001434{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301435 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001436 char pr;
1437 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001438 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001439 int ret, i, argc;
1440 char **argv;
1441
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301442 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001443 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444 if (!argv) {
1445 pr_debug("Failed to split arguments.\n");
1446 return -ENOMEM;
1447 }
1448 if (argc < 2) {
1449 semantic_error("Too few probe arguments.\n");
1450 ret = -ERANGE;
1451 goto out;
1452 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001453
1454 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001455 argv0_str = strdup(argv[0]);
1456 if (argv0_str == NULL) {
1457 ret = -ENOMEM;
1458 goto out;
1459 }
1460 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1461 fmt2_str = strtok_r(NULL, "/", &fmt);
1462 fmt3_str = strtok_r(NULL, " \t", &fmt);
1463 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1464 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465 semantic_error("Failed to parse event name: %s\n", argv[0]);
1466 ret = -EINVAL;
1467 goto out;
1468 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001469 pr = fmt1_str[0];
1470 tev->group = strdup(fmt2_str);
1471 tev->event = strdup(fmt3_str);
1472 if (tev->group == NULL || tev->event == NULL) {
1473 ret = -ENOMEM;
1474 goto out;
1475 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001476 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001477
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001478 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001479
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001480 /* Scan module name(if there), function name and offset */
1481 p = strchr(argv[1], ':');
1482 if (p) {
1483 tp->module = strndup(argv[1], p - argv[1]);
1484 p++;
1485 } else
1486 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001487 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001488 if (fmt1_str[0] == '0') /* only the address started with 0x */
1489 tp->address = strtoul(fmt1_str, NULL, 0);
1490 else {
1491 /* Only the symbol-based probe has offset */
1492 tp->symbol = strdup(fmt1_str);
1493 if (tp->symbol == NULL) {
1494 ret = -ENOMEM;
1495 goto out;
1496 }
1497 fmt2_str = strtok_r(NULL, "", &fmt);
1498 if (fmt2_str == NULL)
1499 tp->offset = 0;
1500 else
1501 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001502 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001503
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001504 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301505 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001506 if (tev->args == NULL) {
1507 ret = -ENOMEM;
1508 goto out;
1509 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001510 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001511 p = strchr(argv[i + 2], '=');
1512 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513 *p++ = '\0';
1514 else
1515 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001516 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001517 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001518 tev->args[i].value = strdup(p);
1519 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1520 ret = -ENOMEM;
1521 goto out;
1522 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001523 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 ret = 0;
1525out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001526 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001527 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001529}
1530
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001531/* Compose only probe arg */
1532int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1533{
1534 struct perf_probe_arg_field *field = pa->field;
1535 int ret;
1536 char *tmp = buf;
1537
Masami Hiramatsu48481932010-04-12 13:16:53 -04001538 if (pa->name && pa->var)
1539 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1540 else
1541 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001542 if (ret <= 0)
1543 goto error;
1544 tmp += ret;
1545 len -= ret;
1546
1547 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001548 if (field->name[0] == '[')
1549 ret = e_snprintf(tmp, len, "%s", field->name);
1550 else
1551 ret = e_snprintf(tmp, len, "%s%s",
1552 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001553 if (ret <= 0)
1554 goto error;
1555 tmp += ret;
1556 len -= ret;
1557 field = field->next;
1558 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001559
1560 if (pa->type) {
1561 ret = e_snprintf(tmp, len, ":%s", pa->type);
1562 if (ret <= 0)
1563 goto error;
1564 tmp += ret;
1565 len -= ret;
1566 }
1567
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001568 return tmp - buf;
1569error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001570 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001571 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001572}
1573
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001574/* Compose only probe point (not argument) */
1575static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001576{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001577 char *buf, *tmp;
1578 char offs[32] = "", line[32] = "", file[32] = "";
1579 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001580
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001581 buf = zalloc(MAX_CMDLEN);
1582 if (buf == NULL) {
1583 ret = -ENOMEM;
1584 goto error;
1585 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001586 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001587 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001588 if (ret <= 0)
1589 goto error;
1590 }
1591 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001592 ret = e_snprintf(line, 32, ":%d", pp->line);
1593 if (ret <= 0)
1594 goto error;
1595 }
1596 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001597 tmp = pp->file;
1598 len = strlen(tmp);
1599 if (len > 30) {
1600 tmp = strchr(pp->file + len - 30, '/');
1601 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1602 }
1603 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001604 if (ret <= 0)
1605 goto error;
1606 }
1607
1608 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001609 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1610 offs, pp->retprobe ? "%return" : "", line,
1611 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001612 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001613 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614 if (ret <= 0)
1615 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001616
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001617 return buf;
1618error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001619 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001620 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001621 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001622}
1623
1624#if 0
1625char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1626{
1627 char *buf;
1628 int i, len, ret;
1629
1630 buf = synthesize_perf_probe_point(&pev->point);
1631 if (!buf)
1632 return NULL;
1633
1634 len = strlen(buf);
1635 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001636 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001637 pev->args[i].name);
1638 if (ret <= 0) {
1639 free(buf);
1640 return NULL;
1641 }
1642 len += ret;
1643 }
1644
1645 return buf;
1646}
1647#endif
1648
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301649static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001650 char **buf, size_t *buflen,
1651 int depth)
1652{
1653 int ret;
1654 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301655 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001656 buflen, depth + 1);
1657 if (depth < 0)
1658 goto out;
1659 }
1660
1661 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1662 if (ret < 0)
1663 depth = ret;
1664 else {
1665 *buf += ret;
1666 *buflen -= ret;
1667 }
1668out:
1669 return depth;
1670
1671}
1672
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301673static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001674 char *buf, size_t buflen)
1675{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301676 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001677 int ret, depth = 0;
1678 char *tmp = buf;
1679
1680 /* Argument name or separator */
1681 if (arg->name)
1682 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1683 else
1684 ret = e_snprintf(buf, buflen, " ");
1685 if (ret < 0)
1686 return ret;
1687 buf += ret;
1688 buflen -= ret;
1689
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001690 /* Special case: @XXX */
1691 if (arg->value[0] == '@' && arg->ref)
1692 ref = ref->next;
1693
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001695 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301696 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001697 &buflen, 1);
1698 if (depth < 0)
1699 return depth;
1700 }
1701
1702 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001703 if (arg->value[0] == '@' && arg->ref)
1704 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1705 arg->ref->offset);
1706 else
1707 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001708 if (ret < 0)
1709 return ret;
1710 buf += ret;
1711 buflen -= ret;
1712
1713 /* Closing */
1714 while (depth--) {
1715 ret = e_snprintf(buf, buflen, ")");
1716 if (ret < 0)
1717 return ret;
1718 buf += ret;
1719 buflen -= ret;
1720 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001721 /* Print argument type */
1722 if (arg->type) {
1723 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1724 if (ret <= 0)
1725 return ret;
1726 buf += ret;
1727 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001728
1729 return buf - tmp;
1730}
1731
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301732char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 char *buf;
1736 int i, len, ret;
1737
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001738 buf = zalloc(MAX_CMDLEN);
1739 if (buf == NULL)
1740 return NULL;
1741
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001742 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1743 tev->group, tev->event);
1744 if (len <= 0)
1745 goto error;
1746
1747 /* Uprobes must have tp->address and tp->module */
1748 if (tev->uprobes && (!tp->address || !tp->module))
1749 goto error;
1750
1751 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301752 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001753 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1754 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301755 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001756 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301757 tp->module ?: "", tp->module ? ":" : "",
1758 tp->symbol, tp->offset);
1759
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001760 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001761 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001762 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001763
1764 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301765 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001767 if (ret <= 0)
1768 goto error;
1769 len += ret;
1770 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001773error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 free(buf);
1775 return NULL;
1776}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001777
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001778static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1779 struct perf_probe_point *pp,
1780 bool is_kprobe)
1781{
1782 struct symbol *sym = NULL;
1783 struct map *map;
1784 u64 addr;
1785 int ret = -ENOENT;
1786
1787 if (!is_kprobe) {
1788 map = dso__new_map(tp->module);
1789 if (!map)
1790 goto out;
1791 addr = tp->address;
1792 sym = map__find_symbol(map, addr, NULL);
1793 } else {
1794 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1795 if (addr) {
1796 addr += tp->offset;
1797 sym = __find_kernel_function(addr, &map);
1798 }
1799 }
1800 if (!sym)
1801 goto out;
1802
1803 pp->retprobe = tp->retprobe;
1804 pp->offset = addr - map->unmap_ip(map, sym->start);
1805 pp->function = strdup(sym->name);
1806 ret = pp->function ? 0 : -ENOMEM;
1807
1808out:
1809 if (map && !is_kprobe) {
1810 dso__delete(map->dso);
1811 map__delete(map);
1812 }
1813
1814 return ret;
1815}
1816
1817static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1818 struct perf_probe_point *pp,
1819 bool is_kprobe)
1820{
1821 char buf[128];
1822 int ret;
1823
1824 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1825 if (!ret)
1826 return 0;
1827 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1828 if (!ret)
1829 return 0;
1830
1831 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1832
1833 if (tp->symbol) {
1834 pp->function = strdup(tp->symbol);
1835 pp->offset = tp->offset;
1836 } else if (!tp->module && !is_kprobe) {
1837 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1838 if (ret < 0)
1839 return ret;
1840 pp->function = strdup(buf);
1841 pp->offset = 0;
1842 }
1843 if (pp->function == NULL)
1844 return -ENOMEM;
1845
1846 pp->retprobe = tp->retprobe;
1847
1848 return 0;
1849}
1850
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301851static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301852 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001853{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001854 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001855 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001856
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001857 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001858 pev->event = strdup(tev->event);
1859 pev->group = strdup(tev->group);
1860 if (pev->event == NULL || pev->group == NULL)
1861 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001862
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001863 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001864 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865 if (ret < 0)
1866 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001867
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001868 /* Convert trace_arg to probe_arg */
1869 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001870 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1871 if (pev->args == NULL)
1872 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001873 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001874 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001875 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301877 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001878 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001879 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001880 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001881 if (pev->args[i].name == NULL && ret >= 0)
1882 ret = -ENOMEM;
1883 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001884
1885 if (ret < 0)
1886 clear_perf_probe_event(pev);
1887
1888 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889}
1890
1891void clear_perf_probe_event(struct perf_probe_event *pev)
1892{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001893 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001894 int i;
1895
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001896 free(pev->event);
1897 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001898 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001899
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001900 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001901 free(pev->args[i].name);
1902 free(pev->args[i].var);
1903 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001904 field = pev->args[i].field;
1905 while (field) {
1906 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001907 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001908 free(field);
1909 field = next;
1910 }
1911 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001912 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001913 memset(pev, 0, sizeof(*pev));
1914}
1915
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301916static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001917{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301918 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001919 int i;
1920
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001921 free(tev->event);
1922 free(tev->group);
1923 free(tev->point.symbol);
1924 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001925 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001926 free(tev->args[i].name);
1927 free(tev->args[i].value);
1928 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929 ref = tev->args[i].ref;
1930 while (ref) {
1931 next = ref->next;
1932 free(ref);
1933 ref = next;
1934 }
1935 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001936 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001937 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001938}
1939
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001940static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301941{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001942 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301943
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001944 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301945 const char *config;
1946
1947 if (!is_kprobe)
1948 config = "CONFIG_UPROBE_EVENTS";
1949 else
1950 config = "CONFIG_KPROBE_EVENTS";
1951
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001952 pr_warning("%cprobe_events file does not exist"
1953 " - please rebuild kernel with %s.\n",
1954 is_kprobe ? 'k' : 'u', config);
1955 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001956 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001957 else
1958 pr_warning("Failed to open %cprobe_events: %s\n",
1959 is_kprobe ? 'k' : 'u',
1960 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301961}
1962
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001963static void print_both_open_warning(int kerr, int uerr)
1964{
1965 /* Both kprobes and uprobes are disabled, warn it. */
1966 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001967 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001968 else if (kerr == -ENOENT && uerr == -ENOENT)
1969 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1970 "or/and CONFIG_UPROBE_EVENTS.\n");
1971 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001972 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001973 pr_warning("Failed to open kprobe events: %s.\n",
1974 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1975 pr_warning("Failed to open uprobe events: %s.\n",
1976 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1977 }
1978}
1979
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001980static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001981{
1982 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001983 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001984 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001985 int ret;
1986
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001987 __debugfs = tracefs_find_mountpoint();
1988 if (__debugfs == NULL) {
1989 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001990
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001991 __debugfs = debugfs_find_mountpoint();
1992 if (__debugfs == NULL)
1993 return -ENOTSUP;
1994 }
1995
1996 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1997 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001998 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001999 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002000 if (readwrite && !probe_event_dry_run)
2001 ret = open(buf, O_RDWR, O_APPEND);
2002 else
2003 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002004
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302005 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002006 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002007 }
2008 return ret;
2009}
2010
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302011static int open_kprobe_events(bool readwrite)
2012{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002013 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302014}
2015
2016static int open_uprobe_events(bool readwrite)
2017{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002018 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302019}
2020
2021/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302022static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002023{
2024 int ret, idx;
2025 FILE *fp;
2026 char buf[MAX_CMDLEN];
2027 char *p;
2028 struct strlist *sl;
2029
2030 sl = strlist__new(true, NULL);
2031
2032 fp = fdopen(dup(fd), "r");
2033 while (!feof(fp)) {
2034 p = fgets(buf, MAX_CMDLEN, fp);
2035 if (!p)
2036 break;
2037
2038 idx = strlen(p) - 1;
2039 if (p[idx] == '\n')
2040 p[idx] = '\0';
2041 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002042 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002043 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002044 strlist__delete(sl);
2045 return NULL;
2046 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002047 }
2048 fclose(fp);
2049
2050 return sl;
2051}
2052
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002053struct kprobe_blacklist_node {
2054 struct list_head list;
2055 unsigned long start;
2056 unsigned long end;
2057 char *symbol;
2058};
2059
2060static void kprobe_blacklist__delete(struct list_head *blacklist)
2061{
2062 struct kprobe_blacklist_node *node;
2063
2064 while (!list_empty(blacklist)) {
2065 node = list_first_entry(blacklist,
2066 struct kprobe_blacklist_node, list);
2067 list_del(&node->list);
2068 free(node->symbol);
2069 free(node);
2070 }
2071}
2072
2073static int kprobe_blacklist__load(struct list_head *blacklist)
2074{
2075 struct kprobe_blacklist_node *node;
2076 const char *__debugfs = debugfs_find_mountpoint();
2077 char buf[PATH_MAX], *p;
2078 FILE *fp;
2079 int ret;
2080
2081 if (__debugfs == NULL)
2082 return -ENOTSUP;
2083
2084 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2085 if (ret < 0)
2086 return ret;
2087
2088 fp = fopen(buf, "r");
2089 if (!fp)
2090 return -errno;
2091
2092 ret = 0;
2093 while (fgets(buf, PATH_MAX, fp)) {
2094 node = zalloc(sizeof(*node));
2095 if (!node) {
2096 ret = -ENOMEM;
2097 break;
2098 }
2099 INIT_LIST_HEAD(&node->list);
2100 list_add_tail(&node->list, blacklist);
2101 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2102 ret = -EINVAL;
2103 break;
2104 }
2105 p = strchr(buf, '\t');
2106 if (p) {
2107 p++;
2108 if (p[strlen(p) - 1] == '\n')
2109 p[strlen(p) - 1] = '\0';
2110 } else
2111 p = (char *)"unknown";
2112 node->symbol = strdup(p);
2113 if (!node->symbol) {
2114 ret = -ENOMEM;
2115 break;
2116 }
2117 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2118 node->start, node->end, node->symbol);
2119 ret++;
2120 }
2121 if (ret < 0)
2122 kprobe_blacklist__delete(blacklist);
2123 fclose(fp);
2124
2125 return ret;
2126}
2127
2128static struct kprobe_blacklist_node *
2129kprobe_blacklist__find_by_address(struct list_head *blacklist,
2130 unsigned long address)
2131{
2132 struct kprobe_blacklist_node *node;
2133
2134 list_for_each_entry(node, blacklist, list) {
2135 if (node->start <= address && address <= node->end)
2136 return node;
2137 }
2138
2139 return NULL;
2140}
2141
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002142/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002143static int show_perf_probe_event(struct perf_probe_event *pev,
2144 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002145{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002146 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002147 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002148 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002149
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 /* Synthesize only event probe point */
2151 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002152 if (!place)
2153 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002154
2155 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002156 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002157 return ret;
2158
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002159 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002160 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002161 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002162
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002163 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002164 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002165 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002166 ret = synthesize_perf_probe_arg(&pev->args[i],
2167 buf, 128);
2168 if (ret < 0)
2169 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002170 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002171 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002172 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002173 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002174 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002175 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002176}
2177
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302178static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002179{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302180 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302181 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002182 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002183 struct strlist *rawlist;
2184 struct str_node *ent;
2185
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002186 memset(&tev, 0, sizeof(tev));
2187 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002188
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302189 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002190 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002191 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002192
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002193 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302194 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002195 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302196 ret = convert_to_perf_probe_event(&tev, &pev,
2197 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002198 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002199 ret = show_perf_probe_event(&pev,
2200 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002201 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002202 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302203 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002204 if (ret < 0)
2205 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002206 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002207 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002208
2209 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002210}
2211
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302212/* List up current perf-probe events */
2213int show_perf_probe_events(void)
2214{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002215 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302216
2217 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302218
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002219 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302220 if (ret < 0)
2221 return ret;
2222
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002223 kp_fd = open_kprobe_events(false);
2224 if (kp_fd >= 0) {
2225 ret = __show_perf_probe_events(kp_fd, true);
2226 close(kp_fd);
2227 if (ret < 0)
2228 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302229 }
2230
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002231 up_fd = open_uprobe_events(false);
2232 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002233 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002234 ret = kp_fd;
2235 goto out;
2236 }
2237
2238 if (up_fd >= 0) {
2239 ret = __show_perf_probe_events(up_fd, false);
2240 close(up_fd);
2241 }
2242out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002243 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302244 return ret;
2245}
2246
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002247/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002249{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002250 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002251 struct strlist *sl, *rawlist;
2252 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302253 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002254 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002255
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002256 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302257 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002258 if (!rawlist)
2259 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002260 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002261 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302262 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002263 if (ret < 0)
2264 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002265 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002266 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2267 tev.event);
2268 if (ret >= 0)
2269 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002270 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002271 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302272 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002273 if (ret < 0)
2274 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002275 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002276 strlist__delete(rawlist);
2277
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002278 if (ret < 0) {
2279 strlist__delete(sl);
2280 return NULL;
2281 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002282 return sl;
2283}
2284
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302285static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002286{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002287 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302288 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002289 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002290
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002291 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302292 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002293 return -EINVAL;
2294 }
2295
Masami Hiramatsufa282442009-12-08 17:03:23 -05002296 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002297 if (!probe_event_dry_run) {
2298 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002299 if (ret <= 0) {
2300 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002301 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002302 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002303 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002304 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002305 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002306 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002307}
2308
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002309static int get_new_event_name(char *buf, size_t len, const char *base,
2310 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002311{
2312 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002313
2314 /* Try no suffix */
2315 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002316 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002317 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002318 return ret;
2319 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002320 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002321 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002322
Masami Hiramatsud761b082009-12-15 10:32:25 -05002323 if (!allow_suffix) {
2324 pr_warning("Error: event \"%s\" already exists. "
2325 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002327 }
2328
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002329 /* Try to add suffix */
2330 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002331 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002332 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002333 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002334 return ret;
2335 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002336 if (!strlist__has_entry(namelist, buf))
2337 break;
2338 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002339 if (i == MAX_EVENT_INDEX) {
2340 pr_warning("Too many events are on the same function.\n");
2341 ret = -ERANGE;
2342 }
2343
2344 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002345}
2346
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002347/* Warn if the current kernel's uprobe implementation is old */
2348static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2349{
2350 int i;
2351 char *buf = synthesize_probe_trace_command(tev);
2352
2353 /* Old uprobe event doesn't support memory dereference */
2354 if (!tev->uprobes || tev->nargs == 0 || !buf)
2355 goto out;
2356
2357 for (i = 0; i < tev->nargs; i++)
2358 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2359 pr_warning("Please upgrade your kernel to at least "
2360 "3.14 to have access to feature %s\n",
2361 tev->args[i].value);
2362 break;
2363 }
2364out:
2365 free(buf);
2366}
2367
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302368static int __add_probe_trace_events(struct perf_probe_event *pev,
2369 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002370 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002371{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002372 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302373 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002374 char buf[64];
2375 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002376 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002377 LIST_HEAD(blacklist);
2378 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002379
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302380 if (pev->uprobes)
2381 fd = open_uprobe_events(true);
2382 else
2383 fd = open_kprobe_events(true);
2384
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002385 if (fd < 0) {
2386 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002387 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002388 }
2389
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002390 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302391 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002392 if (!namelist) {
2393 pr_debug("Failed to get current event list.\n");
2394 return -EIO;
2395 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002396 /* Get kprobe blacklist if exists */
2397 if (!pev->uprobes) {
2398 ret = kprobe_blacklist__load(&blacklist);
2399 if (ret < 0)
2400 pr_debug("No kprobe blacklist support, ignored\n");
2401 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002402
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002403 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002404 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002405 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002406 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002407 /* Ensure that the address is NOT blacklisted */
2408 node = kprobe_blacklist__find_by_address(&blacklist,
2409 tev->point.address);
2410 if (node) {
2411 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2412 continue;
2413 }
2414
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002415 if (pev->event)
2416 event = pev->event;
2417 else
2418 if (pev->point.function)
2419 event = pev->point.function;
2420 else
2421 event = tev->point.symbol;
2422 if (pev->group)
2423 group = pev->group;
2424 else
2425 group = PERFPROBE_GROUP;
2426
2427 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002428 ret = get_new_event_name(buf, 64, event,
2429 namelist, allow_suffix);
2430 if (ret < 0)
2431 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002432 event = buf;
2433
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002434 tev->event = strdup(event);
2435 tev->group = strdup(group);
2436 if (tev->event == NULL || tev->group == NULL) {
2437 ret = -ENOMEM;
2438 break;
2439 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302440 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002441 if (ret < 0)
2442 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002443 /* Add added event name to namelist */
2444 strlist__add(namelist, event);
2445
2446 /* Trick here - save current event/group */
2447 event = pev->event;
2448 group = pev->group;
2449 pev->event = tev->event;
2450 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002451 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002452 /* Trick here - restore current event/group */
2453 pev->event = (char *)event;
2454 pev->group = (char *)group;
2455
2456 /*
2457 * Probes after the first probe which comes from same
2458 * user input are always allowed to add suffix, because
2459 * there might be several addresses corresponding to
2460 * one code line.
2461 */
2462 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002463 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002464 if (ret == -EINVAL && pev->uprobes)
2465 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002466
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002467 /* Note that it is possible to skip all events because of blacklist */
2468 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002469 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002470 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2471 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002472 tev->event);
2473 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002474
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002475 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002476 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002477 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002478 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002479}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002480
Namhyung Kim564c62a2015-01-14 20:18:07 +09002481static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002482{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002483 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002484 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002485
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002486 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002487 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2488 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002489 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002490
2491 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002492}
2493
2494#define strdup_or_goto(str, label) \
2495 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2496
2497/*
2498 * Find probe function addresses from map.
2499 * Return an error or the number of found probe_trace_event
2500 */
2501static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2502 struct probe_trace_event **tevs,
2503 int max_tevs, const char *target)
2504{
2505 struct map *map = NULL;
2506 struct kmap *kmap = NULL;
2507 struct ref_reloc_sym *reloc_sym = NULL;
2508 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002509 struct probe_trace_event *tev;
2510 struct perf_probe_point *pp = &pev->point;
2511 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002512 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002513 int ret, i;
2514
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002515 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002516 if (!map) {
2517 ret = -EINVAL;
2518 goto out;
2519 }
2520
2521 /*
2522 * Load matched symbols: Since the different local symbols may have
2523 * same name but different addresses, this lists all the symbols.
2524 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002525 num_matched_functions = find_probe_functions(map, pp->function);
2526 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002527 pr_err("Failed to find symbol %s in %s\n", pp->function,
2528 target ? : "kernel");
2529 ret = -ENOENT;
2530 goto out;
2531 } else if (num_matched_functions > max_tevs) {
2532 pr_err("Too many functions matched in %s\n",
2533 target ? : "kernel");
2534 ret = -E2BIG;
2535 goto out;
2536 }
2537
Namhyung Kim25dd9172015-01-14 20:18:08 +09002538 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002539 kmap = map__kmap(map);
2540 reloc_sym = kmap->ref_reloc_sym;
2541 if (!reloc_sym) {
2542 pr_warning("Relocated base symbol is not found!\n");
2543 ret = -EINVAL;
2544 goto out;
2545 }
2546 }
2547
2548 /* Setup result trace-probe-events */
2549 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2550 if (!*tevs) {
2551 ret = -ENOMEM;
2552 goto out;
2553 }
2554
2555 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002556
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002557 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002558 tev = (*tevs) + ret;
2559 tp = &tev->point;
2560 if (ret == num_matched_functions) {
2561 pr_warning("Too many symbols are listed. Skip it.\n");
2562 break;
2563 }
2564 ret++;
2565
2566 if (pp->offset > sym->end - sym->start) {
2567 pr_warning("Offset %ld is bigger than the size of %s\n",
2568 pp->offset, sym->name);
2569 ret = -ENOENT;
2570 goto err_out;
2571 }
2572 /* Add one probe point */
2573 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2574 if (reloc_sym) {
2575 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2576 tp->offset = tp->address - reloc_sym->addr;
2577 } else {
2578 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2579 tp->offset = pp->offset;
2580 }
2581 tp->retprobe = pp->retprobe;
2582 if (target)
2583 tev->point.module = strdup_or_goto(target, nomem_out);
2584 tev->uprobes = pev->uprobes;
2585 tev->nargs = pev->nargs;
2586 if (tev->nargs) {
2587 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2588 tev->nargs);
2589 if (tev->args == NULL)
2590 goto nomem_out;
2591 }
2592 for (i = 0; i < tev->nargs; i++) {
2593 if (pev->args[i].name)
2594 tev->args[i].name =
2595 strdup_or_goto(pev->args[i].name,
2596 nomem_out);
2597
2598 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2599 nomem_out);
2600 if (pev->args[i].type)
2601 tev->args[i].type =
2602 strdup_or_goto(pev->args[i].type,
2603 nomem_out);
2604 }
2605 }
2606
2607out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002608 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002609 return ret;
2610
2611nomem_out:
2612 ret = -ENOMEM;
2613err_out:
2614 clear_probe_trace_events(*tevs, num_matched_functions);
2615 zfree(tevs);
2616 goto out;
2617}
2618
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302619static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2620 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302621 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002622{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002623 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002624
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002625 if (pev->uprobes && !pev->group) {
2626 /* Replace group name if not given */
2627 ret = convert_exec_to_group(target, &pev->group);
2628 if (ret != 0) {
2629 pr_warning("Failed to make a group name.\n");
2630 return ret;
2631 }
2632 }
2633
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002634 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302635 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002636 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002637 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002638
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002639 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002640}
2641
2642struct __event_package {
2643 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302644 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002645 int ntevs;
2646};
2647
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002648int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302649 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002650{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002651 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002652 struct __event_package *pkgs;
2653
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302654 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002655 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302656
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002657 if (pkgs == NULL)
2658 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002659
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002660 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002661 if (ret < 0) {
2662 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002663 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002664 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002665
2666 /* Loop 1: convert all events */
2667 for (i = 0; i < npevs; i++) {
2668 pkgs[i].pev = &pevs[i];
2669 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302670 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002671 &pkgs[i].tevs,
2672 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302673 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002674 if (ret < 0)
2675 goto end;
2676 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002677 }
2678
2679 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002680 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302681 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002682 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002683 if (ret < 0)
2684 break;
2685 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002686end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002687 /* Loop 3: cleanup and free trace events */
2688 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302690 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002691 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002692 }
2693 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002694 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002695
2696 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002697}
2698
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302699static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002700{
2701 char *p;
2702 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002703 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002704
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302705 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002706 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2707 if (ret < 0)
2708 goto error;
2709
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002710 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002711 if (!p) {
2712 pr_debug("Internal error: %s should have ':' but not.\n",
2713 ent->s);
2714 ret = -ENOTSUP;
2715 goto error;
2716 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002717 *p = '/';
2718
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002719 pr_debug("Writing event: %s\n", buf);
2720 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002721 if (ret < 0) {
2722 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002723 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002724 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002725
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002726 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002727 return 0;
2728error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002729 pr_warning("Failed to delete event: %s\n",
2730 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002731 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002732}
2733
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302734static int del_trace_probe_event(int fd, const char *buf,
2735 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002736{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002737 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302738 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002739
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002740 if (strpbrk(buf, "*?")) { /* Glob-exp */
2741 strlist__for_each_safe(ent, n, namelist)
2742 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302743 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002744 if (ret < 0)
2745 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002746 strlist__remove(namelist, ent);
2747 }
2748 } else {
2749 ent = strlist__find(namelist, buf);
2750 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302751 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002752 if (ret >= 0)
2753 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002754 }
2755 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002756
2757 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002758}
2759
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002760int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002761{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302762 int ret = -1, ufd = -1, kfd = -1;
2763 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002764 const char *group, *event;
2765 char *p, *str;
2766 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302767 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002768
Masami Hiramatsufa282442009-12-08 17:03:23 -05002769 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302770 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002771 if (kfd >= 0)
2772 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302773
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302774 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002775 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302776 unamelist = get_probe_trace_event_names(ufd, true);
2777
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002778 if (kfd < 0 && ufd < 0) {
2779 print_both_open_warning(kfd, ufd);
2780 goto error;
2781 }
2782
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302783 if (namelist == NULL && unamelist == NULL)
2784 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002785
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002786 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002787 str = strdup(ent->s);
2788 if (str == NULL) {
2789 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302790 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002791 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002792 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002793 p = strchr(str, ':');
2794 if (p) {
2795 group = str;
2796 *p = '\0';
2797 event = p + 1;
2798 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002799 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002800 event = str;
2801 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302802
2803 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2804 if (ret < 0) {
2805 pr_err("Failed to copy event.");
2806 free(str);
2807 goto error;
2808 }
2809
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002810 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302811
2812 if (namelist)
2813 ret = del_trace_probe_event(kfd, buf, namelist);
2814
2815 if (unamelist && ret != 0)
2816 ret = del_trace_probe_event(ufd, buf, unamelist);
2817
2818 if (ret != 0)
2819 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2820
Masami Hiramatsufa282442009-12-08 17:03:23 -05002821 free(str);
2822 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302823
2824error:
2825 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302826 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302827 close(kfd);
2828 }
2829
2830 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302831 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302832 close(ufd);
2833 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002834
2835 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002836}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302837
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002838/* TODO: don't use a global variable for filter ... */
2839static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002840
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002841/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002842 * If a symbol corresponds to a function with global binding and
2843 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002844 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002845static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002846 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002847{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002848 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002849 strfilter__compare(available_func_filter, sym->name))
2850 return 0;
2851 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002852}
2853
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002854int show_available_funcs(const char *target, struct strfilter *_filter,
2855 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002856{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002857 struct map *map;
2858 int ret;
2859
2860 ret = init_symbol_maps(user);
2861 if (ret < 0)
2862 return ret;
2863
2864 /* Get a symbol map */
2865 if (user)
2866 map = dso__new_map(target);
2867 else
2868 map = kernel_get_module_map(target);
2869 if (!map) {
2870 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002871 return -EINVAL;
2872 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002873
2874 /* Load symbols with given filter */
2875 available_func_filter = _filter;
2876 if (map__load(map, filter_available_functions)) {
2877 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2878 goto end;
2879 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002880 if (!dso__sorted_by_name(map->dso, map->type))
2881 dso__sort_by_name(map->dso, map->type);
2882
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002883 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302884 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002885 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2886end:
2887 if (user) {
2888 dso__delete(map->dso);
2889 map__delete(map);
2890 }
2891 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302892
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002893 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302894}
2895