blob: c150ca4343eb216c81244abb73bec70703f142ea [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;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090082 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040083 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) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400187 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
188 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900189 if (strncmp(dso->short_name + 1, module,
190 dso->short_name_len - 2) == 0)
191 goto found;
192 }
193 pr_debug("Failed to find module %s.\n", module);
194 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100195 }
196
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000197 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100198 dso = map->dso;
199
200 vmlinux_name = symbol_conf.vmlinux_name;
201 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300202 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100203 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900204 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100205 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900206 pr_debug("Failed to load kernel map.\n");
207 return NULL;
208 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400209 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900210found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900211 return dso;
212}
213
214const char *kernel_get_module_path(const char *module)
215{
216 struct dso *dso = kernel_get_module_dso(module);
217 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900218}
219
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000220static int convert_exec_to_group(const char *exec, char **result)
221{
222 char *ptr1, *ptr2, *exec_copy;
223 char buf[64];
224 int ret;
225
226 exec_copy = strdup(exec);
227 if (!exec_copy)
228 return -ENOMEM;
229
230 ptr1 = basename(exec_copy);
231 if (!ptr1) {
232 ret = -EINVAL;
233 goto out;
234 }
235
236 ptr2 = strpbrk(ptr1, "-._");
237 if (ptr2)
238 *ptr2 = '\0';
239 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
240 if (ret < 0)
241 goto out;
242
243 *result = strdup(buf);
244 ret = *result ? 0 : -ENOMEM;
245
246out:
247 free(exec_copy);
248 return ret;
249}
250
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000251static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
252{
253 int i;
254
255 for (i = 0; i < ntevs; i++)
256 clear_probe_trace_event(tevs + i);
257}
258
Ingo Molnar89fe8082013-09-30 12:07:11 +0200259#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000260
Masami Hiramatsuff741782011-06-27 16:27:39 +0900261/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000262static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900263{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000264 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000265 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900266
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000267 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900268 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900269 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000270 if (!silent)
271 pr_err("Failed to find path of %s module.\n",
272 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900273 return NULL;
274 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900275 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000276 ret = debuginfo__new(path);
277 if (!ret && !silent) {
278 pr_warning("The %s file has no debug information.\n", path);
279 if (!module || !strtailcmp(path, ".ko"))
280 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
281 else
282 pr_warning("Rebuild with -g, ");
283 pr_warning("or install an appropriate debuginfo package.\n");
284 }
285 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400286}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300287
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000288
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000289static int get_text_start_address(const char *exec, unsigned long *address)
290{
291 Elf *elf;
292 GElf_Ehdr ehdr;
293 GElf_Shdr shdr;
294 int fd, ret = -ENOENT;
295
296 fd = open(exec, O_RDONLY);
297 if (fd < 0)
298 return -errno;
299
300 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
301 if (elf == NULL)
302 return -EINVAL;
303
304 if (gelf_getehdr(elf, &ehdr) == NULL)
305 goto out;
306
307 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
308 goto out;
309
310 *address = shdr.sh_addr - shdr.sh_offset;
311 ret = 0;
312out:
313 elf_end(elf);
314 return ret;
315}
316
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000317/*
318 * Convert trace point to probe point with debuginfo
319 */
320static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
321 struct perf_probe_point *pp,
322 bool is_kprobe)
323{
324 struct debuginfo *dinfo = NULL;
325 unsigned long stext = 0;
326 u64 addr = tp->address;
327 int ret = -ENOENT;
328
329 /* convert the address to dwarf address */
330 if (!is_kprobe) {
331 if (!addr) {
332 ret = -EINVAL;
333 goto error;
334 }
335 ret = get_text_start_address(tp->module, &stext);
336 if (ret < 0)
337 goto error;
338 addr += stext;
339 } else {
340 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
341 if (addr == 0)
342 goto error;
343 addr += tp->offset;
344 }
345
346 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
347 tp->module ? : "kernel");
348
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000349 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000350 if (dinfo) {
351 ret = debuginfo__find_probe_point(dinfo,
352 (unsigned long)addr, pp);
353 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000354 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000355 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000356
357 if (ret > 0) {
358 pp->retprobe = tp->retprobe;
359 return 0;
360 }
361error:
362 pr_debug("Failed to find corresponding probes from debuginfo.\n");
363 return ret ? : -ENOENT;
364}
365
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000366static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
367 int ntevs, const char *exec)
368{
369 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000370 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000371
372 if (!exec)
373 return 0;
374
375 ret = get_text_start_address(exec, &stext);
376 if (ret < 0)
377 return ret;
378
379 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000380 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000381 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000382 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000383 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000384 ret = -ENOMEM;
385 break;
386 }
387 tevs[i].uprobes = true;
388 }
389
390 return ret;
391}
392
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900393static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
394 int ntevs, const char *module)
395{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900396 int i, ret = 0;
397 char *tmp;
398
399 if (!module)
400 return 0;
401
402 tmp = strrchr(module, '/');
403 if (tmp) {
404 /* This is a module path -- get the module name */
405 module = strdup(tmp + 1);
406 if (!module)
407 return -ENOMEM;
408 tmp = strchr(module, '.');
409 if (tmp)
410 *tmp = '\0';
411 tmp = (char *)module; /* For free() */
412 }
413
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900414 for (i = 0; i < ntevs; i++) {
415 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900416 if (!tevs[i].point.module) {
417 ret = -ENOMEM;
418 break;
419 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900420 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900421
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300422 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900423 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900424}
425
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000426/* Post processing the probe events */
427static int post_process_probe_trace_events(struct probe_trace_event *tevs,
428 int ntevs, const char *module,
429 bool uprobe)
430{
431 struct ref_reloc_sym *reloc_sym;
432 char *tmp;
433 int i;
434
435 if (uprobe)
436 return add_exec_to_probe_trace_events(tevs, ntevs, module);
437
438 /* Note that currently ref_reloc_sym based probe is not for drivers */
439 if (module)
440 return add_module_to_probe_trace_events(tevs, ntevs, module);
441
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000442 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000443 if (!reloc_sym) {
444 pr_warning("Relocated base symbol is not found!\n");
445 return -EINVAL;
446 }
447
448 for (i = 0; i < ntevs; i++) {
449 if (tevs[i].point.address) {
450 tmp = strdup(reloc_sym->name);
451 if (!tmp)
452 return -ENOMEM;
453 free(tevs[i].point.symbol);
454 tevs[i].point.symbol = tmp;
455 tevs[i].point.offset = tevs[i].point.address -
456 reloc_sym->unrelocated_addr;
457 }
458 }
459 return 0;
460}
461
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300462/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530463static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900464 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530465 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300466{
467 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530468 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900469 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300470
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000471 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530472
Masami Hiramatsuff741782011-06-27 16:27:39 +0900473 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000474 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900475 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900476 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300477 return 0;
478 }
479
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000480 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900481 /* Searching trace events corresponding to a probe event */
482 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
483
484 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300485
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400486 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000487 pr_debug("Found %d probe_trace_events.\n", ntevs);
488 ret = post_process_probe_trace_events(*tevs, ntevs,
489 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000490 if (ret < 0) {
491 clear_probe_trace_events(*tevs, ntevs);
492 zfree(tevs);
493 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900494 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400495 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300496
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400497 if (ntevs == 0) { /* No error but failed to find probe point. */
498 pr_warning("Probe point '%s' not found.\n",
499 synthesize_perf_probe_point(&pev->point));
500 return -ENOENT;
501 }
502 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400503 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
504 if (ntevs == -EBADF) {
505 pr_warning("Warning: No dwarf info found in the vmlinux - "
506 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
507 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900508 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400509 return 0;
510 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300511 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400512 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300513}
514
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900515/*
516 * Find a src file from a DWARF tag path. Prepend optional source path prefix
517 * and chop off leading directories that do not exist. Result is passed back as
518 * a newly allocated path on success.
519 * Return 0 if file was found and readable, -errno otherwise.
520 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900521static int get_real_path(const char *raw_path, const char *comp_dir,
522 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900523{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900524 const char *prefix = symbol_conf.source_prefix;
525
526 if (!prefix) {
527 if (raw_path[0] != '/' && comp_dir)
528 /* If not an absolute path, try to use comp_dir */
529 prefix = comp_dir;
530 else {
531 if (access(raw_path, R_OK) == 0) {
532 *new_path = strdup(raw_path);
533 return 0;
534 } else
535 return -errno;
536 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900537 }
538
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900539 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900540 if (!*new_path)
541 return -ENOMEM;
542
543 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900544 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900545
546 if (access(*new_path, R_OK) == 0)
547 return 0;
548
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900549 if (!symbol_conf.source_prefix)
550 /* In case of searching comp_dir, don't retry */
551 return -errno;
552
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900553 switch (errno) {
554 case ENAMETOOLONG:
555 case ENOENT:
556 case EROFS:
557 case EFAULT:
558 raw_path = strchr(++raw_path, '/');
559 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300560 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900561 return -ENOENT;
562 }
563 continue;
564
565 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300566 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900567 return -errno;
568 }
569 }
570}
571
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300572#define LINEBUF_SIZE 256
573#define NR_ADDITIONAL_LINES 2
574
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100575static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300576{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000577 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100578 const char *color = show_num ? "" : PERF_COLOR_BLUE;
579 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300580
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100581 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300582 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
583 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100584 if (skip)
585 continue;
586 if (!prefix) {
587 prefix = show_num ? "%7d " : " ";
588 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300589 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100590 color_fprintf(stdout, color, "%s", buf);
591
592 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400593
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100594 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300595error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100596 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000597 pr_warning("File read error: %s\n",
598 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100599 return -1;
600 }
601 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300602}
603
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100604static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
605{
606 int rv = __show_one_line(fp, l, skip, show_num);
607 if (rv == 0) {
608 pr_warning("Source file is shorter than expected.\n");
609 rv = -1;
610 }
611 return rv;
612}
613
614#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
615#define show_one_line(f,l) _show_one_line(f,l,false,false)
616#define skip_one_line(f,l) _show_one_line(f,l,true,false)
617#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
618
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300619/*
620 * Show line-range always requires debuginfo to find source file and
621 * line number.
622 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000623static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300624{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400625 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000626 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900627 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300628 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900629 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900630 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000631 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300632
633 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000634 dinfo = open_debuginfo(module, false);
635 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900636 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400637
Masami Hiramatsuff741782011-06-27 16:27:39 +0900638 ret = debuginfo__find_line_range(dinfo, lr);
639 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000640 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400641 pr_warning("Specified source line is not found.\n");
642 return -ENOENT;
643 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000644 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400645 return ret;
646 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300647
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900648 /* Convert source file path */
649 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900650 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900651 free(tmp); /* Free old path */
652 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000653 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900654 return ret;
655 }
656
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300657 setup_pager();
658
659 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900660 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300661 lr->start - lr->offset);
662 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100663 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300664
665 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400666 if (fp == NULL) {
667 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000668 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400669 return -errno;
670 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300671 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100672 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100673 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100674 if (ret < 0)
675 goto end;
676 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300677
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000678 intlist__for_each(ln, lr->line_list) {
679 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100680 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100681 if (ret < 0)
682 goto end;
683 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100684 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400685 if (ret < 0)
686 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300687 }
688
689 if (lr->end == INT_MAX)
690 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100691 while (l <= lr->end) {
692 ret = show_one_line_or_eof(fp, l++ - lr->offset);
693 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100694 break;
695 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400696end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300697 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400698 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300699}
700
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000701int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000702{
703 int ret;
704
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000705 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000706 if (ret < 0)
707 return ret;
708 ret = __show_line_range(lr, module);
709 exit_symbol_maps();
710
711 return ret;
712}
713
Masami Hiramatsuff741782011-06-27 16:27:39 +0900714static int show_available_vars_at(struct debuginfo *dinfo,
715 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900716 int max_vls, struct strfilter *_filter,
717 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900718{
719 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900720 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900721 struct str_node *node;
722 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900723 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900724
725 buf = synthesize_perf_probe_point(&pev->point);
726 if (!buf)
727 return -EINVAL;
728 pr_debug("Searching variables at %s\n", buf);
729
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
731 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900732 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000733 if (ret == 0 || ret == -ENOENT) {
734 pr_err("Failed to find the address of %s\n", buf);
735 ret = -ENOENT;
736 } else
737 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900738 goto end;
739 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000740
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900741 /* Some variables are found */
742 fprintf(stdout, "Available variables at %s\n", buf);
743 for (i = 0; i < ret; i++) {
744 vl = &vls[i];
745 /*
746 * A probe point might be converted to
747 * several trace points.
748 */
749 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
750 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300751 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900752 nvars = 0;
753 if (vl->vars) {
754 strlist__for_each(node, vl->vars) {
755 var = strchr(node->s, '\t') + 1;
756 if (strfilter__compare(_filter, var)) {
757 fprintf(stdout, "\t\t%s\n", node->s);
758 nvars++;
759 }
760 }
761 strlist__delete(vl->vars);
762 }
763 if (nvars == 0)
764 fprintf(stdout, "\t\t(No matched variables)\n");
765 }
766 free(vls);
767end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900768 free(buf);
769 return ret;
770}
771
772/* Show available variables on given probe point */
773int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900774 int max_vls, const char *module,
775 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900776{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900777 int i, ret = 0;
778 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900779
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000780 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900781 if (ret < 0)
782 return ret;
783
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000784 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900785 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000786 ret = -ENOENT;
787 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900788 }
789
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900790 setup_pager();
791
Masami Hiramatsuff741782011-06-27 16:27:39 +0900792 for (i = 0; i < npevs && ret >= 0; i++)
793 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900794 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900795
796 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000797out:
798 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900799 return ret;
800}
801
Ingo Molnar89fe8082013-09-30 12:07:11 +0200802#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300803
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000804static int
805find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
806 struct perf_probe_point *pp __maybe_unused,
807 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000809 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810}
811
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530812static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300813 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300814 int max_tevs __maybe_unused,
815 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400817 if (perf_probe_event_need_dwarf(pev)) {
818 pr_warning("Debuginfo-analysis is not supported.\n");
819 return -ENOSYS;
820 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530821
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822 return 0;
823}
824
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300825int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000826 const char *module __maybe_unused,
827 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300828{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400829 pr_warning("Debuginfo-analysis is not supported.\n");
830 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831}
832
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300833int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
834 int npevs __maybe_unused, int max_vls __maybe_unused,
835 const char *module __maybe_unused,
836 struct strfilter *filter __maybe_unused,
837 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900838{
839 pr_warning("Debuginfo-analysis is not supported.\n");
840 return -ENOSYS;
841}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400842#endif
843
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000844void line_range__clear(struct line_range *lr)
845{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000846 free(lr->function);
847 free(lr->file);
848 free(lr->path);
849 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000850 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000851 memset(lr, 0, sizeof(*lr));
852}
853
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000854int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000855{
856 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000857 lr->line_list = intlist__new(NULL);
858 if (!lr->line_list)
859 return -ENOMEM;
860 else
861 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000862}
863
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100864static int parse_line_num(char **ptr, int *val, const char *what)
865{
866 const char *start = *ptr;
867
868 errno = 0;
869 *val = strtol(*ptr, ptr, 0);
870 if (errno || *ptr == start) {
871 semantic_error("'%s' is not a valid number.\n", what);
872 return -EINVAL;
873 }
874 return 0;
875}
876
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100877/*
878 * Stuff 'lr' according to the line range described by 'arg'.
879 * The line range syntax is described by:
880 *
881 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900882 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100883 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500885{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900886 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100887 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100888
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100889 if (!name)
890 return -ENOMEM;
891
892 lr->start = 0;
893 lr->end = INT_MAX;
894
895 range = strchr(name, ':');
896 if (range) {
897 *range++ = '\0';
898
899 err = parse_line_num(&range, &lr->start, "start line");
900 if (err)
901 goto err;
902
903 if (*range == '+' || *range == '-') {
904 const char c = *range++;
905
906 err = parse_line_num(&range, &lr->end, "end line");
907 if (err)
908 goto err;
909
910 if (c == '+') {
911 lr->end += lr->start;
912 /*
913 * Adjust the number of lines here.
914 * If the number of lines == 1, the
915 * the end of line should be equal to
916 * the start of line.
917 */
918 lr->end--;
919 }
920 }
921
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400922 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100923
924 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400925 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500926 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400927 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100928 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400929 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100930 if (*range != '\0') {
931 semantic_error("Tailing with invalid str '%s'.\n", range);
932 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400933 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400934 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400935
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900936 file = strchr(name, '@');
937 if (file) {
938 *file = '\0';
939 lr->file = strdup(++file);
940 if (lr->file == NULL) {
941 err = -ENOMEM;
942 goto err;
943 }
944 lr->function = name;
945 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100946 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500947 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100948 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400949
950 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100951err:
952 free(name);
953 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500954}
955
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500956/* Check the name is good for event/group */
957static bool check_event_name(const char *name)
958{
959 if (!isalpha(*name) && *name != '_')
960 return false;
961 while (*++name != '\0') {
962 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
963 return false;
964 }
965 return true;
966}
967
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500968/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400969static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500970{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400971 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500972 char *ptr, *tmp;
973 char c, nc = 0;
974 /*
975 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500976 * perf probe [EVENT=]SRC[:LN|;PTN]
977 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500978 *
979 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500980 */
981
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500982 ptr = strpbrk(arg, ";=@+%");
983 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500984 *ptr = '\0';
985 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 if (strchr(arg, ':')) {
987 semantic_error("Group name is not supported yet.\n");
988 return -ENOTSUP;
989 }
990 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500991 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400992 "follow C symbol-naming rule.\n", arg);
993 return -EINVAL;
994 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400995 pev->event = strdup(arg);
996 if (pev->event == NULL)
997 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400998 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500999 arg = tmp;
1000 }
1001
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001002 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001003 if (ptr) {
1004 nc = *ptr;
1005 *ptr++ = '\0';
1006 }
1007
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001008 tmp = strdup(arg);
1009 if (tmp == NULL)
1010 return -ENOMEM;
1011
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001012 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001013 if (strchr(tmp, '.')) /* File */
1014 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001015 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001016 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001017
1018 /* Parse other options */
1019 while (ptr) {
1020 arg = ptr;
1021 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001022 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001023 pp->lazy_line = strdup(arg);
1024 if (pp->lazy_line == NULL)
1025 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001026 break;
1027 }
1028 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001029 if (ptr) {
1030 nc = *ptr;
1031 *ptr++ = '\0';
1032 }
1033 switch (c) {
1034 case ':': /* Line number */
1035 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001036 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001037 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038 " in line number.\n");
1039 return -EINVAL;
1040 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001041 break;
1042 case '+': /* Byte offset from a symbol */
1043 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001044 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001045 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001046 " in offset.\n");
1047 return -EINVAL;
1048 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001049 break;
1050 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 if (pp->file) {
1052 semantic_error("SRC@SRC is not allowed.\n");
1053 return -EINVAL;
1054 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001055 pp->file = strdup(arg);
1056 if (pp->file == NULL)
1057 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001058 break;
1059 case '%': /* Probe places */
1060 if (strcmp(arg, "return") == 0) {
1061 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001062 } else { /* Others not supported yet */
1063 semantic_error("%%%s is not supported.\n", arg);
1064 return -ENOTSUP;
1065 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001066 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001067 default: /* Buggy case */
1068 pr_err("This program has a bug at %s:%d.\n",
1069 __FILE__, __LINE__);
1070 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001071 break;
1072 }
1073 }
1074
1075 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001076 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001077 semantic_error("Lazy pattern can't be used with"
1078 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001079 return -EINVAL;
1080 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001081
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001082 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001083 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 return -EINVAL;
1085 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001086
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001088 semantic_error("Offset can't be used with line number.\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->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001093 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001094 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001095 return -EINVAL;
1096 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001097
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001098 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001099 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 return -EINVAL;
1101 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001102
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001104 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 return -EINVAL;
1106 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001107
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001109 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001110 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001111 return -EINVAL;
1112 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001113
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001114 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001115 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1116 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001117 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001118}
1119
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001120/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001121static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001122{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001123 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001124 struct perf_probe_arg_field **fieldp;
1125
1126 pr_debug("parsing arg: %s into ", str);
1127
Masami Hiramatsu48481932010-04-12 13:16:53 -04001128 tmp = strchr(str, '=');
1129 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001130 arg->name = strndup(str, tmp - str);
1131 if (arg->name == NULL)
1132 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001133 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001134 str = tmp + 1;
1135 }
1136
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001137 tmp = strchr(str, ':');
1138 if (tmp) { /* Type setting */
1139 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001140 arg->type = strdup(tmp + 1);
1141 if (arg->type == NULL)
1142 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001143 pr_debug("type:%s ", arg->type);
1144 }
1145
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001146 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001147 if (!is_c_varname(str) || !tmp) {
1148 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001149 arg->var = strdup(str);
1150 if (arg->var == NULL)
1151 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001152 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001153 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001154 }
1155
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001156 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001157 arg->var = strndup(str, tmp - str);
1158 if (arg->var == NULL)
1159 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001160 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001161 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001162 fieldp = &arg->field;
1163
1164 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001165 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1166 if (*fieldp == NULL)
1167 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001168 if (*tmp == '[') { /* Array */
1169 str = tmp;
1170 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001171 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001172 if (*tmp != ']' || tmp == str + 1) {
1173 semantic_error("Array index must be a"
1174 " number.\n");
1175 return -EINVAL;
1176 }
1177 tmp++;
1178 if (*tmp == '\0')
1179 tmp = NULL;
1180 } else { /* Structure */
1181 if (*tmp == '.') {
1182 str = tmp + 1;
1183 (*fieldp)->ref = false;
1184 } else if (tmp[1] == '>') {
1185 str = tmp + 2;
1186 (*fieldp)->ref = true;
1187 } else {
1188 semantic_error("Argument parse error: %s\n",
1189 str);
1190 return -EINVAL;
1191 }
1192 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001194 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001195 (*fieldp)->name = strndup(str, tmp - str);
1196 if ((*fieldp)->name == NULL)
1197 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001198 if (*str != '[')
1199 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001200 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1201 fieldp = &(*fieldp)->next;
1202 }
1203 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001204 (*fieldp)->name = strdup(str);
1205 if ((*fieldp)->name == NULL)
1206 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001207 if (*str != '[')
1208 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001209 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001210
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001211 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001212 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001213 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001214 if (arg->name == NULL)
1215 return -ENOMEM;
1216 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001218}
1219
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001220/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001221int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001222{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001223 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001225
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001226 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227 if (!argv) {
1228 pr_debug("Failed to split arguments.\n");
1229 return -ENOMEM;
1230 }
1231 if (argc - 1 > MAX_PROBE_ARGS) {
1232 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1233 ret = -ERANGE;
1234 goto out;
1235 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001236 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 ret = parse_perf_probe_point(argv[0], pev);
1238 if (ret < 0)
1239 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001240
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001241 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001242 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001243 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1244 if (pev->args == NULL) {
1245 ret = -ENOMEM;
1246 goto out;
1247 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1249 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1250 if (ret >= 0 &&
1251 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 " kretprobe.\n");
1254 ret = -EINVAL;
1255 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001256 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001258 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259
1260 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001261}
1262
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001263/* Return true if this perf_probe_event requires debuginfo */
1264bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001265{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001266 int i;
1267
1268 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1269 return true;
1270
1271 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001272 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001273 return true;
1274
1275 return false;
1276}
1277
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301278/* Parse probe_events event into struct probe_point */
1279static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001280 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001281{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301282 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001283 char pr;
1284 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001285 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001286 int ret, i, argc;
1287 char **argv;
1288
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301289 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001290 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 if (!argv) {
1292 pr_debug("Failed to split arguments.\n");
1293 return -ENOMEM;
1294 }
1295 if (argc < 2) {
1296 semantic_error("Too few probe arguments.\n");
1297 ret = -ERANGE;
1298 goto out;
1299 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001300
1301 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001302 argv0_str = strdup(argv[0]);
1303 if (argv0_str == NULL) {
1304 ret = -ENOMEM;
1305 goto out;
1306 }
1307 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1308 fmt2_str = strtok_r(NULL, "/", &fmt);
1309 fmt3_str = strtok_r(NULL, " \t", &fmt);
1310 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1311 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 semantic_error("Failed to parse event name: %s\n", argv[0]);
1313 ret = -EINVAL;
1314 goto out;
1315 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001316 pr = fmt1_str[0];
1317 tev->group = strdup(fmt2_str);
1318 tev->event = strdup(fmt3_str);
1319 if (tev->group == NULL || tev->event == NULL) {
1320 ret = -ENOMEM;
1321 goto out;
1322 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001323 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001324
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001325 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001326
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001327 /* Scan module name(if there), function name and offset */
1328 p = strchr(argv[1], ':');
1329 if (p) {
1330 tp->module = strndup(argv[1], p - argv[1]);
1331 p++;
1332 } else
1333 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001334 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001335 if (fmt1_str[0] == '0') /* only the address started with 0x */
1336 tp->address = strtoul(fmt1_str, NULL, 0);
1337 else {
1338 /* Only the symbol-based probe has offset */
1339 tp->symbol = strdup(fmt1_str);
1340 if (tp->symbol == NULL) {
1341 ret = -ENOMEM;
1342 goto out;
1343 }
1344 fmt2_str = strtok_r(NULL, "", &fmt);
1345 if (fmt2_str == NULL)
1346 tp->offset = 0;
1347 else
1348 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001349 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001350
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001351 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301352 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001353 if (tev->args == NULL) {
1354 ret = -ENOMEM;
1355 goto out;
1356 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001357 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001358 p = strchr(argv[i + 2], '=');
1359 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001360 *p++ = '\0';
1361 else
1362 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001363 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001364 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001365 tev->args[i].value = strdup(p);
1366 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1367 ret = -ENOMEM;
1368 goto out;
1369 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001370 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 ret = 0;
1372out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001373 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001374 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376}
1377
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001378/* Compose only probe arg */
1379int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1380{
1381 struct perf_probe_arg_field *field = pa->field;
1382 int ret;
1383 char *tmp = buf;
1384
Masami Hiramatsu48481932010-04-12 13:16:53 -04001385 if (pa->name && pa->var)
1386 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1387 else
1388 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001389 if (ret <= 0)
1390 goto error;
1391 tmp += ret;
1392 len -= ret;
1393
1394 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001395 if (field->name[0] == '[')
1396 ret = e_snprintf(tmp, len, "%s", field->name);
1397 else
1398 ret = e_snprintf(tmp, len, "%s%s",
1399 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001400 if (ret <= 0)
1401 goto error;
1402 tmp += ret;
1403 len -= ret;
1404 field = field->next;
1405 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001406
1407 if (pa->type) {
1408 ret = e_snprintf(tmp, len, ":%s", pa->type);
1409 if (ret <= 0)
1410 goto error;
1411 tmp += ret;
1412 len -= ret;
1413 }
1414
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001415 return tmp - buf;
1416error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001417 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001419}
1420
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421/* Compose only probe point (not argument) */
1422static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001423{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001424 char *buf, *tmp;
1425 char offs[32] = "", line[32] = "", file[32] = "";
1426 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001427
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001428 buf = zalloc(MAX_CMDLEN);
1429 if (buf == NULL) {
1430 ret = -ENOMEM;
1431 goto error;
1432 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001433 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001434 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001435 if (ret <= 0)
1436 goto error;
1437 }
1438 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001439 ret = e_snprintf(line, 32, ":%d", pp->line);
1440 if (ret <= 0)
1441 goto error;
1442 }
1443 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001444 tmp = pp->file;
1445 len = strlen(tmp);
1446 if (len > 30) {
1447 tmp = strchr(pp->file + len - 30, '/');
1448 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1449 }
1450 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001451 if (ret <= 0)
1452 goto error;
1453 }
1454
1455 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001456 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1457 offs, pp->retprobe ? "%return" : "", line,
1458 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001459 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001460 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001461 if (ret <= 0)
1462 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001463
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 return buf;
1465error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001466 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001467 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001469}
1470
1471#if 0
1472char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1473{
1474 char *buf;
1475 int i, len, ret;
1476
1477 buf = synthesize_perf_probe_point(&pev->point);
1478 if (!buf)
1479 return NULL;
1480
1481 len = strlen(buf);
1482 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001483 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001484 pev->args[i].name);
1485 if (ret <= 0) {
1486 free(buf);
1487 return NULL;
1488 }
1489 len += ret;
1490 }
1491
1492 return buf;
1493}
1494#endif
1495
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301496static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001497 char **buf, size_t *buflen,
1498 int depth)
1499{
1500 int ret;
1501 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301502 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001503 buflen, depth + 1);
1504 if (depth < 0)
1505 goto out;
1506 }
1507
1508 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1509 if (ret < 0)
1510 depth = ret;
1511 else {
1512 *buf += ret;
1513 *buflen -= ret;
1514 }
1515out:
1516 return depth;
1517
1518}
1519
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301520static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001521 char *buf, size_t buflen)
1522{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301523 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001524 int ret, depth = 0;
1525 char *tmp = buf;
1526
1527 /* Argument name or separator */
1528 if (arg->name)
1529 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1530 else
1531 ret = e_snprintf(buf, buflen, " ");
1532 if (ret < 0)
1533 return ret;
1534 buf += ret;
1535 buflen -= ret;
1536
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001537 /* Special case: @XXX */
1538 if (arg->value[0] == '@' && arg->ref)
1539 ref = ref->next;
1540
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001541 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001542 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301543 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544 &buflen, 1);
1545 if (depth < 0)
1546 return depth;
1547 }
1548
1549 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001550 if (arg->value[0] == '@' && arg->ref)
1551 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1552 arg->ref->offset);
1553 else
1554 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001555 if (ret < 0)
1556 return ret;
1557 buf += ret;
1558 buflen -= ret;
1559
1560 /* Closing */
1561 while (depth--) {
1562 ret = e_snprintf(buf, buflen, ")");
1563 if (ret < 0)
1564 return ret;
1565 buf += ret;
1566 buflen -= ret;
1567 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001568 /* Print argument type */
1569 if (arg->type) {
1570 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1571 if (ret <= 0)
1572 return ret;
1573 buf += ret;
1574 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001575
1576 return buf - tmp;
1577}
1578
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301579char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001580{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301581 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001582 char *buf;
1583 int i, len, ret;
1584
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001585 buf = zalloc(MAX_CMDLEN);
1586 if (buf == NULL)
1587 return NULL;
1588
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001589 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1590 tev->group, tev->event);
1591 if (len <= 0)
1592 goto error;
1593
1594 /* Uprobes must have tp->address and tp->module */
1595 if (tev->uprobes && (!tp->address || !tp->module))
1596 goto error;
1597
1598 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301599 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001600 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1601 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301602 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001603 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301604 tp->module ?: "", tp->module ? ":" : "",
1605 tp->symbol, tp->offset);
1606
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001607 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001608 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001609 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001610
1611 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301612 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614 if (ret <= 0)
1615 goto error;
1616 len += ret;
1617 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001618
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001619 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001620error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621 free(buf);
1622 return NULL;
1623}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001624
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001625static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1626 struct perf_probe_point *pp,
1627 bool is_kprobe)
1628{
1629 struct symbol *sym = NULL;
1630 struct map *map;
1631 u64 addr;
1632 int ret = -ENOENT;
1633
1634 if (!is_kprobe) {
1635 map = dso__new_map(tp->module);
1636 if (!map)
1637 goto out;
1638 addr = tp->address;
1639 sym = map__find_symbol(map, addr, NULL);
1640 } else {
1641 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1642 if (addr) {
1643 addr += tp->offset;
1644 sym = __find_kernel_function(addr, &map);
1645 }
1646 }
1647 if (!sym)
1648 goto out;
1649
1650 pp->retprobe = tp->retprobe;
1651 pp->offset = addr - map->unmap_ip(map, sym->start);
1652 pp->function = strdup(sym->name);
1653 ret = pp->function ? 0 : -ENOMEM;
1654
1655out:
1656 if (map && !is_kprobe) {
1657 dso__delete(map->dso);
1658 map__delete(map);
1659 }
1660
1661 return ret;
1662}
1663
1664static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1665 struct perf_probe_point *pp,
1666 bool is_kprobe)
1667{
1668 char buf[128];
1669 int ret;
1670
1671 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1672 if (!ret)
1673 return 0;
1674 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1675 if (!ret)
1676 return 0;
1677
1678 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1679
1680 if (tp->symbol) {
1681 pp->function = strdup(tp->symbol);
1682 pp->offset = tp->offset;
1683 } else if (!tp->module && !is_kprobe) {
1684 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1685 if (ret < 0)
1686 return ret;
1687 pp->function = strdup(buf);
1688 pp->offset = 0;
1689 }
1690 if (pp->function == NULL)
1691 return -ENOMEM;
1692
1693 pp->retprobe = tp->retprobe;
1694
1695 return 0;
1696}
1697
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301698static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301699 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001701 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001702 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001703
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001704 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001705 pev->event = strdup(tev->event);
1706 pev->group = strdup(tev->group);
1707 if (pev->event == NULL || pev->group == NULL)
1708 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001709
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001710 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001711 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001712 if (ret < 0)
1713 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001714
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001715 /* Convert trace_arg to probe_arg */
1716 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001717 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1718 if (pev->args == NULL)
1719 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001720 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001721 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001722 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001723 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301724 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001725 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001726 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001728 if (pev->args[i].name == NULL && ret >= 0)
1729 ret = -ENOMEM;
1730 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001731
1732 if (ret < 0)
1733 clear_perf_probe_event(pev);
1734
1735 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001736}
1737
1738void clear_perf_probe_event(struct perf_probe_event *pev)
1739{
1740 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001741 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001742 int i;
1743
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001744 free(pev->event);
1745 free(pev->group);
1746 free(pp->file);
1747 free(pp->function);
1748 free(pp->lazy_line);
1749
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001750 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001751 free(pev->args[i].name);
1752 free(pev->args[i].var);
1753 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001754 field = pev->args[i].field;
1755 while (field) {
1756 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001757 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001758 free(field);
1759 field = next;
1760 }
1761 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001762 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001763 memset(pev, 0, sizeof(*pev));
1764}
1765
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301766static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001767{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301768 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769 int i;
1770
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001771 free(tev->event);
1772 free(tev->group);
1773 free(tev->point.symbol);
1774 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001775 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001776 free(tev->args[i].name);
1777 free(tev->args[i].value);
1778 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001779 ref = tev->args[i].ref;
1780 while (ref) {
1781 next = ref->next;
1782 free(ref);
1783 ref = next;
1784 }
1785 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001786 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001787 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001788}
1789
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001790static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301791{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001792 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301793
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001794 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301795 const char *config;
1796
1797 if (!is_kprobe)
1798 config = "CONFIG_UPROBE_EVENTS";
1799 else
1800 config = "CONFIG_KPROBE_EVENTS";
1801
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001802 pr_warning("%cprobe_events file does not exist"
1803 " - please rebuild kernel with %s.\n",
1804 is_kprobe ? 'k' : 'u', config);
1805 } else if (err == -ENOTSUP)
1806 pr_warning("Debugfs is not mounted.\n");
1807 else
1808 pr_warning("Failed to open %cprobe_events: %s\n",
1809 is_kprobe ? 'k' : 'u',
1810 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301811}
1812
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001813static void print_both_open_warning(int kerr, int uerr)
1814{
1815 /* Both kprobes and uprobes are disabled, warn it. */
1816 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1817 pr_warning("Debugfs is not mounted.\n");
1818 else if (kerr == -ENOENT && uerr == -ENOENT)
1819 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1820 "or/and CONFIG_UPROBE_EVENTS.\n");
1821 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001822 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001823 pr_warning("Failed to open kprobe events: %s.\n",
1824 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1825 pr_warning("Failed to open uprobe events: %s.\n",
1826 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1827 }
1828}
1829
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001830static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001831{
1832 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001833 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001834 int ret;
1835
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001836 __debugfs = debugfs_find_mountpoint();
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001837 if (__debugfs == NULL)
1838 return -ENOTSUP;
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001839
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301840 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001841 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001842 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001843 if (readwrite && !probe_event_dry_run)
1844 ret = open(buf, O_RDWR, O_APPEND);
1845 else
1846 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001847
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301848 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001849 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001850 }
1851 return ret;
1852}
1853
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301854static int open_kprobe_events(bool readwrite)
1855{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001856 return open_probe_events("tracing/kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301857}
1858
1859static int open_uprobe_events(bool readwrite)
1860{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001861 return open_probe_events("tracing/uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301862}
1863
1864/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301865static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001866{
1867 int ret, idx;
1868 FILE *fp;
1869 char buf[MAX_CMDLEN];
1870 char *p;
1871 struct strlist *sl;
1872
1873 sl = strlist__new(true, NULL);
1874
1875 fp = fdopen(dup(fd), "r");
1876 while (!feof(fp)) {
1877 p = fgets(buf, MAX_CMDLEN, fp);
1878 if (!p)
1879 break;
1880
1881 idx = strlen(p) - 1;
1882 if (p[idx] == '\n')
1883 p[idx] = '\0';
1884 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001885 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001886 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001887 strlist__delete(sl);
1888 return NULL;
1889 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001890 }
1891 fclose(fp);
1892
1893 return sl;
1894}
1895
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001896/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001897static int show_perf_probe_event(struct perf_probe_event *pev,
1898 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001899{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001900 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001901 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001902 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001903
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904 /* Synthesize only event probe point */
1905 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001906 if (!place)
1907 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908
1909 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001910 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001911 return ret;
1912
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001913 printf(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001914 if (module)
1915 printf(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001916
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001917 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001918 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001919 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001920 ret = synthesize_perf_probe_arg(&pev->args[i],
1921 buf, 128);
1922 if (ret < 0)
1923 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001924 printf(" %s", buf);
1925 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001926 }
1927 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001929 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001930}
1931
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301932static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001933{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301934 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301935 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001936 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001937 struct strlist *rawlist;
1938 struct str_node *ent;
1939
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001940 memset(&tev, 0, sizeof(tev));
1941 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001942
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301943 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001944 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001945 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001946
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001947 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301948 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001949 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301950 ret = convert_to_perf_probe_event(&tev, &pev,
1951 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001952 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001953 ret = show_perf_probe_event(&pev,
1954 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001955 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001956 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301957 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001958 if (ret < 0)
1959 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001960 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001961 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001962
1963 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001964}
1965
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301966/* List up current perf-probe events */
1967int show_perf_probe_events(void)
1968{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001969 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301970
1971 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301972
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001973 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301974 if (ret < 0)
1975 return ret;
1976
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001977 kp_fd = open_kprobe_events(false);
1978 if (kp_fd >= 0) {
1979 ret = __show_perf_probe_events(kp_fd, true);
1980 close(kp_fd);
1981 if (ret < 0)
1982 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301983 }
1984
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001985 up_fd = open_uprobe_events(false);
1986 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001987 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001988 ret = kp_fd;
1989 goto out;
1990 }
1991
1992 if (up_fd >= 0) {
1993 ret = __show_perf_probe_events(up_fd, false);
1994 close(up_fd);
1995 }
1996out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001997 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301998 return ret;
1999}
2000
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002001/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302002static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002003{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002004 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002005 struct strlist *sl, *rawlist;
2006 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302007 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002008 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002009
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002010 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302011 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002012 if (!rawlist)
2013 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002014 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002015 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302016 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002017 if (ret < 0)
2018 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002019 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002020 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2021 tev.event);
2022 if (ret >= 0)
2023 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002024 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002025 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302026 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002027 if (ret < 0)
2028 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002029 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002030 strlist__delete(rawlist);
2031
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002032 if (ret < 0) {
2033 strlist__delete(sl);
2034 return NULL;
2035 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002036 return sl;
2037}
2038
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302039static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002040{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002041 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302042 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002043 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002044
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002045 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302046 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002047 return -EINVAL;
2048 }
2049
Masami Hiramatsufa282442009-12-08 17:03:23 -05002050 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002051 if (!probe_event_dry_run) {
2052 ret = write(fd, buf, strlen(buf));
2053 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002054 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002055 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002056 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002057 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002058 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002059}
2060
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002061static int get_new_event_name(char *buf, size_t len, const char *base,
2062 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002063{
2064 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002065
2066 /* Try no suffix */
2067 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002068 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002069 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002070 return ret;
2071 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002072 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002073 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002074
Masami Hiramatsud761b082009-12-15 10:32:25 -05002075 if (!allow_suffix) {
2076 pr_warning("Error: event \"%s\" already exists. "
2077 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002078 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002079 }
2080
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002081 /* Try to add suffix */
2082 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002083 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002084 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002085 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002086 return ret;
2087 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002088 if (!strlist__has_entry(namelist, buf))
2089 break;
2090 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002091 if (i == MAX_EVENT_INDEX) {
2092 pr_warning("Too many events are on the same function.\n");
2093 ret = -ERANGE;
2094 }
2095
2096 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002097}
2098
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302099static int __add_probe_trace_events(struct perf_probe_event *pev,
2100 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002101 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002102{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002103 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302104 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002105 char buf[64];
2106 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002107 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002108
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302109 if (pev->uprobes)
2110 fd = open_uprobe_events(true);
2111 else
2112 fd = open_kprobe_events(true);
2113
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002114 if (fd < 0) {
2115 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002116 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002117 }
2118
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002119 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302120 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002121 if (!namelist) {
2122 pr_debug("Failed to get current event list.\n");
2123 return -EIO;
2124 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002125
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002126 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302127 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002128 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002129 tev = &tevs[i];
2130 if (pev->event)
2131 event = pev->event;
2132 else
2133 if (pev->point.function)
2134 event = pev->point.function;
2135 else
2136 event = tev->point.symbol;
2137 if (pev->group)
2138 group = pev->group;
2139 else
2140 group = PERFPROBE_GROUP;
2141
2142 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002143 ret = get_new_event_name(buf, 64, event,
2144 namelist, allow_suffix);
2145 if (ret < 0)
2146 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002147 event = buf;
2148
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002149 tev->event = strdup(event);
2150 tev->group = strdup(group);
2151 if (tev->event == NULL || tev->group == NULL) {
2152 ret = -ENOMEM;
2153 break;
2154 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302155 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002156 if (ret < 0)
2157 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002158 /* Add added event name to namelist */
2159 strlist__add(namelist, event);
2160
2161 /* Trick here - save current event/group */
2162 event = pev->event;
2163 group = pev->group;
2164 pev->event = tev->event;
2165 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002166 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002167 /* Trick here - restore current event/group */
2168 pev->event = (char *)event;
2169 pev->group = (char *)group;
2170
2171 /*
2172 * Probes after the first probe which comes from same
2173 * user input are always allowed to add suffix, because
2174 * there might be several addresses corresponding to
2175 * one code line.
2176 */
2177 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002178 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002179
2180 if (ret >= 0) {
2181 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302182 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002183 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2184 tev->event);
2185 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002186
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002187 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002188 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002189 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002190}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002191
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002192static char *looking_function_name;
2193static int num_matched_functions;
2194
2195static int probe_function_filter(struct map *map __maybe_unused,
2196 struct symbol *sym)
2197{
2198 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2199 strcmp(looking_function_name, sym->name) == 0) {
2200 num_matched_functions++;
2201 return 0;
2202 }
2203 return 1;
2204}
2205
2206#define strdup_or_goto(str, label) \
2207 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2208
2209/*
2210 * Find probe function addresses from map.
2211 * Return an error or the number of found probe_trace_event
2212 */
2213static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2214 struct probe_trace_event **tevs,
2215 int max_tevs, const char *target)
2216{
2217 struct map *map = NULL;
2218 struct kmap *kmap = NULL;
2219 struct ref_reloc_sym *reloc_sym = NULL;
2220 struct symbol *sym;
2221 struct rb_node *nd;
2222 struct probe_trace_event *tev;
2223 struct perf_probe_point *pp = &pev->point;
2224 struct probe_trace_point *tp;
2225 int ret, i;
2226
2227 /* Init maps of given executable or kernel */
2228 if (pev->uprobes)
2229 map = dso__new_map(target);
2230 else
2231 map = kernel_get_module_map(target);
2232 if (!map) {
2233 ret = -EINVAL;
2234 goto out;
2235 }
2236
2237 /*
2238 * Load matched symbols: Since the different local symbols may have
2239 * same name but different addresses, this lists all the symbols.
2240 */
2241 num_matched_functions = 0;
2242 looking_function_name = pp->function;
2243 ret = map__load(map, probe_function_filter);
2244 if (ret || num_matched_functions == 0) {
2245 pr_err("Failed to find symbol %s in %s\n", pp->function,
2246 target ? : "kernel");
2247 ret = -ENOENT;
2248 goto out;
2249 } else if (num_matched_functions > max_tevs) {
2250 pr_err("Too many functions matched in %s\n",
2251 target ? : "kernel");
2252 ret = -E2BIG;
2253 goto out;
2254 }
2255
2256 if (!pev->uprobes) {
2257 kmap = map__kmap(map);
2258 reloc_sym = kmap->ref_reloc_sym;
2259 if (!reloc_sym) {
2260 pr_warning("Relocated base symbol is not found!\n");
2261 ret = -EINVAL;
2262 goto out;
2263 }
2264 }
2265
2266 /* Setup result trace-probe-events */
2267 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2268 if (!*tevs) {
2269 ret = -ENOMEM;
2270 goto out;
2271 }
2272
2273 ret = 0;
2274 map__for_each_symbol(map, sym, nd) {
2275 tev = (*tevs) + ret;
2276 tp = &tev->point;
2277 if (ret == num_matched_functions) {
2278 pr_warning("Too many symbols are listed. Skip it.\n");
2279 break;
2280 }
2281 ret++;
2282
2283 if (pp->offset > sym->end - sym->start) {
2284 pr_warning("Offset %ld is bigger than the size of %s\n",
2285 pp->offset, sym->name);
2286 ret = -ENOENT;
2287 goto err_out;
2288 }
2289 /* Add one probe point */
2290 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2291 if (reloc_sym) {
2292 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2293 tp->offset = tp->address - reloc_sym->addr;
2294 } else {
2295 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2296 tp->offset = pp->offset;
2297 }
2298 tp->retprobe = pp->retprobe;
2299 if (target)
2300 tev->point.module = strdup_or_goto(target, nomem_out);
2301 tev->uprobes = pev->uprobes;
2302 tev->nargs = pev->nargs;
2303 if (tev->nargs) {
2304 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2305 tev->nargs);
2306 if (tev->args == NULL)
2307 goto nomem_out;
2308 }
2309 for (i = 0; i < tev->nargs; i++) {
2310 if (pev->args[i].name)
2311 tev->args[i].name =
2312 strdup_or_goto(pev->args[i].name,
2313 nomem_out);
2314
2315 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2316 nomem_out);
2317 if (pev->args[i].type)
2318 tev->args[i].type =
2319 strdup_or_goto(pev->args[i].type,
2320 nomem_out);
2321 }
2322 }
2323
2324out:
2325 if (map && pev->uprobes) {
2326 /* Only when using uprobe(exec) map needs to be released */
2327 dso__delete(map->dso);
2328 map__delete(map);
2329 }
2330 return ret;
2331
2332nomem_out:
2333 ret = -ENOMEM;
2334err_out:
2335 clear_probe_trace_events(*tevs, num_matched_functions);
2336 zfree(tevs);
2337 goto out;
2338}
2339
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302340static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2341 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302342 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002343{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002344 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002345
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002346 if (pev->uprobes && !pev->group) {
2347 /* Replace group name if not given */
2348 ret = convert_exec_to_group(target, &pev->group);
2349 if (ret != 0) {
2350 pr_warning("Failed to make a group name.\n");
2351 return ret;
2352 }
2353 }
2354
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002355 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302356 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002357 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002358 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002359
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002360 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002361}
2362
2363struct __event_package {
2364 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302365 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002366 int ntevs;
2367};
2368
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002369int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302370 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002371{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002372 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002373 struct __event_package *pkgs;
2374
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302375 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002376 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302377
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002378 if (pkgs == NULL)
2379 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002380
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002381 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002382 if (ret < 0) {
2383 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002384 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002385 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002386
2387 /* Loop 1: convert all events */
2388 for (i = 0; i < npevs; i++) {
2389 pkgs[i].pev = &pevs[i];
2390 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302391 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002392 &pkgs[i].tevs,
2393 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302394 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002395 if (ret < 0)
2396 goto end;
2397 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002398 }
2399
2400 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002401 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302402 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002403 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002404 if (ret < 0)
2405 break;
2406 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002407end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002408 /* Loop 3: cleanup and free trace events */
2409 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002410 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302411 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002412 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002413 }
2414 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002415 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002416
2417 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002418}
2419
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302420static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002421{
2422 char *p;
2423 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002424 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002425
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302426 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2428 if (ret < 0)
2429 goto error;
2430
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002431 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002432 if (!p) {
2433 pr_debug("Internal error: %s should have ':' but not.\n",
2434 ent->s);
2435 ret = -ENOTSUP;
2436 goto error;
2437 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002438 *p = '/';
2439
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002440 pr_debug("Writing event: %s\n", buf);
2441 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002442 if (ret < 0) {
2443 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002444 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002445 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002446
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302447 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002448 return 0;
2449error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002450 pr_warning("Failed to delete event: %s\n",
2451 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002452 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002453}
2454
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302455static int del_trace_probe_event(int fd, const char *buf,
2456 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002457{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002458 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302459 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002460
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002461 if (strpbrk(buf, "*?")) { /* Glob-exp */
2462 strlist__for_each_safe(ent, n, namelist)
2463 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302464 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002465 if (ret < 0)
2466 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002467 strlist__remove(namelist, ent);
2468 }
2469 } else {
2470 ent = strlist__find(namelist, buf);
2471 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302472 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002473 if (ret >= 0)
2474 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002475 }
2476 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002477
2478 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002479}
2480
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002482{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302483 int ret = -1, ufd = -1, kfd = -1;
2484 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002485 const char *group, *event;
2486 char *p, *str;
2487 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302488 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002489
Masami Hiramatsufa282442009-12-08 17:03:23 -05002490 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302491 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002492 if (kfd >= 0)
2493 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302494
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302495 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002496 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302497 unamelist = get_probe_trace_event_names(ufd, true);
2498
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002499 if (kfd < 0 && ufd < 0) {
2500 print_both_open_warning(kfd, ufd);
2501 goto error;
2502 }
2503
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302504 if (namelist == NULL && unamelist == NULL)
2505 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002506
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002507 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002508 str = strdup(ent->s);
2509 if (str == NULL) {
2510 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302511 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002512 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002513 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002514 p = strchr(str, ':');
2515 if (p) {
2516 group = str;
2517 *p = '\0';
2518 event = p + 1;
2519 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002520 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002521 event = str;
2522 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302523
2524 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2525 if (ret < 0) {
2526 pr_err("Failed to copy event.");
2527 free(str);
2528 goto error;
2529 }
2530
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002531 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302532
2533 if (namelist)
2534 ret = del_trace_probe_event(kfd, buf, namelist);
2535
2536 if (unamelist && ret != 0)
2537 ret = del_trace_probe_event(ufd, buf, unamelist);
2538
2539 if (ret != 0)
2540 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2541
Masami Hiramatsufa282442009-12-08 17:03:23 -05002542 free(str);
2543 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302544
2545error:
2546 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302547 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302548 close(kfd);
2549 }
2550
2551 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302552 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302553 close(ufd);
2554 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002555
2556 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002557}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302558
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002559/* TODO: don't use a global variable for filter ... */
2560static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002561
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002562/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002563 * If a symbol corresponds to a function with global binding and
2564 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002565 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002566static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002567 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002568{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002569 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002570 strfilter__compare(available_func_filter, sym->name))
2571 return 0;
2572 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002573}
2574
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002575int show_available_funcs(const char *target, struct strfilter *_filter,
2576 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002577{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002578 struct map *map;
2579 int ret;
2580
2581 ret = init_symbol_maps(user);
2582 if (ret < 0)
2583 return ret;
2584
2585 /* Get a symbol map */
2586 if (user)
2587 map = dso__new_map(target);
2588 else
2589 map = kernel_get_module_map(target);
2590 if (!map) {
2591 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002592 return -EINVAL;
2593 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002594
2595 /* Load symbols with given filter */
2596 available_func_filter = _filter;
2597 if (map__load(map, filter_available_functions)) {
2598 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2599 goto end;
2600 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002601 if (!dso__sorted_by_name(map->dso, map->type))
2602 dso__sort_by_name(map->dso, map->type);
2603
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002604 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302605 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002606 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2607end:
2608 if (user) {
2609 dso__delete(map->dso);
2610 map__delete(map);
2611 }
2612 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302613
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002614 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302615}
2616