blob: 9dfbed96bf397c81268f32c88b52e6d19c6253fb [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu146a1432010-04-12 13:17:42 -040055#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050056
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030073static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000074static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000075static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000078static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090083 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040084 if (ret < 0) {
85 pr_debug("Failed to init symbol map.\n");
86 goto out;
87 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (host_machine || user_only) /* already initialized */
90 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030091
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000092 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94
95 host_machine = machine__new_host();
96 if (!host_machine) {
97 pr_debug("machine__new_host() failed.\n");
98 symbol__exit();
99 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107static void exit_symbol_maps(void)
108{
109 if (host_machine) {
110 machine__delete(host_machine);
111 host_machine = NULL;
112 }
113 symbol__exit();
114}
115
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900116static struct symbol *__find_kernel_function_by_name(const char *name,
117 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400118{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000119 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120 NULL);
121}
122
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000123static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
124{
125 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
126}
127
128static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
129{
130 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 struct kmap *kmap;
132
133 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
134 return NULL;
135
136 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
154 (reloc) ? 0 : map->reloc;
155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
161 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000162 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000166 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
171 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
172 struct map *pos = rb_entry(nd, struct map, rb_node);
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
181static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900182{
183 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100184 struct map *map;
185 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900186
187 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400188 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
189 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900190 if (strncmp(dso->short_name + 1, module,
191 dso->short_name_len - 2) == 0)
192 goto found;
193 }
194 pr_debug("Failed to find module %s.\n", module);
195 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100196 }
197
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000198 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100199 dso = map->dso;
200
201 vmlinux_name = symbol_conf.vmlinux_name;
202 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300203 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100204 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900205 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100206 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900207 pr_debug("Failed to load kernel map.\n");
208 return NULL;
209 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400210 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900211found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900212 return dso;
213}
214
215const char *kernel_get_module_path(const char *module)
216{
217 struct dso *dso = kernel_get_module_dso(module);
218 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900219}
220
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000221static int convert_exec_to_group(const char *exec, char **result)
222{
223 char *ptr1, *ptr2, *exec_copy;
224 char buf[64];
225 int ret;
226
227 exec_copy = strdup(exec);
228 if (!exec_copy)
229 return -ENOMEM;
230
231 ptr1 = basename(exec_copy);
232 if (!ptr1) {
233 ret = -EINVAL;
234 goto out;
235 }
236
237 ptr2 = strpbrk(ptr1, "-._");
238 if (ptr2)
239 *ptr2 = '\0';
240 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
241 if (ret < 0)
242 goto out;
243
244 *result = strdup(buf);
245 ret = *result ? 0 : -ENOMEM;
246
247out:
248 free(exec_copy);
249 return ret;
250}
251
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000252static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
253{
254 int i;
255
256 for (i = 0; i < ntevs; i++)
257 clear_probe_trace_event(tevs + i);
258}
259
Ingo Molnar89fe8082013-09-30 12:07:11 +0200260#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000261
Masami Hiramatsuff741782011-06-27 16:27:39 +0900262/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000263static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900264{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000265 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000266 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900267
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000268 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900269 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900270 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000271 if (!silent)
272 pr_err("Failed to find path of %s module.\n",
273 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900274 return NULL;
275 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900276 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000277 ret = debuginfo__new(path);
278 if (!ret && !silent) {
279 pr_warning("The %s file has no debug information.\n", path);
280 if (!module || !strtailcmp(path, ".ko"))
281 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
282 else
283 pr_warning("Rebuild with -g, ");
284 pr_warning("or install an appropriate debuginfo package.\n");
285 }
286 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400287}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300288
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000289
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000290static int get_text_start_address(const char *exec, unsigned long *address)
291{
292 Elf *elf;
293 GElf_Ehdr ehdr;
294 GElf_Shdr shdr;
295 int fd, ret = -ENOENT;
296
297 fd = open(exec, O_RDONLY);
298 if (fd < 0)
299 return -errno;
300
301 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
302 if (elf == NULL)
303 return -EINVAL;
304
305 if (gelf_getehdr(elf, &ehdr) == NULL)
306 goto out;
307
308 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
309 goto out;
310
311 *address = shdr.sh_addr - shdr.sh_offset;
312 ret = 0;
313out:
314 elf_end(elf);
315 return ret;
316}
317
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000318/*
319 * Convert trace point to probe point with debuginfo
320 */
321static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
322 struct perf_probe_point *pp,
323 bool is_kprobe)
324{
325 struct debuginfo *dinfo = NULL;
326 unsigned long stext = 0;
327 u64 addr = tp->address;
328 int ret = -ENOENT;
329
330 /* convert the address to dwarf address */
331 if (!is_kprobe) {
332 if (!addr) {
333 ret = -EINVAL;
334 goto error;
335 }
336 ret = get_text_start_address(tp->module, &stext);
337 if (ret < 0)
338 goto error;
339 addr += stext;
340 } else {
341 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
342 if (addr == 0)
343 goto error;
344 addr += tp->offset;
345 }
346
347 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
348 tp->module ? : "kernel");
349
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000350 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000351 if (dinfo) {
352 ret = debuginfo__find_probe_point(dinfo,
353 (unsigned long)addr, pp);
354 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000355 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000356 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000357
358 if (ret > 0) {
359 pp->retprobe = tp->retprobe;
360 return 0;
361 }
362error:
363 pr_debug("Failed to find corresponding probes from debuginfo.\n");
364 return ret ? : -ENOENT;
365}
366
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000367static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
368 int ntevs, const char *exec)
369{
370 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000371 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000372
373 if (!exec)
374 return 0;
375
376 ret = get_text_start_address(exec, &stext);
377 if (ret < 0)
378 return ret;
379
380 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000381 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000382 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000383 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000384 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000385 ret = -ENOMEM;
386 break;
387 }
388 tevs[i].uprobes = true;
389 }
390
391 return ret;
392}
393
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900394static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
395 int ntevs, const char *module)
396{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900397 int i, ret = 0;
398 char *tmp;
399
400 if (!module)
401 return 0;
402
403 tmp = strrchr(module, '/');
404 if (tmp) {
405 /* This is a module path -- get the module name */
406 module = strdup(tmp + 1);
407 if (!module)
408 return -ENOMEM;
409 tmp = strchr(module, '.');
410 if (tmp)
411 *tmp = '\0';
412 tmp = (char *)module; /* For free() */
413 }
414
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900415 for (i = 0; i < ntevs; i++) {
416 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900417 if (!tevs[i].point.module) {
418 ret = -ENOMEM;
419 break;
420 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900421 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900422
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300423 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900424 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900425}
426
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000427/* Post processing the probe events */
428static int post_process_probe_trace_events(struct probe_trace_event *tevs,
429 int ntevs, const char *module,
430 bool uprobe)
431{
432 struct ref_reloc_sym *reloc_sym;
433 char *tmp;
434 int i;
435
436 if (uprobe)
437 return add_exec_to_probe_trace_events(tevs, ntevs, module);
438
439 /* Note that currently ref_reloc_sym based probe is not for drivers */
440 if (module)
441 return add_module_to_probe_trace_events(tevs, ntevs, module);
442
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000443 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000444 if (!reloc_sym) {
445 pr_warning("Relocated base symbol is not found!\n");
446 return -EINVAL;
447 }
448
449 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900450 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000451 tmp = strdup(reloc_sym->name);
452 if (!tmp)
453 return -ENOMEM;
454 free(tevs[i].point.symbol);
455 tevs[i].point.symbol = tmp;
456 tevs[i].point.offset = tevs[i].point.address -
457 reloc_sym->unrelocated_addr;
458 }
459 }
460 return 0;
461}
462
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300463/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530464static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900465 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530466 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300467{
468 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530469 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900470 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300471
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000472 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530473
Masami Hiramatsuff741782011-06-27 16:27:39 +0900474 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000475 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900476 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900477 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300478 return 0;
479 }
480
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000481 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900482 /* Searching trace events corresponding to a probe event */
483 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
484
485 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300486
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400487 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000488 pr_debug("Found %d probe_trace_events.\n", ntevs);
489 ret = post_process_probe_trace_events(*tevs, ntevs,
490 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000491 if (ret < 0) {
492 clear_probe_trace_events(*tevs, ntevs);
493 zfree(tevs);
494 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900495 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400496 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300497
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400498 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900499 pr_warning("Probe point '%s' not found in debuginfo.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400500 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900501 if (need_dwarf)
502 return -ENOENT;
503 return 0;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400504 }
505 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400506 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
507 if (ntevs == -EBADF) {
508 pr_warning("Warning: No dwarf info found in the vmlinux - "
509 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
510 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900511 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400512 return 0;
513 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300514 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400515 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300516}
517
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900518/*
519 * Find a src file from a DWARF tag path. Prepend optional source path prefix
520 * and chop off leading directories that do not exist. Result is passed back as
521 * a newly allocated path on success.
522 * Return 0 if file was found and readable, -errno otherwise.
523 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900524static int get_real_path(const char *raw_path, const char *comp_dir,
525 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900526{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900527 const char *prefix = symbol_conf.source_prefix;
528
529 if (!prefix) {
530 if (raw_path[0] != '/' && comp_dir)
531 /* If not an absolute path, try to use comp_dir */
532 prefix = comp_dir;
533 else {
534 if (access(raw_path, R_OK) == 0) {
535 *new_path = strdup(raw_path);
536 return 0;
537 } else
538 return -errno;
539 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900540 }
541
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900542 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900543 if (!*new_path)
544 return -ENOMEM;
545
546 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900547 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900548
549 if (access(*new_path, R_OK) == 0)
550 return 0;
551
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900552 if (!symbol_conf.source_prefix)
553 /* In case of searching comp_dir, don't retry */
554 return -errno;
555
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900556 switch (errno) {
557 case ENAMETOOLONG:
558 case ENOENT:
559 case EROFS:
560 case EFAULT:
561 raw_path = strchr(++raw_path, '/');
562 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300563 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900564 return -ENOENT;
565 }
566 continue;
567
568 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300569 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900570 return -errno;
571 }
572 }
573}
574
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300575#define LINEBUF_SIZE 256
576#define NR_ADDITIONAL_LINES 2
577
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100578static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300579{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000580 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100581 const char *color = show_num ? "" : PERF_COLOR_BLUE;
582 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300583
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100584 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300585 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
586 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100587 if (skip)
588 continue;
589 if (!prefix) {
590 prefix = show_num ? "%7d " : " ";
591 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300592 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100593 color_fprintf(stdout, color, "%s", buf);
594
595 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400596
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100597 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300598error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100599 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000600 pr_warning("File read error: %s\n",
601 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100602 return -1;
603 }
604 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300605}
606
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100607static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
608{
609 int rv = __show_one_line(fp, l, skip, show_num);
610 if (rv == 0) {
611 pr_warning("Source file is shorter than expected.\n");
612 rv = -1;
613 }
614 return rv;
615}
616
617#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
618#define show_one_line(f,l) _show_one_line(f,l,false,false)
619#define skip_one_line(f,l) _show_one_line(f,l,true,false)
620#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
621
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300622/*
623 * Show line-range always requires debuginfo to find source file and
624 * line number.
625 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000626static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300627{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400628 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000629 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900630 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300631 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900632 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900633 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000634 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300635
636 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000637 dinfo = open_debuginfo(module, false);
638 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900639 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400640
Masami Hiramatsuff741782011-06-27 16:27:39 +0900641 ret = debuginfo__find_line_range(dinfo, lr);
642 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000643 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400644 pr_warning("Specified source line is not found.\n");
645 return -ENOENT;
646 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000647 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400648 return ret;
649 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300650
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900651 /* Convert source file path */
652 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900653 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900654 free(tmp); /* Free old path */
655 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000656 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900657 return ret;
658 }
659
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300660 setup_pager();
661
662 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900663 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300664 lr->start - lr->offset);
665 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100666 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300667
668 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400669 if (fp == NULL) {
670 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000671 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400672 return -errno;
673 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300674 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100675 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100676 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100677 if (ret < 0)
678 goto end;
679 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300680
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000681 intlist__for_each(ln, lr->line_list) {
682 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100683 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100684 if (ret < 0)
685 goto end;
686 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100687 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400688 if (ret < 0)
689 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300690 }
691
692 if (lr->end == INT_MAX)
693 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100694 while (l <= lr->end) {
695 ret = show_one_line_or_eof(fp, l++ - lr->offset);
696 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100697 break;
698 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400699end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300700 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400701 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300702}
703
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000704int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000705{
706 int ret;
707
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000708 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000709 if (ret < 0)
710 return ret;
711 ret = __show_line_range(lr, module);
712 exit_symbol_maps();
713
714 return ret;
715}
716
Masami Hiramatsuff741782011-06-27 16:27:39 +0900717static int show_available_vars_at(struct debuginfo *dinfo,
718 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900719 int max_vls, struct strfilter *_filter,
720 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900721{
722 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900723 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900724 struct str_node *node;
725 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900726 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900727
728 buf = synthesize_perf_probe_point(&pev->point);
729 if (!buf)
730 return -EINVAL;
731 pr_debug("Searching variables at %s\n", buf);
732
Masami Hiramatsuff741782011-06-27 16:27:39 +0900733 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
734 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900735 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000736 if (ret == 0 || ret == -ENOENT) {
737 pr_err("Failed to find the address of %s\n", buf);
738 ret = -ENOENT;
739 } else
740 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900741 goto end;
742 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000743
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900744 /* Some variables are found */
745 fprintf(stdout, "Available variables at %s\n", buf);
746 for (i = 0; i < ret; i++) {
747 vl = &vls[i];
748 /*
749 * A probe point might be converted to
750 * several trace points.
751 */
752 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
753 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300754 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900755 nvars = 0;
756 if (vl->vars) {
757 strlist__for_each(node, vl->vars) {
758 var = strchr(node->s, '\t') + 1;
759 if (strfilter__compare(_filter, var)) {
760 fprintf(stdout, "\t\t%s\n", node->s);
761 nvars++;
762 }
763 }
764 strlist__delete(vl->vars);
765 }
766 if (nvars == 0)
767 fprintf(stdout, "\t\t(No matched variables)\n");
768 }
769 free(vls);
770end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900771 free(buf);
772 return ret;
773}
774
775/* Show available variables on given probe point */
776int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900777 int max_vls, const char *module,
778 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900779{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900780 int i, ret = 0;
781 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900782
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000783 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900784 if (ret < 0)
785 return ret;
786
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000787 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900788 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000789 ret = -ENOENT;
790 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900791 }
792
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900793 setup_pager();
794
Masami Hiramatsuff741782011-06-27 16:27:39 +0900795 for (i = 0; i < npevs && ret >= 0; i++)
796 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900797 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900798
799 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000800out:
801 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900802 return ret;
803}
804
Ingo Molnar89fe8082013-09-30 12:07:11 +0200805#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000807static int
808find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
809 struct perf_probe_point *pp __maybe_unused,
810 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300811{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000812 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813}
814
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530815static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300816 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300817 int max_tevs __maybe_unused,
818 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300819{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400820 if (perf_probe_event_need_dwarf(pev)) {
821 pr_warning("Debuginfo-analysis is not supported.\n");
822 return -ENOSYS;
823 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530824
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300825 return 0;
826}
827
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300828int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000829 const char *module __maybe_unused,
830 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400832 pr_warning("Debuginfo-analysis is not supported.\n");
833 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300834}
835
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300836int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
837 int npevs __maybe_unused, int max_vls __maybe_unused,
838 const char *module __maybe_unused,
839 struct strfilter *filter __maybe_unused,
840 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900841{
842 pr_warning("Debuginfo-analysis is not supported.\n");
843 return -ENOSYS;
844}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400845#endif
846
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000847void line_range__clear(struct line_range *lr)
848{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000849 free(lr->function);
850 free(lr->file);
851 free(lr->path);
852 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000853 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000854 memset(lr, 0, sizeof(*lr));
855}
856
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000857int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000858{
859 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000860 lr->line_list = intlist__new(NULL);
861 if (!lr->line_list)
862 return -ENOMEM;
863 else
864 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000865}
866
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100867static int parse_line_num(char **ptr, int *val, const char *what)
868{
869 const char *start = *ptr;
870
871 errno = 0;
872 *val = strtol(*ptr, ptr, 0);
873 if (errno || *ptr == start) {
874 semantic_error("'%s' is not a valid number.\n", what);
875 return -EINVAL;
876 }
877 return 0;
878}
879
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100880/*
881 * Stuff 'lr' according to the line range described by 'arg'.
882 * The line range syntax is described by:
883 *
884 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900885 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100886 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400887int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500888{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900889 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100890 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100891
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100892 if (!name)
893 return -ENOMEM;
894
895 lr->start = 0;
896 lr->end = INT_MAX;
897
898 range = strchr(name, ':');
899 if (range) {
900 *range++ = '\0';
901
902 err = parse_line_num(&range, &lr->start, "start line");
903 if (err)
904 goto err;
905
906 if (*range == '+' || *range == '-') {
907 const char c = *range++;
908
909 err = parse_line_num(&range, &lr->end, "end line");
910 if (err)
911 goto err;
912
913 if (c == '+') {
914 lr->end += lr->start;
915 /*
916 * Adjust the number of lines here.
917 * If the number of lines == 1, the
918 * the end of line should be equal to
919 * the start of line.
920 */
921 lr->end--;
922 }
923 }
924
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400925 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100926
927 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400928 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500929 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400930 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100931 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400932 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100933 if (*range != '\0') {
934 semantic_error("Tailing with invalid str '%s'.\n", range);
935 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400936 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400937 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400938
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900939 file = strchr(name, '@');
940 if (file) {
941 *file = '\0';
942 lr->file = strdup(++file);
943 if (lr->file == NULL) {
944 err = -ENOMEM;
945 goto err;
946 }
947 lr->function = name;
948 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100949 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500950 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100951 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400952
953 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100954err:
955 free(name);
956 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500957}
958
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500959/* Check the name is good for event/group */
960static bool check_event_name(const char *name)
961{
962 if (!isalpha(*name) && *name != '_')
963 return false;
964 while (*++name != '\0') {
965 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
966 return false;
967 }
968 return true;
969}
970
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500971/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500973{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500975 char *ptr, *tmp;
976 char c, nc = 0;
977 /*
978 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500979 * perf probe [EVENT=]SRC[:LN|;PTN]
980 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500981 *
982 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500983 */
984
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500985 ptr = strpbrk(arg, ";=@+%");
986 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500987 *ptr = '\0';
988 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400989 if (strchr(arg, ':')) {
990 semantic_error("Group name is not supported yet.\n");
991 return -ENOTSUP;
992 }
993 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500994 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400995 "follow C symbol-naming rule.\n", arg);
996 return -EINVAL;
997 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400998 pev->event = strdup(arg);
999 if (pev->event == NULL)
1000 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001001 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001002 arg = tmp;
1003 }
1004
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001005 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001006 if (ptr) {
1007 nc = *ptr;
1008 *ptr++ = '\0';
1009 }
1010
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001011 tmp = strdup(arg);
1012 if (tmp == NULL)
1013 return -ENOMEM;
1014
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001015 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001016 if (strchr(tmp, '.')) /* File */
1017 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001018 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001019 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001020
1021 /* Parse other options */
1022 while (ptr) {
1023 arg = ptr;
1024 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001025 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001026 pp->lazy_line = strdup(arg);
1027 if (pp->lazy_line == NULL)
1028 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001029 break;
1030 }
1031 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001032 if (ptr) {
1033 nc = *ptr;
1034 *ptr++ = '\0';
1035 }
1036 switch (c) {
1037 case ':': /* Line number */
1038 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001039 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001040 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001041 " in line number.\n");
1042 return -EINVAL;
1043 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001044 break;
1045 case '+': /* Byte offset from a symbol */
1046 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001047 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001048 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 " in offset.\n");
1050 return -EINVAL;
1051 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001052 break;
1053 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001054 if (pp->file) {
1055 semantic_error("SRC@SRC is not allowed.\n");
1056 return -EINVAL;
1057 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001058 pp->file = strdup(arg);
1059 if (pp->file == NULL)
1060 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001061 break;
1062 case '%': /* Probe places */
1063 if (strcmp(arg, "return") == 0) {
1064 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001065 } else { /* Others not supported yet */
1066 semantic_error("%%%s is not supported.\n", arg);
1067 return -ENOTSUP;
1068 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001069 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001070 default: /* Buggy case */
1071 pr_err("This program has a bug at %s:%d.\n",
1072 __FILE__, __LINE__);
1073 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001074 break;
1075 }
1076 }
1077
1078 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001079 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001080 semantic_error("Lazy pattern can't be used with"
1081 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001082 return -EINVAL;
1083 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001084
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001086 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 return -EINVAL;
1088 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001089
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001090 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001091 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 return -EINVAL;
1093 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001094
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001095 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001096 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001097 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001098 return -EINVAL;
1099 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001100
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001101 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001102 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 return -EINVAL;
1104 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001105
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001107 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 return -EINVAL;
1109 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001110
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001111 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001112 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001113 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001114 return -EINVAL;
1115 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001116
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001117 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001118 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1119 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001120 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001121}
1122
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001123/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001124static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001125{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001126 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001127 struct perf_probe_arg_field **fieldp;
1128
1129 pr_debug("parsing arg: %s into ", str);
1130
Masami Hiramatsu48481932010-04-12 13:16:53 -04001131 tmp = strchr(str, '=');
1132 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001133 arg->name = strndup(str, tmp - str);
1134 if (arg->name == NULL)
1135 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001136 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001137 str = tmp + 1;
1138 }
1139
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001140 tmp = strchr(str, ':');
1141 if (tmp) { /* Type setting */
1142 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001143 arg->type = strdup(tmp + 1);
1144 if (arg->type == NULL)
1145 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001146 pr_debug("type:%s ", arg->type);
1147 }
1148
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001149 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001150 if (!is_c_varname(str) || !tmp) {
1151 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001152 arg->var = strdup(str);
1153 if (arg->var == NULL)
1154 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001155 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001156 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001157 }
1158
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001159 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001160 arg->var = strndup(str, tmp - str);
1161 if (arg->var == NULL)
1162 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001163 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001164 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001165 fieldp = &arg->field;
1166
1167 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001168 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1169 if (*fieldp == NULL)
1170 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001171 if (*tmp == '[') { /* Array */
1172 str = tmp;
1173 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001174 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001175 if (*tmp != ']' || tmp == str + 1) {
1176 semantic_error("Array index must be a"
1177 " number.\n");
1178 return -EINVAL;
1179 }
1180 tmp++;
1181 if (*tmp == '\0')
1182 tmp = NULL;
1183 } else { /* Structure */
1184 if (*tmp == '.') {
1185 str = tmp + 1;
1186 (*fieldp)->ref = false;
1187 } else if (tmp[1] == '>') {
1188 str = tmp + 2;
1189 (*fieldp)->ref = true;
1190 } else {
1191 semantic_error("Argument parse error: %s\n",
1192 str);
1193 return -EINVAL;
1194 }
1195 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001197 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001198 (*fieldp)->name = strndup(str, tmp - str);
1199 if ((*fieldp)->name == NULL)
1200 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001201 if (*str != '[')
1202 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001203 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1204 fieldp = &(*fieldp)->next;
1205 }
1206 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001207 (*fieldp)->name = strdup(str);
1208 if ((*fieldp)->name == NULL)
1209 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001210 if (*str != '[')
1211 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001212 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001213
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001214 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001215 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001216 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001217 if (arg->name == NULL)
1218 return -ENOMEM;
1219 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001221}
1222
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001223/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001225{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001226 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001228
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001229 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 if (!argv) {
1231 pr_debug("Failed to split arguments.\n");
1232 return -ENOMEM;
1233 }
1234 if (argc - 1 > MAX_PROBE_ARGS) {
1235 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1236 ret = -ERANGE;
1237 goto out;
1238 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001239 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001240 ret = parse_perf_probe_point(argv[0], pev);
1241 if (ret < 0)
1242 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001243
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001244 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001245 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001246 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1247 if (pev->args == NULL) {
1248 ret = -ENOMEM;
1249 goto out;
1250 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001251 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1252 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1253 if (ret >= 0 &&
1254 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001255 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 " kretprobe.\n");
1257 ret = -EINVAL;
1258 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001259 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001261 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001262
1263 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001264}
1265
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001266/* Return true if this perf_probe_event requires debuginfo */
1267bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001268{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 int i;
1270
1271 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1272 return true;
1273
1274 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001275 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001276 return true;
1277
1278 return false;
1279}
1280
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301281/* Parse probe_events event into struct probe_point */
1282static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001283 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301285 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001286 char pr;
1287 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001288 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001289 int ret, i, argc;
1290 char **argv;
1291
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301292 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001293 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001294 if (!argv) {
1295 pr_debug("Failed to split arguments.\n");
1296 return -ENOMEM;
1297 }
1298 if (argc < 2) {
1299 semantic_error("Too few probe arguments.\n");
1300 ret = -ERANGE;
1301 goto out;
1302 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001303
1304 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001305 argv0_str = strdup(argv[0]);
1306 if (argv0_str == NULL) {
1307 ret = -ENOMEM;
1308 goto out;
1309 }
1310 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1311 fmt2_str = strtok_r(NULL, "/", &fmt);
1312 fmt3_str = strtok_r(NULL, " \t", &fmt);
1313 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1314 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001315 semantic_error("Failed to parse event name: %s\n", argv[0]);
1316 ret = -EINVAL;
1317 goto out;
1318 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001319 pr = fmt1_str[0];
1320 tev->group = strdup(fmt2_str);
1321 tev->event = strdup(fmt3_str);
1322 if (tev->group == NULL || tev->event == NULL) {
1323 ret = -ENOMEM;
1324 goto out;
1325 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001327
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001328 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001329
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001330 /* Scan module name(if there), function name and offset */
1331 p = strchr(argv[1], ':');
1332 if (p) {
1333 tp->module = strndup(argv[1], p - argv[1]);
1334 p++;
1335 } else
1336 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001337 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001338 if (fmt1_str[0] == '0') /* only the address started with 0x */
1339 tp->address = strtoul(fmt1_str, NULL, 0);
1340 else {
1341 /* Only the symbol-based probe has offset */
1342 tp->symbol = strdup(fmt1_str);
1343 if (tp->symbol == NULL) {
1344 ret = -ENOMEM;
1345 goto out;
1346 }
1347 fmt2_str = strtok_r(NULL, "", &fmt);
1348 if (fmt2_str == NULL)
1349 tp->offset = 0;
1350 else
1351 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001352 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001353
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001354 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301355 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001356 if (tev->args == NULL) {
1357 ret = -ENOMEM;
1358 goto out;
1359 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001360 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001361 p = strchr(argv[i + 2], '=');
1362 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001363 *p++ = '\0';
1364 else
1365 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001367 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001368 tev->args[i].value = strdup(p);
1369 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1370 ret = -ENOMEM;
1371 goto out;
1372 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001373 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001374 ret = 0;
1375out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001376 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001377 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001378 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001379}
1380
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001381/* Compose only probe arg */
1382int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1383{
1384 struct perf_probe_arg_field *field = pa->field;
1385 int ret;
1386 char *tmp = buf;
1387
Masami Hiramatsu48481932010-04-12 13:16:53 -04001388 if (pa->name && pa->var)
1389 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1390 else
1391 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001392 if (ret <= 0)
1393 goto error;
1394 tmp += ret;
1395 len -= ret;
1396
1397 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001398 if (field->name[0] == '[')
1399 ret = e_snprintf(tmp, len, "%s", field->name);
1400 else
1401 ret = e_snprintf(tmp, len, "%s%s",
1402 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001403 if (ret <= 0)
1404 goto error;
1405 tmp += ret;
1406 len -= ret;
1407 field = field->next;
1408 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001409
1410 if (pa->type) {
1411 ret = e_snprintf(tmp, len, ":%s", pa->type);
1412 if (ret <= 0)
1413 goto error;
1414 tmp += ret;
1415 len -= ret;
1416 }
1417
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001418 return tmp - buf;
1419error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001420 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001421 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001422}
1423
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001424/* Compose only probe point (not argument) */
1425static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001426{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001427 char *buf, *tmp;
1428 char offs[32] = "", line[32] = "", file[32] = "";
1429 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001430
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001431 buf = zalloc(MAX_CMDLEN);
1432 if (buf == NULL) {
1433 ret = -ENOMEM;
1434 goto error;
1435 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001436 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001437 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 if (ret <= 0)
1439 goto error;
1440 }
1441 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001442 ret = e_snprintf(line, 32, ":%d", pp->line);
1443 if (ret <= 0)
1444 goto error;
1445 }
1446 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001447 tmp = pp->file;
1448 len = strlen(tmp);
1449 if (len > 30) {
1450 tmp = strchr(pp->file + len - 30, '/');
1451 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1452 }
1453 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001454 if (ret <= 0)
1455 goto error;
1456 }
1457
1458 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001459 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1460 offs, pp->retprobe ? "%return" : "", line,
1461 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001462 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001463 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001464 if (ret <= 0)
1465 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001466
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001467 return buf;
1468error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001469 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001470 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001471 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472}
1473
1474#if 0
1475char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1476{
1477 char *buf;
1478 int i, len, ret;
1479
1480 buf = synthesize_perf_probe_point(&pev->point);
1481 if (!buf)
1482 return NULL;
1483
1484 len = strlen(buf);
1485 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001486 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001487 pev->args[i].name);
1488 if (ret <= 0) {
1489 free(buf);
1490 return NULL;
1491 }
1492 len += ret;
1493 }
1494
1495 return buf;
1496}
1497#endif
1498
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301499static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001500 char **buf, size_t *buflen,
1501 int depth)
1502{
1503 int ret;
1504 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301505 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001506 buflen, depth + 1);
1507 if (depth < 0)
1508 goto out;
1509 }
1510
1511 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1512 if (ret < 0)
1513 depth = ret;
1514 else {
1515 *buf += ret;
1516 *buflen -= ret;
1517 }
1518out:
1519 return depth;
1520
1521}
1522
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301523static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001524 char *buf, size_t buflen)
1525{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301526 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001527 int ret, depth = 0;
1528 char *tmp = buf;
1529
1530 /* Argument name or separator */
1531 if (arg->name)
1532 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1533 else
1534 ret = e_snprintf(buf, buflen, " ");
1535 if (ret < 0)
1536 return ret;
1537 buf += ret;
1538 buflen -= ret;
1539
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001540 /* Special case: @XXX */
1541 if (arg->value[0] == '@' && arg->ref)
1542 ref = ref->next;
1543
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001545 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301546 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001547 &buflen, 1);
1548 if (depth < 0)
1549 return depth;
1550 }
1551
1552 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001553 if (arg->value[0] == '@' && arg->ref)
1554 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1555 arg->ref->offset);
1556 else
1557 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001558 if (ret < 0)
1559 return ret;
1560 buf += ret;
1561 buflen -= ret;
1562
1563 /* Closing */
1564 while (depth--) {
1565 ret = e_snprintf(buf, buflen, ")");
1566 if (ret < 0)
1567 return ret;
1568 buf += ret;
1569 buflen -= ret;
1570 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001571 /* Print argument type */
1572 if (arg->type) {
1573 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1574 if (ret <= 0)
1575 return ret;
1576 buf += ret;
1577 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001578
1579 return buf - tmp;
1580}
1581
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301582char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001583{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301584 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001585 char *buf;
1586 int i, len, ret;
1587
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001588 buf = zalloc(MAX_CMDLEN);
1589 if (buf == NULL)
1590 return NULL;
1591
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001592 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1593 tev->group, tev->event);
1594 if (len <= 0)
1595 goto error;
1596
1597 /* Uprobes must have tp->address and tp->module */
1598 if (tev->uprobes && (!tp->address || !tp->module))
1599 goto error;
1600
1601 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301602 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001603 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1604 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301605 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001606 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301607 tp->module ?: "", tp->module ? ":" : "",
1608 tp->symbol, tp->offset);
1609
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001610 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001611 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001612 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613
1614 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301615 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001616 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001617 if (ret <= 0)
1618 goto error;
1619 len += ret;
1620 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001621
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001622 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001623error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001624 free(buf);
1625 return NULL;
1626}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001627
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001628static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1629 struct perf_probe_point *pp,
1630 bool is_kprobe)
1631{
1632 struct symbol *sym = NULL;
1633 struct map *map;
1634 u64 addr;
1635 int ret = -ENOENT;
1636
1637 if (!is_kprobe) {
1638 map = dso__new_map(tp->module);
1639 if (!map)
1640 goto out;
1641 addr = tp->address;
1642 sym = map__find_symbol(map, addr, NULL);
1643 } else {
1644 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1645 if (addr) {
1646 addr += tp->offset;
1647 sym = __find_kernel_function(addr, &map);
1648 }
1649 }
1650 if (!sym)
1651 goto out;
1652
1653 pp->retprobe = tp->retprobe;
1654 pp->offset = addr - map->unmap_ip(map, sym->start);
1655 pp->function = strdup(sym->name);
1656 ret = pp->function ? 0 : -ENOMEM;
1657
1658out:
1659 if (map && !is_kprobe) {
1660 dso__delete(map->dso);
1661 map__delete(map);
1662 }
1663
1664 return ret;
1665}
1666
1667static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1668 struct perf_probe_point *pp,
1669 bool is_kprobe)
1670{
1671 char buf[128];
1672 int ret;
1673
1674 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1675 if (!ret)
1676 return 0;
1677 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1678 if (!ret)
1679 return 0;
1680
1681 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1682
1683 if (tp->symbol) {
1684 pp->function = strdup(tp->symbol);
1685 pp->offset = tp->offset;
1686 } else if (!tp->module && !is_kprobe) {
1687 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1688 if (ret < 0)
1689 return ret;
1690 pp->function = strdup(buf);
1691 pp->offset = 0;
1692 }
1693 if (pp->function == NULL)
1694 return -ENOMEM;
1695
1696 pp->retprobe = tp->retprobe;
1697
1698 return 0;
1699}
1700
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301701static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301702 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001703{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001704 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001705 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001706
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001707 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001708 pev->event = strdup(tev->event);
1709 pev->group = strdup(tev->group);
1710 if (pev->event == NULL || pev->group == NULL)
1711 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001712
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001713 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001714 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001715 if (ret < 0)
1716 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001717
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718 /* Convert trace_arg to probe_arg */
1719 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001720 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1721 if (pev->args == NULL)
1722 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001723 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001724 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001725 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001726 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301727 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001728 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001729 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001730 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001731 if (pev->args[i].name == NULL && ret >= 0)
1732 ret = -ENOMEM;
1733 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001734
1735 if (ret < 0)
1736 clear_perf_probe_event(pev);
1737
1738 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001739}
1740
1741void clear_perf_probe_event(struct perf_probe_event *pev)
1742{
1743 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001744 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001745 int i;
1746
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001747 free(pev->event);
1748 free(pev->group);
1749 free(pp->file);
1750 free(pp->function);
1751 free(pp->lazy_line);
1752
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001753 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001754 free(pev->args[i].name);
1755 free(pev->args[i].var);
1756 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001757 field = pev->args[i].field;
1758 while (field) {
1759 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001760 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001761 free(field);
1762 field = next;
1763 }
1764 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001765 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766 memset(pev, 0, sizeof(*pev));
1767}
1768
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301769static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001770{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301771 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772 int i;
1773
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001774 free(tev->event);
1775 free(tev->group);
1776 free(tev->point.symbol);
1777 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001779 free(tev->args[i].name);
1780 free(tev->args[i].value);
1781 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001782 ref = tev->args[i].ref;
1783 while (ref) {
1784 next = ref->next;
1785 free(ref);
1786 ref = next;
1787 }
1788 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001789 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001790 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001791}
1792
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001793static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301794{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001795 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301796
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001797 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301798 const char *config;
1799
1800 if (!is_kprobe)
1801 config = "CONFIG_UPROBE_EVENTS";
1802 else
1803 config = "CONFIG_KPROBE_EVENTS";
1804
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001805 pr_warning("%cprobe_events file does not exist"
1806 " - please rebuild kernel with %s.\n",
1807 is_kprobe ? 'k' : 'u', config);
1808 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001809 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001810 else
1811 pr_warning("Failed to open %cprobe_events: %s\n",
1812 is_kprobe ? 'k' : 'u',
1813 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301814}
1815
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001816static void print_both_open_warning(int kerr, int uerr)
1817{
1818 /* Both kprobes and uprobes are disabled, warn it. */
1819 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001820 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001821 else if (kerr == -ENOENT && uerr == -ENOENT)
1822 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1823 "or/and CONFIG_UPROBE_EVENTS.\n");
1824 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001825 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001826 pr_warning("Failed to open kprobe events: %s.\n",
1827 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1828 pr_warning("Failed to open uprobe events: %s.\n",
1829 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1830 }
1831}
1832
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001833static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001834{
1835 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001836 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001837 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001838 int ret;
1839
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001840 __debugfs = tracefs_find_mountpoint();
1841 if (__debugfs == NULL) {
1842 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001843
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001844 __debugfs = debugfs_find_mountpoint();
1845 if (__debugfs == NULL)
1846 return -ENOTSUP;
1847 }
1848
1849 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1850 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001851 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001852 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001853 if (readwrite && !probe_event_dry_run)
1854 ret = open(buf, O_RDWR, O_APPEND);
1855 else
1856 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001857
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301858 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001859 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001860 }
1861 return ret;
1862}
1863
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301864static int open_kprobe_events(bool readwrite)
1865{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001866 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301867}
1868
1869static int open_uprobe_events(bool readwrite)
1870{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001871 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301872}
1873
1874/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301875static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001876{
1877 int ret, idx;
1878 FILE *fp;
1879 char buf[MAX_CMDLEN];
1880 char *p;
1881 struct strlist *sl;
1882
1883 sl = strlist__new(true, NULL);
1884
1885 fp = fdopen(dup(fd), "r");
1886 while (!feof(fp)) {
1887 p = fgets(buf, MAX_CMDLEN, fp);
1888 if (!p)
1889 break;
1890
1891 idx = strlen(p) - 1;
1892 if (p[idx] == '\n')
1893 p[idx] = '\0';
1894 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001896 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001897 strlist__delete(sl);
1898 return NULL;
1899 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001900 }
1901 fclose(fp);
1902
1903 return sl;
1904}
1905
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001906/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001907static int show_perf_probe_event(struct perf_probe_event *pev,
1908 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001909{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001910 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001911 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001913
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001914 /* Synthesize only event probe point */
1915 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001916 if (!place)
1917 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918
1919 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001920 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001921 return ret;
1922
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001923 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001924 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001925 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001926
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001928 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001929 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001930 ret = synthesize_perf_probe_arg(&pev->args[i],
1931 buf, 128);
1932 if (ret < 0)
1933 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001934 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001935 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001936 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04001937 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001938 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001939 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001940}
1941
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301942static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001943{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301944 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301945 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001946 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001947 struct strlist *rawlist;
1948 struct str_node *ent;
1949
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001950 memset(&tev, 0, sizeof(tev));
1951 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001952
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301953 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001954 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001955 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001956
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001957 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301958 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001959 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301960 ret = convert_to_perf_probe_event(&tev, &pev,
1961 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001962 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001963 ret = show_perf_probe_event(&pev,
1964 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001965 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001966 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301967 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001968 if (ret < 0)
1969 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001970 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001971 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001972
1973 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001974}
1975
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301976/* List up current perf-probe events */
1977int show_perf_probe_events(void)
1978{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001979 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301980
1981 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301982
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001983 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301984 if (ret < 0)
1985 return ret;
1986
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001987 kp_fd = open_kprobe_events(false);
1988 if (kp_fd >= 0) {
1989 ret = __show_perf_probe_events(kp_fd, true);
1990 close(kp_fd);
1991 if (ret < 0)
1992 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301993 }
1994
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001995 up_fd = open_uprobe_events(false);
1996 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001997 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001998 ret = kp_fd;
1999 goto out;
2000 }
2001
2002 if (up_fd >= 0) {
2003 ret = __show_perf_probe_events(up_fd, false);
2004 close(up_fd);
2005 }
2006out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002007 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302008 return ret;
2009}
2010
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002011/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302012static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002013{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002014 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002015 struct strlist *sl, *rawlist;
2016 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302017 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002018 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002019
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002020 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302021 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002022 if (!rawlist)
2023 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002024 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002025 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302026 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002027 if (ret < 0)
2028 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002029 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002030 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2031 tev.event);
2032 if (ret >= 0)
2033 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002034 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002035 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302036 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002037 if (ret < 0)
2038 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002039 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002040 strlist__delete(rawlist);
2041
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002042 if (ret < 0) {
2043 strlist__delete(sl);
2044 return NULL;
2045 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002046 return sl;
2047}
2048
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302049static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002050{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002051 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302052 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002053 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002054
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002055 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302056 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002057 return -EINVAL;
2058 }
2059
Masami Hiramatsufa282442009-12-08 17:03:23 -05002060 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002061 if (!probe_event_dry_run) {
2062 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002063 if (ret <= 0) {
2064 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002065 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002066 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002067 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002068 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002069 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002070 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002071}
2072
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002073static int get_new_event_name(char *buf, size_t len, const char *base,
2074 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002075{
2076 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002077
2078 /* Try no suffix */
2079 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002080 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002081 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002082 return ret;
2083 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002084 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002085 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002086
Masami Hiramatsud761b082009-12-15 10:32:25 -05002087 if (!allow_suffix) {
2088 pr_warning("Error: event \"%s\" already exists. "
2089 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002090 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002091 }
2092
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002093 /* Try to add suffix */
2094 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002095 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002096 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002097 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002098 return ret;
2099 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002100 if (!strlist__has_entry(namelist, buf))
2101 break;
2102 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002103 if (i == MAX_EVENT_INDEX) {
2104 pr_warning("Too many events are on the same function.\n");
2105 ret = -ERANGE;
2106 }
2107
2108 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002109}
2110
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302111static int __add_probe_trace_events(struct perf_probe_event *pev,
2112 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002113 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002114{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002115 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302116 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002117 char buf[64];
2118 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002119 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002120
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302121 if (pev->uprobes)
2122 fd = open_uprobe_events(true);
2123 else
2124 fd = open_kprobe_events(true);
2125
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002126 if (fd < 0) {
2127 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002129 }
2130
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002131 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302132 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002133 if (!namelist) {
2134 pr_debug("Failed to get current event list.\n");
2135 return -EIO;
2136 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002137
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002138 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002139 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002140 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002141 tev = &tevs[i];
2142 if (pev->event)
2143 event = pev->event;
2144 else
2145 if (pev->point.function)
2146 event = pev->point.function;
2147 else
2148 event = tev->point.symbol;
2149 if (pev->group)
2150 group = pev->group;
2151 else
2152 group = PERFPROBE_GROUP;
2153
2154 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002155 ret = get_new_event_name(buf, 64, event,
2156 namelist, allow_suffix);
2157 if (ret < 0)
2158 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002159 event = buf;
2160
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002161 tev->event = strdup(event);
2162 tev->group = strdup(group);
2163 if (tev->event == NULL || tev->group == NULL) {
2164 ret = -ENOMEM;
2165 break;
2166 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302167 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002168 if (ret < 0)
2169 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002170 /* Add added event name to namelist */
2171 strlist__add(namelist, event);
2172
2173 /* Trick here - save current event/group */
2174 event = pev->event;
2175 group = pev->group;
2176 pev->event = tev->event;
2177 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002178 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002179 /* Trick here - restore current event/group */
2180 pev->event = (char *)event;
2181 pev->group = (char *)group;
2182
2183 /*
2184 * Probes after the first probe which comes from same
2185 * user input are always allowed to add suffix, because
2186 * there might be several addresses corresponding to
2187 * one code line.
2188 */
2189 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002190 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002191
2192 if (ret >= 0) {
2193 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002194 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2195 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002196 tev->event);
2197 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002198
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002199 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002200 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002201 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002202}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002203
Namhyung Kim564c62a2015-01-14 20:18:07 +09002204static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002205{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002206 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002207 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002208
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002209 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002210 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2211 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002212 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002213
2214 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002215}
2216
2217#define strdup_or_goto(str, label) \
2218 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2219
2220/*
2221 * Find probe function addresses from map.
2222 * Return an error or the number of found probe_trace_event
2223 */
2224static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2225 struct probe_trace_event **tevs,
2226 int max_tevs, const char *target)
2227{
2228 struct map *map = NULL;
2229 struct kmap *kmap = NULL;
2230 struct ref_reloc_sym *reloc_sym = NULL;
2231 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002232 struct probe_trace_event *tev;
2233 struct perf_probe_point *pp = &pev->point;
2234 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002235 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002236 int ret, i;
2237
2238 /* Init maps of given executable or kernel */
2239 if (pev->uprobes)
2240 map = dso__new_map(target);
2241 else
2242 map = kernel_get_module_map(target);
2243 if (!map) {
2244 ret = -EINVAL;
2245 goto out;
2246 }
2247
2248 /*
2249 * Load matched symbols: Since the different local symbols may have
2250 * same name but different addresses, this lists all the symbols.
2251 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002252 num_matched_functions = find_probe_functions(map, pp->function);
2253 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002254 pr_err("Failed to find symbol %s in %s\n", pp->function,
2255 target ? : "kernel");
2256 ret = -ENOENT;
2257 goto out;
2258 } else if (num_matched_functions > max_tevs) {
2259 pr_err("Too many functions matched in %s\n",
2260 target ? : "kernel");
2261 ret = -E2BIG;
2262 goto out;
2263 }
2264
Namhyung Kim25dd9172015-01-14 20:18:08 +09002265 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002266 kmap = map__kmap(map);
2267 reloc_sym = kmap->ref_reloc_sym;
2268 if (!reloc_sym) {
2269 pr_warning("Relocated base symbol is not found!\n");
2270 ret = -EINVAL;
2271 goto out;
2272 }
2273 }
2274
2275 /* Setup result trace-probe-events */
2276 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2277 if (!*tevs) {
2278 ret = -ENOMEM;
2279 goto out;
2280 }
2281
2282 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002283
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002284 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002285 tev = (*tevs) + ret;
2286 tp = &tev->point;
2287 if (ret == num_matched_functions) {
2288 pr_warning("Too many symbols are listed. Skip it.\n");
2289 break;
2290 }
2291 ret++;
2292
2293 if (pp->offset > sym->end - sym->start) {
2294 pr_warning("Offset %ld is bigger than the size of %s\n",
2295 pp->offset, sym->name);
2296 ret = -ENOENT;
2297 goto err_out;
2298 }
2299 /* Add one probe point */
2300 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2301 if (reloc_sym) {
2302 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2303 tp->offset = tp->address - reloc_sym->addr;
2304 } else {
2305 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2306 tp->offset = pp->offset;
2307 }
2308 tp->retprobe = pp->retprobe;
2309 if (target)
2310 tev->point.module = strdup_or_goto(target, nomem_out);
2311 tev->uprobes = pev->uprobes;
2312 tev->nargs = pev->nargs;
2313 if (tev->nargs) {
2314 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2315 tev->nargs);
2316 if (tev->args == NULL)
2317 goto nomem_out;
2318 }
2319 for (i = 0; i < tev->nargs; i++) {
2320 if (pev->args[i].name)
2321 tev->args[i].name =
2322 strdup_or_goto(pev->args[i].name,
2323 nomem_out);
2324
2325 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2326 nomem_out);
2327 if (pev->args[i].type)
2328 tev->args[i].type =
2329 strdup_or_goto(pev->args[i].type,
2330 nomem_out);
2331 }
2332 }
2333
2334out:
2335 if (map && pev->uprobes) {
2336 /* Only when using uprobe(exec) map needs to be released */
2337 dso__delete(map->dso);
2338 map__delete(map);
2339 }
2340 return ret;
2341
2342nomem_out:
2343 ret = -ENOMEM;
2344err_out:
2345 clear_probe_trace_events(*tevs, num_matched_functions);
2346 zfree(tevs);
2347 goto out;
2348}
2349
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302350static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2351 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302352 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002353{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002354 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002355
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002356 if (pev->uprobes && !pev->group) {
2357 /* Replace group name if not given */
2358 ret = convert_exec_to_group(target, &pev->group);
2359 if (ret != 0) {
2360 pr_warning("Failed to make a group name.\n");
2361 return ret;
2362 }
2363 }
2364
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002365 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302366 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002367 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002368 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002369
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002370 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002371}
2372
2373struct __event_package {
2374 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302375 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002376 int ntevs;
2377};
2378
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002379int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302380 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002381{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002382 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002383 struct __event_package *pkgs;
2384
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302385 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002386 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302387
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002388 if (pkgs == NULL)
2389 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002390
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002391 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002392 if (ret < 0) {
2393 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002394 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002395 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002396
2397 /* Loop 1: convert all events */
2398 for (i = 0; i < npevs; i++) {
2399 pkgs[i].pev = &pevs[i];
2400 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302401 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002402 &pkgs[i].tevs,
2403 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302404 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002405 if (ret < 0)
2406 goto end;
2407 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002408 }
2409
2410 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002411 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302412 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002413 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002414 if (ret < 0)
2415 break;
2416 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002417end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002418 /* Loop 3: cleanup and free trace events */
2419 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002420 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302421 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002422 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002423 }
2424 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002425 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002426
2427 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002428}
2429
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302430static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002431{
2432 char *p;
2433 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002435
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302436 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002437 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2438 if (ret < 0)
2439 goto error;
2440
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002441 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002442 if (!p) {
2443 pr_debug("Internal error: %s should have ':' but not.\n",
2444 ent->s);
2445 ret = -ENOTSUP;
2446 goto error;
2447 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002448 *p = '/';
2449
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002450 pr_debug("Writing event: %s\n", buf);
2451 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002452 if (ret < 0) {
2453 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002454 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002455 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002456
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002457 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002458 return 0;
2459error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002460 pr_warning("Failed to delete event: %s\n",
2461 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002462 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002463}
2464
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302465static int del_trace_probe_event(int fd, const char *buf,
2466 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002467{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002468 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302469 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002470
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002471 if (strpbrk(buf, "*?")) { /* Glob-exp */
2472 strlist__for_each_safe(ent, n, namelist)
2473 if (strglobmatch(ent->s, buf)) {
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 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002477 strlist__remove(namelist, ent);
2478 }
2479 } else {
2480 ent = strlist__find(namelist, buf);
2481 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302482 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002483 if (ret >= 0)
2484 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002485 }
2486 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002487
2488 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002489}
2490
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002491int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002492{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302493 int ret = -1, ufd = -1, kfd = -1;
2494 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002495 const char *group, *event;
2496 char *p, *str;
2497 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302498 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002499
Masami Hiramatsufa282442009-12-08 17:03:23 -05002500 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302501 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002502 if (kfd >= 0)
2503 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302504
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302505 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002506 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302507 unamelist = get_probe_trace_event_names(ufd, true);
2508
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002509 if (kfd < 0 && ufd < 0) {
2510 print_both_open_warning(kfd, ufd);
2511 goto error;
2512 }
2513
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302514 if (namelist == NULL && unamelist == NULL)
2515 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002516
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002517 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002518 str = strdup(ent->s);
2519 if (str == NULL) {
2520 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302521 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002522 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002523 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002524 p = strchr(str, ':');
2525 if (p) {
2526 group = str;
2527 *p = '\0';
2528 event = p + 1;
2529 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002530 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002531 event = str;
2532 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302533
2534 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2535 if (ret < 0) {
2536 pr_err("Failed to copy event.");
2537 free(str);
2538 goto error;
2539 }
2540
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002541 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302542
2543 if (namelist)
2544 ret = del_trace_probe_event(kfd, buf, namelist);
2545
2546 if (unamelist && ret != 0)
2547 ret = del_trace_probe_event(ufd, buf, unamelist);
2548
2549 if (ret != 0)
2550 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2551
Masami Hiramatsufa282442009-12-08 17:03:23 -05002552 free(str);
2553 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302554
2555error:
2556 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302557 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302558 close(kfd);
2559 }
2560
2561 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302562 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302563 close(ufd);
2564 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002565
2566 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002567}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302568
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002569/* TODO: don't use a global variable for filter ... */
2570static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002571
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002572/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002573 * If a symbol corresponds to a function with global binding and
2574 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002575 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002576static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002577 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002578{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002579 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002580 strfilter__compare(available_func_filter, sym->name))
2581 return 0;
2582 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002583}
2584
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002585int show_available_funcs(const char *target, struct strfilter *_filter,
2586 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002587{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002588 struct map *map;
2589 int ret;
2590
2591 ret = init_symbol_maps(user);
2592 if (ret < 0)
2593 return ret;
2594
2595 /* Get a symbol map */
2596 if (user)
2597 map = dso__new_map(target);
2598 else
2599 map = kernel_get_module_map(target);
2600 if (!map) {
2601 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002602 return -EINVAL;
2603 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002604
2605 /* Load symbols with given filter */
2606 available_func_filter = _filter;
2607 if (map__load(map, filter_available_functions)) {
2608 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2609 goto end;
2610 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002611 if (!dso__sorted_by_name(map->dso, map->type))
2612 dso__sort_by_name(map->dso, map->type);
2613
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002614 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302615 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002616 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2617end:
2618 if (user) {
2619 dso__delete(map->dso);
2620 map__delete(map);
2621 }
2622 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302623
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002624 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302625}
2626