blob: 9aa77832099ab7f36979b4c93793001e16a339b4 [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 Hiramatsu4b4da7f2010-03-22 13:10:26 -0300564 struct line_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
617 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100618 for (; ln->line > 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{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900742 struct symbol *sym;
743
744 sym = __find_kernel_function_by_name(tp->symbol, NULL);
745 if (!sym) {
746 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
747 return -ENOENT;
748 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400749
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530750 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300751}
752
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530753static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300754 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300755 int max_tevs __maybe_unused,
756 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300757{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400758 if (perf_probe_event_need_dwarf(pev)) {
759 pr_warning("Debuginfo-analysis is not supported.\n");
760 return -ENOSYS;
761 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530762
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300763 return 0;
764}
765
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300766int show_line_range(struct line_range *lr __maybe_unused,
767 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300768{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400769 pr_warning("Debuginfo-analysis is not supported.\n");
770 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300771}
772
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300773int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
774 int npevs __maybe_unused, int max_vls __maybe_unused,
775 const char *module __maybe_unused,
776 struct strfilter *filter __maybe_unused,
777 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900778{
779 pr_warning("Debuginfo-analysis is not supported.\n");
780 return -ENOSYS;
781}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400782#endif
783
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000784void line_range__clear(struct line_range *lr)
785{
786 struct line_node *ln;
787
788 free(lr->function);
789 free(lr->file);
790 free(lr->path);
791 free(lr->comp_dir);
792 while (!list_empty(&lr->line_list)) {
793 ln = list_first_entry(&lr->line_list, struct line_node, list);
794 list_del(&ln->list);
795 free(ln);
796 }
797 memset(lr, 0, sizeof(*lr));
798}
799
800void line_range__init(struct line_range *lr)
801{
802 memset(lr, 0, sizeof(*lr));
803 INIT_LIST_HEAD(&lr->line_list);
804}
805
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100806static int parse_line_num(char **ptr, int *val, const char *what)
807{
808 const char *start = *ptr;
809
810 errno = 0;
811 *val = strtol(*ptr, ptr, 0);
812 if (errno || *ptr == start) {
813 semantic_error("'%s' is not a valid number.\n", what);
814 return -EINVAL;
815 }
816 return 0;
817}
818
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100819/*
820 * Stuff 'lr' according to the line range described by 'arg'.
821 * The line range syntax is described by:
822 *
823 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900824 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100825 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400826int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500827{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900828 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100829 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100830
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100831 if (!name)
832 return -ENOMEM;
833
834 lr->start = 0;
835 lr->end = INT_MAX;
836
837 range = strchr(name, ':');
838 if (range) {
839 *range++ = '\0';
840
841 err = parse_line_num(&range, &lr->start, "start line");
842 if (err)
843 goto err;
844
845 if (*range == '+' || *range == '-') {
846 const char c = *range++;
847
848 err = parse_line_num(&range, &lr->end, "end line");
849 if (err)
850 goto err;
851
852 if (c == '+') {
853 lr->end += lr->start;
854 /*
855 * Adjust the number of lines here.
856 * If the number of lines == 1, the
857 * the end of line should be equal to
858 * the start of line.
859 */
860 lr->end--;
861 }
862 }
863
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400864 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100865
866 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400867 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500868 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100870 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400871 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100872 if (*range != '\0') {
873 semantic_error("Tailing with invalid str '%s'.\n", range);
874 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400875 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400876 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400877
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900878 file = strchr(name, '@');
879 if (file) {
880 *file = '\0';
881 lr->file = strdup(++file);
882 if (lr->file == NULL) {
883 err = -ENOMEM;
884 goto err;
885 }
886 lr->function = name;
887 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100888 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500889 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100890 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400891
892 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100893err:
894 free(name);
895 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500896}
897
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500898/* Check the name is good for event/group */
899static bool check_event_name(const char *name)
900{
901 if (!isalpha(*name) && *name != '_')
902 return false;
903 while (*++name != '\0') {
904 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
905 return false;
906 }
907 return true;
908}
909
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500910/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400911static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500912{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400913 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500914 char *ptr, *tmp;
915 char c, nc = 0;
916 /*
917 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500918 * perf probe [EVENT=]SRC[:LN|;PTN]
919 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500920 *
921 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500922 */
923
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500924 ptr = strpbrk(arg, ";=@+%");
925 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500926 *ptr = '\0';
927 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400928 if (strchr(arg, ':')) {
929 semantic_error("Group name is not supported yet.\n");
930 return -ENOTSUP;
931 }
932 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500933 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400934 "follow C symbol-naming rule.\n", arg);
935 return -EINVAL;
936 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400937 pev->event = strdup(arg);
938 if (pev->event == NULL)
939 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400940 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500941 arg = tmp;
942 }
943
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500944 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500945 if (ptr) {
946 nc = *ptr;
947 *ptr++ = '\0';
948 }
949
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400950 tmp = strdup(arg);
951 if (tmp == NULL)
952 return -ENOMEM;
953
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500954 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400955 if (strchr(tmp, '.')) /* File */
956 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500957 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400958 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500959
960 /* Parse other options */
961 while (ptr) {
962 arg = ptr;
963 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500964 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400965 pp->lazy_line = strdup(arg);
966 if (pp->lazy_line == NULL)
967 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500968 break;
969 }
970 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500971 if (ptr) {
972 nc = *ptr;
973 *ptr++ = '\0';
974 }
975 switch (c) {
976 case ':': /* Line number */
977 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400978 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500979 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400980 " in line number.\n");
981 return -EINVAL;
982 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500983 break;
984 case '+': /* Byte offset from a symbol */
985 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500987 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400988 " in offset.\n");
989 return -EINVAL;
990 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500991 break;
992 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400993 if (pp->file) {
994 semantic_error("SRC@SRC is not allowed.\n");
995 return -EINVAL;
996 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400997 pp->file = strdup(arg);
998 if (pp->file == NULL)
999 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001000 break;
1001 case '%': /* Probe places */
1002 if (strcmp(arg, "return") == 0) {
1003 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001004 } else { /* Others not supported yet */
1005 semantic_error("%%%s is not supported.\n", arg);
1006 return -ENOTSUP;
1007 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001008 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001009 default: /* Buggy case */
1010 pr_err("This program has a bug at %s:%d.\n",
1011 __FILE__, __LINE__);
1012 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001013 break;
1014 }
1015 }
1016
1017 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001018 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001019 semantic_error("Lazy pattern can't be used with"
1020 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001021 return -EINVAL;
1022 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001023
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001024 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001025 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001026 return -EINVAL;
1027 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001028
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001029 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001030 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001031 return -EINVAL;
1032 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001033
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001034 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001035 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001036 "lazy pattern.\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->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001041 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001042 return -EINVAL;
1043 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001044
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001045 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001046 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001047 return -EINVAL;
1048 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001049
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001050 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001051 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001052 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 return -EINVAL;
1054 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001055
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001056 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001057 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1058 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001059 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001060}
1061
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001062/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001063static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001064{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001065 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001066 struct perf_probe_arg_field **fieldp;
1067
1068 pr_debug("parsing arg: %s into ", str);
1069
Masami Hiramatsu48481932010-04-12 13:16:53 -04001070 tmp = strchr(str, '=');
1071 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001072 arg->name = strndup(str, tmp - str);
1073 if (arg->name == NULL)
1074 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001075 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001076 str = tmp + 1;
1077 }
1078
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001079 tmp = strchr(str, ':');
1080 if (tmp) { /* Type setting */
1081 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001082 arg->type = strdup(tmp + 1);
1083 if (arg->type == NULL)
1084 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001085 pr_debug("type:%s ", arg->type);
1086 }
1087
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001088 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001089 if (!is_c_varname(str) || !tmp) {
1090 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001091 arg->var = strdup(str);
1092 if (arg->var == NULL)
1093 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001094 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001095 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001096 }
1097
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001098 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001099 arg->var = strndup(str, tmp - str);
1100 if (arg->var == NULL)
1101 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001102 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001103 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001104 fieldp = &arg->field;
1105
1106 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001107 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1108 if (*fieldp == NULL)
1109 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001110 if (*tmp == '[') { /* Array */
1111 str = tmp;
1112 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001113 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001114 if (*tmp != ']' || tmp == str + 1) {
1115 semantic_error("Array index must be a"
1116 " number.\n");
1117 return -EINVAL;
1118 }
1119 tmp++;
1120 if (*tmp == '\0')
1121 tmp = NULL;
1122 } else { /* Structure */
1123 if (*tmp == '.') {
1124 str = tmp + 1;
1125 (*fieldp)->ref = false;
1126 } else if (tmp[1] == '>') {
1127 str = tmp + 2;
1128 (*fieldp)->ref = true;
1129 } else {
1130 semantic_error("Argument parse error: %s\n",
1131 str);
1132 return -EINVAL;
1133 }
1134 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001135 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001136 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001137 (*fieldp)->name = strndup(str, tmp - str);
1138 if ((*fieldp)->name == NULL)
1139 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001140 if (*str != '[')
1141 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001142 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1143 fieldp = &(*fieldp)->next;
1144 }
1145 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001146 (*fieldp)->name = strdup(str);
1147 if ((*fieldp)->name == NULL)
1148 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001149 if (*str != '[')
1150 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001151 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001152
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001153 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001154 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001155 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001156 if (arg->name == NULL)
1157 return -ENOMEM;
1158 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001160}
1161
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001162/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001163int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001164{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001165 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001166 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001167
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001168 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001169 if (!argv) {
1170 pr_debug("Failed to split arguments.\n");
1171 return -ENOMEM;
1172 }
1173 if (argc - 1 > MAX_PROBE_ARGS) {
1174 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1175 ret = -ERANGE;
1176 goto out;
1177 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001178 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001179 ret = parse_perf_probe_point(argv[0], pev);
1180 if (ret < 0)
1181 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001182
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001183 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001185 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1186 if (pev->args == NULL) {
1187 ret = -ENOMEM;
1188 goto out;
1189 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1191 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1192 if (ret >= 0 &&
1193 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001194 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 " kretprobe.\n");
1196 ret = -EINVAL;
1197 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001198 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001200 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201
1202 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203}
1204
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205/* Return true if this perf_probe_event requires debuginfo */
1206bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001207{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001208 int i;
1209
1210 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1211 return true;
1212
1213 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001214 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001215 return true;
1216
1217 return false;
1218}
1219
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301220/* Parse probe_events event into struct probe_point */
1221static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001222 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001223{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301224 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001225 char pr;
1226 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001227 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001228 int ret, i, argc;
1229 char **argv;
1230
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301231 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001232 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001233 if (!argv) {
1234 pr_debug("Failed to split arguments.\n");
1235 return -ENOMEM;
1236 }
1237 if (argc < 2) {
1238 semantic_error("Too few probe arguments.\n");
1239 ret = -ERANGE;
1240 goto out;
1241 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001242
1243 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001244 argv0_str = strdup(argv[0]);
1245 if (argv0_str == NULL) {
1246 ret = -ENOMEM;
1247 goto out;
1248 }
1249 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1250 fmt2_str = strtok_r(NULL, "/", &fmt);
1251 fmt3_str = strtok_r(NULL, " \t", &fmt);
1252 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1253 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254 semantic_error("Failed to parse event name: %s\n", argv[0]);
1255 ret = -EINVAL;
1256 goto out;
1257 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001258 pr = fmt1_str[0];
1259 tev->group = strdup(fmt2_str);
1260 tev->event = strdup(fmt3_str);
1261 if (tev->group == NULL || tev->event == NULL) {
1262 ret = -ENOMEM;
1263 goto out;
1264 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001268
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001269 /* Scan module name(if there), function name and offset */
1270 p = strchr(argv[1], ':');
1271 if (p) {
1272 tp->module = strndup(argv[1], p - argv[1]);
1273 p++;
1274 } else
1275 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001276 fmt1_str = strtok_r(p, "+", &fmt);
1277 tp->symbol = strdup(fmt1_str);
1278 if (tp->symbol == NULL) {
1279 ret = -ENOMEM;
1280 goto out;
1281 }
1282 fmt2_str = strtok_r(NULL, "", &fmt);
1283 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001285 else
1286 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001287
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001288 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301289 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001290 if (tev->args == NULL) {
1291 ret = -ENOMEM;
1292 goto out;
1293 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001294 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001295 p = strchr(argv[i + 2], '=');
1296 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001297 *p++ = '\0';
1298 else
1299 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001300 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001301 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001302 tev->args[i].value = strdup(p);
1303 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1304 ret = -ENOMEM;
1305 goto out;
1306 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001307 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 ret = 0;
1309out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001310 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001311 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001313}
1314
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001315/* Compose only probe arg */
1316int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1317{
1318 struct perf_probe_arg_field *field = pa->field;
1319 int ret;
1320 char *tmp = buf;
1321
Masami Hiramatsu48481932010-04-12 13:16:53 -04001322 if (pa->name && pa->var)
1323 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1324 else
1325 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001326 if (ret <= 0)
1327 goto error;
1328 tmp += ret;
1329 len -= ret;
1330
1331 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001332 if (field->name[0] == '[')
1333 ret = e_snprintf(tmp, len, "%s", field->name);
1334 else
1335 ret = e_snprintf(tmp, len, "%s%s",
1336 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001337 if (ret <= 0)
1338 goto error;
1339 tmp += ret;
1340 len -= ret;
1341 field = field->next;
1342 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001343
1344 if (pa->type) {
1345 ret = e_snprintf(tmp, len, ":%s", pa->type);
1346 if (ret <= 0)
1347 goto error;
1348 tmp += ret;
1349 len -= ret;
1350 }
1351
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001352 return tmp - buf;
1353error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001354 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 strerror(-ret));
1356 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001357}
1358
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359/* Compose only probe point (not argument) */
1360static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001361{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001362 char *buf, *tmp;
1363 char offs[32] = "", line[32] = "", file[32] = "";
1364 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001365
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001366 buf = zalloc(MAX_CMDLEN);
1367 if (buf == NULL) {
1368 ret = -ENOMEM;
1369 goto error;
1370 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001371 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001372 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001373 if (ret <= 0)
1374 goto error;
1375 }
1376 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001377 ret = e_snprintf(line, 32, ":%d", pp->line);
1378 if (ret <= 0)
1379 goto error;
1380 }
1381 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001382 tmp = pp->file;
1383 len = strlen(tmp);
1384 if (len > 30) {
1385 tmp = strchr(pp->file + len - 30, '/');
1386 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1387 }
1388 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001389 if (ret <= 0)
1390 goto error;
1391 }
1392
1393 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001394 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1395 offs, pp->retprobe ? "%return" : "", line,
1396 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001397 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001398 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001399 if (ret <= 0)
1400 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001402 return buf;
1403error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001404 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405 strerror(-ret));
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001406 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001407 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001408}
1409
1410#if 0
1411char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1412{
1413 char *buf;
1414 int i, len, ret;
1415
1416 buf = synthesize_perf_probe_point(&pev->point);
1417 if (!buf)
1418 return NULL;
1419
1420 len = strlen(buf);
1421 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001422 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001423 pev->args[i].name);
1424 if (ret <= 0) {
1425 free(buf);
1426 return NULL;
1427 }
1428 len += ret;
1429 }
1430
1431 return buf;
1432}
1433#endif
1434
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301435static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001436 char **buf, size_t *buflen,
1437 int depth)
1438{
1439 int ret;
1440 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301441 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001442 buflen, depth + 1);
1443 if (depth < 0)
1444 goto out;
1445 }
1446
1447 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1448 if (ret < 0)
1449 depth = ret;
1450 else {
1451 *buf += ret;
1452 *buflen -= ret;
1453 }
1454out:
1455 return depth;
1456
1457}
1458
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301459static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001460 char *buf, size_t buflen)
1461{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301462 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001463 int ret, depth = 0;
1464 char *tmp = buf;
1465
1466 /* Argument name or separator */
1467 if (arg->name)
1468 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1469 else
1470 ret = e_snprintf(buf, buflen, " ");
1471 if (ret < 0)
1472 return ret;
1473 buf += ret;
1474 buflen -= ret;
1475
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001476 /* Special case: @XXX */
1477 if (arg->value[0] == '@' && arg->ref)
1478 ref = ref->next;
1479
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001480 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001481 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301482 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001483 &buflen, 1);
1484 if (depth < 0)
1485 return depth;
1486 }
1487
1488 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001489 if (arg->value[0] == '@' && arg->ref)
1490 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1491 arg->ref->offset);
1492 else
1493 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494 if (ret < 0)
1495 return ret;
1496 buf += ret;
1497 buflen -= ret;
1498
1499 /* Closing */
1500 while (depth--) {
1501 ret = e_snprintf(buf, buflen, ")");
1502 if (ret < 0)
1503 return ret;
1504 buf += ret;
1505 buflen -= ret;
1506 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001507 /* Print argument type */
1508 if (arg->type) {
1509 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1510 if (ret <= 0)
1511 return ret;
1512 buf += ret;
1513 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001514
1515 return buf - tmp;
1516}
1517
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301518char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001519{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301520 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001521 char *buf;
1522 int i, len, ret;
1523
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001524 buf = zalloc(MAX_CMDLEN);
1525 if (buf == NULL)
1526 return NULL;
1527
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301528 if (tev->uprobes)
1529 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1530 tp->retprobe ? 'r' : 'p',
1531 tev->group, tev->event,
1532 tp->module, tp->symbol);
1533 else
1534 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1535 tp->retprobe ? 'r' : 'p',
1536 tev->group, tev->event,
1537 tp->module ?: "", tp->module ? ":" : "",
1538 tp->symbol, tp->offset);
1539
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001540 if (len <= 0)
1541 goto error;
1542
1543 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301544 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001545 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001546 if (ret <= 0)
1547 goto error;
1548 len += ret;
1549 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001550
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001551 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001552error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001553 free(buf);
1554 return NULL;
1555}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001556
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301557static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301558 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001559{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001560 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001561 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001562
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001563 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001564 pev->event = strdup(tev->event);
1565 pev->group = strdup(tev->group);
1566 if (pev->event == NULL || pev->group == NULL)
1567 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001568
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001569 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301570 if (is_kprobe)
1571 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1572 else
1573 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1574
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001575 if (ret < 0)
1576 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001577
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001578 /* Convert trace_arg to probe_arg */
1579 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001580 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1581 if (pev->args == NULL)
1582 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001583 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001584 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001585 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001586 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301587 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001588 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001589 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001590 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001591 if (pev->args[i].name == NULL && ret >= 0)
1592 ret = -ENOMEM;
1593 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594
1595 if (ret < 0)
1596 clear_perf_probe_event(pev);
1597
1598 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001599}
1600
1601void clear_perf_probe_event(struct perf_probe_event *pev)
1602{
1603 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001604 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605 int i;
1606
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001607 free(pev->event);
1608 free(pev->group);
1609 free(pp->file);
1610 free(pp->function);
1611 free(pp->lazy_line);
1612
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001613 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001614 free(pev->args[i].name);
1615 free(pev->args[i].var);
1616 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001617 field = pev->args[i].field;
1618 while (field) {
1619 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001620 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001621 free(field);
1622 field = next;
1623 }
1624 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001625 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001626 memset(pev, 0, sizeof(*pev));
1627}
1628
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301629static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001630{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301631 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632 int i;
1633
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001634 free(tev->event);
1635 free(tev->group);
1636 free(tev->point.symbol);
1637 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001639 free(tev->args[i].name);
1640 free(tev->args[i].value);
1641 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001642 ref = tev->args[i].ref;
1643 while (ref) {
1644 next = ref->next;
1645 free(ref);
1646 ref = next;
1647 }
1648 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001649 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001650 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001651}
1652
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301653static void print_warn_msg(const char *file, bool is_kprobe)
1654{
1655
1656 if (errno == ENOENT) {
1657 const char *config;
1658
1659 if (!is_kprobe)
1660 config = "CONFIG_UPROBE_EVENTS";
1661 else
1662 config = "CONFIG_KPROBE_EVENTS";
1663
1664 pr_warning("%s file does not exist - please rebuild kernel"
1665 " with %s.\n", file, config);
1666 } else
1667 pr_warning("Failed to open %s file: %s\n", file,
1668 strerror(errno));
1669}
1670
1671static int open_probe_events(const char *trace_file, bool readwrite,
1672 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001673{
1674 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001675 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001676 int ret;
1677
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001678 __debugfs = debugfs_find_mountpoint();
1679 if (__debugfs == NULL) {
1680 pr_warning("Debugfs is not mounted.\n");
1681 return -ENOENT;
1682 }
1683
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301684 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001685 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001686 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001687 if (readwrite && !probe_event_dry_run)
1688 ret = open(buf, O_RDWR, O_APPEND);
1689 else
1690 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001691
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301692 if (ret < 0)
1693 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001694 }
1695 return ret;
1696}
1697
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301698static int open_kprobe_events(bool readwrite)
1699{
1700 return open_probe_events("tracing/kprobe_events", readwrite, true);
1701}
1702
1703static int open_uprobe_events(bool readwrite)
1704{
1705 return open_probe_events("tracing/uprobe_events", readwrite, false);
1706}
1707
1708/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301709static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001710{
1711 int ret, idx;
1712 FILE *fp;
1713 char buf[MAX_CMDLEN];
1714 char *p;
1715 struct strlist *sl;
1716
1717 sl = strlist__new(true, NULL);
1718
1719 fp = fdopen(dup(fd), "r");
1720 while (!feof(fp)) {
1721 p = fgets(buf, MAX_CMDLEN, fp);
1722 if (!p)
1723 break;
1724
1725 idx = strlen(p) - 1;
1726 if (p[idx] == '\n')
1727 p[idx] = '\0';
1728 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001729 if (ret < 0) {
1730 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1731 strlist__delete(sl);
1732 return NULL;
1733 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001734 }
1735 fclose(fp);
1736
1737 return sl;
1738}
1739
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001740/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001741static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001742{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001743 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001744 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001745 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001746
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001747 /* Synthesize only event probe point */
1748 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001749 if (!place)
1750 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001751
1752 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001753 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001754 return ret;
1755
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001756 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001757
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001758 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001759 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001760 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001761 ret = synthesize_perf_probe_arg(&pev->args[i],
1762 buf, 128);
1763 if (ret < 0)
1764 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001765 printf(" %s", buf);
1766 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001767 }
1768 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001770 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001771}
1772
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301773static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001774{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301775 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301776 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001777 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001778 struct strlist *rawlist;
1779 struct str_node *ent;
1780
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001781 memset(&tev, 0, sizeof(tev));
1782 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001783
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301784 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001785 if (!rawlist)
1786 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001787
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001788 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301789 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001790 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301791 ret = convert_to_perf_probe_event(&tev, &pev,
1792 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001793 if (ret >= 0)
1794 ret = show_perf_probe_event(&pev);
1795 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001796 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301797 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001798 if (ret < 0)
1799 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001800 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001801 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001802
1803 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001804}
1805
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301806/* List up current perf-probe events */
1807int show_perf_probe_events(void)
1808{
1809 int fd, ret;
1810
1811 setup_pager();
1812 fd = open_kprobe_events(false);
1813
1814 if (fd < 0)
1815 return fd;
1816
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001817 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301818 if (ret < 0)
1819 return ret;
1820
1821 ret = __show_perf_probe_events(fd, true);
1822 close(fd);
1823
1824 fd = open_uprobe_events(false);
1825 if (fd >= 0) {
1826 ret = __show_perf_probe_events(fd, false);
1827 close(fd);
1828 }
1829
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001830 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301831 return ret;
1832}
1833
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001834/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301835static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001836{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001837 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001838 struct strlist *sl, *rawlist;
1839 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301840 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001841 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001842
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001843 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301844 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001845 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001846 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301847 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001848 if (ret < 0)
1849 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001850 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001851 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1852 tev.event);
1853 if (ret >= 0)
1854 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001855 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001856 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301857 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001858 if (ret < 0)
1859 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001860 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001861 strlist__delete(rawlist);
1862
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001863 if (ret < 0) {
1864 strlist__delete(sl);
1865 return NULL;
1866 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001867 return sl;
1868}
1869
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301870static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001871{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001872 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301873 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001874
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301876 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001877 return -EINVAL;
1878 }
1879
Masami Hiramatsufa282442009-12-08 17:03:23 -05001880 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001881 if (!probe_event_dry_run) {
1882 ret = write(fd, buf, strlen(buf));
1883 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001884 pr_warning("Failed to write event: %s\n",
1885 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001886 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001887 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001888 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001889}
1890
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001891static int get_new_event_name(char *buf, size_t len, const char *base,
1892 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001893{
1894 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001895
1896 /* Try no suffix */
1897 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001898 if (ret < 0) {
1899 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1900 return ret;
1901 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001902 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001903 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001904
Masami Hiramatsud761b082009-12-15 10:32:25 -05001905 if (!allow_suffix) {
1906 pr_warning("Error: event \"%s\" already exists. "
1907 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001908 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001909 }
1910
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001911 /* Try to add suffix */
1912 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001913 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001914 if (ret < 0) {
1915 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1916 return ret;
1917 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001918 if (!strlist__has_entry(namelist, buf))
1919 break;
1920 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001921 if (i == MAX_EVENT_INDEX) {
1922 pr_warning("Too many events are on the same function.\n");
1923 ret = -ERANGE;
1924 }
1925
1926 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001927}
1928
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301929static int __add_probe_trace_events(struct perf_probe_event *pev,
1930 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001931 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001932{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001933 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301934 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001935 char buf[64];
1936 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001937 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001938
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301939 if (pev->uprobes)
1940 fd = open_uprobe_events(true);
1941 else
1942 fd = open_kprobe_events(true);
1943
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001944 if (fd < 0)
1945 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001946 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301947 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001948 if (!namelist) {
1949 pr_debug("Failed to get current event list.\n");
1950 return -EIO;
1951 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001952
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001953 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301954 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001955 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001956 tev = &tevs[i];
1957 if (pev->event)
1958 event = pev->event;
1959 else
1960 if (pev->point.function)
1961 event = pev->point.function;
1962 else
1963 event = tev->point.symbol;
1964 if (pev->group)
1965 group = pev->group;
1966 else
1967 group = PERFPROBE_GROUP;
1968
1969 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001970 ret = get_new_event_name(buf, 64, event,
1971 namelist, allow_suffix);
1972 if (ret < 0)
1973 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001974 event = buf;
1975
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001976 tev->event = strdup(event);
1977 tev->group = strdup(group);
1978 if (tev->event == NULL || tev->group == NULL) {
1979 ret = -ENOMEM;
1980 break;
1981 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301982 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001983 if (ret < 0)
1984 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001985 /* Add added event name to namelist */
1986 strlist__add(namelist, event);
1987
1988 /* Trick here - save current event/group */
1989 event = pev->event;
1990 group = pev->group;
1991 pev->event = tev->event;
1992 pev->group = tev->group;
1993 show_perf_probe_event(pev);
1994 /* Trick here - restore current event/group */
1995 pev->event = (char *)event;
1996 pev->group = (char *)group;
1997
1998 /*
1999 * Probes after the first probe which comes from same
2000 * user input are always allowed to add suffix, because
2001 * there might be several addresses corresponding to
2002 * one code line.
2003 */
2004 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002005 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002006
2007 if (ret >= 0) {
2008 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302009 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002010 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2011 tev->event);
2012 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002013
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002014 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002015 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002016 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002017}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002018
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302019static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2020 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302021 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002022{
2023 struct symbol *sym;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002024 int ret, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302025 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002026
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002027 if (pev->uprobes && !pev->group) {
2028 /* Replace group name if not given */
2029 ret = convert_exec_to_group(target, &pev->group);
2030 if (ret != 0) {
2031 pr_warning("Failed to make a group name.\n");
2032 return ret;
2033 }
2034 }
2035
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002036 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302037 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002038 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002039 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002040
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002041 if (pev->uprobes) {
2042 ret = convert_name_to_addr(pev, target);
2043 if (ret < 0)
2044 return ret;
2045 }
2046
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002047 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302048 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002049 if (tev == NULL)
2050 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002051
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002052 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002053 tev->point.symbol = strdup(pev->point.function);
2054 if (tev->point.symbol == NULL) {
2055 ret = -ENOMEM;
2056 goto error;
2057 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002058
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302059 if (target) {
2060 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08002061 if (tev->point.module == NULL) {
2062 ret = -ENOMEM;
2063 goto error;
2064 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002065 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002066
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002067 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09002068 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002069 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302070 tev->uprobes = pev->uprobes;
2071
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002072 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302073 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002074 * tev->nargs);
2075 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002076 ret = -ENOMEM;
2077 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002078 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002079 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002080 if (pev->args[i].name) {
2081 tev->args[i].name = strdup(pev->args[i].name);
2082 if (tev->args[i].name == NULL) {
2083 ret = -ENOMEM;
2084 goto error;
2085 }
2086 }
2087 tev->args[i].value = strdup(pev->args[i].var);
2088 if (tev->args[i].value == NULL) {
2089 ret = -ENOMEM;
2090 goto error;
2091 }
2092 if (pev->args[i].type) {
2093 tev->args[i].type = strdup(pev->args[i].type);
2094 if (tev->args[i].type == NULL) {
2095 ret = -ENOMEM;
2096 goto error;
2097 }
2098 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002099 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002100 }
2101
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302102 if (pev->uprobes)
2103 return 1;
2104
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002105 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002106 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002107 if (!sym) {
2108 pr_warning("Kernel symbol \'%s\' not found.\n",
2109 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002110 ret = -ENOENT;
2111 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05302112 } else if (tev->point.offset > sym->end - sym->start) {
2113 pr_warning("Offset specified is greater than size of %s\n",
2114 tev->point.symbol);
2115 ret = -ENOENT;
2116 goto error;
2117
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002118 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002119
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002120 return 1;
2121error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302122 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002123 free(tev);
2124 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002125 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002126}
2127
2128struct __event_package {
2129 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302130 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002131 int ntevs;
2132};
2133
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002134int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302135 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002136{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002137 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002138 struct __event_package *pkgs;
2139
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302140 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002141 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302142
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002143 if (pkgs == NULL)
2144 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002145
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002146 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002147 if (ret < 0) {
2148 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002149 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002150 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002151
2152 /* Loop 1: convert all events */
2153 for (i = 0; i < npevs; i++) {
2154 pkgs[i].pev = &pevs[i];
2155 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302156 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002157 &pkgs[i].tevs,
2158 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302159 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002160 if (ret < 0)
2161 goto end;
2162 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002163 }
2164
2165 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002166 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302167 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002168 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002169 if (ret < 0)
2170 break;
2171 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002172end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002173 /* Loop 3: cleanup and free trace events */
2174 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002175 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302176 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002177 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002178 }
2179 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002180 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002181
2182 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002183}
2184
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302185static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002186{
2187 char *p;
2188 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002190
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302191 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002192 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2193 if (ret < 0)
2194 goto error;
2195
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002196 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002197 if (!p) {
2198 pr_debug("Internal error: %s should have ':' but not.\n",
2199 ent->s);
2200 ret = -ENOTSUP;
2201 goto error;
2202 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002203 *p = '/';
2204
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002205 pr_debug("Writing event: %s\n", buf);
2206 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002207 if (ret < 0) {
2208 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002210 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002211
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302212 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002213 return 0;
2214error:
2215 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2216 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002217}
2218
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219static int del_trace_probe_event(int fd, const char *buf,
2220 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002221{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002222 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302223 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002224
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002225 if (strpbrk(buf, "*?")) { /* Glob-exp */
2226 strlist__for_each_safe(ent, n, namelist)
2227 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302228 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002229 if (ret < 0)
2230 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002231 strlist__remove(namelist, ent);
2232 }
2233 } else {
2234 ent = strlist__find(namelist, buf);
2235 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302236 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237 if (ret >= 0)
2238 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002239 }
2240 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002241
2242 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002243}
2244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002245int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002246{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302247 int ret = -1, ufd = -1, kfd = -1;
2248 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002249 const char *group, *event;
2250 char *p, *str;
2251 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302252 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002253
Masami Hiramatsufa282442009-12-08 17:03:23 -05002254 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302255 kfd = open_kprobe_events(true);
2256 if (kfd < 0)
2257 return kfd;
2258
2259 namelist = get_probe_trace_event_names(kfd, true);
2260 ufd = open_uprobe_events(true);
2261
2262 if (ufd >= 0)
2263 unamelist = get_probe_trace_event_names(ufd, true);
2264
2265 if (namelist == NULL && unamelist == NULL)
2266 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002267
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002268 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002269 str = strdup(ent->s);
2270 if (str == NULL) {
2271 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302272 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002273 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002274 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002275 p = strchr(str, ':');
2276 if (p) {
2277 group = str;
2278 *p = '\0';
2279 event = p + 1;
2280 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002281 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002282 event = str;
2283 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302284
2285 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2286 if (ret < 0) {
2287 pr_err("Failed to copy event.");
2288 free(str);
2289 goto error;
2290 }
2291
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002292 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302293
2294 if (namelist)
2295 ret = del_trace_probe_event(kfd, buf, namelist);
2296
2297 if (unamelist && ret != 0)
2298 ret = del_trace_probe_event(ufd, buf, unamelist);
2299
2300 if (ret != 0)
2301 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2302
Masami Hiramatsufa282442009-12-08 17:03:23 -05002303 free(str);
2304 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302305
2306error:
2307 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302308 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302309 close(kfd);
2310 }
2311
2312 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302313 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302314 close(ufd);
2315 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002316
2317 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002318}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302319
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002320/* TODO: don't use a global variable for filter ... */
2321static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002322
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002323/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002324 * If a symbol corresponds to a function with global binding and
2325 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002326 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002327static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002328 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002329{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002330 if (sym->binding == STB_GLOBAL &&
2331 strfilter__compare(available_func_filter, sym->name))
2332 return 0;
2333 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002334}
2335
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302336static int __show_available_funcs(struct map *map)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002337{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002338 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002339 pr_err("Failed to load map.\n");
2340 return -EINVAL;
2341 }
2342 if (!dso__sorted_by_name(map->dso, map->type))
2343 dso__sort_by_name(map->dso, map->type);
2344
2345 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2346 return 0;
2347}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302348
2349static int available_kernel_funcs(const char *module)
2350{
2351 struct map *map;
2352 int ret;
2353
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002354 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302355 if (ret < 0)
2356 return ret;
2357
2358 map = kernel_get_module_map(module);
2359 if (!map) {
2360 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2361 return -EINVAL;
2362 }
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002363 ret = __show_available_funcs(map);
2364 exit_symbol_maps();
2365
2366 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302367}
2368
2369static int available_user_funcs(const char *target)
2370{
2371 struct map *map;
2372 int ret;
2373
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002374 ret = init_symbol_maps(true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302375 if (ret < 0)
2376 return ret;
2377
2378 map = dso__new_map(target);
2379 ret = __show_available_funcs(map);
2380 dso__delete(map->dso);
2381 map__delete(map);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002382 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302383 return ret;
2384}
2385
2386int show_available_funcs(const char *target, struct strfilter *_filter,
2387 bool user)
2388{
2389 setup_pager();
2390 available_func_filter = _filter;
2391
2392 if (!user)
2393 return available_kernel_funcs(target);
2394
2395 return available_user_funcs(target);
2396}
2397
2398/*
2399 * uprobe_events only accepts address:
2400 * Convert function and any offset to address
2401 */
2402static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2403{
2404 struct perf_probe_point *pp = &pev->point;
2405 struct symbol *sym;
2406 struct map *map = NULL;
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002407 char *function = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302408 int ret = -EINVAL;
2409 unsigned long long vaddr = 0;
2410
2411 if (!pp->function) {
2412 pr_warning("No function specified for uprobes");
2413 goto out;
2414 }
2415
2416 function = strdup(pp->function);
2417 if (!function) {
2418 pr_warning("Failed to allocate memory by strdup.\n");
2419 ret = -ENOMEM;
2420 goto out;
2421 }
2422
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002423 map = dso__new_map(exec);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302424 if (!map) {
2425 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2426 goto out;
2427 }
2428 available_func_filter = strfilter__new(function, NULL);
2429 if (map__load(map, filter_available_functions)) {
2430 pr_err("Failed to load map.\n");
2431 goto out;
2432 }
2433
2434 sym = map__find_symbol_by_name(map, function, NULL);
2435 if (!sym) {
2436 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2437 goto out;
2438 }
2439
2440 if (map->start > sym->start)
2441 vaddr = map->start;
2442 vaddr += sym->start + pp->offset + map->pgoff;
2443 pp->offset = 0;
2444
2445 if (!pev->event) {
2446 pev->event = function;
2447 function = NULL;
2448 }
2449 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002450 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302451
2452 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002453 exec_copy = strdup(exec);
2454 if (!exec_copy) {
2455 ret = -ENOMEM;
2456 pr_warning("Failed to copy exec string.\n");
2457 goto out;
2458 }
2459
2460 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302461 if (ptr1) {
2462 ptr2 = strpbrk(ptr1, "-._");
2463 if (ptr2)
2464 *ptr2 = '\0';
2465 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2466 ptr1);
2467 free(ptr1);
2468 }
David Ahern1fb89442012-09-08 09:06:51 -06002469 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302470 }
2471 free(pp->function);
2472 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2473 if (!pp->function) {
2474 ret = -ENOMEM;
2475 pr_warning("Failed to allocate memory by zalloc.\n");
2476 goto out;
2477 }
2478 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2479 ret = 0;
2480
2481out:
2482 if (map) {
2483 dso__delete(map->dso);
2484 map__delete(map);
2485 }
2486 if (function)
2487 free(function);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302488 return ret;
2489}