blob: 7f9b8632e4331bc2216b29dd15374cac69e83d96 [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. */
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900498 pr_warning("Probe point '%s' not found in debuginfo.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400499 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900500 if (need_dwarf)
501 return -ENOENT;
502 return 0;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400503 }
504 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400505 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
506 if (ntevs == -EBADF) {
507 pr_warning("Warning: No dwarf info found in the vmlinux - "
508 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
509 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900510 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400511 return 0;
512 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300513 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400514 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300515}
516
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900517/*
518 * Find a src file from a DWARF tag path. Prepend optional source path prefix
519 * and chop off leading directories that do not exist. Result is passed back as
520 * a newly allocated path on success.
521 * Return 0 if file was found and readable, -errno otherwise.
522 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900523static int get_real_path(const char *raw_path, const char *comp_dir,
524 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900525{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900526 const char *prefix = symbol_conf.source_prefix;
527
528 if (!prefix) {
529 if (raw_path[0] != '/' && comp_dir)
530 /* If not an absolute path, try to use comp_dir */
531 prefix = comp_dir;
532 else {
533 if (access(raw_path, R_OK) == 0) {
534 *new_path = strdup(raw_path);
535 return 0;
536 } else
537 return -errno;
538 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900539 }
540
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900541 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900542 if (!*new_path)
543 return -ENOMEM;
544
545 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900546 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900547
548 if (access(*new_path, R_OK) == 0)
549 return 0;
550
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900551 if (!symbol_conf.source_prefix)
552 /* In case of searching comp_dir, don't retry */
553 return -errno;
554
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900555 switch (errno) {
556 case ENAMETOOLONG:
557 case ENOENT:
558 case EROFS:
559 case EFAULT:
560 raw_path = strchr(++raw_path, '/');
561 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300562 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900563 return -ENOENT;
564 }
565 continue;
566
567 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300568 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900569 return -errno;
570 }
571 }
572}
573
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300574#define LINEBUF_SIZE 256
575#define NR_ADDITIONAL_LINES 2
576
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100577static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300578{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000579 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100580 const char *color = show_num ? "" : PERF_COLOR_BLUE;
581 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300582
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100583 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300584 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
585 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100586 if (skip)
587 continue;
588 if (!prefix) {
589 prefix = show_num ? "%7d " : " ";
590 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300591 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100592 color_fprintf(stdout, color, "%s", buf);
593
594 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400595
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100596 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300597error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100598 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000599 pr_warning("File read error: %s\n",
600 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100601 return -1;
602 }
603 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300604}
605
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100606static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
607{
608 int rv = __show_one_line(fp, l, skip, show_num);
609 if (rv == 0) {
610 pr_warning("Source file is shorter than expected.\n");
611 rv = -1;
612 }
613 return rv;
614}
615
616#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
617#define show_one_line(f,l) _show_one_line(f,l,false,false)
618#define skip_one_line(f,l) _show_one_line(f,l,true,false)
619#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
620
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300621/*
622 * Show line-range always requires debuginfo to find source file and
623 * line number.
624 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000625static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300626{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400627 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000628 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900629 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300630 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900631 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900632 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000633 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300634
635 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000636 dinfo = open_debuginfo(module, false);
637 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900638 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400639
Masami Hiramatsuff741782011-06-27 16:27:39 +0900640 ret = debuginfo__find_line_range(dinfo, lr);
641 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000642 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400643 pr_warning("Specified source line is not found.\n");
644 return -ENOENT;
645 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000646 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400647 return ret;
648 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300649
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900650 /* Convert source file path */
651 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900652 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900653 free(tmp); /* Free old path */
654 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000655 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900656 return ret;
657 }
658
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300659 setup_pager();
660
661 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900662 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300663 lr->start - lr->offset);
664 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100665 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300666
667 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400668 if (fp == NULL) {
669 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000670 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400671 return -errno;
672 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300673 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100674 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100675 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100676 if (ret < 0)
677 goto end;
678 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300679
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000680 intlist__for_each(ln, lr->line_list) {
681 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100682 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100683 if (ret < 0)
684 goto end;
685 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100686 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400687 if (ret < 0)
688 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300689 }
690
691 if (lr->end == INT_MAX)
692 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100693 while (l <= lr->end) {
694 ret = show_one_line_or_eof(fp, l++ - lr->offset);
695 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100696 break;
697 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400698end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300699 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400700 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300701}
702
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000703int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000704{
705 int ret;
706
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000707 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000708 if (ret < 0)
709 return ret;
710 ret = __show_line_range(lr, module);
711 exit_symbol_maps();
712
713 return ret;
714}
715
Masami Hiramatsuff741782011-06-27 16:27:39 +0900716static int show_available_vars_at(struct debuginfo *dinfo,
717 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900718 int max_vls, struct strfilter *_filter,
719 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900720{
721 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900722 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900723 struct str_node *node;
724 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900725 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900726
727 buf = synthesize_perf_probe_point(&pev->point);
728 if (!buf)
729 return -EINVAL;
730 pr_debug("Searching variables at %s\n", buf);
731
Masami Hiramatsuff741782011-06-27 16:27:39 +0900732 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
733 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900734 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000735 if (ret == 0 || ret == -ENOENT) {
736 pr_err("Failed to find the address of %s\n", buf);
737 ret = -ENOENT;
738 } else
739 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900740 goto end;
741 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000742
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900743 /* Some variables are found */
744 fprintf(stdout, "Available variables at %s\n", buf);
745 for (i = 0; i < ret; i++) {
746 vl = &vls[i];
747 /*
748 * A probe point might be converted to
749 * several trace points.
750 */
751 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
752 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300753 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900754 nvars = 0;
755 if (vl->vars) {
756 strlist__for_each(node, vl->vars) {
757 var = strchr(node->s, '\t') + 1;
758 if (strfilter__compare(_filter, var)) {
759 fprintf(stdout, "\t\t%s\n", node->s);
760 nvars++;
761 }
762 }
763 strlist__delete(vl->vars);
764 }
765 if (nvars == 0)
766 fprintf(stdout, "\t\t(No matched variables)\n");
767 }
768 free(vls);
769end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900770 free(buf);
771 return ret;
772}
773
774/* Show available variables on given probe point */
775int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900776 int max_vls, const char *module,
777 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900778{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900779 int i, ret = 0;
780 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900781
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000782 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900783 if (ret < 0)
784 return ret;
785
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000786 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900787 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000788 ret = -ENOENT;
789 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900790 }
791
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900792 setup_pager();
793
Masami Hiramatsuff741782011-06-27 16:27:39 +0900794 for (i = 0; i < npevs && ret >= 0; i++)
795 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900796 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900797
798 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000799out:
800 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900801 return ret;
802}
803
Ingo Molnar89fe8082013-09-30 12:07:11 +0200804#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300805
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000806static int
807find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
808 struct perf_probe_point *pp __maybe_unused,
809 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000811 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812}
813
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530814static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300815 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300816 int max_tevs __maybe_unused,
817 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300818{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400819 if (perf_probe_event_need_dwarf(pev)) {
820 pr_warning("Debuginfo-analysis is not supported.\n");
821 return -ENOSYS;
822 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530823
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300824 return 0;
825}
826
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300827int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000828 const char *module __maybe_unused,
829 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 pr_warning("Debuginfo-analysis is not supported.\n");
832 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300833}
834
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300835int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
836 int npevs __maybe_unused, int max_vls __maybe_unused,
837 const char *module __maybe_unused,
838 struct strfilter *filter __maybe_unused,
839 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900840{
841 pr_warning("Debuginfo-analysis is not supported.\n");
842 return -ENOSYS;
843}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400844#endif
845
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000846void line_range__clear(struct line_range *lr)
847{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000848 free(lr->function);
849 free(lr->file);
850 free(lr->path);
851 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000852 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000853 memset(lr, 0, sizeof(*lr));
854}
855
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000856int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000857{
858 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000859 lr->line_list = intlist__new(NULL);
860 if (!lr->line_list)
861 return -ENOMEM;
862 else
863 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000864}
865
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100866static int parse_line_num(char **ptr, int *val, const char *what)
867{
868 const char *start = *ptr;
869
870 errno = 0;
871 *val = strtol(*ptr, ptr, 0);
872 if (errno || *ptr == start) {
873 semantic_error("'%s' is not a valid number.\n", what);
874 return -EINVAL;
875 }
876 return 0;
877}
878
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100879/*
880 * Stuff 'lr' according to the line range described by 'arg'.
881 * The line range syntax is described by:
882 *
883 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900884 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100885 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400886int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500887{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900888 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100889 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100890
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100891 if (!name)
892 return -ENOMEM;
893
894 lr->start = 0;
895 lr->end = INT_MAX;
896
897 range = strchr(name, ':');
898 if (range) {
899 *range++ = '\0';
900
901 err = parse_line_num(&range, &lr->start, "start line");
902 if (err)
903 goto err;
904
905 if (*range == '+' || *range == '-') {
906 const char c = *range++;
907
908 err = parse_line_num(&range, &lr->end, "end line");
909 if (err)
910 goto err;
911
912 if (c == '+') {
913 lr->end += lr->start;
914 /*
915 * Adjust the number of lines here.
916 * If the number of lines == 1, the
917 * the end of line should be equal to
918 * the start of line.
919 */
920 lr->end--;
921 }
922 }
923
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400924 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100925
926 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400927 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500928 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400929 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100930 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400931 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100932 if (*range != '\0') {
933 semantic_error("Tailing with invalid str '%s'.\n", range);
934 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400935 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400936 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400937
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900938 file = strchr(name, '@');
939 if (file) {
940 *file = '\0';
941 lr->file = strdup(++file);
942 if (lr->file == NULL) {
943 err = -ENOMEM;
944 goto err;
945 }
946 lr->function = name;
947 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100948 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500949 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100950 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400951
952 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100953err:
954 free(name);
955 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500956}
957
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500958/* Check the name is good for event/group */
959static bool check_event_name(const char *name)
960{
961 if (!isalpha(*name) && *name != '_')
962 return false;
963 while (*++name != '\0') {
964 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
965 return false;
966 }
967 return true;
968}
969
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500970/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400971static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500972{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400973 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500974 char *ptr, *tmp;
975 char c, nc = 0;
976 /*
977 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500978 * perf probe [EVENT=]SRC[:LN|;PTN]
979 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500980 *
981 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500982 */
983
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500984 ptr = strpbrk(arg, ";=@+%");
985 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500986 *ptr = '\0';
987 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400988 if (strchr(arg, ':')) {
989 semantic_error("Group name is not supported yet.\n");
990 return -ENOTSUP;
991 }
992 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500993 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400994 "follow C symbol-naming rule.\n", arg);
995 return -EINVAL;
996 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400997 pev->event = strdup(arg);
998 if (pev->event == NULL)
999 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001001 arg = tmp;
1002 }
1003
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001004 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001005 if (ptr) {
1006 nc = *ptr;
1007 *ptr++ = '\0';
1008 }
1009
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001010 tmp = strdup(arg);
1011 if (tmp == NULL)
1012 return -ENOMEM;
1013
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001014 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001015 if (strchr(tmp, '.')) /* File */
1016 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001017 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001018 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001019
1020 /* Parse other options */
1021 while (ptr) {
1022 arg = ptr;
1023 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001024 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001025 pp->lazy_line = strdup(arg);
1026 if (pp->lazy_line == NULL)
1027 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001028 break;
1029 }
1030 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001031 if (ptr) {
1032 nc = *ptr;
1033 *ptr++ = '\0';
1034 }
1035 switch (c) {
1036 case ':': /* Line number */
1037 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001039 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040 " in line number.\n");
1041 return -EINVAL;
1042 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001043 break;
1044 case '+': /* Byte offset from a symbol */
1045 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001046 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001047 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001048 " in offset.\n");
1049 return -EINVAL;
1050 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001051 break;
1052 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 if (pp->file) {
1054 semantic_error("SRC@SRC is not allowed.\n");
1055 return -EINVAL;
1056 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001057 pp->file = strdup(arg);
1058 if (pp->file == NULL)
1059 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001060 break;
1061 case '%': /* Probe places */
1062 if (strcmp(arg, "return") == 0) {
1063 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001064 } else { /* Others not supported yet */
1065 semantic_error("%%%s is not supported.\n", arg);
1066 return -ENOTSUP;
1067 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001068 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001069 default: /* Buggy case */
1070 pr_err("This program has a bug at %s:%d.\n",
1071 __FILE__, __LINE__);
1072 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001073 break;
1074 }
1075 }
1076
1077 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001078 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001079 semantic_error("Lazy pattern can't be used with"
1080 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081 return -EINVAL;
1082 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001083
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001085 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001086 return -EINVAL;
1087 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001088
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001090 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001091 return -EINVAL;
1092 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001093
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001094 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001095 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001096 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001097 return -EINVAL;
1098 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001099
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001101 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001102 return -EINVAL;
1103 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001104
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001106 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001107 return -EINVAL;
1108 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001109
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001111 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001112 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001113 return -EINVAL;
1114 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001115
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001116 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001117 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1118 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001119 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001120}
1121
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001122/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001123static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001124{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001125 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001126 struct perf_probe_arg_field **fieldp;
1127
1128 pr_debug("parsing arg: %s into ", str);
1129
Masami Hiramatsu48481932010-04-12 13:16:53 -04001130 tmp = strchr(str, '=');
1131 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001132 arg->name = strndup(str, tmp - str);
1133 if (arg->name == NULL)
1134 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001135 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001136 str = tmp + 1;
1137 }
1138
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001139 tmp = strchr(str, ':');
1140 if (tmp) { /* Type setting */
1141 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001142 arg->type = strdup(tmp + 1);
1143 if (arg->type == NULL)
1144 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001145 pr_debug("type:%s ", arg->type);
1146 }
1147
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001148 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001149 if (!is_c_varname(str) || !tmp) {
1150 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001151 arg->var = strdup(str);
1152 if (arg->var == NULL)
1153 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001154 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001155 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001156 }
1157
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001158 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001159 arg->var = strndup(str, tmp - str);
1160 if (arg->var == NULL)
1161 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001162 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001163 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001164 fieldp = &arg->field;
1165
1166 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001167 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1168 if (*fieldp == NULL)
1169 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001170 if (*tmp == '[') { /* Array */
1171 str = tmp;
1172 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001173 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001174 if (*tmp != ']' || tmp == str + 1) {
1175 semantic_error("Array index must be a"
1176 " number.\n");
1177 return -EINVAL;
1178 }
1179 tmp++;
1180 if (*tmp == '\0')
1181 tmp = NULL;
1182 } else { /* Structure */
1183 if (*tmp == '.') {
1184 str = tmp + 1;
1185 (*fieldp)->ref = false;
1186 } else if (tmp[1] == '>') {
1187 str = tmp + 2;
1188 (*fieldp)->ref = true;
1189 } else {
1190 semantic_error("Argument parse error: %s\n",
1191 str);
1192 return -EINVAL;
1193 }
1194 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001196 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001197 (*fieldp)->name = strndup(str, tmp - str);
1198 if ((*fieldp)->name == NULL)
1199 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001200 if (*str != '[')
1201 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001202 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1203 fieldp = &(*fieldp)->next;
1204 }
1205 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001206 (*fieldp)->name = strdup(str);
1207 if ((*fieldp)->name == NULL)
1208 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001209 if (*str != '[')
1210 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001211 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001212
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001213 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001214 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001215 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001216 if (arg->name == NULL)
1217 return -ENOMEM;
1218 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001220}
1221
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001224{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001225 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001227
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001228 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 if (!argv) {
1230 pr_debug("Failed to split arguments.\n");
1231 return -ENOMEM;
1232 }
1233 if (argc - 1 > MAX_PROBE_ARGS) {
1234 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1235 ret = -ERANGE;
1236 goto out;
1237 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001238 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 ret = parse_perf_probe_point(argv[0], pev);
1240 if (ret < 0)
1241 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001242
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001243 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001245 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1246 if (pev->args == NULL) {
1247 ret = -ENOMEM;
1248 goto out;
1249 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1251 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1252 if (ret >= 0 &&
1253 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001254 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 " kretprobe.\n");
1256 ret = -EINVAL;
1257 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001258 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001260 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261
1262 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001263}
1264
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265/* Return true if this perf_probe_event requires debuginfo */
1266bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001267{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268 int i;
1269
1270 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1271 return true;
1272
1273 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001274 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001275 return true;
1276
1277 return false;
1278}
1279
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301280/* Parse probe_events event into struct probe_point */
1281static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001282 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001283{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301284 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001285 char pr;
1286 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001287 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001288 int ret, i, argc;
1289 char **argv;
1290
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301291 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001292 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001293 if (!argv) {
1294 pr_debug("Failed to split arguments.\n");
1295 return -ENOMEM;
1296 }
1297 if (argc < 2) {
1298 semantic_error("Too few probe arguments.\n");
1299 ret = -ERANGE;
1300 goto out;
1301 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001302
1303 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001304 argv0_str = strdup(argv[0]);
1305 if (argv0_str == NULL) {
1306 ret = -ENOMEM;
1307 goto out;
1308 }
1309 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1310 fmt2_str = strtok_r(NULL, "/", &fmt);
1311 fmt3_str = strtok_r(NULL, " \t", &fmt);
1312 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1313 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001314 semantic_error("Failed to parse event name: %s\n", argv[0]);
1315 ret = -EINVAL;
1316 goto out;
1317 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001318 pr = fmt1_str[0];
1319 tev->group = strdup(fmt2_str);
1320 tev->event = strdup(fmt3_str);
1321 if (tev->group == NULL || tev->event == NULL) {
1322 ret = -ENOMEM;
1323 goto out;
1324 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001325 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001326
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001327 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001328
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001329 /* Scan module name(if there), function name and offset */
1330 p = strchr(argv[1], ':');
1331 if (p) {
1332 tp->module = strndup(argv[1], p - argv[1]);
1333 p++;
1334 } else
1335 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001336 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001337 if (fmt1_str[0] == '0') /* only the address started with 0x */
1338 tp->address = strtoul(fmt1_str, NULL, 0);
1339 else {
1340 /* Only the symbol-based probe has offset */
1341 tp->symbol = strdup(fmt1_str);
1342 if (tp->symbol == NULL) {
1343 ret = -ENOMEM;
1344 goto out;
1345 }
1346 fmt2_str = strtok_r(NULL, "", &fmt);
1347 if (fmt2_str == NULL)
1348 tp->offset = 0;
1349 else
1350 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001351 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001352
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301354 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001355 if (tev->args == NULL) {
1356 ret = -ENOMEM;
1357 goto out;
1358 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001360 p = strchr(argv[i + 2], '=');
1361 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001362 *p++ = '\0';
1363 else
1364 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001365 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001366 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001367 tev->args[i].value = strdup(p);
1368 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1369 ret = -ENOMEM;
1370 goto out;
1371 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001372 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001373 ret = 0;
1374out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001375 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001378}
1379
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001380/* Compose only probe arg */
1381int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1382{
1383 struct perf_probe_arg_field *field = pa->field;
1384 int ret;
1385 char *tmp = buf;
1386
Masami Hiramatsu48481932010-04-12 13:16:53 -04001387 if (pa->name && pa->var)
1388 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1389 else
1390 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001391 if (ret <= 0)
1392 goto error;
1393 tmp += ret;
1394 len -= ret;
1395
1396 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001397 if (field->name[0] == '[')
1398 ret = e_snprintf(tmp, len, "%s", field->name);
1399 else
1400 ret = e_snprintf(tmp, len, "%s%s",
1401 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001402 if (ret <= 0)
1403 goto error;
1404 tmp += ret;
1405 len -= ret;
1406 field = field->next;
1407 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001408
1409 if (pa->type) {
1410 ret = e_snprintf(tmp, len, ":%s", pa->type);
1411 if (ret <= 0)
1412 goto error;
1413 tmp += ret;
1414 len -= ret;
1415 }
1416
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001417 return tmp - buf;
1418error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001419 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001421}
1422
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001423/* Compose only probe point (not argument) */
1424static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001425{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001426 char *buf, *tmp;
1427 char offs[32] = "", line[32] = "", file[32] = "";
1428 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001429
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001430 buf = zalloc(MAX_CMDLEN);
1431 if (buf == NULL) {
1432 ret = -ENOMEM;
1433 goto error;
1434 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001435 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001436 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001437 if (ret <= 0)
1438 goto error;
1439 }
1440 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001441 ret = e_snprintf(line, 32, ":%d", pp->line);
1442 if (ret <= 0)
1443 goto error;
1444 }
1445 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001446 tmp = pp->file;
1447 len = strlen(tmp);
1448 if (len > 30) {
1449 tmp = strchr(pp->file + len - 30, '/');
1450 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1451 }
1452 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001453 if (ret <= 0)
1454 goto error;
1455 }
1456
1457 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001458 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1459 offs, pp->retprobe ? "%return" : "", line,
1460 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001461 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001462 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001463 if (ret <= 0)
1464 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001465
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001466 return buf;
1467error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001468 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001469 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001471}
1472
1473#if 0
1474char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1475{
1476 char *buf;
1477 int i, len, ret;
1478
1479 buf = synthesize_perf_probe_point(&pev->point);
1480 if (!buf)
1481 return NULL;
1482
1483 len = strlen(buf);
1484 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001485 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001486 pev->args[i].name);
1487 if (ret <= 0) {
1488 free(buf);
1489 return NULL;
1490 }
1491 len += ret;
1492 }
1493
1494 return buf;
1495}
1496#endif
1497
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301498static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001499 char **buf, size_t *buflen,
1500 int depth)
1501{
1502 int ret;
1503 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301504 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001505 buflen, depth + 1);
1506 if (depth < 0)
1507 goto out;
1508 }
1509
1510 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1511 if (ret < 0)
1512 depth = ret;
1513 else {
1514 *buf += ret;
1515 *buflen -= ret;
1516 }
1517out:
1518 return depth;
1519
1520}
1521
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301522static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001523 char *buf, size_t buflen)
1524{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301525 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 int ret, depth = 0;
1527 char *tmp = buf;
1528
1529 /* Argument name or separator */
1530 if (arg->name)
1531 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1532 else
1533 ret = e_snprintf(buf, buflen, " ");
1534 if (ret < 0)
1535 return ret;
1536 buf += ret;
1537 buflen -= ret;
1538
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001539 /* Special case: @XXX */
1540 if (arg->value[0] == '@' && arg->ref)
1541 ref = ref->next;
1542
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001543 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001544 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301545 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 &buflen, 1);
1547 if (depth < 0)
1548 return depth;
1549 }
1550
1551 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001552 if (arg->value[0] == '@' && arg->ref)
1553 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1554 arg->ref->offset);
1555 else
1556 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001557 if (ret < 0)
1558 return ret;
1559 buf += ret;
1560 buflen -= ret;
1561
1562 /* Closing */
1563 while (depth--) {
1564 ret = e_snprintf(buf, buflen, ")");
1565 if (ret < 0)
1566 return ret;
1567 buf += ret;
1568 buflen -= ret;
1569 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001570 /* Print argument type */
1571 if (arg->type) {
1572 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1573 if (ret <= 0)
1574 return ret;
1575 buf += ret;
1576 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577
1578 return buf - tmp;
1579}
1580
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301581char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001582{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301583 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001584 char *buf;
1585 int i, len, ret;
1586
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001587 buf = zalloc(MAX_CMDLEN);
1588 if (buf == NULL)
1589 return NULL;
1590
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001591 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1592 tev->group, tev->event);
1593 if (len <= 0)
1594 goto error;
1595
1596 /* Uprobes must have tp->address and tp->module */
1597 if (tev->uprobes && (!tp->address || !tp->module))
1598 goto error;
1599
1600 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301601 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001602 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1603 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301604 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001605 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301606 tp->module ?: "", tp->module ? ":" : "",
1607 tp->symbol, tp->offset);
1608
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001609 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001610 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001611 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001612
1613 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301614 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001615 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001616 if (ret <= 0)
1617 goto error;
1618 len += ret;
1619 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001620
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001622error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001623 free(buf);
1624 return NULL;
1625}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001626
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001627static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1628 struct perf_probe_point *pp,
1629 bool is_kprobe)
1630{
1631 struct symbol *sym = NULL;
1632 struct map *map;
1633 u64 addr;
1634 int ret = -ENOENT;
1635
1636 if (!is_kprobe) {
1637 map = dso__new_map(tp->module);
1638 if (!map)
1639 goto out;
1640 addr = tp->address;
1641 sym = map__find_symbol(map, addr, NULL);
1642 } else {
1643 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1644 if (addr) {
1645 addr += tp->offset;
1646 sym = __find_kernel_function(addr, &map);
1647 }
1648 }
1649 if (!sym)
1650 goto out;
1651
1652 pp->retprobe = tp->retprobe;
1653 pp->offset = addr - map->unmap_ip(map, sym->start);
1654 pp->function = strdup(sym->name);
1655 ret = pp->function ? 0 : -ENOMEM;
1656
1657out:
1658 if (map && !is_kprobe) {
1659 dso__delete(map->dso);
1660 map__delete(map);
1661 }
1662
1663 return ret;
1664}
1665
1666static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1667 struct perf_probe_point *pp,
1668 bool is_kprobe)
1669{
1670 char buf[128];
1671 int ret;
1672
1673 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1674 if (!ret)
1675 return 0;
1676 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1677 if (!ret)
1678 return 0;
1679
1680 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1681
1682 if (tp->symbol) {
1683 pp->function = strdup(tp->symbol);
1684 pp->offset = tp->offset;
1685 } else if (!tp->module && !is_kprobe) {
1686 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1687 if (ret < 0)
1688 return ret;
1689 pp->function = strdup(buf);
1690 pp->offset = 0;
1691 }
1692 if (pp->function == NULL)
1693 return -ENOMEM;
1694
1695 pp->retprobe = tp->retprobe;
1696
1697 return 0;
1698}
1699
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301700static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301701 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001703 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001704 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001705
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001706 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001707 pev->event = strdup(tev->event);
1708 pev->group = strdup(tev->group);
1709 if (pev->event == NULL || pev->group == NULL)
1710 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001711
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001712 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001713 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001714 if (ret < 0)
1715 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001716
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001717 /* Convert trace_arg to probe_arg */
1718 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001719 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1720 if (pev->args == NULL)
1721 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001722 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001723 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001724 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001725 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301726 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001727 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001728 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001729 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001730 if (pev->args[i].name == NULL && ret >= 0)
1731 ret = -ENOMEM;
1732 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001733
1734 if (ret < 0)
1735 clear_perf_probe_event(pev);
1736
1737 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738}
1739
1740void clear_perf_probe_event(struct perf_probe_event *pev)
1741{
1742 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001743 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744 int i;
1745
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001746 free(pev->event);
1747 free(pev->group);
1748 free(pp->file);
1749 free(pp->function);
1750 free(pp->lazy_line);
1751
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001752 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001753 free(pev->args[i].name);
1754 free(pev->args[i].var);
1755 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001756 field = pev->args[i].field;
1757 while (field) {
1758 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001759 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001760 free(field);
1761 field = next;
1762 }
1763 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001764 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765 memset(pev, 0, sizeof(*pev));
1766}
1767
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301768static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301770 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 int i;
1772
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001773 free(tev->event);
1774 free(tev->group);
1775 free(tev->point.symbol);
1776 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001777 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001778 free(tev->args[i].name);
1779 free(tev->args[i].value);
1780 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001781 ref = tev->args[i].ref;
1782 while (ref) {
1783 next = ref->next;
1784 free(ref);
1785 ref = next;
1786 }
1787 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001788 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001789 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001790}
1791
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001792static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301793{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001794 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301795
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001796 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301797 const char *config;
1798
1799 if (!is_kprobe)
1800 config = "CONFIG_UPROBE_EVENTS";
1801 else
1802 config = "CONFIG_KPROBE_EVENTS";
1803
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001804 pr_warning("%cprobe_events file does not exist"
1805 " - please rebuild kernel with %s.\n",
1806 is_kprobe ? 'k' : 'u', config);
1807 } else if (err == -ENOTSUP)
1808 pr_warning("Debugfs is not mounted.\n");
1809 else
1810 pr_warning("Failed to open %cprobe_events: %s\n",
1811 is_kprobe ? 'k' : 'u',
1812 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301813}
1814
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001815static void print_both_open_warning(int kerr, int uerr)
1816{
1817 /* Both kprobes and uprobes are disabled, warn it. */
1818 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1819 pr_warning("Debugfs is not mounted.\n");
1820 else if (kerr == -ENOENT && uerr == -ENOENT)
1821 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1822 "or/and CONFIG_UPROBE_EVENTS.\n");
1823 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001824 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001825 pr_warning("Failed to open kprobe events: %s.\n",
1826 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1827 pr_warning("Failed to open uprobe events: %s.\n",
1828 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1829 }
1830}
1831
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001832static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001833{
1834 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001835 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001836 int ret;
1837
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001838 __debugfs = debugfs_find_mountpoint();
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001839 if (__debugfs == NULL)
1840 return -ENOTSUP;
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001841
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301842 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001843 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001844 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001845 if (readwrite && !probe_event_dry_run)
1846 ret = open(buf, O_RDWR, O_APPEND);
1847 else
1848 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001849
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301850 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001851 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001852 }
1853 return ret;
1854}
1855
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301856static int open_kprobe_events(bool readwrite)
1857{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001858 return open_probe_events("tracing/kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301859}
1860
1861static int open_uprobe_events(bool readwrite)
1862{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001863 return open_probe_events("tracing/uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301864}
1865
1866/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301867static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001868{
1869 int ret, idx;
1870 FILE *fp;
1871 char buf[MAX_CMDLEN];
1872 char *p;
1873 struct strlist *sl;
1874
1875 sl = strlist__new(true, NULL);
1876
1877 fp = fdopen(dup(fd), "r");
1878 while (!feof(fp)) {
1879 p = fgets(buf, MAX_CMDLEN, fp);
1880 if (!p)
1881 break;
1882
1883 idx = strlen(p) - 1;
1884 if (p[idx] == '\n')
1885 p[idx] = '\0';
1886 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001887 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001888 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001889 strlist__delete(sl);
1890 return NULL;
1891 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001892 }
1893 fclose(fp);
1894
1895 return sl;
1896}
1897
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001898/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001899static int show_perf_probe_event(struct perf_probe_event *pev,
1900 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001901{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001902 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001903 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001905
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001906 /* Synthesize only event probe point */
1907 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001908 if (!place)
1909 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001910
1911 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001912 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001913 return ret;
1914
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001915 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001916 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001917 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001918
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001919 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001920 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001921 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001922 ret = synthesize_perf_probe_arg(&pev->args[i],
1923 buf, 128);
1924 if (ret < 0)
1925 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001926 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001927 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001928 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001929 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001930 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001931 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001932}
1933
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301934static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001935{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301936 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301937 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001938 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001939 struct strlist *rawlist;
1940 struct str_node *ent;
1941
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942 memset(&tev, 0, sizeof(tev));
1943 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001944
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301945 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001946 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001947 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001948
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001949 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301950 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001951 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301952 ret = convert_to_perf_probe_event(&tev, &pev,
1953 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001954 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001955 ret = show_perf_probe_event(&pev,
1956 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001957 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001958 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301959 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001960 if (ret < 0)
1961 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001962 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001963 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001964
1965 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001966}
1967
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301968/* List up current perf-probe events */
1969int show_perf_probe_events(void)
1970{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001971 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301972
1973 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301974
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001975 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301976 if (ret < 0)
1977 return ret;
1978
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001979 kp_fd = open_kprobe_events(false);
1980 if (kp_fd >= 0) {
1981 ret = __show_perf_probe_events(kp_fd, true);
1982 close(kp_fd);
1983 if (ret < 0)
1984 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301985 }
1986
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001987 up_fd = open_uprobe_events(false);
1988 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001989 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001990 ret = kp_fd;
1991 goto out;
1992 }
1993
1994 if (up_fd >= 0) {
1995 ret = __show_perf_probe_events(up_fd, false);
1996 close(up_fd);
1997 }
1998out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001999 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302000 return ret;
2001}
2002
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002003/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302004static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002005{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002006 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002007 struct strlist *sl, *rawlist;
2008 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302009 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002010 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002011
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002012 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302013 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002014 if (!rawlist)
2015 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002016 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002017 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302018 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002019 if (ret < 0)
2020 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002021 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002022 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2023 tev.event);
2024 if (ret >= 0)
2025 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002026 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002027 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302028 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002029 if (ret < 0)
2030 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002031 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002032 strlist__delete(rawlist);
2033
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002034 if (ret < 0) {
2035 strlist__delete(sl);
2036 return NULL;
2037 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002038 return sl;
2039}
2040
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302041static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002042{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002043 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302044 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002045 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002046
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002047 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302048 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002049 return -EINVAL;
2050 }
2051
Masami Hiramatsufa282442009-12-08 17:03:23 -05002052 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002053 if (!probe_event_dry_run) {
2054 ret = write(fd, buf, strlen(buf));
2055 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002056 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002057 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002058 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002059 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002060 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002061}
2062
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002063static int get_new_event_name(char *buf, size_t len, const char *base,
2064 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002065{
2066 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002067
2068 /* Try no suffix */
2069 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002070 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002071 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002072 return ret;
2073 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002074 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002075 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002076
Masami Hiramatsud761b082009-12-15 10:32:25 -05002077 if (!allow_suffix) {
2078 pr_warning("Error: event \"%s\" already exists. "
2079 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002080 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002081 }
2082
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002083 /* Try to add suffix */
2084 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002085 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002086 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002087 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002088 return ret;
2089 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002090 if (!strlist__has_entry(namelist, buf))
2091 break;
2092 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002093 if (i == MAX_EVENT_INDEX) {
2094 pr_warning("Too many events are on the same function.\n");
2095 ret = -ERANGE;
2096 }
2097
2098 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002099}
2100
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302101static int __add_probe_trace_events(struct perf_probe_event *pev,
2102 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002103 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002104{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002105 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302106 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002107 char buf[64];
2108 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002109 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002110
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302111 if (pev->uprobes)
2112 fd = open_uprobe_events(true);
2113 else
2114 fd = open_kprobe_events(true);
2115
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002116 if (fd < 0) {
2117 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002118 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002119 }
2120
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002121 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302122 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002123 if (!namelist) {
2124 pr_debug("Failed to get current event list.\n");
2125 return -EIO;
2126 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002127
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002129 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002130 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002131 tev = &tevs[i];
2132 if (pev->event)
2133 event = pev->event;
2134 else
2135 if (pev->point.function)
2136 event = pev->point.function;
2137 else
2138 event = tev->point.symbol;
2139 if (pev->group)
2140 group = pev->group;
2141 else
2142 group = PERFPROBE_GROUP;
2143
2144 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002145 ret = get_new_event_name(buf, 64, event,
2146 namelist, allow_suffix);
2147 if (ret < 0)
2148 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002149 event = buf;
2150
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002151 tev->event = strdup(event);
2152 tev->group = strdup(group);
2153 if (tev->event == NULL || tev->group == NULL) {
2154 ret = -ENOMEM;
2155 break;
2156 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302157 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002158 if (ret < 0)
2159 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002160 /* Add added event name to namelist */
2161 strlist__add(namelist, event);
2162
2163 /* Trick here - save current event/group */
2164 event = pev->event;
2165 group = pev->group;
2166 pev->event = tev->event;
2167 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002168 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002169 /* Trick here - restore current event/group */
2170 pev->event = (char *)event;
2171 pev->group = (char *)group;
2172
2173 /*
2174 * Probes after the first probe which comes from same
2175 * user input are always allowed to add suffix, because
2176 * there might be several addresses corresponding to
2177 * one code line.
2178 */
2179 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002180 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002181
2182 if (ret >= 0) {
2183 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002184 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2185 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002186 tev->event);
2187 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002188
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002189 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002190 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002191 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002192}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002193
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002194static char *looking_function_name;
2195static int num_matched_functions;
2196
2197static int probe_function_filter(struct map *map __maybe_unused,
2198 struct symbol *sym)
2199{
2200 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2201 strcmp(looking_function_name, sym->name) == 0) {
2202 num_matched_functions++;
2203 return 0;
2204 }
2205 return 1;
2206}
2207
2208#define strdup_or_goto(str, label) \
2209 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2210
2211/*
2212 * Find probe function addresses from map.
2213 * Return an error or the number of found probe_trace_event
2214 */
2215static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2216 struct probe_trace_event **tevs,
2217 int max_tevs, const char *target)
2218{
2219 struct map *map = NULL;
2220 struct kmap *kmap = NULL;
2221 struct ref_reloc_sym *reloc_sym = NULL;
2222 struct symbol *sym;
2223 struct rb_node *nd;
2224 struct probe_trace_event *tev;
2225 struct perf_probe_point *pp = &pev->point;
2226 struct probe_trace_point *tp;
2227 int ret, i;
2228
2229 /* Init maps of given executable or kernel */
2230 if (pev->uprobes)
2231 map = dso__new_map(target);
2232 else
2233 map = kernel_get_module_map(target);
2234 if (!map) {
2235 ret = -EINVAL;
2236 goto out;
2237 }
2238
2239 /*
2240 * Load matched symbols: Since the different local symbols may have
2241 * same name but different addresses, this lists all the symbols.
2242 */
2243 num_matched_functions = 0;
2244 looking_function_name = pp->function;
2245 ret = map__load(map, probe_function_filter);
2246 if (ret || num_matched_functions == 0) {
2247 pr_err("Failed to find symbol %s in %s\n", pp->function,
2248 target ? : "kernel");
2249 ret = -ENOENT;
2250 goto out;
2251 } else if (num_matched_functions > max_tevs) {
2252 pr_err("Too many functions matched in %s\n",
2253 target ? : "kernel");
2254 ret = -E2BIG;
2255 goto out;
2256 }
2257
2258 if (!pev->uprobes) {
2259 kmap = map__kmap(map);
2260 reloc_sym = kmap->ref_reloc_sym;
2261 if (!reloc_sym) {
2262 pr_warning("Relocated base symbol is not found!\n");
2263 ret = -EINVAL;
2264 goto out;
2265 }
2266 }
2267
2268 /* Setup result trace-probe-events */
2269 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2270 if (!*tevs) {
2271 ret = -ENOMEM;
2272 goto out;
2273 }
2274
2275 ret = 0;
2276 map__for_each_symbol(map, sym, nd) {
2277 tev = (*tevs) + ret;
2278 tp = &tev->point;
2279 if (ret == num_matched_functions) {
2280 pr_warning("Too many symbols are listed. Skip it.\n");
2281 break;
2282 }
2283 ret++;
2284
2285 if (pp->offset > sym->end - sym->start) {
2286 pr_warning("Offset %ld is bigger than the size of %s\n",
2287 pp->offset, sym->name);
2288 ret = -ENOENT;
2289 goto err_out;
2290 }
2291 /* Add one probe point */
2292 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2293 if (reloc_sym) {
2294 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2295 tp->offset = tp->address - reloc_sym->addr;
2296 } else {
2297 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2298 tp->offset = pp->offset;
2299 }
2300 tp->retprobe = pp->retprobe;
2301 if (target)
2302 tev->point.module = strdup_or_goto(target, nomem_out);
2303 tev->uprobes = pev->uprobes;
2304 tev->nargs = pev->nargs;
2305 if (tev->nargs) {
2306 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2307 tev->nargs);
2308 if (tev->args == NULL)
2309 goto nomem_out;
2310 }
2311 for (i = 0; i < tev->nargs; i++) {
2312 if (pev->args[i].name)
2313 tev->args[i].name =
2314 strdup_or_goto(pev->args[i].name,
2315 nomem_out);
2316
2317 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2318 nomem_out);
2319 if (pev->args[i].type)
2320 tev->args[i].type =
2321 strdup_or_goto(pev->args[i].type,
2322 nomem_out);
2323 }
2324 }
2325
2326out:
2327 if (map && pev->uprobes) {
2328 /* Only when using uprobe(exec) map needs to be released */
2329 dso__delete(map->dso);
2330 map__delete(map);
2331 }
2332 return ret;
2333
2334nomem_out:
2335 ret = -ENOMEM;
2336err_out:
2337 clear_probe_trace_events(*tevs, num_matched_functions);
2338 zfree(tevs);
2339 goto out;
2340}
2341
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302342static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2343 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302344 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002345{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002346 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002347
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002348 if (pev->uprobes && !pev->group) {
2349 /* Replace group name if not given */
2350 ret = convert_exec_to_group(target, &pev->group);
2351 if (ret != 0) {
2352 pr_warning("Failed to make a group name.\n");
2353 return ret;
2354 }
2355 }
2356
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002357 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302358 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002359 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002360 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002361
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002362 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002363}
2364
2365struct __event_package {
2366 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302367 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002368 int ntevs;
2369};
2370
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002371int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302372 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002373{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002374 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002375 struct __event_package *pkgs;
2376
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302377 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002378 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302379
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002380 if (pkgs == NULL)
2381 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002382
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002383 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002384 if (ret < 0) {
2385 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002386 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002387 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002388
2389 /* Loop 1: convert all events */
2390 for (i = 0; i < npevs; i++) {
2391 pkgs[i].pev = &pevs[i];
2392 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302393 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002394 &pkgs[i].tevs,
2395 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302396 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 if (ret < 0)
2398 goto end;
2399 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002400 }
2401
2402 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002403 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302404 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002405 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002406 if (ret < 0)
2407 break;
2408 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002409end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002410 /* Loop 3: cleanup and free trace events */
2411 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002412 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302413 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002414 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002415 }
2416 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002417 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002418
2419 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002420}
2421
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302422static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002423{
2424 char *p;
2425 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002426 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002427
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302428 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002429 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2430 if (ret < 0)
2431 goto error;
2432
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002433 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002434 if (!p) {
2435 pr_debug("Internal error: %s should have ':' but not.\n",
2436 ent->s);
2437 ret = -ENOTSUP;
2438 goto error;
2439 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002440 *p = '/';
2441
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002442 pr_debug("Writing event: %s\n", buf);
2443 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002444 if (ret < 0) {
2445 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002446 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002447 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002448
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002449 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002450 return 0;
2451error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002452 pr_warning("Failed to delete event: %s\n",
2453 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002454 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002455}
2456
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302457static int del_trace_probe_event(int fd, const char *buf,
2458 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002459{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002460 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302461 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002462
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002463 if (strpbrk(buf, "*?")) { /* Glob-exp */
2464 strlist__for_each_safe(ent, n, namelist)
2465 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302466 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002467 if (ret < 0)
2468 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002469 strlist__remove(namelist, ent);
2470 }
2471 } else {
2472 ent = strlist__find(namelist, buf);
2473 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302474 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002475 if (ret >= 0)
2476 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002477 }
2478 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002479
2480 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002481}
2482
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002483int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002484{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302485 int ret = -1, ufd = -1, kfd = -1;
2486 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002487 const char *group, *event;
2488 char *p, *str;
2489 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302490 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002491
Masami Hiramatsufa282442009-12-08 17:03:23 -05002492 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302493 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002494 if (kfd >= 0)
2495 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302496
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302497 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002498 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302499 unamelist = get_probe_trace_event_names(ufd, true);
2500
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002501 if (kfd < 0 && ufd < 0) {
2502 print_both_open_warning(kfd, ufd);
2503 goto error;
2504 }
2505
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302506 if (namelist == NULL && unamelist == NULL)
2507 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002508
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002509 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002510 str = strdup(ent->s);
2511 if (str == NULL) {
2512 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302513 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002514 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002515 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002516 p = strchr(str, ':');
2517 if (p) {
2518 group = str;
2519 *p = '\0';
2520 event = p + 1;
2521 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002522 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002523 event = str;
2524 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302525
2526 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2527 if (ret < 0) {
2528 pr_err("Failed to copy event.");
2529 free(str);
2530 goto error;
2531 }
2532
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002533 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302534
2535 if (namelist)
2536 ret = del_trace_probe_event(kfd, buf, namelist);
2537
2538 if (unamelist && ret != 0)
2539 ret = del_trace_probe_event(ufd, buf, unamelist);
2540
2541 if (ret != 0)
2542 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2543
Masami Hiramatsufa282442009-12-08 17:03:23 -05002544 free(str);
2545 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302546
2547error:
2548 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302549 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302550 close(kfd);
2551 }
2552
2553 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302554 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302555 close(ufd);
2556 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002557
2558 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002559}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302560
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002561/* TODO: don't use a global variable for filter ... */
2562static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002563
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002564/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002565 * If a symbol corresponds to a function with global binding and
2566 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002567 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002568static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002569 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002570{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002571 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002572 strfilter__compare(available_func_filter, sym->name))
2573 return 0;
2574 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002575}
2576
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002577int show_available_funcs(const char *target, struct strfilter *_filter,
2578 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002579{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002580 struct map *map;
2581 int ret;
2582
2583 ret = init_symbol_maps(user);
2584 if (ret < 0)
2585 return ret;
2586
2587 /* Get a symbol map */
2588 if (user)
2589 map = dso__new_map(target);
2590 else
2591 map = kernel_get_module_map(target);
2592 if (!map) {
2593 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002594 return -EINVAL;
2595 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002596
2597 /* Load symbols with given filter */
2598 available_func_filter = _filter;
2599 if (map__load(map, filter_available_functions)) {
2600 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2601 goto end;
2602 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002603 if (!dso__sorted_by_name(map->dso, map->type))
2604 dso__sort_by_name(map->dso, map->type);
2605
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002606 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302607 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002608 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2609end:
2610 if (user) {
2611 dso__delete(map->dso);
2612 map__delete(map);
2613 }
2614 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302615
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002616 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302617}
2618