blob: 7c0e765fa2e3ee320836581de42bcc9ec1196ef6 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu146a1432010-04-12 13:17:42 -040055#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050056
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030073static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000074static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000075static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000078static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090083 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040084 if (ret < 0) {
85 pr_debug("Failed to init symbol map.\n");
86 goto out;
87 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (host_machine || user_only) /* already initialized */
90 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030091
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000092 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94
95 host_machine = machine__new_host();
96 if (!host_machine) {
97 pr_debug("machine__new_host() failed.\n");
98 symbol__exit();
99 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107static void exit_symbol_maps(void)
108{
109 if (host_machine) {
110 machine__delete(host_machine);
111 host_machine = NULL;
112 }
113 symbol__exit();
114}
115
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900116static struct symbol *__find_kernel_function_by_name(const char *name,
117 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400118{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000119 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120 NULL);
121}
122
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000123static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
124{
125 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
126}
127
128static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
129{
130 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 struct kmap *kmap;
132
133 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
134 return NULL;
135
136 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800154 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
161 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000162 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000166 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
171 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
172 struct map *pos = rb_entry(nd, struct map, rb_node);
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
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);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300536 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900537 } 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 Hiramatsueb47cb22015-02-26 17:25:04 +0900552 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900553 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900554 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900555 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900556 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900557
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900558 switch (errno) {
559 case ENAMETOOLONG:
560 case ENOENT:
561 case EROFS:
562 case EFAULT:
563 raw_path = strchr(++raw_path, '/');
564 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300565 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900566 return -ENOENT;
567 }
568 continue;
569
570 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300571 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900572 return -errno;
573 }
574 }
575}
576
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300577#define LINEBUF_SIZE 256
578#define NR_ADDITIONAL_LINES 2
579
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100580static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300581{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000582 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100583 const char *color = show_num ? "" : PERF_COLOR_BLUE;
584 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300585
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100586 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300587 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
588 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100589 if (skip)
590 continue;
591 if (!prefix) {
592 prefix = show_num ? "%7d " : " ";
593 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300594 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100595 color_fprintf(stdout, color, "%s", buf);
596
597 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400598
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100599 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300600error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100601 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000602 pr_warning("File read error: %s\n",
603 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100604 return -1;
605 }
606 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300607}
608
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100609static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
610{
611 int rv = __show_one_line(fp, l, skip, show_num);
612 if (rv == 0) {
613 pr_warning("Source file is shorter than expected.\n");
614 rv = -1;
615 }
616 return rv;
617}
618
619#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
620#define show_one_line(f,l) _show_one_line(f,l,false,false)
621#define skip_one_line(f,l) _show_one_line(f,l,true,false)
622#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
623
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300624/*
625 * Show line-range always requires debuginfo to find source file and
626 * line number.
627 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000628static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300629{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400630 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000631 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900632 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300633 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900634 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900635 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000636 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300637
638 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000639 dinfo = open_debuginfo(module, false);
640 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900641 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400642
Masami Hiramatsuff741782011-06-27 16:27:39 +0900643 ret = debuginfo__find_line_range(dinfo, lr);
644 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000645 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400646 pr_warning("Specified source line is not found.\n");
647 return -ENOENT;
648 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000649 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400650 return ret;
651 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300652
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900653 /* Convert source file path */
654 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900655 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900656 free(tmp); /* Free old path */
657 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000658 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900659 return ret;
660 }
661
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300662 setup_pager();
663
664 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900665 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300666 lr->start - lr->offset);
667 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100668 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300669
670 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400671 if (fp == NULL) {
672 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000673 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400674 return -errno;
675 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300676 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100677 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100678 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100679 if (ret < 0)
680 goto end;
681 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300682
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000683 intlist__for_each(ln, lr->line_list) {
684 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100685 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100686 if (ret < 0)
687 goto end;
688 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100689 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400690 if (ret < 0)
691 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300692 }
693
694 if (lr->end == INT_MAX)
695 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100696 while (l <= lr->end) {
697 ret = show_one_line_or_eof(fp, l++ - lr->offset);
698 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100699 break;
700 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400701end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300702 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400703 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300704}
705
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000706int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000707{
708 int ret;
709
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000710 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000711 if (ret < 0)
712 return ret;
713 ret = __show_line_range(lr, module);
714 exit_symbol_maps();
715
716 return ret;
717}
718
Masami Hiramatsuff741782011-06-27 16:27:39 +0900719static int show_available_vars_at(struct debuginfo *dinfo,
720 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900721 int max_vls, struct strfilter *_filter,
722 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900723{
724 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900725 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900726 struct str_node *node;
727 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900728 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900729
730 buf = synthesize_perf_probe_point(&pev->point);
731 if (!buf)
732 return -EINVAL;
733 pr_debug("Searching variables at %s\n", buf);
734
Masami Hiramatsuff741782011-06-27 16:27:39 +0900735 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
736 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900737 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000738 if (ret == 0 || ret == -ENOENT) {
739 pr_err("Failed to find the address of %s\n", buf);
740 ret = -ENOENT;
741 } else
742 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900743 goto end;
744 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000745
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900746 /* Some variables are found */
747 fprintf(stdout, "Available variables at %s\n", buf);
748 for (i = 0; i < ret; i++) {
749 vl = &vls[i];
750 /*
751 * A probe point might be converted to
752 * several trace points.
753 */
754 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
755 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300756 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900757 nvars = 0;
758 if (vl->vars) {
759 strlist__for_each(node, vl->vars) {
760 var = strchr(node->s, '\t') + 1;
761 if (strfilter__compare(_filter, var)) {
762 fprintf(stdout, "\t\t%s\n", node->s);
763 nvars++;
764 }
765 }
766 strlist__delete(vl->vars);
767 }
768 if (nvars == 0)
769 fprintf(stdout, "\t\t(No matched variables)\n");
770 }
771 free(vls);
772end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900773 free(buf);
774 return ret;
775}
776
777/* Show available variables on given probe point */
778int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900779 int max_vls, const char *module,
780 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900781{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900782 int i, ret = 0;
783 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900784
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000785 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900786 if (ret < 0)
787 return ret;
788
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000789 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900790 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000791 ret = -ENOENT;
792 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900793 }
794
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900795 setup_pager();
796
Masami Hiramatsuff741782011-06-27 16:27:39 +0900797 for (i = 0; i < npevs && ret >= 0; i++)
798 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900799 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900800
801 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000802out:
803 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900804 return ret;
805}
806
Ingo Molnar89fe8082013-09-30 12:07:11 +0200807#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000809static int
810find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
811 struct perf_probe_point *pp __maybe_unused,
812 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000814 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300815}
816
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530817static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300818 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300819 int max_tevs __maybe_unused,
820 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300821{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400822 if (perf_probe_event_need_dwarf(pev)) {
823 pr_warning("Debuginfo-analysis is not supported.\n");
824 return -ENOSYS;
825 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530826
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300827 return 0;
828}
829
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300830int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000831 const char *module __maybe_unused,
832 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300833{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400834 pr_warning("Debuginfo-analysis is not supported.\n");
835 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300836}
837
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300838int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
839 int npevs __maybe_unused, int max_vls __maybe_unused,
840 const char *module __maybe_unused,
841 struct strfilter *filter __maybe_unused,
842 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900843{
844 pr_warning("Debuginfo-analysis is not supported.\n");
845 return -ENOSYS;
846}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400847#endif
848
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000849void line_range__clear(struct line_range *lr)
850{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000851 free(lr->function);
852 free(lr->file);
853 free(lr->path);
854 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000855 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000856 memset(lr, 0, sizeof(*lr));
857}
858
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000859int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000860{
861 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000862 lr->line_list = intlist__new(NULL);
863 if (!lr->line_list)
864 return -ENOMEM;
865 else
866 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000867}
868
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100869static int parse_line_num(char **ptr, int *val, const char *what)
870{
871 const char *start = *ptr;
872
873 errno = 0;
874 *val = strtol(*ptr, ptr, 0);
875 if (errno || *ptr == start) {
876 semantic_error("'%s' is not a valid number.\n", what);
877 return -EINVAL;
878 }
879 return 0;
880}
881
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100882/*
883 * Stuff 'lr' according to the line range described by 'arg'.
884 * The line range syntax is described by:
885 *
886 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900887 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100888 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400889int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500890{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900891 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100892 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100893
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100894 if (!name)
895 return -ENOMEM;
896
897 lr->start = 0;
898 lr->end = INT_MAX;
899
900 range = strchr(name, ':');
901 if (range) {
902 *range++ = '\0';
903
904 err = parse_line_num(&range, &lr->start, "start line");
905 if (err)
906 goto err;
907
908 if (*range == '+' || *range == '-') {
909 const char c = *range++;
910
911 err = parse_line_num(&range, &lr->end, "end line");
912 if (err)
913 goto err;
914
915 if (c == '+') {
916 lr->end += lr->start;
917 /*
918 * Adjust the number of lines here.
919 * If the number of lines == 1, the
920 * the end of line should be equal to
921 * the start of line.
922 */
923 lr->end--;
924 }
925 }
926
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400927 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100928
929 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400930 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500931 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400932 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100933 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400934 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100935 if (*range != '\0') {
936 semantic_error("Tailing with invalid str '%s'.\n", range);
937 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400938 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400939 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400940
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900941 file = strchr(name, '@');
942 if (file) {
943 *file = '\0';
944 lr->file = strdup(++file);
945 if (lr->file == NULL) {
946 err = -ENOMEM;
947 goto err;
948 }
949 lr->function = name;
950 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100951 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500952 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100953 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400954
955 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100956err:
957 free(name);
958 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500959}
960
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500961/* Check the name is good for event/group */
962static bool check_event_name(const char *name)
963{
964 if (!isalpha(*name) && *name != '_')
965 return false;
966 while (*++name != '\0') {
967 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
968 return false;
969 }
970 return true;
971}
972
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500973/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400974static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500975{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400976 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500977 char *ptr, *tmp;
978 char c, nc = 0;
979 /*
980 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500981 * perf probe [EVENT=]SRC[:LN|;PTN]
982 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500983 *
984 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500985 */
986
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500987 ptr = strpbrk(arg, ";=@+%");
988 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500989 *ptr = '\0';
990 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400991 if (strchr(arg, ':')) {
992 semantic_error("Group name is not supported yet.\n");
993 return -ENOTSUP;
994 }
995 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500996 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400997 "follow C symbol-naming rule.\n", arg);
998 return -EINVAL;
999 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001000 pev->event = strdup(arg);
1001 if (pev->event == NULL)
1002 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001004 arg = tmp;
1005 }
1006
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001007 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001008 if (ptr) {
1009 nc = *ptr;
1010 *ptr++ = '\0';
1011 }
1012
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001013 tmp = strdup(arg);
1014 if (tmp == NULL)
1015 return -ENOMEM;
1016
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001017 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001018 if (strchr(tmp, '.')) /* File */
1019 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001020 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001021 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001022
1023 /* Parse other options */
1024 while (ptr) {
1025 arg = ptr;
1026 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001027 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001028 pp->lazy_line = strdup(arg);
1029 if (pp->lazy_line == NULL)
1030 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001031 break;
1032 }
1033 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001034 if (ptr) {
1035 nc = *ptr;
1036 *ptr++ = '\0';
1037 }
1038 switch (c) {
1039 case ':': /* Line number */
1040 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001041 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001042 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001043 " in line number.\n");
1044 return -EINVAL;
1045 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001046 break;
1047 case '+': /* Byte offset from a symbol */
1048 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001050 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 " in offset.\n");
1052 return -EINVAL;
1053 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001054 break;
1055 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001056 if (pp->file) {
1057 semantic_error("SRC@SRC is not allowed.\n");
1058 return -EINVAL;
1059 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001060 pp->file = strdup(arg);
1061 if (pp->file == NULL)
1062 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001063 break;
1064 case '%': /* Probe places */
1065 if (strcmp(arg, "return") == 0) {
1066 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001067 } else { /* Others not supported yet */
1068 semantic_error("%%%s is not supported.\n", arg);
1069 return -ENOTSUP;
1070 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001071 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001072 default: /* Buggy case */
1073 pr_err("This program has a bug at %s:%d.\n",
1074 __FILE__, __LINE__);
1075 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001076 break;
1077 }
1078 }
1079
1080 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001082 semantic_error("Lazy pattern can't be used with"
1083 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 return -EINVAL;
1085 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001086
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001088 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089 return -EINVAL;
1090 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001091
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001093 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001094 return -EINVAL;
1095 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001096
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001097 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001098 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001099 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 return -EINVAL;
1101 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001102
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001104 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 return -EINVAL;
1106 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001107
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001109 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 return -EINVAL;
1111 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001112
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001113 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001114 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001115 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001116 return -EINVAL;
1117 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001118
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001119 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001120 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1121 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001122 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001123}
1124
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001125/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001126static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001127{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001128 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001129 struct perf_probe_arg_field **fieldp;
1130
1131 pr_debug("parsing arg: %s into ", str);
1132
Masami Hiramatsu48481932010-04-12 13:16:53 -04001133 tmp = strchr(str, '=');
1134 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001135 arg->name = strndup(str, tmp - str);
1136 if (arg->name == NULL)
1137 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001138 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001139 str = tmp + 1;
1140 }
1141
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001142 tmp = strchr(str, ':');
1143 if (tmp) { /* Type setting */
1144 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001145 arg->type = strdup(tmp + 1);
1146 if (arg->type == NULL)
1147 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001148 pr_debug("type:%s ", arg->type);
1149 }
1150
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001151 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001152 if (!is_c_varname(str) || !tmp) {
1153 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001154 arg->var = strdup(str);
1155 if (arg->var == NULL)
1156 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001157 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001158 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001159 }
1160
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001161 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001162 arg->var = strndup(str, tmp - str);
1163 if (arg->var == NULL)
1164 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001165 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001166 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001167 fieldp = &arg->field;
1168
1169 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001170 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1171 if (*fieldp == NULL)
1172 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001173 if (*tmp == '[') { /* Array */
1174 str = tmp;
1175 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001176 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001177 if (*tmp != ']' || tmp == str + 1) {
1178 semantic_error("Array index must be a"
1179 " number.\n");
1180 return -EINVAL;
1181 }
1182 tmp++;
1183 if (*tmp == '\0')
1184 tmp = NULL;
1185 } else { /* Structure */
1186 if (*tmp == '.') {
1187 str = tmp + 1;
1188 (*fieldp)->ref = false;
1189 } else if (tmp[1] == '>') {
1190 str = tmp + 2;
1191 (*fieldp)->ref = true;
1192 } else {
1193 semantic_error("Argument parse error: %s\n",
1194 str);
1195 return -EINVAL;
1196 }
1197 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001199 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200 (*fieldp)->name = strndup(str, tmp - str);
1201 if ((*fieldp)->name == NULL)
1202 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001203 if (*str != '[')
1204 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001205 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1206 fieldp = &(*fieldp)->next;
1207 }
1208 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001209 (*fieldp)->name = strdup(str);
1210 if ((*fieldp)->name == NULL)
1211 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001212 if (*str != '[')
1213 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001214 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001215
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001216 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001217 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001218 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001219 if (arg->name == NULL)
1220 return -ENOMEM;
1221 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001223}
1224
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001225/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001227{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001228 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001230
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001231 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001232 if (!argv) {
1233 pr_debug("Failed to split arguments.\n");
1234 return -ENOMEM;
1235 }
1236 if (argc - 1 > MAX_PROBE_ARGS) {
1237 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1238 ret = -ERANGE;
1239 goto out;
1240 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001241 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 ret = parse_perf_probe_point(argv[0], pev);
1243 if (ret < 0)
1244 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001245
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001246 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001247 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001248 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1249 if (pev->args == NULL) {
1250 ret = -ENOMEM;
1251 goto out;
1252 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1254 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1255 if (ret >= 0 &&
1256 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001257 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 " kretprobe.\n");
1259 ret = -EINVAL;
1260 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001261 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001262out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001263 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001264
1265 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266}
1267
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268/* Return true if this perf_probe_event requires debuginfo */
1269bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001270{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001271 int i;
1272
1273 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1274 return true;
1275
1276 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001277 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278 return true;
1279
1280 return false;
1281}
1282
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301283/* Parse probe_events event into struct probe_point */
1284static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001285 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001286{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301287 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001288 char pr;
1289 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001290 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001291 int ret, i, argc;
1292 char **argv;
1293
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301294 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001295 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 if (!argv) {
1297 pr_debug("Failed to split arguments.\n");
1298 return -ENOMEM;
1299 }
1300 if (argc < 2) {
1301 semantic_error("Too few probe arguments.\n");
1302 ret = -ERANGE;
1303 goto out;
1304 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001305
1306 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001307 argv0_str = strdup(argv[0]);
1308 if (argv0_str == NULL) {
1309 ret = -ENOMEM;
1310 goto out;
1311 }
1312 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1313 fmt2_str = strtok_r(NULL, "/", &fmt);
1314 fmt3_str = strtok_r(NULL, " \t", &fmt);
1315 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1316 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 semantic_error("Failed to parse event name: %s\n", argv[0]);
1318 ret = -EINVAL;
1319 goto out;
1320 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001321 pr = fmt1_str[0];
1322 tev->group = strdup(fmt2_str);
1323 tev->event = strdup(fmt3_str);
1324 if (tev->group == NULL || tev->event == NULL) {
1325 ret = -ENOMEM;
1326 goto out;
1327 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001328 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001329
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001330 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001331
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001332 /* Scan module name(if there), function name and offset */
1333 p = strchr(argv[1], ':');
1334 if (p) {
1335 tp->module = strndup(argv[1], p - argv[1]);
1336 p++;
1337 } else
1338 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001339 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001340 if (fmt1_str[0] == '0') /* only the address started with 0x */
1341 tp->address = strtoul(fmt1_str, NULL, 0);
1342 else {
1343 /* Only the symbol-based probe has offset */
1344 tp->symbol = strdup(fmt1_str);
1345 if (tp->symbol == NULL) {
1346 ret = -ENOMEM;
1347 goto out;
1348 }
1349 fmt2_str = strtok_r(NULL, "", &fmt);
1350 if (fmt2_str == NULL)
1351 tp->offset = 0;
1352 else
1353 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001354 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001355
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001356 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301357 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001358 if (tev->args == NULL) {
1359 ret = -ENOMEM;
1360 goto out;
1361 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001362 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001363 p = strchr(argv[i + 2], '=');
1364 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001365 *p++ = '\0';
1366 else
1367 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001368 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001369 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001370 tev->args[i].value = strdup(p);
1371 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1372 ret = -ENOMEM;
1373 goto out;
1374 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001375 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 ret = 0;
1377out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001378 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001379 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001381}
1382
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001383/* Compose only probe arg */
1384int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1385{
1386 struct perf_probe_arg_field *field = pa->field;
1387 int ret;
1388 char *tmp = buf;
1389
Masami Hiramatsu48481932010-04-12 13:16:53 -04001390 if (pa->name && pa->var)
1391 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1392 else
1393 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001394 if (ret <= 0)
1395 goto error;
1396 tmp += ret;
1397 len -= ret;
1398
1399 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001400 if (field->name[0] == '[')
1401 ret = e_snprintf(tmp, len, "%s", field->name);
1402 else
1403 ret = e_snprintf(tmp, len, "%s%s",
1404 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001405 if (ret <= 0)
1406 goto error;
1407 tmp += ret;
1408 len -= ret;
1409 field = field->next;
1410 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001411
1412 if (pa->type) {
1413 ret = e_snprintf(tmp, len, ":%s", pa->type);
1414 if (ret <= 0)
1415 goto error;
1416 tmp += ret;
1417 len -= ret;
1418 }
1419
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001420 return tmp - buf;
1421error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001422 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001423 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001424}
1425
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426/* Compose only probe point (not argument) */
1427static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001428{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001429 char *buf, *tmp;
1430 char offs[32] = "", line[32] = "", file[32] = "";
1431 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001432
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001433 buf = zalloc(MAX_CMDLEN);
1434 if (buf == NULL) {
1435 ret = -ENOMEM;
1436 goto error;
1437 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001439 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001440 if (ret <= 0)
1441 goto error;
1442 }
1443 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001444 ret = e_snprintf(line, 32, ":%d", pp->line);
1445 if (ret <= 0)
1446 goto error;
1447 }
1448 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001449 tmp = pp->file;
1450 len = strlen(tmp);
1451 if (len > 30) {
1452 tmp = strchr(pp->file + len - 30, '/');
1453 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1454 }
1455 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001456 if (ret <= 0)
1457 goto error;
1458 }
1459
1460 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001461 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1462 offs, pp->retprobe ? "%return" : "", line,
1463 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001464 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001465 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001466 if (ret <= 0)
1467 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001468
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001469 return buf;
1470error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001471 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001472 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001473 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001474}
1475
1476#if 0
1477char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1478{
1479 char *buf;
1480 int i, len, ret;
1481
1482 buf = synthesize_perf_probe_point(&pev->point);
1483 if (!buf)
1484 return NULL;
1485
1486 len = strlen(buf);
1487 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001488 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001489 pev->args[i].name);
1490 if (ret <= 0) {
1491 free(buf);
1492 return NULL;
1493 }
1494 len += ret;
1495 }
1496
1497 return buf;
1498}
1499#endif
1500
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301501static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001502 char **buf, size_t *buflen,
1503 int depth)
1504{
1505 int ret;
1506 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301507 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001508 buflen, depth + 1);
1509 if (depth < 0)
1510 goto out;
1511 }
1512
1513 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1514 if (ret < 0)
1515 depth = ret;
1516 else {
1517 *buf += ret;
1518 *buflen -= ret;
1519 }
1520out:
1521 return depth;
1522
1523}
1524
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301525static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 char *buf, size_t buflen)
1527{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301528 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001529 int ret, depth = 0;
1530 char *tmp = buf;
1531
1532 /* Argument name or separator */
1533 if (arg->name)
1534 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1535 else
1536 ret = e_snprintf(buf, buflen, " ");
1537 if (ret < 0)
1538 return ret;
1539 buf += ret;
1540 buflen -= ret;
1541
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001542 /* Special case: @XXX */
1543 if (arg->value[0] == '@' && arg->ref)
1544 ref = ref->next;
1545
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001547 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301548 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001549 &buflen, 1);
1550 if (depth < 0)
1551 return depth;
1552 }
1553
1554 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001555 if (arg->value[0] == '@' && arg->ref)
1556 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1557 arg->ref->offset);
1558 else
1559 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001560 if (ret < 0)
1561 return ret;
1562 buf += ret;
1563 buflen -= ret;
1564
1565 /* Closing */
1566 while (depth--) {
1567 ret = e_snprintf(buf, buflen, ")");
1568 if (ret < 0)
1569 return ret;
1570 buf += ret;
1571 buflen -= ret;
1572 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001573 /* Print argument type */
1574 if (arg->type) {
1575 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1576 if (ret <= 0)
1577 return ret;
1578 buf += ret;
1579 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001580
1581 return buf - tmp;
1582}
1583
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301584char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001585{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301586 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001587 char *buf;
1588 int i, len, ret;
1589
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001590 buf = zalloc(MAX_CMDLEN);
1591 if (buf == NULL)
1592 return NULL;
1593
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001594 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1595 tev->group, tev->event);
1596 if (len <= 0)
1597 goto error;
1598
1599 /* Uprobes must have tp->address and tp->module */
1600 if (tev->uprobes && (!tp->address || !tp->module))
1601 goto error;
1602
1603 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301604 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001605 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1606 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301607 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001608 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301609 tp->module ?: "", tp->module ? ":" : "",
1610 tp->symbol, tp->offset);
1611
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001612 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001614 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001615
1616 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301617 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001618 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001619 if (ret <= 0)
1620 goto error;
1621 len += ret;
1622 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001623
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001624 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001625error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001626 free(buf);
1627 return NULL;
1628}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001629
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001630static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1631 struct perf_probe_point *pp,
1632 bool is_kprobe)
1633{
1634 struct symbol *sym = NULL;
1635 struct map *map;
1636 u64 addr;
1637 int ret = -ENOENT;
1638
1639 if (!is_kprobe) {
1640 map = dso__new_map(tp->module);
1641 if (!map)
1642 goto out;
1643 addr = tp->address;
1644 sym = map__find_symbol(map, addr, NULL);
1645 } else {
1646 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1647 if (addr) {
1648 addr += tp->offset;
1649 sym = __find_kernel_function(addr, &map);
1650 }
1651 }
1652 if (!sym)
1653 goto out;
1654
1655 pp->retprobe = tp->retprobe;
1656 pp->offset = addr - map->unmap_ip(map, sym->start);
1657 pp->function = strdup(sym->name);
1658 ret = pp->function ? 0 : -ENOMEM;
1659
1660out:
1661 if (map && !is_kprobe) {
1662 dso__delete(map->dso);
1663 map__delete(map);
1664 }
1665
1666 return ret;
1667}
1668
1669static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1670 struct perf_probe_point *pp,
1671 bool is_kprobe)
1672{
1673 char buf[128];
1674 int ret;
1675
1676 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1677 if (!ret)
1678 return 0;
1679 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1680 if (!ret)
1681 return 0;
1682
1683 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1684
1685 if (tp->symbol) {
1686 pp->function = strdup(tp->symbol);
1687 pp->offset = tp->offset;
1688 } else if (!tp->module && !is_kprobe) {
1689 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1690 if (ret < 0)
1691 return ret;
1692 pp->function = strdup(buf);
1693 pp->offset = 0;
1694 }
1695 if (pp->function == NULL)
1696 return -ENOMEM;
1697
1698 pp->retprobe = tp->retprobe;
1699
1700 return 0;
1701}
1702
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301703static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301704 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001705{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001706 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001707 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001708
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001709 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001710 pev->event = strdup(tev->event);
1711 pev->group = strdup(tev->group);
1712 if (pev->event == NULL || pev->group == NULL)
1713 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001714
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001715 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001716 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001717 if (ret < 0)
1718 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001719
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001720 /* Convert trace_arg to probe_arg */
1721 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001722 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1723 if (pev->args == NULL)
1724 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001725 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001726 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001727 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001728 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301729 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001730 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001731 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001732 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001733 if (pev->args[i].name == NULL && ret >= 0)
1734 ret = -ENOMEM;
1735 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736
1737 if (ret < 0)
1738 clear_perf_probe_event(pev);
1739
1740 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001741}
1742
1743void clear_perf_probe_event(struct perf_probe_event *pev)
1744{
1745 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001746 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001747 int i;
1748
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001749 free(pev->event);
1750 free(pev->group);
1751 free(pp->file);
1752 free(pp->function);
1753 free(pp->lazy_line);
1754
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001755 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001756 free(pev->args[i].name);
1757 free(pev->args[i].var);
1758 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001759 field = pev->args[i].field;
1760 while (field) {
1761 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001762 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001763 free(field);
1764 field = next;
1765 }
1766 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001767 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001768 memset(pev, 0, sizeof(*pev));
1769}
1770
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301771static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301773 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 int i;
1775
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001776 free(tev->event);
1777 free(tev->group);
1778 free(tev->point.symbol);
1779 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001780 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001781 free(tev->args[i].name);
1782 free(tev->args[i].value);
1783 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001784 ref = tev->args[i].ref;
1785 while (ref) {
1786 next = ref->next;
1787 free(ref);
1788 ref = next;
1789 }
1790 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001791 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001792 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001793}
1794
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001795static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301796{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001797 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301798
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001799 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301800 const char *config;
1801
1802 if (!is_kprobe)
1803 config = "CONFIG_UPROBE_EVENTS";
1804 else
1805 config = "CONFIG_KPROBE_EVENTS";
1806
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001807 pr_warning("%cprobe_events file does not exist"
1808 " - please rebuild kernel with %s.\n",
1809 is_kprobe ? 'k' : 'u', config);
1810 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001811 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001812 else
1813 pr_warning("Failed to open %cprobe_events: %s\n",
1814 is_kprobe ? 'k' : 'u',
1815 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301816}
1817
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001818static void print_both_open_warning(int kerr, int uerr)
1819{
1820 /* Both kprobes and uprobes are disabled, warn it. */
1821 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001822 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001823 else if (kerr == -ENOENT && uerr == -ENOENT)
1824 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1825 "or/and CONFIG_UPROBE_EVENTS.\n");
1826 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001827 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001828 pr_warning("Failed to open kprobe events: %s.\n",
1829 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1830 pr_warning("Failed to open uprobe events: %s.\n",
1831 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1832 }
1833}
1834
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001835static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001836{
1837 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001838 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001839 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001840 int ret;
1841
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001842 __debugfs = tracefs_find_mountpoint();
1843 if (__debugfs == NULL) {
1844 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001845
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001846 __debugfs = debugfs_find_mountpoint();
1847 if (__debugfs == NULL)
1848 return -ENOTSUP;
1849 }
1850
1851 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1852 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001853 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001854 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001855 if (readwrite && !probe_event_dry_run)
1856 ret = open(buf, O_RDWR, O_APPEND);
1857 else
1858 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001859
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301860 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001861 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001862 }
1863 return ret;
1864}
1865
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301866static int open_kprobe_events(bool readwrite)
1867{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001868 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301869}
1870
1871static int open_uprobe_events(bool readwrite)
1872{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001873 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301874}
1875
1876/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301877static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001878{
1879 int ret, idx;
1880 FILE *fp;
1881 char buf[MAX_CMDLEN];
1882 char *p;
1883 struct strlist *sl;
1884
1885 sl = strlist__new(true, NULL);
1886
1887 fp = fdopen(dup(fd), "r");
1888 while (!feof(fp)) {
1889 p = fgets(buf, MAX_CMDLEN, fp);
1890 if (!p)
1891 break;
1892
1893 idx = strlen(p) - 1;
1894 if (p[idx] == '\n')
1895 p[idx] = '\0';
1896 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001897 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00001898 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001899 strlist__delete(sl);
1900 return NULL;
1901 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001902 }
1903 fclose(fp);
1904
1905 return sl;
1906}
1907
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09001908struct kprobe_blacklist_node {
1909 struct list_head list;
1910 unsigned long start;
1911 unsigned long end;
1912 char *symbol;
1913};
1914
1915static void kprobe_blacklist__delete(struct list_head *blacklist)
1916{
1917 struct kprobe_blacklist_node *node;
1918
1919 while (!list_empty(blacklist)) {
1920 node = list_first_entry(blacklist,
1921 struct kprobe_blacklist_node, list);
1922 list_del(&node->list);
1923 free(node->symbol);
1924 free(node);
1925 }
1926}
1927
1928static int kprobe_blacklist__load(struct list_head *blacklist)
1929{
1930 struct kprobe_blacklist_node *node;
1931 const char *__debugfs = debugfs_find_mountpoint();
1932 char buf[PATH_MAX], *p;
1933 FILE *fp;
1934 int ret;
1935
1936 if (__debugfs == NULL)
1937 return -ENOTSUP;
1938
1939 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
1940 if (ret < 0)
1941 return ret;
1942
1943 fp = fopen(buf, "r");
1944 if (!fp)
1945 return -errno;
1946
1947 ret = 0;
1948 while (fgets(buf, PATH_MAX, fp)) {
1949 node = zalloc(sizeof(*node));
1950 if (!node) {
1951 ret = -ENOMEM;
1952 break;
1953 }
1954 INIT_LIST_HEAD(&node->list);
1955 list_add_tail(&node->list, blacklist);
1956 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
1957 ret = -EINVAL;
1958 break;
1959 }
1960 p = strchr(buf, '\t');
1961 if (p) {
1962 p++;
1963 if (p[strlen(p) - 1] == '\n')
1964 p[strlen(p) - 1] = '\0';
1965 } else
1966 p = (char *)"unknown";
1967 node->symbol = strdup(p);
1968 if (!node->symbol) {
1969 ret = -ENOMEM;
1970 break;
1971 }
1972 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
1973 node->start, node->end, node->symbol);
1974 ret++;
1975 }
1976 if (ret < 0)
1977 kprobe_blacklist__delete(blacklist);
1978 fclose(fp);
1979
1980 return ret;
1981}
1982
1983static struct kprobe_blacklist_node *
1984kprobe_blacklist__find_by_address(struct list_head *blacklist,
1985 unsigned long address)
1986{
1987 struct kprobe_blacklist_node *node;
1988
1989 list_for_each_entry(node, blacklist, list) {
1990 if (node->start <= address && address <= node->end)
1991 return node;
1992 }
1993
1994 return NULL;
1995}
1996
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001997/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001998static int show_perf_probe_event(struct perf_probe_event *pev,
1999 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002000{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002001 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002002 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002003 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002004
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002005 /* Synthesize only event probe point */
2006 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002007 if (!place)
2008 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002009
2010 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002011 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002012 return ret;
2013
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002014 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002015 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002016 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002017
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002018 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002019 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002020 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002021 ret = synthesize_perf_probe_arg(&pev->args[i],
2022 buf, 128);
2023 if (ret < 0)
2024 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002025 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002026 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002027 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002028 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002029 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002030 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002031}
2032
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302033static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002034{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302035 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302036 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002037 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002038 struct strlist *rawlist;
2039 struct str_node *ent;
2040
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002041 memset(&tev, 0, sizeof(tev));
2042 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002043
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302044 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002045 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002046 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002047
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002048 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302049 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002050 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302051 ret = convert_to_perf_probe_event(&tev, &pev,
2052 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002053 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002054 ret = show_perf_probe_event(&pev,
2055 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002056 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002057 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302058 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002059 if (ret < 0)
2060 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002061 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002062 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002063
2064 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002065}
2066
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302067/* List up current perf-probe events */
2068int show_perf_probe_events(void)
2069{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002070 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302071
2072 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302073
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002074 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302075 if (ret < 0)
2076 return ret;
2077
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002078 kp_fd = open_kprobe_events(false);
2079 if (kp_fd >= 0) {
2080 ret = __show_perf_probe_events(kp_fd, true);
2081 close(kp_fd);
2082 if (ret < 0)
2083 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302084 }
2085
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002086 up_fd = open_uprobe_events(false);
2087 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002088 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002089 ret = kp_fd;
2090 goto out;
2091 }
2092
2093 if (up_fd >= 0) {
2094 ret = __show_perf_probe_events(up_fd, false);
2095 close(up_fd);
2096 }
2097out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002098 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302099 return ret;
2100}
2101
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002102/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302103static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002104{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002105 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002106 struct strlist *sl, *rawlist;
2107 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302108 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002109 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002110
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002111 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302112 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002113 if (!rawlist)
2114 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002115 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002116 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302117 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002118 if (ret < 0)
2119 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002120 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002121 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2122 tev.event);
2123 if (ret >= 0)
2124 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002125 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002126 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302127 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 if (ret < 0)
2129 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002130 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002131 strlist__delete(rawlist);
2132
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002133 if (ret < 0) {
2134 strlist__delete(sl);
2135 return NULL;
2136 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002137 return sl;
2138}
2139
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302140static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002141{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002142 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302143 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002144 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002145
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002146 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302147 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002148 return -EINVAL;
2149 }
2150
Masami Hiramatsufa282442009-12-08 17:03:23 -05002151 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002152 if (!probe_event_dry_run) {
2153 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002154 if (ret <= 0) {
2155 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002156 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002157 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002158 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002159 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002160 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002161 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002162}
2163
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002164static int get_new_event_name(char *buf, size_t len, const char *base,
2165 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002166{
2167 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002168
2169 /* Try no suffix */
2170 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002171 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002172 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002173 return ret;
2174 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002175 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002176 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002177
Masami Hiramatsud761b082009-12-15 10:32:25 -05002178 if (!allow_suffix) {
2179 pr_warning("Error: event \"%s\" already exists. "
2180 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002181 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002182 }
2183
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002184 /* Try to add suffix */
2185 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002186 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002187 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002188 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002189 return ret;
2190 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002191 if (!strlist__has_entry(namelist, buf))
2192 break;
2193 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002194 if (i == MAX_EVENT_INDEX) {
2195 pr_warning("Too many events are on the same function.\n");
2196 ret = -ERANGE;
2197 }
2198
2199 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002200}
2201
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302202static int __add_probe_trace_events(struct perf_probe_event *pev,
2203 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002204 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002205{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002206 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302207 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002208 char buf[64];
2209 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002210 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002211 LIST_HEAD(blacklist);
2212 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002213
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302214 if (pev->uprobes)
2215 fd = open_uprobe_events(true);
2216 else
2217 fd = open_kprobe_events(true);
2218
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002219 if (fd < 0) {
2220 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002221 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002222 }
2223
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002224 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302225 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002226 if (!namelist) {
2227 pr_debug("Failed to get current event list.\n");
2228 return -EIO;
2229 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002230 /* Get kprobe blacklist if exists */
2231 if (!pev->uprobes) {
2232 ret = kprobe_blacklist__load(&blacklist);
2233 if (ret < 0)
2234 pr_debug("No kprobe blacklist support, ignored\n");
2235 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002236
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002238 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002239 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002240 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002241 /* Ensure that the address is NOT blacklisted */
2242 node = kprobe_blacklist__find_by_address(&blacklist,
2243 tev->point.address);
2244 if (node) {
2245 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2246 continue;
2247 }
2248
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002249 if (pev->event)
2250 event = pev->event;
2251 else
2252 if (pev->point.function)
2253 event = pev->point.function;
2254 else
2255 event = tev->point.symbol;
2256 if (pev->group)
2257 group = pev->group;
2258 else
2259 group = PERFPROBE_GROUP;
2260
2261 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002262 ret = get_new_event_name(buf, 64, event,
2263 namelist, allow_suffix);
2264 if (ret < 0)
2265 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002266 event = buf;
2267
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002268 tev->event = strdup(event);
2269 tev->group = strdup(group);
2270 if (tev->event == NULL || tev->group == NULL) {
2271 ret = -ENOMEM;
2272 break;
2273 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302274 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002275 if (ret < 0)
2276 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002277 /* Add added event name to namelist */
2278 strlist__add(namelist, event);
2279
2280 /* Trick here - save current event/group */
2281 event = pev->event;
2282 group = pev->group;
2283 pev->event = tev->event;
2284 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002285 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002286 /* Trick here - restore current event/group */
2287 pev->event = (char *)event;
2288 pev->group = (char *)group;
2289
2290 /*
2291 * Probes after the first probe which comes from same
2292 * user input are always allowed to add suffix, because
2293 * there might be several addresses corresponding to
2294 * one code line.
2295 */
2296 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002297 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002298
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002299 /* Note that it is possible to skip all events because of blacklist */
2300 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002301 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002302 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2303 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002304 tev->event);
2305 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002306
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002307 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002308 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002309 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002310 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002311}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002312
Namhyung Kim564c62a2015-01-14 20:18:07 +09002313static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002314{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002315 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002316 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002317
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002318 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002319 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2320 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002321 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002322
2323 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002324}
2325
2326#define strdup_or_goto(str, label) \
2327 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2328
2329/*
2330 * Find probe function addresses from map.
2331 * Return an error or the number of found probe_trace_event
2332 */
2333static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2334 struct probe_trace_event **tevs,
2335 int max_tevs, const char *target)
2336{
2337 struct map *map = NULL;
2338 struct kmap *kmap = NULL;
2339 struct ref_reloc_sym *reloc_sym = NULL;
2340 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002341 struct probe_trace_event *tev;
2342 struct perf_probe_point *pp = &pev->point;
2343 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002344 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002345 int ret, i;
2346
2347 /* Init maps of given executable or kernel */
2348 if (pev->uprobes)
2349 map = dso__new_map(target);
2350 else
2351 map = kernel_get_module_map(target);
2352 if (!map) {
2353 ret = -EINVAL;
2354 goto out;
2355 }
2356
2357 /*
2358 * Load matched symbols: Since the different local symbols may have
2359 * same name but different addresses, this lists all the symbols.
2360 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002361 num_matched_functions = find_probe_functions(map, pp->function);
2362 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002363 pr_err("Failed to find symbol %s in %s\n", pp->function,
2364 target ? : "kernel");
2365 ret = -ENOENT;
2366 goto out;
2367 } else if (num_matched_functions > max_tevs) {
2368 pr_err("Too many functions matched in %s\n",
2369 target ? : "kernel");
2370 ret = -E2BIG;
2371 goto out;
2372 }
2373
Namhyung Kim25dd9172015-01-14 20:18:08 +09002374 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002375 kmap = map__kmap(map);
2376 reloc_sym = kmap->ref_reloc_sym;
2377 if (!reloc_sym) {
2378 pr_warning("Relocated base symbol is not found!\n");
2379 ret = -EINVAL;
2380 goto out;
2381 }
2382 }
2383
2384 /* Setup result trace-probe-events */
2385 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2386 if (!*tevs) {
2387 ret = -ENOMEM;
2388 goto out;
2389 }
2390
2391 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002392
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002393 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002394 tev = (*tevs) + ret;
2395 tp = &tev->point;
2396 if (ret == num_matched_functions) {
2397 pr_warning("Too many symbols are listed. Skip it.\n");
2398 break;
2399 }
2400 ret++;
2401
2402 if (pp->offset > sym->end - sym->start) {
2403 pr_warning("Offset %ld is bigger than the size of %s\n",
2404 pp->offset, sym->name);
2405 ret = -ENOENT;
2406 goto err_out;
2407 }
2408 /* Add one probe point */
2409 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2410 if (reloc_sym) {
2411 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2412 tp->offset = tp->address - reloc_sym->addr;
2413 } else {
2414 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2415 tp->offset = pp->offset;
2416 }
2417 tp->retprobe = pp->retprobe;
2418 if (target)
2419 tev->point.module = strdup_or_goto(target, nomem_out);
2420 tev->uprobes = pev->uprobes;
2421 tev->nargs = pev->nargs;
2422 if (tev->nargs) {
2423 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2424 tev->nargs);
2425 if (tev->args == NULL)
2426 goto nomem_out;
2427 }
2428 for (i = 0; i < tev->nargs; i++) {
2429 if (pev->args[i].name)
2430 tev->args[i].name =
2431 strdup_or_goto(pev->args[i].name,
2432 nomem_out);
2433
2434 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2435 nomem_out);
2436 if (pev->args[i].type)
2437 tev->args[i].type =
2438 strdup_or_goto(pev->args[i].type,
2439 nomem_out);
2440 }
2441 }
2442
2443out:
2444 if (map && pev->uprobes) {
2445 /* Only when using uprobe(exec) map needs to be released */
2446 dso__delete(map->dso);
2447 map__delete(map);
2448 }
2449 return ret;
2450
2451nomem_out:
2452 ret = -ENOMEM;
2453err_out:
2454 clear_probe_trace_events(*tevs, num_matched_functions);
2455 zfree(tevs);
2456 goto out;
2457}
2458
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302459static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2460 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302461 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002462{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002463 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002464
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002465 if (pev->uprobes && !pev->group) {
2466 /* Replace group name if not given */
2467 ret = convert_exec_to_group(target, &pev->group);
2468 if (ret != 0) {
2469 pr_warning("Failed to make a group name.\n");
2470 return ret;
2471 }
2472 }
2473
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002474 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302475 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002476 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002477 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002478
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002479 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002480}
2481
2482struct __event_package {
2483 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302484 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002485 int ntevs;
2486};
2487
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002488int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302489 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002490{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002491 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002492 struct __event_package *pkgs;
2493
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302494 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002495 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302496
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002497 if (pkgs == NULL)
2498 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002499
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002500 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002501 if (ret < 0) {
2502 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002503 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002504 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002505
2506 /* Loop 1: convert all events */
2507 for (i = 0; i < npevs; i++) {
2508 pkgs[i].pev = &pevs[i];
2509 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302510 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002511 &pkgs[i].tevs,
2512 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302513 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002514 if (ret < 0)
2515 goto end;
2516 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002517 }
2518
2519 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002520 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302521 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002522 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002523 if (ret < 0)
2524 break;
2525 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002526end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002527 /* Loop 3: cleanup and free trace events */
2528 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002529 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302530 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002531 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002532 }
2533 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002534 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002535
2536 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002537}
2538
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302539static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002540{
2541 char *p;
2542 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002543 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002544
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302545 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002546 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2547 if (ret < 0)
2548 goto error;
2549
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002550 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002551 if (!p) {
2552 pr_debug("Internal error: %s should have ':' but not.\n",
2553 ent->s);
2554 ret = -ENOTSUP;
2555 goto error;
2556 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002557 *p = '/';
2558
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002559 pr_debug("Writing event: %s\n", buf);
2560 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002561 if (ret < 0) {
2562 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002563 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002564 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002565
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002566 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002567 return 0;
2568error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002569 pr_warning("Failed to delete event: %s\n",
2570 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002571 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002572}
2573
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302574static int del_trace_probe_event(int fd, const char *buf,
2575 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002576{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002577 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302578 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002579
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002580 if (strpbrk(buf, "*?")) { /* Glob-exp */
2581 strlist__for_each_safe(ent, n, namelist)
2582 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302583 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002584 if (ret < 0)
2585 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002586 strlist__remove(namelist, ent);
2587 }
2588 } else {
2589 ent = strlist__find(namelist, buf);
2590 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302591 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002592 if (ret >= 0)
2593 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002594 }
2595 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002596
2597 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002598}
2599
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002600int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002601{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302602 int ret = -1, ufd = -1, kfd = -1;
2603 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002604 const char *group, *event;
2605 char *p, *str;
2606 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302607 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002608
Masami Hiramatsufa282442009-12-08 17:03:23 -05002609 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302610 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002611 if (kfd >= 0)
2612 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302613
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302614 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002615 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302616 unamelist = get_probe_trace_event_names(ufd, true);
2617
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002618 if (kfd < 0 && ufd < 0) {
2619 print_both_open_warning(kfd, ufd);
2620 goto error;
2621 }
2622
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302623 if (namelist == NULL && unamelist == NULL)
2624 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002625
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002626 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002627 str = strdup(ent->s);
2628 if (str == NULL) {
2629 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302630 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002631 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002632 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002633 p = strchr(str, ':');
2634 if (p) {
2635 group = str;
2636 *p = '\0';
2637 event = p + 1;
2638 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002639 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002640 event = str;
2641 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302642
2643 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2644 if (ret < 0) {
2645 pr_err("Failed to copy event.");
2646 free(str);
2647 goto error;
2648 }
2649
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002650 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302651
2652 if (namelist)
2653 ret = del_trace_probe_event(kfd, buf, namelist);
2654
2655 if (unamelist && ret != 0)
2656 ret = del_trace_probe_event(ufd, buf, unamelist);
2657
2658 if (ret != 0)
2659 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2660
Masami Hiramatsufa282442009-12-08 17:03:23 -05002661 free(str);
2662 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302663
2664error:
2665 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302666 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302667 close(kfd);
2668 }
2669
2670 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302671 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302672 close(ufd);
2673 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002674
2675 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002676}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302677
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002678/* TODO: don't use a global variable for filter ... */
2679static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002680
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002681/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002682 * If a symbol corresponds to a function with global binding and
2683 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002684 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002685static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002686 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002687{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002688 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002689 strfilter__compare(available_func_filter, sym->name))
2690 return 0;
2691 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002692}
2693
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002694int show_available_funcs(const char *target, struct strfilter *_filter,
2695 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002696{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002697 struct map *map;
2698 int ret;
2699
2700 ret = init_symbol_maps(user);
2701 if (ret < 0)
2702 return ret;
2703
2704 /* Get a symbol map */
2705 if (user)
2706 map = dso__new_map(target);
2707 else
2708 map = kernel_get_module_map(target);
2709 if (!map) {
2710 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002711 return -EINVAL;
2712 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002713
2714 /* Load symbols with given filter */
2715 available_func_filter = _filter;
2716 if (map__load(map, filter_available_functions)) {
2717 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2718 goto end;
2719 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002720 if (!dso__sorted_by_name(map->dso, map->type))
2721 dso__sort_by_name(map->dso, map->type);
2722
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002723 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302724 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002725 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2726end:
2727 if (user) {
2728 dso__delete(map->dso);
2729 map__delete(map);
2730 }
2731 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302732
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002733 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302734}
2735