blob: 42bec67aaa383c6dd60396dfb96ce62ea695dc80 [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>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053047#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048
49#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050#define PERFPROBE_GROUP "probe"
51
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040052bool probe_event_dry_run; /* Dry run flag */
53
Masami Hiramatsu146a1432010-04-12 13:17:42 -040054#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050055
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050056/* If there is no space to write, returns -E2BIG. */
57static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050058 __attribute__((format(printf, 3, 4)));
59
60static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050061{
62 int ret;
63 va_list ap;
64 va_start(ap, format);
65 ret = vsnprintf(str, size, format, ap);
66 va_end(ap);
67 if (ret >= (int)size)
68 ret = -E2BIG;
69 return ret;
70}
71
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030072static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000073static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000074static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090076/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000077static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079 int ret;
80
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040081 symbol_conf.sort_by_name = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -040082 ret = symbol__init();
83 if (ret < 0) {
84 pr_debug("Failed to init symbol map.\n");
85 goto out;
86 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040087
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000088 if (host_machine || user_only) /* already initialized */
89 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030090
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000091 if (symbol_conf.vmlinux_name)
92 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
93
94 host_machine = machine__new_host();
95 if (!host_machine) {
96 pr_debug("machine__new_host() failed.\n");
97 symbol__exit();
98 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090099 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400100out:
101 if (ret < 0)
102 pr_warning("Failed to init vmlinux path.\n");
103 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400104}
105
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000106static void exit_symbol_maps(void)
107{
108 if (host_machine) {
109 machine__delete(host_machine);
110 host_machine = NULL;
111 }
112 symbol__exit();
113}
114
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900115static struct symbol *__find_kernel_function_by_name(const char *name,
116 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400117{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000118 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900119 NULL);
120}
121
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000122static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
123{
124 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
125}
126
127static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
128{
129 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
130 struct kmap *kmap;
131
132 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
133 return NULL;
134
135 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
136 return kmap->ref_reloc_sym;
137}
138
139static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
140{
141 struct ref_reloc_sym *reloc_sym;
142 struct symbol *sym;
143 struct map *map;
144
145 /* ref_reloc_sym is just a label. Need a special fix*/
146 reloc_sym = kernel_get_ref_reloc_sym();
147 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
148 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
149 else {
150 sym = __find_kernel_function_by_name(name, &map);
151 if (sym)
152 return map->unmap_ip(map, sym->start) -
153 (reloc) ? 0 : map->reloc;
154 }
155 return 0;
156}
157
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900158static struct map *kernel_get_module_map(const char *module)
159{
160 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000161 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900162
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900163 /* A file path -- this is an offline module */
164 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000165 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900166
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900167 if (!module)
168 module = "kernel";
169
170 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
171 struct map *pos = rb_entry(nd, struct map, rb_node);
172 if (strncmp(pos->dso->short_name + 1, module,
173 pos->dso->short_name_len - 2) == 0) {
174 return pos;
175 }
176 }
177 return NULL;
178}
179
180static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900181{
182 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100183 struct map *map;
184 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900185
186 if (module) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000187 list_for_each_entry(dso, &host_machine->kernel_dsos, node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900188 if (strncmp(dso->short_name + 1, module,
189 dso->short_name_len - 2) == 0)
190 goto found;
191 }
192 pr_debug("Failed to find module %s.\n", module);
193 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100194 }
195
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000196 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100197 dso = map->dso;
198
199 vmlinux_name = symbol_conf.vmlinux_name;
200 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300201 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100202 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900203 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100204 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900205 pr_debug("Failed to load kernel map.\n");
206 return NULL;
207 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400208 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900209found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900210 return dso;
211}
212
213const char *kernel_get_module_path(const char *module)
214{
215 struct dso *dso = kernel_get_module_dso(module);
216 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900217}
218
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000219static int convert_exec_to_group(const char *exec, char **result)
220{
221 char *ptr1, *ptr2, *exec_copy;
222 char buf[64];
223 int ret;
224
225 exec_copy = strdup(exec);
226 if (!exec_copy)
227 return -ENOMEM;
228
229 ptr1 = basename(exec_copy);
230 if (!ptr1) {
231 ret = -EINVAL;
232 goto out;
233 }
234
235 ptr2 = strpbrk(ptr1, "-._");
236 if (ptr2)
237 *ptr2 = '\0';
238 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
239 if (ret < 0)
240 goto out;
241
242 *result = strdup(buf);
243 ret = *result ? 0 : -ENOMEM;
244
245out:
246 free(exec_copy);
247 return ret;
248}
249
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000250static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
251{
252 int i;
253
254 for (i = 0; i < ntevs; i++)
255 clear_probe_trace_event(tevs + i);
256}
257
Ingo Molnar89fe8082013-09-30 12:07:11 +0200258#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900259/* Open new debuginfo of given module */
260static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900261{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900262 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900263
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900264 /* A file path -- this is an offline module */
265 if (module && strchr(module, '/'))
266 path = module;
267 else {
268 path = kernel_get_module_path(module);
269
270 if (!path) {
271 pr_err("Failed to find path of %s module.\n",
272 module ?: "kernel");
273 return NULL;
274 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900275 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900276 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400277}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300278
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000279static int get_text_start_address(const char *exec, unsigned long *address)
280{
281 Elf *elf;
282 GElf_Ehdr ehdr;
283 GElf_Shdr shdr;
284 int fd, ret = -ENOENT;
285
286 fd = open(exec, O_RDONLY);
287 if (fd < 0)
288 return -errno;
289
290 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
291 if (elf == NULL)
292 return -EINVAL;
293
294 if (gelf_getehdr(elf, &ehdr) == NULL)
295 goto out;
296
297 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
298 goto out;
299
300 *address = shdr.sh_addr - shdr.sh_offset;
301 ret = 0;
302out:
303 elf_end(elf);
304 return ret;
305}
306
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000307/*
308 * Convert trace point to probe point with debuginfo
309 */
310static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
311 struct perf_probe_point *pp,
312 bool is_kprobe)
313{
314 struct debuginfo *dinfo = NULL;
315 unsigned long stext = 0;
316 u64 addr = tp->address;
317 int ret = -ENOENT;
318
319 /* convert the address to dwarf address */
320 if (!is_kprobe) {
321 if (!addr) {
322 ret = -EINVAL;
323 goto error;
324 }
325 ret = get_text_start_address(tp->module, &stext);
326 if (ret < 0)
327 goto error;
328 addr += stext;
329 } else {
330 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
331 if (addr == 0)
332 goto error;
333 addr += tp->offset;
334 }
335
336 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
337 tp->module ? : "kernel");
338
339 dinfo = open_debuginfo(tp->module);
340 if (dinfo) {
341 ret = debuginfo__find_probe_point(dinfo,
342 (unsigned long)addr, pp);
343 debuginfo__delete(dinfo);
344 } else {
345 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n", addr);
346 ret = -ENOENT;
347 }
348
349 if (ret > 0) {
350 pp->retprobe = tp->retprobe;
351 return 0;
352 }
353error:
354 pr_debug("Failed to find corresponding probes from debuginfo.\n");
355 return ret ? : -ENOENT;
356}
357
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000358static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
359 int ntevs, const char *exec)
360{
361 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000362 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000363
364 if (!exec)
365 return 0;
366
367 ret = get_text_start_address(exec, &stext);
368 if (ret < 0)
369 return ret;
370
371 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000372 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000373 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000374 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000375 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000376 ret = -ENOMEM;
377 break;
378 }
379 tevs[i].uprobes = true;
380 }
381
382 return ret;
383}
384
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900385static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
386 int ntevs, const char *module)
387{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900388 int i, ret = 0;
389 char *tmp;
390
391 if (!module)
392 return 0;
393
394 tmp = strrchr(module, '/');
395 if (tmp) {
396 /* This is a module path -- get the module name */
397 module = strdup(tmp + 1);
398 if (!module)
399 return -ENOMEM;
400 tmp = strchr(module, '.');
401 if (tmp)
402 *tmp = '\0';
403 tmp = (char *)module; /* For free() */
404 }
405
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900406 for (i = 0; i < ntevs; i++) {
407 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900408 if (!tevs[i].point.module) {
409 ret = -ENOMEM;
410 break;
411 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900412 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900413
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300414 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900415 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900416}
417
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000418/* Post processing the probe events */
419static int post_process_probe_trace_events(struct probe_trace_event *tevs,
420 int ntevs, const char *module,
421 bool uprobe)
422{
423 struct ref_reloc_sym *reloc_sym;
424 char *tmp;
425 int i;
426
427 if (uprobe)
428 return add_exec_to_probe_trace_events(tevs, ntevs, module);
429
430 /* Note that currently ref_reloc_sym based probe is not for drivers */
431 if (module)
432 return add_module_to_probe_trace_events(tevs, ntevs, module);
433
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000434 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000435 if (!reloc_sym) {
436 pr_warning("Relocated base symbol is not found!\n");
437 return -EINVAL;
438 }
439
440 for (i = 0; i < ntevs; i++) {
441 if (tevs[i].point.address) {
442 tmp = strdup(reloc_sym->name);
443 if (!tmp)
444 return -ENOMEM;
445 free(tevs[i].point.symbol);
446 tevs[i].point.symbol = tmp;
447 tevs[i].point.offset = tevs[i].point.address -
448 reloc_sym->unrelocated_addr;
449 }
450 }
451 return 0;
452}
453
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300454/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530455static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900456 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530457 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300458{
459 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530460 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900461 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300462
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530463 dinfo = open_debuginfo(target);
464
Masami Hiramatsuff741782011-06-27 16:27:39 +0900465 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400466 if (need_dwarf) {
467 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900468 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400469 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900470 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300471 return 0;
472 }
473
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000474 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900475 /* Searching trace events corresponding to a probe event */
476 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
477
478 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300479
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400480 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000481 pr_debug("Found %d probe_trace_events.\n", ntevs);
482 ret = post_process_probe_trace_events(*tevs, ntevs,
483 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000484 if (ret < 0) {
485 clear_probe_trace_events(*tevs, ntevs);
486 zfree(tevs);
487 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900488 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400489 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300490
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400491 if (ntevs == 0) { /* No error but failed to find probe point. */
492 pr_warning("Probe point '%s' not found.\n",
493 synthesize_perf_probe_point(&pev->point));
494 return -ENOENT;
495 }
496 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400497 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
498 if (ntevs == -EBADF) {
499 pr_warning("Warning: No dwarf info found in the vmlinux - "
500 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
501 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900502 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400503 return 0;
504 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300505 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400506 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300507}
508
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900509/*
510 * Find a src file from a DWARF tag path. Prepend optional source path prefix
511 * and chop off leading directories that do not exist. Result is passed back as
512 * a newly allocated path on success.
513 * Return 0 if file was found and readable, -errno otherwise.
514 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900515static int get_real_path(const char *raw_path, const char *comp_dir,
516 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900517{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900518 const char *prefix = symbol_conf.source_prefix;
519
520 if (!prefix) {
521 if (raw_path[0] != '/' && comp_dir)
522 /* If not an absolute path, try to use comp_dir */
523 prefix = comp_dir;
524 else {
525 if (access(raw_path, R_OK) == 0) {
526 *new_path = strdup(raw_path);
527 return 0;
528 } else
529 return -errno;
530 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900531 }
532
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900533 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900534 if (!*new_path)
535 return -ENOMEM;
536
537 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900538 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900539
540 if (access(*new_path, R_OK) == 0)
541 return 0;
542
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900543 if (!symbol_conf.source_prefix)
544 /* In case of searching comp_dir, don't retry */
545 return -errno;
546
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900547 switch (errno) {
548 case ENAMETOOLONG:
549 case ENOENT:
550 case EROFS:
551 case EFAULT:
552 raw_path = strchr(++raw_path, '/');
553 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300554 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900555 return -ENOENT;
556 }
557 continue;
558
559 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300560 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900561 return -errno;
562 }
563 }
564}
565
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300566#define LINEBUF_SIZE 256
567#define NR_ADDITIONAL_LINES 2
568
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100569static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300570{
571 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100572 const char *color = show_num ? "" : PERF_COLOR_BLUE;
573 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300574
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100575 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300576 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
577 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100578 if (skip)
579 continue;
580 if (!prefix) {
581 prefix = show_num ? "%7d " : " ";
582 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300583 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100584 color_fprintf(stdout, color, "%s", buf);
585
586 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400587
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100588 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300589error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100590 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100591 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100592 return -1;
593 }
594 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300595}
596
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100597static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
598{
599 int rv = __show_one_line(fp, l, skip, show_num);
600 if (rv == 0) {
601 pr_warning("Source file is shorter than expected.\n");
602 rv = -1;
603 }
604 return rv;
605}
606
607#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
608#define show_one_line(f,l) _show_one_line(f,l,false,false)
609#define skip_one_line(f,l) _show_one_line(f,l,true,false)
610#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
611
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300612/*
613 * Show line-range always requires debuginfo to find source file and
614 * line number.
615 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000616static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300617{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400618 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000619 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900620 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300621 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900622 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900623 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300624
625 /* Search a line range */
Masami Hiramatsuff741782011-06-27 16:27:39 +0900626 dinfo = open_debuginfo(module);
627 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400628 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900629 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630 }
631
Masami Hiramatsuff741782011-06-27 16:27:39 +0900632 ret = debuginfo__find_line_range(dinfo, lr);
633 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400634 if (ret == 0) {
635 pr_warning("Specified source line is not found.\n");
636 return -ENOENT;
637 } else if (ret < 0) {
638 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
639 return ret;
640 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300641
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900642 /* Convert source file path */
643 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900644 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900645 free(tmp); /* Free old path */
646 if (ret < 0) {
647 pr_warning("Failed to find source file. (%d)\n", ret);
648 return ret;
649 }
650
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300651 setup_pager();
652
653 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900654 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300655 lr->start - lr->offset);
656 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100657 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300658
659 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400660 if (fp == NULL) {
661 pr_warning("Failed to open %s: %s\n", lr->path,
662 strerror(errno));
663 return -errno;
664 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300665 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100666 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100667 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100668 if (ret < 0)
669 goto end;
670 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300671
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000672 intlist__for_each(ln, lr->line_list) {
673 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100674 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100675 if (ret < 0)
676 goto end;
677 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100678 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400679 if (ret < 0)
680 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300681 }
682
683 if (lr->end == INT_MAX)
684 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100685 while (l <= lr->end) {
686 ret = show_one_line_or_eof(fp, l++ - lr->offset);
687 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100688 break;
689 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400690end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300691 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400692 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300693}
694
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000695int show_line_range(struct line_range *lr, const char *module)
696{
697 int ret;
698
699 ret = init_symbol_maps(false);
700 if (ret < 0)
701 return ret;
702 ret = __show_line_range(lr, module);
703 exit_symbol_maps();
704
705 return ret;
706}
707
Masami Hiramatsuff741782011-06-27 16:27:39 +0900708static int show_available_vars_at(struct debuginfo *dinfo,
709 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900710 int max_vls, struct strfilter *_filter,
711 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900712{
713 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900714 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900715 struct str_node *node;
716 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900717 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900718
719 buf = synthesize_perf_probe_point(&pev->point);
720 if (!buf)
721 return -EINVAL;
722 pr_debug("Searching variables at %s\n", buf);
723
Masami Hiramatsuff741782011-06-27 16:27:39 +0900724 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
725 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900726 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900727 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900728 goto end;
729 }
730 /* Some variables are found */
731 fprintf(stdout, "Available variables at %s\n", buf);
732 for (i = 0; i < ret; i++) {
733 vl = &vls[i];
734 /*
735 * A probe point might be converted to
736 * several trace points.
737 */
738 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
739 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300740 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900741 nvars = 0;
742 if (vl->vars) {
743 strlist__for_each(node, vl->vars) {
744 var = strchr(node->s, '\t') + 1;
745 if (strfilter__compare(_filter, var)) {
746 fprintf(stdout, "\t\t%s\n", node->s);
747 nvars++;
748 }
749 }
750 strlist__delete(vl->vars);
751 }
752 if (nvars == 0)
753 fprintf(stdout, "\t\t(No matched variables)\n");
754 }
755 free(vls);
756end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900757 free(buf);
758 return ret;
759}
760
761/* Show available variables on given probe point */
762int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900763 int max_vls, const char *module,
764 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900765{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900766 int i, ret = 0;
767 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900768
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000769 ret = init_symbol_maps(false);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900770 if (ret < 0)
771 return ret;
772
Masami Hiramatsuff741782011-06-27 16:27:39 +0900773 dinfo = open_debuginfo(module);
774 if (!dinfo) {
775 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000776 ret = -ENOENT;
777 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900778 }
779
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900780 setup_pager();
781
Masami Hiramatsuff741782011-06-27 16:27:39 +0900782 for (i = 0; i < npevs && ret >= 0; i++)
783 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900784 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900785
786 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000787out:
788 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900789 return ret;
790}
791
Ingo Molnar89fe8082013-09-30 12:07:11 +0200792#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300793
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000794static int
795find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
796 struct perf_probe_point *pp __maybe_unused,
797 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300798{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000799 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800}
801
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530802static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300803 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300804 int max_tevs __maybe_unused,
805 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400807 if (perf_probe_event_need_dwarf(pev)) {
808 pr_warning("Debuginfo-analysis is not supported.\n");
809 return -ENOSYS;
810 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530811
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812 return 0;
813}
814
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300815int show_line_range(struct line_range *lr __maybe_unused,
816 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300817{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400818 pr_warning("Debuginfo-analysis is not supported.\n");
819 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300820}
821
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300822int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
823 int npevs __maybe_unused, int max_vls __maybe_unused,
824 const char *module __maybe_unused,
825 struct strfilter *filter __maybe_unused,
826 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900827{
828 pr_warning("Debuginfo-analysis is not supported.\n");
829 return -ENOSYS;
830}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400831#endif
832
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000833void line_range__clear(struct line_range *lr)
834{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000835 free(lr->function);
836 free(lr->file);
837 free(lr->path);
838 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000839 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000840 memset(lr, 0, sizeof(*lr));
841}
842
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000843int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000844{
845 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000846 lr->line_list = intlist__new(NULL);
847 if (!lr->line_list)
848 return -ENOMEM;
849 else
850 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000851}
852
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100853static int parse_line_num(char **ptr, int *val, const char *what)
854{
855 const char *start = *ptr;
856
857 errno = 0;
858 *val = strtol(*ptr, ptr, 0);
859 if (errno || *ptr == start) {
860 semantic_error("'%s' is not a valid number.\n", what);
861 return -EINVAL;
862 }
863 return 0;
864}
865
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100866/*
867 * Stuff 'lr' according to the line range described by 'arg'.
868 * The line range syntax is described by:
869 *
870 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900871 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100872 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400873int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500874{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900875 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100876 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100877
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100878 if (!name)
879 return -ENOMEM;
880
881 lr->start = 0;
882 lr->end = INT_MAX;
883
884 range = strchr(name, ':');
885 if (range) {
886 *range++ = '\0';
887
888 err = parse_line_num(&range, &lr->start, "start line");
889 if (err)
890 goto err;
891
892 if (*range == '+' || *range == '-') {
893 const char c = *range++;
894
895 err = parse_line_num(&range, &lr->end, "end line");
896 if (err)
897 goto err;
898
899 if (c == '+') {
900 lr->end += lr->start;
901 /*
902 * Adjust the number of lines here.
903 * If the number of lines == 1, the
904 * the end of line should be equal to
905 * the start of line.
906 */
907 lr->end--;
908 }
909 }
910
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400911 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100912
913 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400914 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500915 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400916 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100917 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400918 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100919 if (*range != '\0') {
920 semantic_error("Tailing with invalid str '%s'.\n", range);
921 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400922 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400923 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400924
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900925 file = strchr(name, '@');
926 if (file) {
927 *file = '\0';
928 lr->file = strdup(++file);
929 if (lr->file == NULL) {
930 err = -ENOMEM;
931 goto err;
932 }
933 lr->function = name;
934 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100935 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500936 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100937 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400938
939 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100940err:
941 free(name);
942 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500943}
944
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500945/* Check the name is good for event/group */
946static bool check_event_name(const char *name)
947{
948 if (!isalpha(*name) && *name != '_')
949 return false;
950 while (*++name != '\0') {
951 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
952 return false;
953 }
954 return true;
955}
956
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500957/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400958static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500959{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400960 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500961 char *ptr, *tmp;
962 char c, nc = 0;
963 /*
964 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500965 * perf probe [EVENT=]SRC[:LN|;PTN]
966 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500967 *
968 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500969 */
970
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500971 ptr = strpbrk(arg, ";=@+%");
972 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500973 *ptr = '\0';
974 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400975 if (strchr(arg, ':')) {
976 semantic_error("Group name is not supported yet.\n");
977 return -ENOTSUP;
978 }
979 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500980 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400981 "follow C symbol-naming rule.\n", arg);
982 return -EINVAL;
983 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400984 pev->event = strdup(arg);
985 if (pev->event == NULL)
986 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400987 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500988 arg = tmp;
989 }
990
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500991 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500992 if (ptr) {
993 nc = *ptr;
994 *ptr++ = '\0';
995 }
996
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400997 tmp = strdup(arg);
998 if (tmp == NULL)
999 return -ENOMEM;
1000
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001001 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001002 if (strchr(tmp, '.')) /* File */
1003 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001004 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001005 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001006
1007 /* Parse other options */
1008 while (ptr) {
1009 arg = ptr;
1010 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001011 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001012 pp->lazy_line = strdup(arg);
1013 if (pp->lazy_line == NULL)
1014 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001015 break;
1016 }
1017 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001018 if (ptr) {
1019 nc = *ptr;
1020 *ptr++ = '\0';
1021 }
1022 switch (c) {
1023 case ':': /* Line number */
1024 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001025 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001026 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001027 " in line number.\n");
1028 return -EINVAL;
1029 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001030 break;
1031 case '+': /* Byte offset from a symbol */
1032 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001034 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001035 " in offset.\n");
1036 return -EINVAL;
1037 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001038 break;
1039 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040 if (pp->file) {
1041 semantic_error("SRC@SRC is not allowed.\n");
1042 return -EINVAL;
1043 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001044 pp->file = strdup(arg);
1045 if (pp->file == NULL)
1046 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001047 break;
1048 case '%': /* Probe places */
1049 if (strcmp(arg, "return") == 0) {
1050 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 } else { /* Others not supported yet */
1052 semantic_error("%%%s is not supported.\n", arg);
1053 return -ENOTSUP;
1054 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001055 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001056 default: /* Buggy case */
1057 pr_err("This program has a bug at %s:%d.\n",
1058 __FILE__, __LINE__);
1059 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001060 break;
1061 }
1062 }
1063
1064 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001065 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001066 semantic_error("Lazy pattern can't be used with"
1067 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001068 return -EINVAL;
1069 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001070
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001071 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001072 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001073 return -EINVAL;
1074 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001075
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001076 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001077 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001078 return -EINVAL;
1079 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001080
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001082 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001083 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 return -EINVAL;
1085 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001086
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001088 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089 return -EINVAL;
1090 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001091
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001093 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001094 return -EINVAL;
1095 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001096
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001097 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001098 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001099 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 return -EINVAL;
1101 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001102
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001103 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001104 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1105 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001107}
1108
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001109/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001111{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001112 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001113 struct perf_probe_arg_field **fieldp;
1114
1115 pr_debug("parsing arg: %s into ", str);
1116
Masami Hiramatsu48481932010-04-12 13:16:53 -04001117 tmp = strchr(str, '=');
1118 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001119 arg->name = strndup(str, tmp - str);
1120 if (arg->name == NULL)
1121 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001122 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001123 str = tmp + 1;
1124 }
1125
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001126 tmp = strchr(str, ':');
1127 if (tmp) { /* Type setting */
1128 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001129 arg->type = strdup(tmp + 1);
1130 if (arg->type == NULL)
1131 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001132 pr_debug("type:%s ", arg->type);
1133 }
1134
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001135 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001136 if (!is_c_varname(str) || !tmp) {
1137 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001138 arg->var = strdup(str);
1139 if (arg->var == NULL)
1140 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001141 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001142 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001143 }
1144
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001145 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001146 arg->var = strndup(str, tmp - str);
1147 if (arg->var == NULL)
1148 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001149 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001150 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001151 fieldp = &arg->field;
1152
1153 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001154 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1155 if (*fieldp == NULL)
1156 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001157 if (*tmp == '[') { /* Array */
1158 str = tmp;
1159 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001160 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001161 if (*tmp != ']' || tmp == str + 1) {
1162 semantic_error("Array index must be a"
1163 " number.\n");
1164 return -EINVAL;
1165 }
1166 tmp++;
1167 if (*tmp == '\0')
1168 tmp = NULL;
1169 } else { /* Structure */
1170 if (*tmp == '.') {
1171 str = tmp + 1;
1172 (*fieldp)->ref = false;
1173 } else if (tmp[1] == '>') {
1174 str = tmp + 2;
1175 (*fieldp)->ref = true;
1176 } else {
1177 semantic_error("Argument parse error: %s\n",
1178 str);
1179 return -EINVAL;
1180 }
1181 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001182 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001183 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001184 (*fieldp)->name = strndup(str, tmp - str);
1185 if ((*fieldp)->name == NULL)
1186 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001187 if (*str != '[')
1188 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001189 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1190 fieldp = &(*fieldp)->next;
1191 }
1192 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001193 (*fieldp)->name = strdup(str);
1194 if ((*fieldp)->name == NULL)
1195 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001196 if (*str != '[')
1197 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001198 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001199
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001200 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001201 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001202 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001203 if (arg->name == NULL)
1204 return -ENOMEM;
1205 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001207}
1208
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001209/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001210int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001211{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001212 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001213 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001214
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001215 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001216 if (!argv) {
1217 pr_debug("Failed to split arguments.\n");
1218 return -ENOMEM;
1219 }
1220 if (argc - 1 > MAX_PROBE_ARGS) {
1221 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1222 ret = -ERANGE;
1223 goto out;
1224 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001225 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226 ret = parse_perf_probe_point(argv[0], pev);
1227 if (ret < 0)
1228 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001229
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001230 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001231 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001232 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1233 if (pev->args == NULL) {
1234 ret = -ENOMEM;
1235 goto out;
1236 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1238 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1239 if (ret >= 0 &&
1240 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001241 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 " kretprobe.\n");
1243 ret = -EINVAL;
1244 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001245 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001246out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001247 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248
1249 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001250}
1251
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252/* Return true if this perf_probe_event requires debuginfo */
1253bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001254{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001255 int i;
1256
1257 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1258 return true;
1259
1260 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001261 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001262 return true;
1263
1264 return false;
1265}
1266
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301267/* Parse probe_events event into struct probe_point */
1268static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001269 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001270{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301271 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001272 char pr;
1273 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001274 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001275 int ret, i, argc;
1276 char **argv;
1277
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301278 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001279 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280 if (!argv) {
1281 pr_debug("Failed to split arguments.\n");
1282 return -ENOMEM;
1283 }
1284 if (argc < 2) {
1285 semantic_error("Too few probe arguments.\n");
1286 ret = -ERANGE;
1287 goto out;
1288 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001289
1290 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001291 argv0_str = strdup(argv[0]);
1292 if (argv0_str == NULL) {
1293 ret = -ENOMEM;
1294 goto out;
1295 }
1296 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1297 fmt2_str = strtok_r(NULL, "/", &fmt);
1298 fmt3_str = strtok_r(NULL, " \t", &fmt);
1299 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1300 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001301 semantic_error("Failed to parse event name: %s\n", argv[0]);
1302 ret = -EINVAL;
1303 goto out;
1304 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001305 pr = fmt1_str[0];
1306 tev->group = strdup(fmt2_str);
1307 tev->event = strdup(fmt3_str);
1308 if (tev->group == NULL || tev->event == NULL) {
1309 ret = -ENOMEM;
1310 goto out;
1311 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001312 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001313
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001314 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001315
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001316 /* Scan module name(if there), function name and offset */
1317 p = strchr(argv[1], ':');
1318 if (p) {
1319 tp->module = strndup(argv[1], p - argv[1]);
1320 p++;
1321 } else
1322 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001323 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001324 if (fmt1_str[0] == '0') /* only the address started with 0x */
1325 tp->address = strtoul(fmt1_str, NULL, 0);
1326 else {
1327 /* Only the symbol-based probe has offset */
1328 tp->symbol = strdup(fmt1_str);
1329 if (tp->symbol == NULL) {
1330 ret = -ENOMEM;
1331 goto out;
1332 }
1333 fmt2_str = strtok_r(NULL, "", &fmt);
1334 if (fmt2_str == NULL)
1335 tp->offset = 0;
1336 else
1337 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001338 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001339
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001340 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301341 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001342 if (tev->args == NULL) {
1343 ret = -ENOMEM;
1344 goto out;
1345 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001346 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001347 p = strchr(argv[i + 2], '=');
1348 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001349 *p++ = '\0';
1350 else
1351 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001352 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001354 tev->args[i].value = strdup(p);
1355 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1356 ret = -ENOMEM;
1357 goto out;
1358 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001359 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 ret = 0;
1361out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001362 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001363 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001364 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001365}
1366
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001367/* Compose only probe arg */
1368int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1369{
1370 struct perf_probe_arg_field *field = pa->field;
1371 int ret;
1372 char *tmp = buf;
1373
Masami Hiramatsu48481932010-04-12 13:16:53 -04001374 if (pa->name && pa->var)
1375 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1376 else
1377 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001378 if (ret <= 0)
1379 goto error;
1380 tmp += ret;
1381 len -= ret;
1382
1383 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001384 if (field->name[0] == '[')
1385 ret = e_snprintf(tmp, len, "%s", field->name);
1386 else
1387 ret = e_snprintf(tmp, len, "%s%s",
1388 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001389 if (ret <= 0)
1390 goto error;
1391 tmp += ret;
1392 len -= ret;
1393 field = field->next;
1394 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001395
1396 if (pa->type) {
1397 ret = e_snprintf(tmp, len, ":%s", pa->type);
1398 if (ret <= 0)
1399 goto error;
1400 tmp += ret;
1401 len -= ret;
1402 }
1403
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001404 return tmp - buf;
1405error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001406 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001407 strerror(-ret));
1408 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001409}
1410
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001411/* Compose only probe point (not argument) */
1412static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001413{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001414 char *buf, *tmp;
1415 char offs[32] = "", line[32] = "", file[32] = "";
1416 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001417
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001418 buf = zalloc(MAX_CMDLEN);
1419 if (buf == NULL) {
1420 ret = -ENOMEM;
1421 goto error;
1422 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001423 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001424 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001425 if (ret <= 0)
1426 goto error;
1427 }
1428 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001429 ret = e_snprintf(line, 32, ":%d", pp->line);
1430 if (ret <= 0)
1431 goto error;
1432 }
1433 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001434 tmp = pp->file;
1435 len = strlen(tmp);
1436 if (len > 30) {
1437 tmp = strchr(pp->file + len - 30, '/');
1438 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1439 }
1440 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001441 if (ret <= 0)
1442 goto error;
1443 }
1444
1445 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001446 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1447 offs, pp->retprobe ? "%return" : "", line,
1448 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001449 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001450 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001451 if (ret <= 0)
1452 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001453
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001454 return buf;
1455error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001456 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001457 strerror(-ret));
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001458 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001460}
1461
1462#if 0
1463char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1464{
1465 char *buf;
1466 int i, len, ret;
1467
1468 buf = synthesize_perf_probe_point(&pev->point);
1469 if (!buf)
1470 return NULL;
1471
1472 len = strlen(buf);
1473 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475 pev->args[i].name);
1476 if (ret <= 0) {
1477 free(buf);
1478 return NULL;
1479 }
1480 len += ret;
1481 }
1482
1483 return buf;
1484}
1485#endif
1486
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301487static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001488 char **buf, size_t *buflen,
1489 int depth)
1490{
1491 int ret;
1492 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301493 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494 buflen, depth + 1);
1495 if (depth < 0)
1496 goto out;
1497 }
1498
1499 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1500 if (ret < 0)
1501 depth = ret;
1502 else {
1503 *buf += ret;
1504 *buflen -= ret;
1505 }
1506out:
1507 return depth;
1508
1509}
1510
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301511static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001512 char *buf, size_t buflen)
1513{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301514 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 int ret, depth = 0;
1516 char *tmp = buf;
1517
1518 /* Argument name or separator */
1519 if (arg->name)
1520 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1521 else
1522 ret = e_snprintf(buf, buflen, " ");
1523 if (ret < 0)
1524 return ret;
1525 buf += ret;
1526 buflen -= ret;
1527
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001528 /* Special case: @XXX */
1529 if (arg->value[0] == '@' && arg->ref)
1530 ref = ref->next;
1531
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001532 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001533 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301534 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001535 &buflen, 1);
1536 if (depth < 0)
1537 return depth;
1538 }
1539
1540 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001541 if (arg->value[0] == '@' && arg->ref)
1542 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1543 arg->ref->offset);
1544 else
1545 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 if (ret < 0)
1547 return ret;
1548 buf += ret;
1549 buflen -= ret;
1550
1551 /* Closing */
1552 while (depth--) {
1553 ret = e_snprintf(buf, buflen, ")");
1554 if (ret < 0)
1555 return ret;
1556 buf += ret;
1557 buflen -= ret;
1558 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001559 /* Print argument type */
1560 if (arg->type) {
1561 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1562 if (ret <= 0)
1563 return ret;
1564 buf += ret;
1565 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001566
1567 return buf - tmp;
1568}
1569
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301570char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001571{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301572 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573 char *buf;
1574 int i, len, ret;
1575
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001576 buf = zalloc(MAX_CMDLEN);
1577 if (buf == NULL)
1578 return NULL;
1579
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001580 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1581 tev->group, tev->event);
1582 if (len <= 0)
1583 goto error;
1584
1585 /* Uprobes must have tp->address and tp->module */
1586 if (tev->uprobes && (!tp->address || !tp->module))
1587 goto error;
1588
1589 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301590 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001591 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1592 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301593 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001594 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301595 tp->module ?: "", tp->module ? ":" : "",
1596 tp->symbol, tp->offset);
1597
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001598 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001599 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001600 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001601
1602 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301603 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001604 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001605 if (ret <= 0)
1606 goto error;
1607 len += ret;
1608 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001609
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001610 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001611error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001612 free(buf);
1613 return NULL;
1614}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001615
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001616static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1617 struct perf_probe_point *pp,
1618 bool is_kprobe)
1619{
1620 struct symbol *sym = NULL;
1621 struct map *map;
1622 u64 addr;
1623 int ret = -ENOENT;
1624
1625 if (!is_kprobe) {
1626 map = dso__new_map(tp->module);
1627 if (!map)
1628 goto out;
1629 addr = tp->address;
1630 sym = map__find_symbol(map, addr, NULL);
1631 } else {
1632 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1633 if (addr) {
1634 addr += tp->offset;
1635 sym = __find_kernel_function(addr, &map);
1636 }
1637 }
1638 if (!sym)
1639 goto out;
1640
1641 pp->retprobe = tp->retprobe;
1642 pp->offset = addr - map->unmap_ip(map, sym->start);
1643 pp->function = strdup(sym->name);
1644 ret = pp->function ? 0 : -ENOMEM;
1645
1646out:
1647 if (map && !is_kprobe) {
1648 dso__delete(map->dso);
1649 map__delete(map);
1650 }
1651
1652 return ret;
1653}
1654
1655static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1656 struct perf_probe_point *pp,
1657 bool is_kprobe)
1658{
1659 char buf[128];
1660 int ret;
1661
1662 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1663 if (!ret)
1664 return 0;
1665 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1666 if (!ret)
1667 return 0;
1668
1669 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1670
1671 if (tp->symbol) {
1672 pp->function = strdup(tp->symbol);
1673 pp->offset = tp->offset;
1674 } else if (!tp->module && !is_kprobe) {
1675 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1676 if (ret < 0)
1677 return ret;
1678 pp->function = strdup(buf);
1679 pp->offset = 0;
1680 }
1681 if (pp->function == NULL)
1682 return -ENOMEM;
1683
1684 pp->retprobe = tp->retprobe;
1685
1686 return 0;
1687}
1688
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301689static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301690 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001691{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001692 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001693 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001695 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001696 pev->event = strdup(tev->event);
1697 pev->group = strdup(tev->group);
1698 if (pev->event == NULL || pev->group == NULL)
1699 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001700
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001701 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001702 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001703 if (ret < 0)
1704 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001705
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001706 /* Convert trace_arg to probe_arg */
1707 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001708 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1709 if (pev->args == NULL)
1710 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001711 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001712 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001713 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301715 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001716 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001717 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001719 if (pev->args[i].name == NULL && ret >= 0)
1720 ret = -ENOMEM;
1721 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001722
1723 if (ret < 0)
1724 clear_perf_probe_event(pev);
1725
1726 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727}
1728
1729void clear_perf_probe_event(struct perf_probe_event *pev)
1730{
1731 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001732 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 int i;
1734
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001735 free(pev->event);
1736 free(pev->group);
1737 free(pp->file);
1738 free(pp->function);
1739 free(pp->lazy_line);
1740
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001741 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001742 free(pev->args[i].name);
1743 free(pev->args[i].var);
1744 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001745 field = pev->args[i].field;
1746 while (field) {
1747 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001748 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001749 free(field);
1750 field = next;
1751 }
1752 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001753 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001754 memset(pev, 0, sizeof(*pev));
1755}
1756
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301757static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001758{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301759 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001760 int i;
1761
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001762 free(tev->event);
1763 free(tev->group);
1764 free(tev->point.symbol);
1765 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001767 free(tev->args[i].name);
1768 free(tev->args[i].value);
1769 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001770 ref = tev->args[i].ref;
1771 while (ref) {
1772 next = ref->next;
1773 free(ref);
1774 ref = next;
1775 }
1776 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001777 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001779}
1780
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301781static void print_warn_msg(const char *file, bool is_kprobe)
1782{
1783
1784 if (errno == ENOENT) {
1785 const char *config;
1786
1787 if (!is_kprobe)
1788 config = "CONFIG_UPROBE_EVENTS";
1789 else
1790 config = "CONFIG_KPROBE_EVENTS";
1791
1792 pr_warning("%s file does not exist - please rebuild kernel"
1793 " with %s.\n", file, config);
1794 } else
1795 pr_warning("Failed to open %s file: %s\n", file,
1796 strerror(errno));
1797}
1798
1799static int open_probe_events(const char *trace_file, bool readwrite,
1800 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001801{
1802 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001803 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001804 int ret;
1805
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001806 __debugfs = debugfs_find_mountpoint();
1807 if (__debugfs == NULL) {
1808 pr_warning("Debugfs is not mounted.\n");
1809 return -ENOENT;
1810 }
1811
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301812 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001813 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001814 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001815 if (readwrite && !probe_event_dry_run)
1816 ret = open(buf, O_RDWR, O_APPEND);
1817 else
1818 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001819
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301820 if (ret < 0)
1821 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001822 }
1823 return ret;
1824}
1825
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301826static int open_kprobe_events(bool readwrite)
1827{
1828 return open_probe_events("tracing/kprobe_events", readwrite, true);
1829}
1830
1831static int open_uprobe_events(bool readwrite)
1832{
1833 return open_probe_events("tracing/uprobe_events", readwrite, false);
1834}
1835
1836/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301837static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001838{
1839 int ret, idx;
1840 FILE *fp;
1841 char buf[MAX_CMDLEN];
1842 char *p;
1843 struct strlist *sl;
1844
1845 sl = strlist__new(true, NULL);
1846
1847 fp = fdopen(dup(fd), "r");
1848 while (!feof(fp)) {
1849 p = fgets(buf, MAX_CMDLEN, fp);
1850 if (!p)
1851 break;
1852
1853 idx = strlen(p) - 1;
1854 if (p[idx] == '\n')
1855 p[idx] = '\0';
1856 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001857 if (ret < 0) {
1858 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1859 strlist__delete(sl);
1860 return NULL;
1861 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001862 }
1863 fclose(fp);
1864
1865 return sl;
1866}
1867
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001868/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001869static int show_perf_probe_event(struct perf_probe_event *pev,
1870 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001871{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001872 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001873 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001874 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001875
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876 /* Synthesize only event probe point */
1877 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001878 if (!place)
1879 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001880
1881 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001882 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001883 return ret;
1884
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001885 printf(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001886 if (module)
1887 printf(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001888
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001890 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001891 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892 ret = synthesize_perf_probe_arg(&pev->args[i],
1893 buf, 128);
1894 if (ret < 0)
1895 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001896 printf(" %s", buf);
1897 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001898 }
1899 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001901 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001902}
1903
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301904static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001905{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301906 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301907 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001909 struct strlist *rawlist;
1910 struct str_node *ent;
1911
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912 memset(&tev, 0, sizeof(tev));
1913 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001914
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301915 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001916 if (!rawlist)
1917 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001918
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001919 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301920 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001921 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301922 ret = convert_to_perf_probe_event(&tev, &pev,
1923 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001924 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001925 ret = show_perf_probe_event(&pev,
1926 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001927 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301929 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001930 if (ret < 0)
1931 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001932 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001933 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001934
1935 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001936}
1937
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301938/* List up current perf-probe events */
1939int show_perf_probe_events(void)
1940{
1941 int fd, ret;
1942
1943 setup_pager();
1944 fd = open_kprobe_events(false);
1945
1946 if (fd < 0)
1947 return fd;
1948
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001949 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301950 if (ret < 0)
1951 return ret;
1952
1953 ret = __show_perf_probe_events(fd, true);
1954 close(fd);
1955
1956 fd = open_uprobe_events(false);
1957 if (fd >= 0) {
1958 ret = __show_perf_probe_events(fd, false);
1959 close(fd);
1960 }
1961
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001962 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301963 return ret;
1964}
1965
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001966/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301967static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001968{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001969 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001970 struct strlist *sl, *rawlist;
1971 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301972 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001973 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001974
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001975 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301976 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001977 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001978 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301979 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001980 if (ret < 0)
1981 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001982 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001983 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1984 tev.event);
1985 if (ret >= 0)
1986 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001987 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001988 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301989 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001990 if (ret < 0)
1991 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001992 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001993 strlist__delete(rawlist);
1994
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001995 if (ret < 0) {
1996 strlist__delete(sl);
1997 return NULL;
1998 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001999 return sl;
2000}
2001
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302002static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002003{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002004 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302005 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002006
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002007 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302008 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002009 return -EINVAL;
2010 }
2011
Masami Hiramatsufa282442009-12-08 17:03:23 -05002012 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002013 if (!probe_event_dry_run) {
2014 ret = write(fd, buf, strlen(buf));
2015 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002016 pr_warning("Failed to write event: %s\n",
2017 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002018 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002019 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002020 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002021}
2022
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002023static int get_new_event_name(char *buf, size_t len, const char *base,
2024 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002025{
2026 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002027
2028 /* Try no suffix */
2029 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002030 if (ret < 0) {
2031 pr_debug("snprintf() failed: %s\n", strerror(-ret));
2032 return ret;
2033 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002034 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002035 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002036
Masami Hiramatsud761b082009-12-15 10:32:25 -05002037 if (!allow_suffix) {
2038 pr_warning("Error: event \"%s\" already exists. "
2039 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002040 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002041 }
2042
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002043 /* Try to add suffix */
2044 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002045 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002046 if (ret < 0) {
2047 pr_debug("snprintf() failed: %s\n", strerror(-ret));
2048 return ret;
2049 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002050 if (!strlist__has_entry(namelist, buf))
2051 break;
2052 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002053 if (i == MAX_EVENT_INDEX) {
2054 pr_warning("Too many events are on the same function.\n");
2055 ret = -ERANGE;
2056 }
2057
2058 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002059}
2060
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302061static int __add_probe_trace_events(struct perf_probe_event *pev,
2062 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002063 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002064{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002065 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302066 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002067 char buf[64];
2068 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002069 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002070
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302071 if (pev->uprobes)
2072 fd = open_uprobe_events(true);
2073 else
2074 fd = open_kprobe_events(true);
2075
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002076 if (fd < 0)
2077 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002078 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302079 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002080 if (!namelist) {
2081 pr_debug("Failed to get current event list.\n");
2082 return -EIO;
2083 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002084
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002085 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302086 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002087 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002088 tev = &tevs[i];
2089 if (pev->event)
2090 event = pev->event;
2091 else
2092 if (pev->point.function)
2093 event = pev->point.function;
2094 else
2095 event = tev->point.symbol;
2096 if (pev->group)
2097 group = pev->group;
2098 else
2099 group = PERFPROBE_GROUP;
2100
2101 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002102 ret = get_new_event_name(buf, 64, event,
2103 namelist, allow_suffix);
2104 if (ret < 0)
2105 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002106 event = buf;
2107
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002108 tev->event = strdup(event);
2109 tev->group = strdup(group);
2110 if (tev->event == NULL || tev->group == NULL) {
2111 ret = -ENOMEM;
2112 break;
2113 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302114 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002115 if (ret < 0)
2116 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002117 /* Add added event name to namelist */
2118 strlist__add(namelist, event);
2119
2120 /* Trick here - save current event/group */
2121 event = pev->event;
2122 group = pev->group;
2123 pev->event = tev->event;
2124 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002125 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002126 /* Trick here - restore current event/group */
2127 pev->event = (char *)event;
2128 pev->group = (char *)group;
2129
2130 /*
2131 * Probes after the first probe which comes from same
2132 * user input are always allowed to add suffix, because
2133 * there might be several addresses corresponding to
2134 * one code line.
2135 */
2136 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002137 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002138
2139 if (ret >= 0) {
2140 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302141 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002142 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2143 tev->event);
2144 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002145
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002146 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002147 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002148 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002149}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002150
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002151static char *looking_function_name;
2152static int num_matched_functions;
2153
2154static int probe_function_filter(struct map *map __maybe_unused,
2155 struct symbol *sym)
2156{
2157 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2158 strcmp(looking_function_name, sym->name) == 0) {
2159 num_matched_functions++;
2160 return 0;
2161 }
2162 return 1;
2163}
2164
2165#define strdup_or_goto(str, label) \
2166 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2167
2168/*
2169 * Find probe function addresses from map.
2170 * Return an error or the number of found probe_trace_event
2171 */
2172static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2173 struct probe_trace_event **tevs,
2174 int max_tevs, const char *target)
2175{
2176 struct map *map = NULL;
2177 struct kmap *kmap = NULL;
2178 struct ref_reloc_sym *reloc_sym = NULL;
2179 struct symbol *sym;
2180 struct rb_node *nd;
2181 struct probe_trace_event *tev;
2182 struct perf_probe_point *pp = &pev->point;
2183 struct probe_trace_point *tp;
2184 int ret, i;
2185
2186 /* Init maps of given executable or kernel */
2187 if (pev->uprobes)
2188 map = dso__new_map(target);
2189 else
2190 map = kernel_get_module_map(target);
2191 if (!map) {
2192 ret = -EINVAL;
2193 goto out;
2194 }
2195
2196 /*
2197 * Load matched symbols: Since the different local symbols may have
2198 * same name but different addresses, this lists all the symbols.
2199 */
2200 num_matched_functions = 0;
2201 looking_function_name = pp->function;
2202 ret = map__load(map, probe_function_filter);
2203 if (ret || num_matched_functions == 0) {
2204 pr_err("Failed to find symbol %s in %s\n", pp->function,
2205 target ? : "kernel");
2206 ret = -ENOENT;
2207 goto out;
2208 } else if (num_matched_functions > max_tevs) {
2209 pr_err("Too many functions matched in %s\n",
2210 target ? : "kernel");
2211 ret = -E2BIG;
2212 goto out;
2213 }
2214
2215 if (!pev->uprobes) {
2216 kmap = map__kmap(map);
2217 reloc_sym = kmap->ref_reloc_sym;
2218 if (!reloc_sym) {
2219 pr_warning("Relocated base symbol is not found!\n");
2220 ret = -EINVAL;
2221 goto out;
2222 }
2223 }
2224
2225 /* Setup result trace-probe-events */
2226 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2227 if (!*tevs) {
2228 ret = -ENOMEM;
2229 goto out;
2230 }
2231
2232 ret = 0;
2233 map__for_each_symbol(map, sym, nd) {
2234 tev = (*tevs) + ret;
2235 tp = &tev->point;
2236 if (ret == num_matched_functions) {
2237 pr_warning("Too many symbols are listed. Skip it.\n");
2238 break;
2239 }
2240 ret++;
2241
2242 if (pp->offset > sym->end - sym->start) {
2243 pr_warning("Offset %ld is bigger than the size of %s\n",
2244 pp->offset, sym->name);
2245 ret = -ENOENT;
2246 goto err_out;
2247 }
2248 /* Add one probe point */
2249 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2250 if (reloc_sym) {
2251 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2252 tp->offset = tp->address - reloc_sym->addr;
2253 } else {
2254 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2255 tp->offset = pp->offset;
2256 }
2257 tp->retprobe = pp->retprobe;
2258 if (target)
2259 tev->point.module = strdup_or_goto(target, nomem_out);
2260 tev->uprobes = pev->uprobes;
2261 tev->nargs = pev->nargs;
2262 if (tev->nargs) {
2263 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2264 tev->nargs);
2265 if (tev->args == NULL)
2266 goto nomem_out;
2267 }
2268 for (i = 0; i < tev->nargs; i++) {
2269 if (pev->args[i].name)
2270 tev->args[i].name =
2271 strdup_or_goto(pev->args[i].name,
2272 nomem_out);
2273
2274 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2275 nomem_out);
2276 if (pev->args[i].type)
2277 tev->args[i].type =
2278 strdup_or_goto(pev->args[i].type,
2279 nomem_out);
2280 }
2281 }
2282
2283out:
2284 if (map && pev->uprobes) {
2285 /* Only when using uprobe(exec) map needs to be released */
2286 dso__delete(map->dso);
2287 map__delete(map);
2288 }
2289 return ret;
2290
2291nomem_out:
2292 ret = -ENOMEM;
2293err_out:
2294 clear_probe_trace_events(*tevs, num_matched_functions);
2295 zfree(tevs);
2296 goto out;
2297}
2298
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302299static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2300 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302301 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002302{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002303 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002304
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002305 if (pev->uprobes && !pev->group) {
2306 /* Replace group name if not given */
2307 ret = convert_exec_to_group(target, &pev->group);
2308 if (ret != 0) {
2309 pr_warning("Failed to make a group name.\n");
2310 return ret;
2311 }
2312 }
2313
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002314 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302315 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002316 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002317 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002318
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002319 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002320}
2321
2322struct __event_package {
2323 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302324 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002325 int ntevs;
2326};
2327
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002328int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302329 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002330{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002331 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002332 struct __event_package *pkgs;
2333
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302334 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002335 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302336
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002337 if (pkgs == NULL)
2338 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002339
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002340 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002341 if (ret < 0) {
2342 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002343 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002344 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002345
2346 /* Loop 1: convert all events */
2347 for (i = 0; i < npevs; i++) {
2348 pkgs[i].pev = &pevs[i];
2349 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302350 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002351 &pkgs[i].tevs,
2352 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302353 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002354 if (ret < 0)
2355 goto end;
2356 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002357 }
2358
2359 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002360 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302361 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002362 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002363 if (ret < 0)
2364 break;
2365 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002366end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002367 /* Loop 3: cleanup and free trace events */
2368 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002369 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302370 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002371 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002372 }
2373 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002374 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002375
2376 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002377}
2378
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302379static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002380{
2381 char *p;
2382 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002383 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002384
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302385 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002386 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2387 if (ret < 0)
2388 goto error;
2389
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002390 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002391 if (!p) {
2392 pr_debug("Internal error: %s should have ':' but not.\n",
2393 ent->s);
2394 ret = -ENOTSUP;
2395 goto error;
2396 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002397 *p = '/';
2398
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002399 pr_debug("Writing event: %s\n", buf);
2400 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002401 if (ret < 0) {
2402 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002403 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002404 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002405
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302406 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002407 return 0;
2408error:
2409 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2410 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002411}
2412
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302413static int del_trace_probe_event(int fd, const char *buf,
2414 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002415{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002416 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302417 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002418
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002419 if (strpbrk(buf, "*?")) { /* Glob-exp */
2420 strlist__for_each_safe(ent, n, namelist)
2421 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302422 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002423 if (ret < 0)
2424 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002425 strlist__remove(namelist, ent);
2426 }
2427 } else {
2428 ent = strlist__find(namelist, buf);
2429 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302430 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002431 if (ret >= 0)
2432 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002433 }
2434 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002435
2436 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002437}
2438
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002439int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002440{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302441 int ret = -1, ufd = -1, kfd = -1;
2442 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002443 const char *group, *event;
2444 char *p, *str;
2445 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302446 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447
Masami Hiramatsufa282442009-12-08 17:03:23 -05002448 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302449 kfd = open_kprobe_events(true);
2450 if (kfd < 0)
2451 return kfd;
2452
2453 namelist = get_probe_trace_event_names(kfd, true);
2454 ufd = open_uprobe_events(true);
2455
2456 if (ufd >= 0)
2457 unamelist = get_probe_trace_event_names(ufd, true);
2458
2459 if (namelist == NULL && unamelist == NULL)
2460 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002461
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002462 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002463 str = strdup(ent->s);
2464 if (str == NULL) {
2465 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302466 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002467 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002468 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002469 p = strchr(str, ':');
2470 if (p) {
2471 group = str;
2472 *p = '\0';
2473 event = p + 1;
2474 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002475 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002476 event = str;
2477 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302478
2479 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2480 if (ret < 0) {
2481 pr_err("Failed to copy event.");
2482 free(str);
2483 goto error;
2484 }
2485
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002486 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302487
2488 if (namelist)
2489 ret = del_trace_probe_event(kfd, buf, namelist);
2490
2491 if (unamelist && ret != 0)
2492 ret = del_trace_probe_event(ufd, buf, unamelist);
2493
2494 if (ret != 0)
2495 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2496
Masami Hiramatsufa282442009-12-08 17:03:23 -05002497 free(str);
2498 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302499
2500error:
2501 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302502 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302503 close(kfd);
2504 }
2505
2506 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302507 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302508 close(ufd);
2509 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002510
2511 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002512}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302513
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002514/* TODO: don't use a global variable for filter ... */
2515static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002516
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002517/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002518 * If a symbol corresponds to a function with global binding and
2519 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002520 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002521static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002522 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002523{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002524 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002525 strfilter__compare(available_func_filter, sym->name))
2526 return 0;
2527 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002528}
2529
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002530int show_available_funcs(const char *target, struct strfilter *_filter,
2531 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002532{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002533 struct map *map;
2534 int ret;
2535
2536 ret = init_symbol_maps(user);
2537 if (ret < 0)
2538 return ret;
2539
2540 /* Get a symbol map */
2541 if (user)
2542 map = dso__new_map(target);
2543 else
2544 map = kernel_get_module_map(target);
2545 if (!map) {
2546 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002547 return -EINVAL;
2548 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002549
2550 /* Load symbols with given filter */
2551 available_func_filter = _filter;
2552 if (map__load(map, filter_available_functions)) {
2553 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2554 goto end;
2555 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002556 if (!dso__sorted_by_name(map->dso, map->type))
2557 dso__sort_by_name(map->dso, map->type);
2558
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002559 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302560 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002561 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2562end:
2563 if (user) {
2564 dso__delete(map->dso);
2565 map__delete(map);
2566 }
2567 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302568
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002569 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302570}
2571