blob: de9fe906f3be8a5a47f985660be7b26b0f505ff4 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053047#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048
49#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050#define PERFPROBE_GROUP "probe"
51
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040052bool probe_event_dry_run; /* Dry run flag */
53
Masami Hiramatsu146a1432010-04-12 13:17:42 -040054#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050055
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050056/* If there is no space to write, returns -E2BIG. */
57static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050058 __attribute__((format(printf, 3, 4)));
59
60static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050061{
62 int ret;
63 va_list ap;
64 va_start(ap, format);
65 ret = vsnprintf(str, size, format, ap);
66 va_end(ap);
67 if (ret >= (int)size)
68 ret = -E2BIG;
69 return ret;
70}
71
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030072static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Srikar Dronamraju225466f2012-04-16 17:39:09 +053073static int convert_name_to_addr(struct perf_probe_event *pev,
74 const char *exec);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000075static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000076static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090078/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000079static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -040084 ret = symbol__init();
85 if (ret < 0) {
86 pr_debug("Failed to init symbol map.\n");
87 goto out;
88 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040089
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000090 if (host_machine || user_only) /* already initialized */
91 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030092
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000093 if (symbol_conf.vmlinux_name)
94 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95
96 host_machine = machine__new_host();
97 if (!host_machine) {
98 pr_debug("machine__new_host() failed.\n");
99 symbol__exit();
100 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900101 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400102out:
103 if (ret < 0)
104 pr_warning("Failed to init vmlinux path.\n");
105 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106}
107
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000108static void exit_symbol_maps(void)
109{
110 if (host_machine) {
111 machine__delete(host_machine);
112 host_machine = NULL;
113 }
114 symbol__exit();
115}
116
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900117static struct symbol *__find_kernel_function_by_name(const char *name,
118 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400119{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000120 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900121 NULL);
122}
123
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900124static struct map *kernel_get_module_map(const char *module)
125{
126 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000127 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900128
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900129 /* A file path -- this is an offline module */
130 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000131 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900132
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900133 if (!module)
134 module = "kernel";
135
136 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
137 struct map *pos = rb_entry(nd, struct map, rb_node);
138 if (strncmp(pos->dso->short_name + 1, module,
139 pos->dso->short_name_len - 2) == 0) {
140 return pos;
141 }
142 }
143 return NULL;
144}
145
146static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900147{
148 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100149 struct map *map;
150 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900151
152 if (module) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000153 list_for_each_entry(dso, &host_machine->kernel_dsos, node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900154 if (strncmp(dso->short_name + 1, module,
155 dso->short_name_len - 2) == 0)
156 goto found;
157 }
158 pr_debug("Failed to find module %s.\n", module);
159 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100160 }
161
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000162 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100163 dso = map->dso;
164
165 vmlinux_name = symbol_conf.vmlinux_name;
166 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300167 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100168 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900169 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100170 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900171 pr_debug("Failed to load kernel map.\n");
172 return NULL;
173 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400174 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900175found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900176 return dso;
177}
178
179const char *kernel_get_module_path(const char *module)
180{
181 struct dso *dso = kernel_get_module_dso(module);
182 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900183}
184
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000185static int convert_exec_to_group(const char *exec, char **result)
186{
187 char *ptr1, *ptr2, *exec_copy;
188 char buf[64];
189 int ret;
190
191 exec_copy = strdup(exec);
192 if (!exec_copy)
193 return -ENOMEM;
194
195 ptr1 = basename(exec_copy);
196 if (!ptr1) {
197 ret = -EINVAL;
198 goto out;
199 }
200
201 ptr2 = strpbrk(ptr1, "-._");
202 if (ptr2)
203 *ptr2 = '\0';
204 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
205 if (ret < 0)
206 goto out;
207
208 *result = strdup(buf);
209 ret = *result ? 0 : -ENOMEM;
210
211out:
212 free(exec_copy);
213 return ret;
214}
215
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530216static int convert_to_perf_probe_point(struct probe_trace_point *tp,
217 struct perf_probe_point *pp)
218{
219 pp->function = strdup(tp->symbol);
220
221 if (pp->function == NULL)
222 return -ENOMEM;
223
224 pp->offset = tp->offset;
225 pp->retprobe = tp->retprobe;
226
227 return 0;
228}
229
Ingo Molnar89fe8082013-09-30 12:07:11 +0200230#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900231/* Open new debuginfo of given module */
232static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900233{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900234 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900235
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900236 /* A file path -- this is an offline module */
237 if (module && strchr(module, '/'))
238 path = module;
239 else {
240 path = kernel_get_module_path(module);
241
242 if (!path) {
243 pr_err("Failed to find path of %s module.\n",
244 module ?: "kernel");
245 return NULL;
246 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900247 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900248 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400249}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300250
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530251/*
252 * Convert trace point to probe point with debuginfo
253 * Currently only handles kprobes.
254 */
255static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900256 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300257{
258 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900259 struct map *map;
260 u64 addr;
261 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900262 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300263
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900264 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300265 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900266 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200267 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900268 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900269
270 dinfo = debuginfo__new_online_kernel(addr);
271 if (dinfo) {
272 ret = debuginfo__find_probe_point(dinfo,
273 (unsigned long)addr, pp);
274 debuginfo__delete(dinfo);
275 } else {
276 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
277 addr);
278 ret = -ENOENT;
279 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300280 }
281 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400282 pr_debug("Failed to find corresponding probes from "
283 "debuginfo. Use kprobe event information.\n");
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530284 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300285 }
286 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400287
288 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300289}
290
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000291static int get_text_start_address(const char *exec, unsigned long *address)
292{
293 Elf *elf;
294 GElf_Ehdr ehdr;
295 GElf_Shdr shdr;
296 int fd, ret = -ENOENT;
297
298 fd = open(exec, O_RDONLY);
299 if (fd < 0)
300 return -errno;
301
302 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
303 if (elf == NULL)
304 return -EINVAL;
305
306 if (gelf_getehdr(elf, &ehdr) == NULL)
307 goto out;
308
309 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
310 goto out;
311
312 *address = shdr.sh_addr - shdr.sh_offset;
313 ret = 0;
314out:
315 elf_end(elf);
316 return ret;
317}
318
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000319static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
320 int ntevs, const char *exec)
321{
322 int i, ret = 0;
323 unsigned long offset, stext = 0;
324 char buf[32];
325
326 if (!exec)
327 return 0;
328
329 ret = get_text_start_address(exec, &stext);
330 if (ret < 0)
331 return ret;
332
333 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000334 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000335 offset = tevs[i].point.address - stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000336 tevs[i].point.offset = 0;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300337 zfree(&tevs[i].point.symbol);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000338 ret = e_snprintf(buf, 32, "0x%lx", offset);
339 if (ret < 0)
340 break;
341 tevs[i].point.module = strdup(exec);
342 tevs[i].point.symbol = strdup(buf);
343 if (!tevs[i].point.symbol || !tevs[i].point.module) {
344 ret = -ENOMEM;
345 break;
346 }
347 tevs[i].uprobes = true;
348 }
349
350 return ret;
351}
352
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900353static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
354 int ntevs, const char *module)
355{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900356 int i, ret = 0;
357 char *tmp;
358
359 if (!module)
360 return 0;
361
362 tmp = strrchr(module, '/');
363 if (tmp) {
364 /* This is a module path -- get the module name */
365 module = strdup(tmp + 1);
366 if (!module)
367 return -ENOMEM;
368 tmp = strchr(module, '.');
369 if (tmp)
370 *tmp = '\0';
371 tmp = (char *)module; /* For free() */
372 }
373
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900374 for (i = 0; i < ntevs; i++) {
375 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900376 if (!tevs[i].point.module) {
377 ret = -ENOMEM;
378 break;
379 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900380 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900381
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300382 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900383 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900384}
385
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000386static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
387{
388 int i;
389
390 for (i = 0; i < ntevs; i++)
391 clear_probe_trace_event(tevs + i);
392}
393
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300394/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530395static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900396 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530397 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300398{
399 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530400 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900401 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300402
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530403 dinfo = open_debuginfo(target);
404
Masami Hiramatsuff741782011-06-27 16:27:39 +0900405 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400406 if (need_dwarf) {
407 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900408 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400409 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900410 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300411 return 0;
412 }
413
Masami Hiramatsuff741782011-06-27 16:27:39 +0900414 /* Searching trace events corresponding to a probe event */
415 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
416
417 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300418
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400419 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530420 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000421 if (target) {
422 if (pev->uprobes)
423 ret = add_exec_to_probe_trace_events(*tevs,
424 ntevs, target);
425 else
426 ret = add_module_to_probe_trace_events(*tevs,
427 ntevs, target);
428 }
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000429 if (ret < 0) {
430 clear_probe_trace_events(*tevs, ntevs);
431 zfree(tevs);
432 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900433 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400434 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300435
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400436 if (ntevs == 0) { /* No error but failed to find probe point. */
437 pr_warning("Probe point '%s' not found.\n",
438 synthesize_perf_probe_point(&pev->point));
439 return -ENOENT;
440 }
441 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400442 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
443 if (ntevs == -EBADF) {
444 pr_warning("Warning: No dwarf info found in the vmlinux - "
445 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
446 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900447 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400448 return 0;
449 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300450 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400451 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300452}
453
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900454/*
455 * Find a src file from a DWARF tag path. Prepend optional source path prefix
456 * and chop off leading directories that do not exist. Result is passed back as
457 * a newly allocated path on success.
458 * Return 0 if file was found and readable, -errno otherwise.
459 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900460static int get_real_path(const char *raw_path, const char *comp_dir,
461 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900462{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900463 const char *prefix = symbol_conf.source_prefix;
464
465 if (!prefix) {
466 if (raw_path[0] != '/' && comp_dir)
467 /* If not an absolute path, try to use comp_dir */
468 prefix = comp_dir;
469 else {
470 if (access(raw_path, R_OK) == 0) {
471 *new_path = strdup(raw_path);
472 return 0;
473 } else
474 return -errno;
475 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900476 }
477
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900478 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900479 if (!*new_path)
480 return -ENOMEM;
481
482 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900483 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900484
485 if (access(*new_path, R_OK) == 0)
486 return 0;
487
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900488 if (!symbol_conf.source_prefix)
489 /* In case of searching comp_dir, don't retry */
490 return -errno;
491
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900492 switch (errno) {
493 case ENAMETOOLONG:
494 case ENOENT:
495 case EROFS:
496 case EFAULT:
497 raw_path = strchr(++raw_path, '/');
498 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300499 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900500 return -ENOENT;
501 }
502 continue;
503
504 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300505 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900506 return -errno;
507 }
508 }
509}
510
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300511#define LINEBUF_SIZE 256
512#define NR_ADDITIONAL_LINES 2
513
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100514static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300515{
516 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100517 const char *color = show_num ? "" : PERF_COLOR_BLUE;
518 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300519
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100520 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300521 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
522 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100523 if (skip)
524 continue;
525 if (!prefix) {
526 prefix = show_num ? "%7d " : " ";
527 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300528 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100529 color_fprintf(stdout, color, "%s", buf);
530
531 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400532
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100533 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300534error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100535 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100536 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100537 return -1;
538 }
539 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300540}
541
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100542static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
543{
544 int rv = __show_one_line(fp, l, skip, show_num);
545 if (rv == 0) {
546 pr_warning("Source file is shorter than expected.\n");
547 rv = -1;
548 }
549 return rv;
550}
551
552#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
553#define show_one_line(f,l) _show_one_line(f,l,false,false)
554#define skip_one_line(f,l) _show_one_line(f,l,true,false)
555#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
556
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300557/*
558 * Show line-range always requires debuginfo to find source file and
559 * line number.
560 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000561static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300562{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400563 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000564 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900565 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300566 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900567 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900568 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300569
570 /* Search a line range */
Masami Hiramatsuff741782011-06-27 16:27:39 +0900571 dinfo = open_debuginfo(module);
572 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400573 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900574 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400575 }
576
Masami Hiramatsuff741782011-06-27 16:27:39 +0900577 ret = debuginfo__find_line_range(dinfo, lr);
578 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400579 if (ret == 0) {
580 pr_warning("Specified source line is not found.\n");
581 return -ENOENT;
582 } else if (ret < 0) {
583 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
584 return ret;
585 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300586
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900587 /* Convert source file path */
588 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900589 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900590 free(tmp); /* Free old path */
591 if (ret < 0) {
592 pr_warning("Failed to find source file. (%d)\n", ret);
593 return ret;
594 }
595
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300596 setup_pager();
597
598 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900599 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300600 lr->start - lr->offset);
601 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100602 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300603
604 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400605 if (fp == NULL) {
606 pr_warning("Failed to open %s: %s\n", lr->path,
607 strerror(errno));
608 return -errno;
609 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300610 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100611 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100612 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100613 if (ret < 0)
614 goto end;
615 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300616
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000617 intlist__for_each(ln, lr->line_list) {
618 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100619 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100620 if (ret < 0)
621 goto end;
622 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100623 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400624 if (ret < 0)
625 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300626 }
627
628 if (lr->end == INT_MAX)
629 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100630 while (l <= lr->end) {
631 ret = show_one_line_or_eof(fp, l++ - lr->offset);
632 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100633 break;
634 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400635end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300636 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400637 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300638}
639
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000640int show_line_range(struct line_range *lr, const char *module)
641{
642 int ret;
643
644 ret = init_symbol_maps(false);
645 if (ret < 0)
646 return ret;
647 ret = __show_line_range(lr, module);
648 exit_symbol_maps();
649
650 return ret;
651}
652
Masami Hiramatsuff741782011-06-27 16:27:39 +0900653static int show_available_vars_at(struct debuginfo *dinfo,
654 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900655 int max_vls, struct strfilter *_filter,
656 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900657{
658 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900659 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900660 struct str_node *node;
661 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900662 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900663
664 buf = synthesize_perf_probe_point(&pev->point);
665 if (!buf)
666 return -EINVAL;
667 pr_debug("Searching variables at %s\n", buf);
668
Masami Hiramatsuff741782011-06-27 16:27:39 +0900669 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
670 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900671 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900672 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900673 goto end;
674 }
675 /* Some variables are found */
676 fprintf(stdout, "Available variables at %s\n", buf);
677 for (i = 0; i < ret; i++) {
678 vl = &vls[i];
679 /*
680 * A probe point might be converted to
681 * several trace points.
682 */
683 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
684 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300685 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900686 nvars = 0;
687 if (vl->vars) {
688 strlist__for_each(node, vl->vars) {
689 var = strchr(node->s, '\t') + 1;
690 if (strfilter__compare(_filter, var)) {
691 fprintf(stdout, "\t\t%s\n", node->s);
692 nvars++;
693 }
694 }
695 strlist__delete(vl->vars);
696 }
697 if (nvars == 0)
698 fprintf(stdout, "\t\t(No matched variables)\n");
699 }
700 free(vls);
701end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900702 free(buf);
703 return ret;
704}
705
706/* Show available variables on given probe point */
707int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900708 int max_vls, const char *module,
709 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900710{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900711 int i, ret = 0;
712 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900713
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000714 ret = init_symbol_maps(false);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900715 if (ret < 0)
716 return ret;
717
Masami Hiramatsuff741782011-06-27 16:27:39 +0900718 dinfo = open_debuginfo(module);
719 if (!dinfo) {
720 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000721 ret = -ENOENT;
722 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900723 }
724
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900725 setup_pager();
726
Masami Hiramatsuff741782011-06-27 16:27:39 +0900727 for (i = 0; i < npevs && ret >= 0; i++)
728 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900729 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730
731 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000732out:
733 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900734 return ret;
735}
736
Ingo Molnar89fe8082013-09-30 12:07:11 +0200737#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530739static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900740 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300741{
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530742 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743}
744
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530745static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300746 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300747 int max_tevs __maybe_unused,
748 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300749{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400750 if (perf_probe_event_need_dwarf(pev)) {
751 pr_warning("Debuginfo-analysis is not supported.\n");
752 return -ENOSYS;
753 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530754
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300755 return 0;
756}
757
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300758int show_line_range(struct line_range *lr __maybe_unused,
759 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300760{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400761 pr_warning("Debuginfo-analysis is not supported.\n");
762 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300763}
764
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300765int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
766 int npevs __maybe_unused, int max_vls __maybe_unused,
767 const char *module __maybe_unused,
768 struct strfilter *filter __maybe_unused,
769 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900770{
771 pr_warning("Debuginfo-analysis is not supported.\n");
772 return -ENOSYS;
773}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400774#endif
775
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000776void line_range__clear(struct line_range *lr)
777{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000778 free(lr->function);
779 free(lr->file);
780 free(lr->path);
781 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000782 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000783 memset(lr, 0, sizeof(*lr));
784}
785
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000786int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000787{
788 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000789 lr->line_list = intlist__new(NULL);
790 if (!lr->line_list)
791 return -ENOMEM;
792 else
793 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000794}
795
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100796static int parse_line_num(char **ptr, int *val, const char *what)
797{
798 const char *start = *ptr;
799
800 errno = 0;
801 *val = strtol(*ptr, ptr, 0);
802 if (errno || *ptr == start) {
803 semantic_error("'%s' is not a valid number.\n", what);
804 return -EINVAL;
805 }
806 return 0;
807}
808
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100809/*
810 * Stuff 'lr' according to the line range described by 'arg'.
811 * The line range syntax is described by:
812 *
813 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900814 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100815 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400816int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500817{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900818 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100819 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100820
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100821 if (!name)
822 return -ENOMEM;
823
824 lr->start = 0;
825 lr->end = INT_MAX;
826
827 range = strchr(name, ':');
828 if (range) {
829 *range++ = '\0';
830
831 err = parse_line_num(&range, &lr->start, "start line");
832 if (err)
833 goto err;
834
835 if (*range == '+' || *range == '-') {
836 const char c = *range++;
837
838 err = parse_line_num(&range, &lr->end, "end line");
839 if (err)
840 goto err;
841
842 if (c == '+') {
843 lr->end += lr->start;
844 /*
845 * Adjust the number of lines here.
846 * If the number of lines == 1, the
847 * the end of line should be equal to
848 * the start of line.
849 */
850 lr->end--;
851 }
852 }
853
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400854 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100855
856 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400857 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500858 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400859 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100860 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100862 if (*range != '\0') {
863 semantic_error("Tailing with invalid str '%s'.\n", range);
864 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400866 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400867
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900868 file = strchr(name, '@');
869 if (file) {
870 *file = '\0';
871 lr->file = strdup(++file);
872 if (lr->file == NULL) {
873 err = -ENOMEM;
874 goto err;
875 }
876 lr->function = name;
877 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100878 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500879 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100880 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400881
882 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100883err:
884 free(name);
885 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500886}
887
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500888/* Check the name is good for event/group */
889static bool check_event_name(const char *name)
890{
891 if (!isalpha(*name) && *name != '_')
892 return false;
893 while (*++name != '\0') {
894 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
895 return false;
896 }
897 return true;
898}
899
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500900/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400901static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500902{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400903 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500904 char *ptr, *tmp;
905 char c, nc = 0;
906 /*
907 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500908 * perf probe [EVENT=]SRC[:LN|;PTN]
909 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500910 *
911 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500912 */
913
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500914 ptr = strpbrk(arg, ";=@+%");
915 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500916 *ptr = '\0';
917 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400918 if (strchr(arg, ':')) {
919 semantic_error("Group name is not supported yet.\n");
920 return -ENOTSUP;
921 }
922 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500923 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400924 "follow C symbol-naming rule.\n", arg);
925 return -EINVAL;
926 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400927 pev->event = strdup(arg);
928 if (pev->event == NULL)
929 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400930 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500931 arg = tmp;
932 }
933
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500934 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500935 if (ptr) {
936 nc = *ptr;
937 *ptr++ = '\0';
938 }
939
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400940 tmp = strdup(arg);
941 if (tmp == NULL)
942 return -ENOMEM;
943
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500944 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400945 if (strchr(tmp, '.')) /* File */
946 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500947 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400948 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500949
950 /* Parse other options */
951 while (ptr) {
952 arg = ptr;
953 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500954 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400955 pp->lazy_line = strdup(arg);
956 if (pp->lazy_line == NULL)
957 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500958 break;
959 }
960 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500961 if (ptr) {
962 nc = *ptr;
963 *ptr++ = '\0';
964 }
965 switch (c) {
966 case ':': /* Line number */
967 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400968 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500969 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400970 " in line number.\n");
971 return -EINVAL;
972 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500973 break;
974 case '+': /* Byte offset from a symbol */
975 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400976 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500977 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400978 " in offset.\n");
979 return -EINVAL;
980 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500981 break;
982 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400983 if (pp->file) {
984 semantic_error("SRC@SRC is not allowed.\n");
985 return -EINVAL;
986 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400987 pp->file = strdup(arg);
988 if (pp->file == NULL)
989 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500990 break;
991 case '%': /* Probe places */
992 if (strcmp(arg, "return") == 0) {
993 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400994 } else { /* Others not supported yet */
995 semantic_error("%%%s is not supported.\n", arg);
996 return -ENOTSUP;
997 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500998 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400999 default: /* Buggy case */
1000 pr_err("This program has a bug at %s:%d.\n",
1001 __FILE__, __LINE__);
1002 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001003 break;
1004 }
1005 }
1006
1007 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001008 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001009 semantic_error("Lazy pattern can't be used with"
1010 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001011 return -EINVAL;
1012 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001013
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001014 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001015 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001016 return -EINVAL;
1017 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001018
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001019 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001020 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001021 return -EINVAL;
1022 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001023
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001024 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001025 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001026 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001027 return -EINVAL;
1028 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001029
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001030 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001031 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001032 return -EINVAL;
1033 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001034
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001035 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001036 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001037 return -EINVAL;
1038 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001039
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001041 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001042 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001043 return -EINVAL;
1044 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001045
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001046 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001047 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1048 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001050}
1051
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001052/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001054{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001055 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001056 struct perf_probe_arg_field **fieldp;
1057
1058 pr_debug("parsing arg: %s into ", str);
1059
Masami Hiramatsu48481932010-04-12 13:16:53 -04001060 tmp = strchr(str, '=');
1061 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001062 arg->name = strndup(str, tmp - str);
1063 if (arg->name == NULL)
1064 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001065 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001066 str = tmp + 1;
1067 }
1068
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001069 tmp = strchr(str, ':');
1070 if (tmp) { /* Type setting */
1071 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001072 arg->type = strdup(tmp + 1);
1073 if (arg->type == NULL)
1074 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001075 pr_debug("type:%s ", arg->type);
1076 }
1077
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001078 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001079 if (!is_c_varname(str) || !tmp) {
1080 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001081 arg->var = strdup(str);
1082 if (arg->var == NULL)
1083 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001084 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001086 }
1087
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001088 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001089 arg->var = strndup(str, tmp - str);
1090 if (arg->var == NULL)
1091 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001092 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001093 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001094 fieldp = &arg->field;
1095
1096 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001097 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1098 if (*fieldp == NULL)
1099 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001100 if (*tmp == '[') { /* Array */
1101 str = tmp;
1102 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001103 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001104 if (*tmp != ']' || tmp == str + 1) {
1105 semantic_error("Array index must be a"
1106 " number.\n");
1107 return -EINVAL;
1108 }
1109 tmp++;
1110 if (*tmp == '\0')
1111 tmp = NULL;
1112 } else { /* Structure */
1113 if (*tmp == '.') {
1114 str = tmp + 1;
1115 (*fieldp)->ref = false;
1116 } else if (tmp[1] == '>') {
1117 str = tmp + 2;
1118 (*fieldp)->ref = true;
1119 } else {
1120 semantic_error("Argument parse error: %s\n",
1121 str);
1122 return -EINVAL;
1123 }
1124 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001125 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001126 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001127 (*fieldp)->name = strndup(str, tmp - str);
1128 if ((*fieldp)->name == NULL)
1129 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001130 if (*str != '[')
1131 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001132 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1133 fieldp = &(*fieldp)->next;
1134 }
1135 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001136 (*fieldp)->name = strdup(str);
1137 if ((*fieldp)->name == NULL)
1138 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001139 if (*str != '[')
1140 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001141 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001142
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001143 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001144 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001145 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001146 if (arg->name == NULL)
1147 return -ENOMEM;
1148 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001149 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001150}
1151
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001152/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001153int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001154{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001155 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001156 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001157
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001158 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 if (!argv) {
1160 pr_debug("Failed to split arguments.\n");
1161 return -ENOMEM;
1162 }
1163 if (argc - 1 > MAX_PROBE_ARGS) {
1164 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1165 ret = -ERANGE;
1166 goto out;
1167 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001168 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001169 ret = parse_perf_probe_point(argv[0], pev);
1170 if (ret < 0)
1171 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001172
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001173 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001174 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001175 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1176 if (pev->args == NULL) {
1177 ret = -ENOMEM;
1178 goto out;
1179 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001180 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1181 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1182 if (ret >= 0 &&
1183 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001185 " kretprobe.\n");
1186 ret = -EINVAL;
1187 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001188 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001190 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191
1192 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001193}
1194
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001195/* Return true if this perf_probe_event requires debuginfo */
1196bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001197{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001198 int i;
1199
1200 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1201 return true;
1202
1203 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001204 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205 return true;
1206
1207 return false;
1208}
1209
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301210/* Parse probe_events event into struct probe_point */
1211static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001212 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001213{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301214 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001215 char pr;
1216 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001217 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001218 int ret, i, argc;
1219 char **argv;
1220
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301221 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 if (!argv) {
1224 pr_debug("Failed to split arguments.\n");
1225 return -ENOMEM;
1226 }
1227 if (argc < 2) {
1228 semantic_error("Too few probe arguments.\n");
1229 ret = -ERANGE;
1230 goto out;
1231 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001232
1233 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001234 argv0_str = strdup(argv[0]);
1235 if (argv0_str == NULL) {
1236 ret = -ENOMEM;
1237 goto out;
1238 }
1239 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1240 fmt2_str = strtok_r(NULL, "/", &fmt);
1241 fmt3_str = strtok_r(NULL, " \t", &fmt);
1242 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1243 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244 semantic_error("Failed to parse event name: %s\n", argv[0]);
1245 ret = -EINVAL;
1246 goto out;
1247 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001248 pr = fmt1_str[0];
1249 tev->group = strdup(fmt2_str);
1250 tev->event = strdup(fmt3_str);
1251 if (tev->group == NULL || tev->event == NULL) {
1252 ret = -ENOMEM;
1253 goto out;
1254 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001255 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001256
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001257 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001258
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001259 /* Scan module name(if there), function name and offset */
1260 p = strchr(argv[1], ':');
1261 if (p) {
1262 tp->module = strndup(argv[1], p - argv[1]);
1263 p++;
1264 } else
1265 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001266 fmt1_str = strtok_r(p, "+", &fmt);
1267 tp->symbol = strdup(fmt1_str);
1268 if (tp->symbol == NULL) {
1269 ret = -ENOMEM;
1270 goto out;
1271 }
1272 fmt2_str = strtok_r(NULL, "", &fmt);
1273 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001274 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001275 else
1276 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001277
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301279 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001280 if (tev->args == NULL) {
1281 ret = -ENOMEM;
1282 goto out;
1283 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001285 p = strchr(argv[i + 2], '=');
1286 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001287 *p++ = '\0';
1288 else
1289 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001290 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001291 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001292 tev->args[i].value = strdup(p);
1293 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1294 ret = -ENOMEM;
1295 goto out;
1296 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001297 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001298 ret = 0;
1299out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001300 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001301 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001302 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001303}
1304
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001305/* Compose only probe arg */
1306int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1307{
1308 struct perf_probe_arg_field *field = pa->field;
1309 int ret;
1310 char *tmp = buf;
1311
Masami Hiramatsu48481932010-04-12 13:16:53 -04001312 if (pa->name && pa->var)
1313 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1314 else
1315 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001316 if (ret <= 0)
1317 goto error;
1318 tmp += ret;
1319 len -= ret;
1320
1321 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001322 if (field->name[0] == '[')
1323 ret = e_snprintf(tmp, len, "%s", field->name);
1324 else
1325 ret = e_snprintf(tmp, len, "%s%s",
1326 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001327 if (ret <= 0)
1328 goto error;
1329 tmp += ret;
1330 len -= ret;
1331 field = field->next;
1332 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001333
1334 if (pa->type) {
1335 ret = e_snprintf(tmp, len, ":%s", pa->type);
1336 if (ret <= 0)
1337 goto error;
1338 tmp += ret;
1339 len -= ret;
1340 }
1341
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001342 return tmp - buf;
1343error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001344 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001345 strerror(-ret));
1346 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001347}
1348
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001349/* Compose only probe point (not argument) */
1350static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001351{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001352 char *buf, *tmp;
1353 char offs[32] = "", line[32] = "", file[32] = "";
1354 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001355
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001356 buf = zalloc(MAX_CMDLEN);
1357 if (buf == NULL) {
1358 ret = -ENOMEM;
1359 goto error;
1360 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001361 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001362 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001363 if (ret <= 0)
1364 goto error;
1365 }
1366 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001367 ret = e_snprintf(line, 32, ":%d", pp->line);
1368 if (ret <= 0)
1369 goto error;
1370 }
1371 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001372 tmp = pp->file;
1373 len = strlen(tmp);
1374 if (len > 30) {
1375 tmp = strchr(pp->file + len - 30, '/');
1376 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1377 }
1378 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001379 if (ret <= 0)
1380 goto error;
1381 }
1382
1383 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001384 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1385 offs, pp->retprobe ? "%return" : "", line,
1386 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001387 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001388 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001389 if (ret <= 0)
1390 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001391
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001392 return buf;
1393error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001394 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395 strerror(-ret));
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001396 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398}
1399
1400#if 0
1401char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1402{
1403 char *buf;
1404 int i, len, ret;
1405
1406 buf = synthesize_perf_probe_point(&pev->point);
1407 if (!buf)
1408 return NULL;
1409
1410 len = strlen(buf);
1411 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001412 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001413 pev->args[i].name);
1414 if (ret <= 0) {
1415 free(buf);
1416 return NULL;
1417 }
1418 len += ret;
1419 }
1420
1421 return buf;
1422}
1423#endif
1424
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301425static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426 char **buf, size_t *buflen,
1427 int depth)
1428{
1429 int ret;
1430 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301431 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001432 buflen, depth + 1);
1433 if (depth < 0)
1434 goto out;
1435 }
1436
1437 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1438 if (ret < 0)
1439 depth = ret;
1440 else {
1441 *buf += ret;
1442 *buflen -= ret;
1443 }
1444out:
1445 return depth;
1446
1447}
1448
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301449static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001450 char *buf, size_t buflen)
1451{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301452 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 int ret, depth = 0;
1454 char *tmp = buf;
1455
1456 /* Argument name or separator */
1457 if (arg->name)
1458 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1459 else
1460 ret = e_snprintf(buf, buflen, " ");
1461 if (ret < 0)
1462 return ret;
1463 buf += ret;
1464 buflen -= ret;
1465
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001466 /* Special case: @XXX */
1467 if (arg->value[0] == '@' && arg->ref)
1468 ref = ref->next;
1469
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001470 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001471 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301472 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001473 &buflen, 1);
1474 if (depth < 0)
1475 return depth;
1476 }
1477
1478 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001479 if (arg->value[0] == '@' && arg->ref)
1480 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1481 arg->ref->offset);
1482 else
1483 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001484 if (ret < 0)
1485 return ret;
1486 buf += ret;
1487 buflen -= ret;
1488
1489 /* Closing */
1490 while (depth--) {
1491 ret = e_snprintf(buf, buflen, ")");
1492 if (ret < 0)
1493 return ret;
1494 buf += ret;
1495 buflen -= ret;
1496 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001497 /* Print argument type */
1498 if (arg->type) {
1499 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1500 if (ret <= 0)
1501 return ret;
1502 buf += ret;
1503 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001504
1505 return buf - tmp;
1506}
1507
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301508char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301510 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001511 char *buf;
1512 int i, len, ret;
1513
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001514 buf = zalloc(MAX_CMDLEN);
1515 if (buf == NULL)
1516 return NULL;
1517
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301518 if (tev->uprobes)
1519 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1520 tp->retprobe ? 'r' : 'p',
1521 tev->group, tev->event,
1522 tp->module, tp->symbol);
1523 else
1524 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1525 tp->retprobe ? 'r' : 'p',
1526 tev->group, tev->event,
1527 tp->module ?: "", tp->module ? ":" : "",
1528 tp->symbol, tp->offset);
1529
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001530 if (len <= 0)
1531 goto error;
1532
1533 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301534 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001535 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001536 if (ret <= 0)
1537 goto error;
1538 len += ret;
1539 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001540
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001541 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001542error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001543 free(buf);
1544 return NULL;
1545}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001546
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301547static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301548 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001549{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001550 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001551 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001552
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001553 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001554 pev->event = strdup(tev->event);
1555 pev->group = strdup(tev->group);
1556 if (pev->event == NULL || pev->group == NULL)
1557 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001558
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001559 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301560 if (is_kprobe)
1561 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1562 else
1563 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1564
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001565 if (ret < 0)
1566 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001567
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001568 /* Convert trace_arg to probe_arg */
1569 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001570 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1571 if (pev->args == NULL)
1572 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001573 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001574 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001575 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001576 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301577 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001578 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001579 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001580 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001581 if (pev->args[i].name == NULL && ret >= 0)
1582 ret = -ENOMEM;
1583 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001584
1585 if (ret < 0)
1586 clear_perf_probe_event(pev);
1587
1588 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001589}
1590
1591void clear_perf_probe_event(struct perf_probe_event *pev)
1592{
1593 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001594 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001595 int i;
1596
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001597 free(pev->event);
1598 free(pev->group);
1599 free(pp->file);
1600 free(pp->function);
1601 free(pp->lazy_line);
1602
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001603 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001604 free(pev->args[i].name);
1605 free(pev->args[i].var);
1606 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001607 field = pev->args[i].field;
1608 while (field) {
1609 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001610 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001611 free(field);
1612 field = next;
1613 }
1614 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001615 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001616 memset(pev, 0, sizeof(*pev));
1617}
1618
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301619static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001620{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301621 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001622 int i;
1623
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001624 free(tev->event);
1625 free(tev->group);
1626 free(tev->point.symbol);
1627 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001628 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001629 free(tev->args[i].name);
1630 free(tev->args[i].value);
1631 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632 ref = tev->args[i].ref;
1633 while (ref) {
1634 next = ref->next;
1635 free(ref);
1636 ref = next;
1637 }
1638 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001639 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001640 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001641}
1642
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301643static void print_warn_msg(const char *file, bool is_kprobe)
1644{
1645
1646 if (errno == ENOENT) {
1647 const char *config;
1648
1649 if (!is_kprobe)
1650 config = "CONFIG_UPROBE_EVENTS";
1651 else
1652 config = "CONFIG_KPROBE_EVENTS";
1653
1654 pr_warning("%s file does not exist - please rebuild kernel"
1655 " with %s.\n", file, config);
1656 } else
1657 pr_warning("Failed to open %s file: %s\n", file,
1658 strerror(errno));
1659}
1660
1661static int open_probe_events(const char *trace_file, bool readwrite,
1662 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001663{
1664 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001665 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001666 int ret;
1667
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001668 __debugfs = debugfs_find_mountpoint();
1669 if (__debugfs == NULL) {
1670 pr_warning("Debugfs is not mounted.\n");
1671 return -ENOENT;
1672 }
1673
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301674 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001675 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001676 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001677 if (readwrite && !probe_event_dry_run)
1678 ret = open(buf, O_RDWR, O_APPEND);
1679 else
1680 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001681
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301682 if (ret < 0)
1683 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001684 }
1685 return ret;
1686}
1687
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301688static int open_kprobe_events(bool readwrite)
1689{
1690 return open_probe_events("tracing/kprobe_events", readwrite, true);
1691}
1692
1693static int open_uprobe_events(bool readwrite)
1694{
1695 return open_probe_events("tracing/uprobe_events", readwrite, false);
1696}
1697
1698/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301699static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001700{
1701 int ret, idx;
1702 FILE *fp;
1703 char buf[MAX_CMDLEN];
1704 char *p;
1705 struct strlist *sl;
1706
1707 sl = strlist__new(true, NULL);
1708
1709 fp = fdopen(dup(fd), "r");
1710 while (!feof(fp)) {
1711 p = fgets(buf, MAX_CMDLEN, fp);
1712 if (!p)
1713 break;
1714
1715 idx = strlen(p) - 1;
1716 if (p[idx] == '\n')
1717 p[idx] = '\0';
1718 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001719 if (ret < 0) {
1720 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1721 strlist__delete(sl);
1722 return NULL;
1723 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001724 }
1725 fclose(fp);
1726
1727 return sl;
1728}
1729
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001730/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001731static int show_perf_probe_event(struct perf_probe_event *pev,
1732 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001733{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001734 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001735 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001736 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001737
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738 /* Synthesize only event probe point */
1739 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001740 if (!place)
1741 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001742
1743 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001744 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001745 return ret;
1746
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001747 printf(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001748 if (module)
1749 printf(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001750
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001751 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001752 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001753 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001754 ret = synthesize_perf_probe_arg(&pev->args[i],
1755 buf, 128);
1756 if (ret < 0)
1757 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001758 printf(" %s", buf);
1759 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001760 }
1761 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001763 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001764}
1765
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301766static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001767{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301768 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301769 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001770 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771 struct strlist *rawlist;
1772 struct str_node *ent;
1773
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 memset(&tev, 0, sizeof(tev));
1775 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001776
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301777 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001778 if (!rawlist)
1779 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001780
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001781 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301782 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301784 ret = convert_to_perf_probe_event(&tev, &pev,
1785 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001786 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001787 ret = show_perf_probe_event(&pev,
1788 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001789 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001790 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301791 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001792 if (ret < 0)
1793 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001794 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001795 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001796
1797 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001798}
1799
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301800/* List up current perf-probe events */
1801int show_perf_probe_events(void)
1802{
1803 int fd, ret;
1804
1805 setup_pager();
1806 fd = open_kprobe_events(false);
1807
1808 if (fd < 0)
1809 return fd;
1810
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001811 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301812 if (ret < 0)
1813 return ret;
1814
1815 ret = __show_perf_probe_events(fd, true);
1816 close(fd);
1817
1818 fd = open_uprobe_events(false);
1819 if (fd >= 0) {
1820 ret = __show_perf_probe_events(fd, false);
1821 close(fd);
1822 }
1823
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001824 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301825 return ret;
1826}
1827
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001828/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301829static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001830{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001831 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001832 struct strlist *sl, *rawlist;
1833 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301834 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001835 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001836
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001837 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301838 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001839 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001840 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301841 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001842 if (ret < 0)
1843 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001844 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001845 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1846 tev.event);
1847 if (ret >= 0)
1848 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001849 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001850 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301851 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001852 if (ret < 0)
1853 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001854 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001855 strlist__delete(rawlist);
1856
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001857 if (ret < 0) {
1858 strlist__delete(sl);
1859 return NULL;
1860 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001861 return sl;
1862}
1863
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301864static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001865{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001866 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301867 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001868
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001869 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301870 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001871 return -EINVAL;
1872 }
1873
Masami Hiramatsufa282442009-12-08 17:03:23 -05001874 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001875 if (!probe_event_dry_run) {
1876 ret = write(fd, buf, strlen(buf));
1877 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001878 pr_warning("Failed to write event: %s\n",
1879 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001880 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001882 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001883}
1884
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001885static int get_new_event_name(char *buf, size_t len, const char *base,
1886 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001887{
1888 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001889
1890 /* Try no suffix */
1891 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892 if (ret < 0) {
1893 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1894 return ret;
1895 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001896 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001897 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001898
Masami Hiramatsud761b082009-12-15 10:32:25 -05001899 if (!allow_suffix) {
1900 pr_warning("Error: event \"%s\" already exists. "
1901 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001902 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001903 }
1904
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001905 /* Try to add suffix */
1906 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001907 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001908 if (ret < 0) {
1909 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1910 return ret;
1911 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001912 if (!strlist__has_entry(namelist, buf))
1913 break;
1914 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001915 if (i == MAX_EVENT_INDEX) {
1916 pr_warning("Too many events are on the same function.\n");
1917 ret = -ERANGE;
1918 }
1919
1920 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001921}
1922
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301923static int __add_probe_trace_events(struct perf_probe_event *pev,
1924 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001925 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001926{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001927 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301928 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929 char buf[64];
1930 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001931 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001932
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301933 if (pev->uprobes)
1934 fd = open_uprobe_events(true);
1935 else
1936 fd = open_kprobe_events(true);
1937
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001938 if (fd < 0)
1939 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001940 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301941 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001942 if (!namelist) {
1943 pr_debug("Failed to get current event list.\n");
1944 return -EIO;
1945 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001946
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001947 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301948 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001949 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001950 tev = &tevs[i];
1951 if (pev->event)
1952 event = pev->event;
1953 else
1954 if (pev->point.function)
1955 event = pev->point.function;
1956 else
1957 event = tev->point.symbol;
1958 if (pev->group)
1959 group = pev->group;
1960 else
1961 group = PERFPROBE_GROUP;
1962
1963 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001964 ret = get_new_event_name(buf, 64, event,
1965 namelist, allow_suffix);
1966 if (ret < 0)
1967 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001968 event = buf;
1969
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001970 tev->event = strdup(event);
1971 tev->group = strdup(group);
1972 if (tev->event == NULL || tev->group == NULL) {
1973 ret = -ENOMEM;
1974 break;
1975 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301976 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001977 if (ret < 0)
1978 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001979 /* Add added event name to namelist */
1980 strlist__add(namelist, event);
1981
1982 /* Trick here - save current event/group */
1983 event = pev->event;
1984 group = pev->group;
1985 pev->event = tev->event;
1986 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00001987 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001988 /* Trick here - restore current event/group */
1989 pev->event = (char *)event;
1990 pev->group = (char *)group;
1991
1992 /*
1993 * Probes after the first probe which comes from same
1994 * user input are always allowed to add suffix, because
1995 * there might be several addresses corresponding to
1996 * one code line.
1997 */
1998 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001999 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002000
2001 if (ret >= 0) {
2002 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302003 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002004 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2005 tev->event);
2006 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002007
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002008 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002009 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002010 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002011}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002012
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302013static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2014 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302015 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002016{
2017 struct symbol *sym;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002018 int ret, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302019 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002020
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002021 if (pev->uprobes && !pev->group) {
2022 /* Replace group name if not given */
2023 ret = convert_exec_to_group(target, &pev->group);
2024 if (ret != 0) {
2025 pr_warning("Failed to make a group name.\n");
2026 return ret;
2027 }
2028 }
2029
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002030 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302031 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002032 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002033 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002034
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002035 if (pev->uprobes) {
2036 ret = convert_name_to_addr(pev, target);
2037 if (ret < 0)
2038 return ret;
2039 }
2040
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002041 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302042 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002043 if (tev == NULL)
2044 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002045
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002046 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002047 tev->point.symbol = strdup(pev->point.function);
2048 if (tev->point.symbol == NULL) {
2049 ret = -ENOMEM;
2050 goto error;
2051 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002052
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302053 if (target) {
2054 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08002055 if (tev->point.module == NULL) {
2056 ret = -ENOMEM;
2057 goto error;
2058 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002059 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002060
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002061 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09002062 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002063 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302064 tev->uprobes = pev->uprobes;
2065
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002066 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302067 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002068 * tev->nargs);
2069 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002070 ret = -ENOMEM;
2071 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002072 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002073 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002074 if (pev->args[i].name) {
2075 tev->args[i].name = strdup(pev->args[i].name);
2076 if (tev->args[i].name == NULL) {
2077 ret = -ENOMEM;
2078 goto error;
2079 }
2080 }
2081 tev->args[i].value = strdup(pev->args[i].var);
2082 if (tev->args[i].value == NULL) {
2083 ret = -ENOMEM;
2084 goto error;
2085 }
2086 if (pev->args[i].type) {
2087 tev->args[i].type = strdup(pev->args[i].type);
2088 if (tev->args[i].type == NULL) {
2089 ret = -ENOMEM;
2090 goto error;
2091 }
2092 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002093 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002094 }
2095
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302096 if (pev->uprobes)
2097 return 1;
2098
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002099 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002100 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002101 if (!sym) {
2102 pr_warning("Kernel symbol \'%s\' not found.\n",
2103 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002104 ret = -ENOENT;
2105 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05302106 } else if (tev->point.offset > sym->end - sym->start) {
2107 pr_warning("Offset specified is greater than size of %s\n",
2108 tev->point.symbol);
2109 ret = -ENOENT;
2110 goto error;
2111
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002112 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002113
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002114 return 1;
2115error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302116 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002117 free(tev);
2118 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002119 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002120}
2121
2122struct __event_package {
2123 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302124 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002125 int ntevs;
2126};
2127
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302129 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002130{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002131 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002132 struct __event_package *pkgs;
2133
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302134 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002135 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302136
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002137 if (pkgs == NULL)
2138 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002139
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002140 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002141 if (ret < 0) {
2142 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002143 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002144 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002145
2146 /* Loop 1: convert all events */
2147 for (i = 0; i < npevs; i++) {
2148 pkgs[i].pev = &pevs[i];
2149 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302150 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002151 &pkgs[i].tevs,
2152 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302153 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002154 if (ret < 0)
2155 goto end;
2156 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002157 }
2158
2159 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002160 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302161 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002163 if (ret < 0)
2164 break;
2165 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002166end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002167 /* Loop 3: cleanup and free trace events */
2168 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302170 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002171 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002172 }
2173 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002174 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002175
2176 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002177}
2178
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302179static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002180{
2181 char *p;
2182 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002183 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002184
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302185 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002186 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2187 if (ret < 0)
2188 goto error;
2189
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002190 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002191 if (!p) {
2192 pr_debug("Internal error: %s should have ':' but not.\n",
2193 ent->s);
2194 ret = -ENOTSUP;
2195 goto error;
2196 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002197 *p = '/';
2198
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002199 pr_debug("Writing event: %s\n", buf);
2200 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002201 if (ret < 0) {
2202 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002204 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002205
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302206 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207 return 0;
2208error:
2209 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2210 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002211}
2212
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302213static int del_trace_probe_event(int fd, const char *buf,
2214 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002215{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002216 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002218
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002219 if (strpbrk(buf, "*?")) { /* Glob-exp */
2220 strlist__for_each_safe(ent, n, namelist)
2221 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302222 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002223 if (ret < 0)
2224 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002225 strlist__remove(namelist, ent);
2226 }
2227 } else {
2228 ent = strlist__find(namelist, buf);
2229 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302230 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002231 if (ret >= 0)
2232 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002233 }
2234 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002235
2236 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002237}
2238
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002239int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002240{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302241 int ret = -1, ufd = -1, kfd = -1;
2242 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002243 const char *group, *event;
2244 char *p, *str;
2245 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302246 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002247
Masami Hiramatsufa282442009-12-08 17:03:23 -05002248 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302249 kfd = open_kprobe_events(true);
2250 if (kfd < 0)
2251 return kfd;
2252
2253 namelist = get_probe_trace_event_names(kfd, true);
2254 ufd = open_uprobe_events(true);
2255
2256 if (ufd >= 0)
2257 unamelist = get_probe_trace_event_names(ufd, true);
2258
2259 if (namelist == NULL && unamelist == NULL)
2260 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002261
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002262 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002263 str = strdup(ent->s);
2264 if (str == NULL) {
2265 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302266 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002267 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002268 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002269 p = strchr(str, ':');
2270 if (p) {
2271 group = str;
2272 *p = '\0';
2273 event = p + 1;
2274 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002275 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002276 event = str;
2277 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302278
2279 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2280 if (ret < 0) {
2281 pr_err("Failed to copy event.");
2282 free(str);
2283 goto error;
2284 }
2285
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002286 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302287
2288 if (namelist)
2289 ret = del_trace_probe_event(kfd, buf, namelist);
2290
2291 if (unamelist && ret != 0)
2292 ret = del_trace_probe_event(ufd, buf, unamelist);
2293
2294 if (ret != 0)
2295 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2296
Masami Hiramatsufa282442009-12-08 17:03:23 -05002297 free(str);
2298 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302299
2300error:
2301 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302302 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302303 close(kfd);
2304 }
2305
2306 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302307 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302308 close(ufd);
2309 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002310
2311 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002312}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302313
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002314/* TODO: don't use a global variable for filter ... */
2315static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002316
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002317/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002318 * If a symbol corresponds to a function with global binding and
2319 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002320 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002321static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002322 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002323{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002324 if (sym->binding == STB_GLOBAL &&
2325 strfilter__compare(available_func_filter, sym->name))
2326 return 0;
2327 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002328}
2329
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002330int show_available_funcs(const char *target, struct strfilter *_filter,
2331 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002332{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002333 struct map *map;
2334 int ret;
2335
2336 ret = init_symbol_maps(user);
2337 if (ret < 0)
2338 return ret;
2339
2340 /* Get a symbol map */
2341 if (user)
2342 map = dso__new_map(target);
2343 else
2344 map = kernel_get_module_map(target);
2345 if (!map) {
2346 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002347 return -EINVAL;
2348 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002349
2350 /* Load symbols with given filter */
2351 available_func_filter = _filter;
2352 if (map__load(map, filter_available_functions)) {
2353 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2354 goto end;
2355 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002356 if (!dso__sorted_by_name(map->dso, map->type))
2357 dso__sort_by_name(map->dso, map->type);
2358
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002359 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302360 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002361 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2362end:
2363 if (user) {
2364 dso__delete(map->dso);
2365 map__delete(map);
2366 }
2367 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302368
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002369 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302370}
2371
2372/*
2373 * uprobe_events only accepts address:
2374 * Convert function and any offset to address
2375 */
2376static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2377{
2378 struct perf_probe_point *pp = &pev->point;
2379 struct symbol *sym;
2380 struct map *map = NULL;
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002381 char *function = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302382 int ret = -EINVAL;
2383 unsigned long long vaddr = 0;
2384
2385 if (!pp->function) {
2386 pr_warning("No function specified for uprobes");
2387 goto out;
2388 }
2389
2390 function = strdup(pp->function);
2391 if (!function) {
2392 pr_warning("Failed to allocate memory by strdup.\n");
2393 ret = -ENOMEM;
2394 goto out;
2395 }
2396
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002397 map = dso__new_map(exec);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302398 if (!map) {
2399 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2400 goto out;
2401 }
2402 available_func_filter = strfilter__new(function, NULL);
2403 if (map__load(map, filter_available_functions)) {
2404 pr_err("Failed to load map.\n");
2405 goto out;
2406 }
2407
2408 sym = map__find_symbol_by_name(map, function, NULL);
2409 if (!sym) {
2410 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2411 goto out;
2412 }
2413
2414 if (map->start > sym->start)
2415 vaddr = map->start;
2416 vaddr += sym->start + pp->offset + map->pgoff;
2417 pp->offset = 0;
2418
2419 if (!pev->event) {
2420 pev->event = function;
2421 function = NULL;
2422 }
2423 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002424 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302425
2426 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002427 exec_copy = strdup(exec);
2428 if (!exec_copy) {
2429 ret = -ENOMEM;
2430 pr_warning("Failed to copy exec string.\n");
2431 goto out;
2432 }
2433
2434 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302435 if (ptr1) {
2436 ptr2 = strpbrk(ptr1, "-._");
2437 if (ptr2)
2438 *ptr2 = '\0';
2439 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2440 ptr1);
2441 free(ptr1);
2442 }
David Ahern1fb89442012-09-08 09:06:51 -06002443 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302444 }
2445 free(pp->function);
2446 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2447 if (!pp->function) {
2448 ret = -ENOMEM;
2449 pr_warning("Failed to allocate memory by zalloc.\n");
2450 goto out;
2451 }
2452 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2453 ret = 0;
2454
2455out:
2456 if (map) {
2457 dso__delete(map->dso);
2458 map__delete(map);
2459 }
2460 if (function)
2461 free(function);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302462 return ret;
2463}