blob: a8a9b6cd93a8f080a968f1cd05e7f80bb0df0ca6 [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);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030076static struct machine 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 Hiramatsu146a1432010-04-12 13:17:42 -040079static int init_vmlinux(void)
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;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040088 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040093
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090094 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030095 if (ret < 0)
96 goto out;
97
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090098 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090099 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 goto out;
101 }
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 Hiramatsu469b9b82010-10-21 19:13:41 +0900108static struct symbol *__find_kernel_function_by_name(const char *name,
109 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400110{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900111 return machine__find_kernel_function_by_name(&machine, name, mapp,
112 NULL);
113}
114
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900115static struct map *kernel_get_module_map(const char *module)
116{
117 struct rb_node *nd;
118 struct map_groups *grp = &machine.kmaps;
119
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900120 /* A file path -- this is an offline module */
121 if (module && strchr(module, '/'))
122 return machine__new_module(&machine, 0, module);
123
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900124 if (!module)
125 module = "kernel";
126
127 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
128 struct map *pos = rb_entry(nd, struct map, rb_node);
129 if (strncmp(pos->dso->short_name + 1, module,
130 pos->dso->short_name_len - 2) == 0) {
131 return pos;
132 }
133 }
134 return NULL;
135}
136
137static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900138{
139 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100140 struct map *map;
141 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900142
143 if (module) {
144 list_for_each_entry(dso, &machine.kernel_dsos, node) {
145 if (strncmp(dso->short_name + 1, module,
146 dso->short_name_len - 2) == 0)
147 goto found;
148 }
149 pr_debug("Failed to find module %s.\n", module);
150 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100151 }
152
153 map = machine.vmlinux_maps[MAP__FUNCTION];
154 dso = map->dso;
155
156 vmlinux_name = symbol_conf.vmlinux_name;
157 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300158 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100159 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900160 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100161 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900162 pr_debug("Failed to load kernel map.\n");
163 return NULL;
164 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400165 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900166found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900167 return dso;
168}
169
170const char *kernel_get_module_path(const char *module)
171{
172 struct dso *dso = kernel_get_module_dso(module);
173 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900174}
175
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530176static int init_user_exec(void)
177{
178 int ret = 0;
179
180 symbol_conf.try_vmlinux_path = false;
181 symbol_conf.sort_by_name = true;
182 ret = symbol__init();
183
184 if (ret < 0)
185 pr_debug("Failed to init symbol map.\n");
186
187 return ret;
188}
189
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000190static int convert_exec_to_group(const char *exec, char **result)
191{
192 char *ptr1, *ptr2, *exec_copy;
193 char buf[64];
194 int ret;
195
196 exec_copy = strdup(exec);
197 if (!exec_copy)
198 return -ENOMEM;
199
200 ptr1 = basename(exec_copy);
201 if (!ptr1) {
202 ret = -EINVAL;
203 goto out;
204 }
205
206 ptr2 = strpbrk(ptr1, "-._");
207 if (ptr2)
208 *ptr2 = '\0';
209 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
210 if (ret < 0)
211 goto out;
212
213 *result = strdup(buf);
214 ret = *result ? 0 : -ENOMEM;
215
216out:
217 free(exec_copy);
218 return ret;
219}
220
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530221static int convert_to_perf_probe_point(struct probe_trace_point *tp,
222 struct perf_probe_point *pp)
223{
224 pp->function = strdup(tp->symbol);
225
226 if (pp->function == NULL)
227 return -ENOMEM;
228
229 pp->offset = tp->offset;
230 pp->retprobe = tp->retprobe;
231
232 return 0;
233}
234
Ingo Molnar89fe8082013-09-30 12:07:11 +0200235#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900236/* Open new debuginfo of given module */
237static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900238{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900239 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900240
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900241 /* A file path -- this is an offline module */
242 if (module && strchr(module, '/'))
243 path = module;
244 else {
245 path = kernel_get_module_path(module);
246
247 if (!path) {
248 pr_err("Failed to find path of %s module.\n",
249 module ?: "kernel");
250 return NULL;
251 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900252 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900253 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400254}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300255
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530256/*
257 * Convert trace point to probe point with debuginfo
258 * Currently only handles kprobes.
259 */
260static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900261 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300262{
263 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900264 struct map *map;
265 u64 addr;
266 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900267 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300268
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900269 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300270 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900271 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200272 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900273 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900274
275 dinfo = debuginfo__new_online_kernel(addr);
276 if (dinfo) {
277 ret = debuginfo__find_probe_point(dinfo,
278 (unsigned long)addr, pp);
279 debuginfo__delete(dinfo);
280 } else {
281 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
282 addr);
283 ret = -ENOENT;
284 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300285 }
286 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400287 pr_debug("Failed to find corresponding probes from "
288 "debuginfo. Use kprobe event information.\n");
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530289 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300290 }
291 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400292
293 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300294}
295
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000296static int get_text_start_address(const char *exec, unsigned long *address)
297{
298 Elf *elf;
299 GElf_Ehdr ehdr;
300 GElf_Shdr shdr;
301 int fd, ret = -ENOENT;
302
303 fd = open(exec, O_RDONLY);
304 if (fd < 0)
305 return -errno;
306
307 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
308 if (elf == NULL)
309 return -EINVAL;
310
311 if (gelf_getehdr(elf, &ehdr) == NULL)
312 goto out;
313
314 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
315 goto out;
316
317 *address = shdr.sh_addr - shdr.sh_offset;
318 ret = 0;
319out:
320 elf_end(elf);
321 return ret;
322}
323
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000324static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
325 int ntevs, const char *exec)
326{
327 int i, ret = 0;
328 unsigned long offset, stext = 0;
329 char buf[32];
330
331 if (!exec)
332 return 0;
333
334 ret = get_text_start_address(exec, &stext);
335 if (ret < 0)
336 return ret;
337
338 for (i = 0; i < ntevs && ret >= 0; i++) {
339 offset = tevs[i].point.address - stext;
340 offset += tevs[i].point.offset;
341 tevs[i].point.offset = 0;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300342 zfree(&tevs[i].point.symbol);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000343 ret = e_snprintf(buf, 32, "0x%lx", offset);
344 if (ret < 0)
345 break;
346 tevs[i].point.module = strdup(exec);
347 tevs[i].point.symbol = strdup(buf);
348 if (!tevs[i].point.symbol || !tevs[i].point.module) {
349 ret = -ENOMEM;
350 break;
351 }
352 tevs[i].uprobes = true;
353 }
354
355 return ret;
356}
357
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900358static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
359 int ntevs, const char *module)
360{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900361 int i, ret = 0;
362 char *tmp;
363
364 if (!module)
365 return 0;
366
367 tmp = strrchr(module, '/');
368 if (tmp) {
369 /* This is a module path -- get the module name */
370 module = strdup(tmp + 1);
371 if (!module)
372 return -ENOMEM;
373 tmp = strchr(module, '.');
374 if (tmp)
375 *tmp = '\0';
376 tmp = (char *)module; /* For free() */
377 }
378
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900379 for (i = 0; i < ntevs; i++) {
380 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900381 if (!tevs[i].point.module) {
382 ret = -ENOMEM;
383 break;
384 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900385 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900386
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300387 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900388 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900389}
390
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000391static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
392{
393 int i;
394
395 for (i = 0; i < ntevs; i++)
396 clear_probe_trace_event(tevs + i);
397}
398
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300399/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530400static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900401 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530402 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300403{
404 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530405 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900406 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300407
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530408 dinfo = open_debuginfo(target);
409
Masami Hiramatsuff741782011-06-27 16:27:39 +0900410 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400411 if (need_dwarf) {
412 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900413 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400414 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900415 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300416 return 0;
417 }
418
Masami Hiramatsuff741782011-06-27 16:27:39 +0900419 /* Searching trace events corresponding to a probe event */
420 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
421
422 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300423
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400424 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530425 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000426 if (target) {
427 if (pev->uprobes)
428 ret = add_exec_to_probe_trace_events(*tevs,
429 ntevs, target);
430 else
431 ret = add_module_to_probe_trace_events(*tevs,
432 ntevs, target);
433 }
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000434 if (ret < 0) {
435 clear_probe_trace_events(*tevs, ntevs);
436 zfree(tevs);
437 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900438 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400439 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300440
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400441 if (ntevs == 0) { /* No error but failed to find probe point. */
442 pr_warning("Probe point '%s' not found.\n",
443 synthesize_perf_probe_point(&pev->point));
444 return -ENOENT;
445 }
446 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400447 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
448 if (ntevs == -EBADF) {
449 pr_warning("Warning: No dwarf info found in the vmlinux - "
450 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
451 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900452 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400453 return 0;
454 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300455 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400456 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300457}
458
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900459/*
460 * Find a src file from a DWARF tag path. Prepend optional source path prefix
461 * and chop off leading directories that do not exist. Result is passed back as
462 * a newly allocated path on success.
463 * Return 0 if file was found and readable, -errno otherwise.
464 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900465static int get_real_path(const char *raw_path, const char *comp_dir,
466 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900467{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900468 const char *prefix = symbol_conf.source_prefix;
469
470 if (!prefix) {
471 if (raw_path[0] != '/' && comp_dir)
472 /* If not an absolute path, try to use comp_dir */
473 prefix = comp_dir;
474 else {
475 if (access(raw_path, R_OK) == 0) {
476 *new_path = strdup(raw_path);
477 return 0;
478 } else
479 return -errno;
480 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900481 }
482
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900483 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900484 if (!*new_path)
485 return -ENOMEM;
486
487 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900488 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900489
490 if (access(*new_path, R_OK) == 0)
491 return 0;
492
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900493 if (!symbol_conf.source_prefix)
494 /* In case of searching comp_dir, don't retry */
495 return -errno;
496
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900497 switch (errno) {
498 case ENAMETOOLONG:
499 case ENOENT:
500 case EROFS:
501 case EFAULT:
502 raw_path = strchr(++raw_path, '/');
503 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300504 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900505 return -ENOENT;
506 }
507 continue;
508
509 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300510 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900511 return -errno;
512 }
513 }
514}
515
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300516#define LINEBUF_SIZE 256
517#define NR_ADDITIONAL_LINES 2
518
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100519static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300520{
521 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100522 const char *color = show_num ? "" : PERF_COLOR_BLUE;
523 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300524
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100525 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300526 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
527 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100528 if (skip)
529 continue;
530 if (!prefix) {
531 prefix = show_num ? "%7d " : " ";
532 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300533 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100534 color_fprintf(stdout, color, "%s", buf);
535
536 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400537
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100538 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300539error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100540 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100541 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100542 return -1;
543 }
544 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300545}
546
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100547static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
548{
549 int rv = __show_one_line(fp, l, skip, show_num);
550 if (rv == 0) {
551 pr_warning("Source file is shorter than expected.\n");
552 rv = -1;
553 }
554 return rv;
555}
556
557#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
558#define show_one_line(f,l) _show_one_line(f,l,false,false)
559#define skip_one_line(f,l) _show_one_line(f,l,true,false)
560#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
561
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300562/*
563 * Show line-range always requires debuginfo to find source file and
564 * line number.
565 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900566int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300567{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400568 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300569 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900570 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300571 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900572 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900573 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300574
575 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400576 ret = init_vmlinux();
577 if (ret < 0)
578 return ret;
579
Masami Hiramatsuff741782011-06-27 16:27:39 +0900580 dinfo = open_debuginfo(module);
581 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400582 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900583 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400584 }
585
Masami Hiramatsuff741782011-06-27 16:27:39 +0900586 ret = debuginfo__find_line_range(dinfo, lr);
587 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400588 if (ret == 0) {
589 pr_warning("Specified source line is not found.\n");
590 return -ENOENT;
591 } else if (ret < 0) {
592 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
593 return ret;
594 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300595
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900596 /* Convert source file path */
597 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900598 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900599 free(tmp); /* Free old path */
600 if (ret < 0) {
601 pr_warning("Failed to find source file. (%d)\n", ret);
602 return ret;
603 }
604
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300605 setup_pager();
606
607 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900608 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300609 lr->start - lr->offset);
610 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100611 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300612
613 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400614 if (fp == NULL) {
615 pr_warning("Failed to open %s: %s\n", lr->path,
616 strerror(errno));
617 return -errno;
618 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300619 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100620 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100621 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100622 if (ret < 0)
623 goto end;
624 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300625
626 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100627 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100628 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100629 if (ret < 0)
630 goto end;
631 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100632 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400633 if (ret < 0)
634 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300635 }
636
637 if (lr->end == INT_MAX)
638 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100639 while (l <= lr->end) {
640 ret = show_one_line_or_eof(fp, l++ - lr->offset);
641 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100642 break;
643 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400644end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300645 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400646 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300647}
648
Masami Hiramatsuff741782011-06-27 16:27:39 +0900649static int show_available_vars_at(struct debuginfo *dinfo,
650 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900651 int max_vls, struct strfilter *_filter,
652 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900653{
654 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900655 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900656 struct str_node *node;
657 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900658 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900659
660 buf = synthesize_perf_probe_point(&pev->point);
661 if (!buf)
662 return -EINVAL;
663 pr_debug("Searching variables at %s\n", buf);
664
Masami Hiramatsuff741782011-06-27 16:27:39 +0900665 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
666 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900667 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900668 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900669 goto end;
670 }
671 /* Some variables are found */
672 fprintf(stdout, "Available variables at %s\n", buf);
673 for (i = 0; i < ret; i++) {
674 vl = &vls[i];
675 /*
676 * A probe point might be converted to
677 * several trace points.
678 */
679 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
680 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300681 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900682 nvars = 0;
683 if (vl->vars) {
684 strlist__for_each(node, vl->vars) {
685 var = strchr(node->s, '\t') + 1;
686 if (strfilter__compare(_filter, var)) {
687 fprintf(stdout, "\t\t%s\n", node->s);
688 nvars++;
689 }
690 }
691 strlist__delete(vl->vars);
692 }
693 if (nvars == 0)
694 fprintf(stdout, "\t\t(No matched variables)\n");
695 }
696 free(vls);
697end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900698 free(buf);
699 return ret;
700}
701
702/* Show available variables on given probe point */
703int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900704 int max_vls, const char *module,
705 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900706{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900707 int i, ret = 0;
708 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900709
710 ret = init_vmlinux();
711 if (ret < 0)
712 return ret;
713
Masami Hiramatsuff741782011-06-27 16:27:39 +0900714 dinfo = open_debuginfo(module);
715 if (!dinfo) {
716 pr_warning("Failed to open debuginfo file.\n");
717 return -ENOENT;
718 }
719
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900720 setup_pager();
721
Masami Hiramatsuff741782011-06-27 16:27:39 +0900722 for (i = 0; i < npevs && ret >= 0; i++)
723 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900724 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900725
726 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900727 return ret;
728}
729
Ingo Molnar89fe8082013-09-30 12:07:11 +0200730#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530732static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900733 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300734{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900735 struct symbol *sym;
736
737 sym = __find_kernel_function_by_name(tp->symbol, NULL);
738 if (!sym) {
739 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
740 return -ENOENT;
741 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400742
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530743 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300744}
745
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530746static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300747 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300748 int max_tevs __maybe_unused,
749 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300750{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400751 if (perf_probe_event_need_dwarf(pev)) {
752 pr_warning("Debuginfo-analysis is not supported.\n");
753 return -ENOSYS;
754 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530755
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300756 return 0;
757}
758
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300759int show_line_range(struct line_range *lr __maybe_unused,
760 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300761{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400762 pr_warning("Debuginfo-analysis is not supported.\n");
763 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300764}
765
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300766int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
767 int npevs __maybe_unused, int max_vls __maybe_unused,
768 const char *module __maybe_unused,
769 struct strfilter *filter __maybe_unused,
770 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900771{
772 pr_warning("Debuginfo-analysis is not supported.\n");
773 return -ENOSYS;
774}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400775#endif
776
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000777void line_range__clear(struct line_range *lr)
778{
779 struct line_node *ln;
780
781 free(lr->function);
782 free(lr->file);
783 free(lr->path);
784 free(lr->comp_dir);
785 while (!list_empty(&lr->line_list)) {
786 ln = list_first_entry(&lr->line_list, struct line_node, list);
787 list_del(&ln->list);
788 free(ln);
789 }
790 memset(lr, 0, sizeof(*lr));
791}
792
793void line_range__init(struct line_range *lr)
794{
795 memset(lr, 0, sizeof(*lr));
796 INIT_LIST_HEAD(&lr->line_list);
797}
798
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100799static int parse_line_num(char **ptr, int *val, const char *what)
800{
801 const char *start = *ptr;
802
803 errno = 0;
804 *val = strtol(*ptr, ptr, 0);
805 if (errno || *ptr == start) {
806 semantic_error("'%s' is not a valid number.\n", what);
807 return -EINVAL;
808 }
809 return 0;
810}
811
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100812/*
813 * Stuff 'lr' according to the line range described by 'arg'.
814 * The line range syntax is described by:
815 *
816 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900817 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100818 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400819int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500820{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900821 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100822 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100823
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100824 if (!name)
825 return -ENOMEM;
826
827 lr->start = 0;
828 lr->end = INT_MAX;
829
830 range = strchr(name, ':');
831 if (range) {
832 *range++ = '\0';
833
834 err = parse_line_num(&range, &lr->start, "start line");
835 if (err)
836 goto err;
837
838 if (*range == '+' || *range == '-') {
839 const char c = *range++;
840
841 err = parse_line_num(&range, &lr->end, "end line");
842 if (err)
843 goto err;
844
845 if (c == '+') {
846 lr->end += lr->start;
847 /*
848 * Adjust the number of lines here.
849 * If the number of lines == 1, the
850 * the end of line should be equal to
851 * the start of line.
852 */
853 lr->end--;
854 }
855 }
856
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400857 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100858
859 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400860 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500861 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400862 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100863 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400864 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100865 if (*range != '\0') {
866 semantic_error("Tailing with invalid str '%s'.\n", range);
867 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400868 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400869 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400870
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900871 file = strchr(name, '@');
872 if (file) {
873 *file = '\0';
874 lr->file = strdup(++file);
875 if (lr->file == NULL) {
876 err = -ENOMEM;
877 goto err;
878 }
879 lr->function = name;
880 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100881 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500882 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100883 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884
885 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100886err:
887 free(name);
888 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500889}
890
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500891/* Check the name is good for event/group */
892static bool check_event_name(const char *name)
893{
894 if (!isalpha(*name) && *name != '_')
895 return false;
896 while (*++name != '\0') {
897 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
898 return false;
899 }
900 return true;
901}
902
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500903/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400904static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500905{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400906 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500907 char *ptr, *tmp;
908 char c, nc = 0;
909 /*
910 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500911 * perf probe [EVENT=]SRC[:LN|;PTN]
912 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500913 *
914 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500915 */
916
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500917 ptr = strpbrk(arg, ";=@+%");
918 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500919 *ptr = '\0';
920 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400921 if (strchr(arg, ':')) {
922 semantic_error("Group name is not supported yet.\n");
923 return -ENOTSUP;
924 }
925 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500926 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400927 "follow C symbol-naming rule.\n", arg);
928 return -EINVAL;
929 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400930 pev->event = strdup(arg);
931 if (pev->event == NULL)
932 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400933 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500934 arg = tmp;
935 }
936
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500937 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500938 if (ptr) {
939 nc = *ptr;
940 *ptr++ = '\0';
941 }
942
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400943 tmp = strdup(arg);
944 if (tmp == NULL)
945 return -ENOMEM;
946
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500947 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400948 if (strchr(tmp, '.')) /* File */
949 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500950 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400951 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500952
953 /* Parse other options */
954 while (ptr) {
955 arg = ptr;
956 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500957 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400958 pp->lazy_line = strdup(arg);
959 if (pp->lazy_line == NULL)
960 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500961 break;
962 }
963 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500964 if (ptr) {
965 nc = *ptr;
966 *ptr++ = '\0';
967 }
968 switch (c) {
969 case ':': /* Line number */
970 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400971 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500972 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 " in line number.\n");
974 return -EINVAL;
975 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500976 break;
977 case '+': /* Byte offset from a symbol */
978 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400979 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500980 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400981 " in offset.\n");
982 return -EINVAL;
983 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500984 break;
985 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 if (pp->file) {
987 semantic_error("SRC@SRC is not allowed.\n");
988 return -EINVAL;
989 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400990 pp->file = strdup(arg);
991 if (pp->file == NULL)
992 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500993 break;
994 case '%': /* Probe places */
995 if (strcmp(arg, "return") == 0) {
996 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400997 } else { /* Others not supported yet */
998 semantic_error("%%%s is not supported.\n", arg);
999 return -ENOTSUP;
1000 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001001 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001002 default: /* Buggy case */
1003 pr_err("This program has a bug at %s:%d.\n",
1004 __FILE__, __LINE__);
1005 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001006 break;
1007 }
1008 }
1009
1010 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001011 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001012 semantic_error("Lazy pattern can't be used with"
1013 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001014 return -EINVAL;
1015 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001016
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001017 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001018 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001019 return -EINVAL;
1020 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001021
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001022 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001023 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001024 return -EINVAL;
1025 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001026
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001027 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001028 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001029 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001030 return -EINVAL;
1031 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001032
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001034 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001035 return -EINVAL;
1036 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001037
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001039 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040 return -EINVAL;
1041 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001042
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001043 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001044 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001045 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001046 return -EINVAL;
1047 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001048
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001049 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001050 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1051 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001052 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001053}
1054
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001055/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001056static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001057{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001058 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001059 struct perf_probe_arg_field **fieldp;
1060
1061 pr_debug("parsing arg: %s into ", str);
1062
Masami Hiramatsu48481932010-04-12 13:16:53 -04001063 tmp = strchr(str, '=');
1064 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001065 arg->name = strndup(str, tmp - str);
1066 if (arg->name == NULL)
1067 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001068 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001069 str = tmp + 1;
1070 }
1071
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001072 tmp = strchr(str, ':');
1073 if (tmp) { /* Type setting */
1074 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001075 arg->type = strdup(tmp + 1);
1076 if (arg->type == NULL)
1077 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001078 pr_debug("type:%s ", arg->type);
1079 }
1080
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001081 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001082 if (!is_c_varname(str) || !tmp) {
1083 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001084 arg->var = strdup(str);
1085 if (arg->var == NULL)
1086 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001087 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001088 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001089 }
1090
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001091 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001092 arg->var = strndup(str, tmp - str);
1093 if (arg->var == NULL)
1094 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001095 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001096 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001097 fieldp = &arg->field;
1098
1099 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001100 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1101 if (*fieldp == NULL)
1102 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001103 if (*tmp == '[') { /* Array */
1104 str = tmp;
1105 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001106 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001107 if (*tmp != ']' || tmp == str + 1) {
1108 semantic_error("Array index must be a"
1109 " number.\n");
1110 return -EINVAL;
1111 }
1112 tmp++;
1113 if (*tmp == '\0')
1114 tmp = NULL;
1115 } else { /* Structure */
1116 if (*tmp == '.') {
1117 str = tmp + 1;
1118 (*fieldp)->ref = false;
1119 } else if (tmp[1] == '>') {
1120 str = tmp + 2;
1121 (*fieldp)->ref = true;
1122 } else {
1123 semantic_error("Argument parse error: %s\n",
1124 str);
1125 return -EINVAL;
1126 }
1127 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001128 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001129 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001130 (*fieldp)->name = strndup(str, tmp - str);
1131 if ((*fieldp)->name == NULL)
1132 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001133 if (*str != '[')
1134 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001135 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1136 fieldp = &(*fieldp)->next;
1137 }
1138 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001139 (*fieldp)->name = strdup(str);
1140 if ((*fieldp)->name == NULL)
1141 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001142 if (*str != '[')
1143 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001144 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001145
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001146 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001147 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001148 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001149 if (arg->name == NULL)
1150 return -ENOMEM;
1151 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001152 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001153}
1154
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001155/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001156int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001158 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001160
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001161 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001162 if (!argv) {
1163 pr_debug("Failed to split arguments.\n");
1164 return -ENOMEM;
1165 }
1166 if (argc - 1 > MAX_PROBE_ARGS) {
1167 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1168 ret = -ERANGE;
1169 goto out;
1170 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001171 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 ret = parse_perf_probe_point(argv[0], pev);
1173 if (ret < 0)
1174 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001175
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001176 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001177 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001178 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1179 if (pev->args == NULL) {
1180 ret = -ENOMEM;
1181 goto out;
1182 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001183 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1184 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1185 if (ret >= 0 &&
1186 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001187 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001188 " kretprobe.\n");
1189 ret = -EINVAL;
1190 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001191 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001193 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194
1195 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001196}
1197
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001198/* Return true if this perf_probe_event requires debuginfo */
1199bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001200{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001201 int i;
1202
1203 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1204 return true;
1205
1206 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001207 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001208 return true;
1209
1210 return false;
1211}
1212
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301213/* Parse probe_events event into struct probe_point */
1214static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001215 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001216{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301217 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001218 char pr;
1219 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001220 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001221 int ret, i, argc;
1222 char **argv;
1223
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301224 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001225 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226 if (!argv) {
1227 pr_debug("Failed to split arguments.\n");
1228 return -ENOMEM;
1229 }
1230 if (argc < 2) {
1231 semantic_error("Too few probe arguments.\n");
1232 ret = -ERANGE;
1233 goto out;
1234 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001235
1236 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001237 argv0_str = strdup(argv[0]);
1238 if (argv0_str == NULL) {
1239 ret = -ENOMEM;
1240 goto out;
1241 }
1242 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1243 fmt2_str = strtok_r(NULL, "/", &fmt);
1244 fmt3_str = strtok_r(NULL, " \t", &fmt);
1245 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1246 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 semantic_error("Failed to parse event name: %s\n", argv[0]);
1248 ret = -EINVAL;
1249 goto out;
1250 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001251 pr = fmt1_str[0];
1252 tev->group = strdup(fmt2_str);
1253 tev->event = strdup(fmt3_str);
1254 if (tev->group == NULL || tev->event == NULL) {
1255 ret = -ENOMEM;
1256 goto out;
1257 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001258 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001259
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001260 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001261
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001262 /* Scan module name(if there), function name and offset */
1263 p = strchr(argv[1], ':');
1264 if (p) {
1265 tp->module = strndup(argv[1], p - argv[1]);
1266 p++;
1267 } else
1268 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001269 fmt1_str = strtok_r(p, "+", &fmt);
1270 tp->symbol = strdup(fmt1_str);
1271 if (tp->symbol == NULL) {
1272 ret = -ENOMEM;
1273 goto out;
1274 }
1275 fmt2_str = strtok_r(NULL, "", &fmt);
1276 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001278 else
1279 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001280
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001281 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301282 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001283 if (tev->args == NULL) {
1284 ret = -ENOMEM;
1285 goto out;
1286 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001287 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001288 p = strchr(argv[i + 2], '=');
1289 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001290 *p++ = '\0';
1291 else
1292 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001293 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001294 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001295 tev->args[i].value = strdup(p);
1296 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1297 ret = -ENOMEM;
1298 goto out;
1299 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001300 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001301 ret = 0;
1302out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001303 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001304 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001305 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001306}
1307
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001308/* Compose only probe arg */
1309int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1310{
1311 struct perf_probe_arg_field *field = pa->field;
1312 int ret;
1313 char *tmp = buf;
1314
Masami Hiramatsu48481932010-04-12 13:16:53 -04001315 if (pa->name && pa->var)
1316 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1317 else
1318 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001319 if (ret <= 0)
1320 goto error;
1321 tmp += ret;
1322 len -= ret;
1323
1324 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001325 if (field->name[0] == '[')
1326 ret = e_snprintf(tmp, len, "%s", field->name);
1327 else
1328 ret = e_snprintf(tmp, len, "%s%s",
1329 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001330 if (ret <= 0)
1331 goto error;
1332 tmp += ret;
1333 len -= ret;
1334 field = field->next;
1335 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001336
1337 if (pa->type) {
1338 ret = e_snprintf(tmp, len, ":%s", pa->type);
1339 if (ret <= 0)
1340 goto error;
1341 tmp += ret;
1342 len -= ret;
1343 }
1344
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001345 return tmp - buf;
1346error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001347 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001348 strerror(-ret));
1349 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001350}
1351
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001352/* Compose only probe point (not argument) */
1353static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001354{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001355 char *buf, *tmp;
1356 char offs[32] = "", line[32] = "", file[32] = "";
1357 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001358
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001359 buf = zalloc(MAX_CMDLEN);
1360 if (buf == NULL) {
1361 ret = -ENOMEM;
1362 goto error;
1363 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001364 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001365 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001366 if (ret <= 0)
1367 goto error;
1368 }
1369 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001370 ret = e_snprintf(line, 32, ":%d", pp->line);
1371 if (ret <= 0)
1372 goto error;
1373 }
1374 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001375 tmp = pp->file;
1376 len = strlen(tmp);
1377 if (len > 30) {
1378 tmp = strchr(pp->file + len - 30, '/');
1379 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1380 }
1381 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001382 if (ret <= 0)
1383 goto error;
1384 }
1385
1386 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001387 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1388 offs, pp->retprobe ? "%return" : "", line,
1389 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001390 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001391 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001392 if (ret <= 0)
1393 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001394
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001395 return buf;
1396error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001397 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001398 strerror(-ret));
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001399 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001401}
1402
1403#if 0
1404char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1405{
1406 char *buf;
1407 int i, len, ret;
1408
1409 buf = synthesize_perf_probe_point(&pev->point);
1410 if (!buf)
1411 return NULL;
1412
1413 len = strlen(buf);
1414 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001415 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001416 pev->args[i].name);
1417 if (ret <= 0) {
1418 free(buf);
1419 return NULL;
1420 }
1421 len += ret;
1422 }
1423
1424 return buf;
1425}
1426#endif
1427
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301428static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 char **buf, size_t *buflen,
1430 int depth)
1431{
1432 int ret;
1433 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301434 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001435 buflen, depth + 1);
1436 if (depth < 0)
1437 goto out;
1438 }
1439
1440 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1441 if (ret < 0)
1442 depth = ret;
1443 else {
1444 *buf += ret;
1445 *buflen -= ret;
1446 }
1447out:
1448 return depth;
1449
1450}
1451
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301452static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 char *buf, size_t buflen)
1454{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301455 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001456 int ret, depth = 0;
1457 char *tmp = buf;
1458
1459 /* Argument name or separator */
1460 if (arg->name)
1461 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1462 else
1463 ret = e_snprintf(buf, buflen, " ");
1464 if (ret < 0)
1465 return ret;
1466 buf += ret;
1467 buflen -= ret;
1468
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001469 /* Special case: @XXX */
1470 if (arg->value[0] == '@' && arg->ref)
1471 ref = ref->next;
1472
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001473 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001474 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301475 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001476 &buflen, 1);
1477 if (depth < 0)
1478 return depth;
1479 }
1480
1481 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001482 if (arg->value[0] == '@' && arg->ref)
1483 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1484 arg->ref->offset);
1485 else
1486 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001487 if (ret < 0)
1488 return ret;
1489 buf += ret;
1490 buflen -= ret;
1491
1492 /* Closing */
1493 while (depth--) {
1494 ret = e_snprintf(buf, buflen, ")");
1495 if (ret < 0)
1496 return ret;
1497 buf += ret;
1498 buflen -= ret;
1499 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001500 /* Print argument type */
1501 if (arg->type) {
1502 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1503 if (ret <= 0)
1504 return ret;
1505 buf += ret;
1506 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001507
1508 return buf - tmp;
1509}
1510
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301511char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001512{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301513 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001514 char *buf;
1515 int i, len, ret;
1516
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001517 buf = zalloc(MAX_CMDLEN);
1518 if (buf == NULL)
1519 return NULL;
1520
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301521 if (tev->uprobes)
1522 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1523 tp->retprobe ? 'r' : 'p',
1524 tev->group, tev->event,
1525 tp->module, tp->symbol);
1526 else
1527 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1528 tp->retprobe ? 'r' : 'p',
1529 tev->group, tev->event,
1530 tp->module ?: "", tp->module ? ":" : "",
1531 tp->symbol, tp->offset);
1532
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001533 if (len <= 0)
1534 goto error;
1535
1536 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301537 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001538 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001539 if (ret <= 0)
1540 goto error;
1541 len += ret;
1542 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001543
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001545error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 free(buf);
1547 return NULL;
1548}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001549
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301550static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301551 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001552{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001553 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001555
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001556 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001557 pev->event = strdup(tev->event);
1558 pev->group = strdup(tev->group);
1559 if (pev->event == NULL || pev->group == NULL)
1560 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001561
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001562 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301563 if (is_kprobe)
1564 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1565 else
1566 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1567
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001568 if (ret < 0)
1569 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001570
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001571 /* Convert trace_arg to probe_arg */
1572 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001573 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1574 if (pev->args == NULL)
1575 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001576 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001578 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001579 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301580 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001581 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001582 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001583 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001584 if (pev->args[i].name == NULL && ret >= 0)
1585 ret = -ENOMEM;
1586 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587
1588 if (ret < 0)
1589 clear_perf_probe_event(pev);
1590
1591 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001592}
1593
1594void clear_perf_probe_event(struct perf_probe_event *pev)
1595{
1596 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001597 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 int i;
1599
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001600 free(pev->event);
1601 free(pev->group);
1602 free(pp->file);
1603 free(pp->function);
1604 free(pp->lazy_line);
1605
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001606 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001607 free(pev->args[i].name);
1608 free(pev->args[i].var);
1609 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001610 field = pev->args[i].field;
1611 while (field) {
1612 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001613 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001614 free(field);
1615 field = next;
1616 }
1617 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001618 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001619 memset(pev, 0, sizeof(*pev));
1620}
1621
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301622static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001623{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301624 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625 int i;
1626
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001627 free(tev->event);
1628 free(tev->group);
1629 free(tev->point.symbol);
1630 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001631 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001632 free(tev->args[i].name);
1633 free(tev->args[i].value);
1634 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001635 ref = tev->args[i].ref;
1636 while (ref) {
1637 next = ref->next;
1638 free(ref);
1639 ref = next;
1640 }
1641 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001642 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001643 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001644}
1645
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301646static void print_warn_msg(const char *file, bool is_kprobe)
1647{
1648
1649 if (errno == ENOENT) {
1650 const char *config;
1651
1652 if (!is_kprobe)
1653 config = "CONFIG_UPROBE_EVENTS";
1654 else
1655 config = "CONFIG_KPROBE_EVENTS";
1656
1657 pr_warning("%s file does not exist - please rebuild kernel"
1658 " with %s.\n", file, config);
1659 } else
1660 pr_warning("Failed to open %s file: %s\n", file,
1661 strerror(errno));
1662}
1663
1664static int open_probe_events(const char *trace_file, bool readwrite,
1665 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001666{
1667 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001668 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001669 int ret;
1670
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001671 __debugfs = debugfs_find_mountpoint();
1672 if (__debugfs == NULL) {
1673 pr_warning("Debugfs is not mounted.\n");
1674 return -ENOENT;
1675 }
1676
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301677 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001678 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001679 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001680 if (readwrite && !probe_event_dry_run)
1681 ret = open(buf, O_RDWR, O_APPEND);
1682 else
1683 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001684
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301685 if (ret < 0)
1686 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001687 }
1688 return ret;
1689}
1690
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301691static int open_kprobe_events(bool readwrite)
1692{
1693 return open_probe_events("tracing/kprobe_events", readwrite, true);
1694}
1695
1696static int open_uprobe_events(bool readwrite)
1697{
1698 return open_probe_events("tracing/uprobe_events", readwrite, false);
1699}
1700
1701/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301702static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001703{
1704 int ret, idx;
1705 FILE *fp;
1706 char buf[MAX_CMDLEN];
1707 char *p;
1708 struct strlist *sl;
1709
1710 sl = strlist__new(true, NULL);
1711
1712 fp = fdopen(dup(fd), "r");
1713 while (!feof(fp)) {
1714 p = fgets(buf, MAX_CMDLEN, fp);
1715 if (!p)
1716 break;
1717
1718 idx = strlen(p) - 1;
1719 if (p[idx] == '\n')
1720 p[idx] = '\0';
1721 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001722 if (ret < 0) {
1723 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1724 strlist__delete(sl);
1725 return NULL;
1726 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001727 }
1728 fclose(fp);
1729
1730 return sl;
1731}
1732
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001733/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001734static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001735{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001736 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001737 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001738 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001739
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001740 /* Synthesize only event probe point */
1741 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001742 if (!place)
1743 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744
1745 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001746 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001747 return ret;
1748
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001749 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001750
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001751 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001752 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001753 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001754 ret = synthesize_perf_probe_arg(&pev->args[i],
1755 buf, 128);
1756 if (ret < 0)
1757 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001758 printf(" %s", buf);
1759 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001760 }
1761 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001763 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001764}
1765
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301766static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001767{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301768 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301769 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001770 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771 struct strlist *rawlist;
1772 struct str_node *ent;
1773
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 memset(&tev, 0, sizeof(tev));
1775 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001776
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301777 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001778 if (!rawlist)
1779 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001780
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001781 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301782 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301784 ret = convert_to_perf_probe_event(&tev, &pev,
1785 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001786 if (ret >= 0)
1787 ret = show_perf_probe_event(&pev);
1788 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001789 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301790 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001791 if (ret < 0)
1792 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001793 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001794 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001795
1796 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001797}
1798
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301799/* List up current perf-probe events */
1800int show_perf_probe_events(void)
1801{
1802 int fd, ret;
1803
1804 setup_pager();
1805 fd = open_kprobe_events(false);
1806
1807 if (fd < 0)
1808 return fd;
1809
1810 ret = init_vmlinux();
1811 if (ret < 0)
1812 return ret;
1813
1814 ret = __show_perf_probe_events(fd, true);
1815 close(fd);
1816
1817 fd = open_uprobe_events(false);
1818 if (fd >= 0) {
1819 ret = __show_perf_probe_events(fd, false);
1820 close(fd);
1821 }
1822
1823 return ret;
1824}
1825
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001826/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301827static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001828{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001829 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001830 struct strlist *sl, *rawlist;
1831 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301832 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001833 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001834
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001835 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301836 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001837 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001838 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301839 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001840 if (ret < 0)
1841 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001842 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001843 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1844 tev.event);
1845 if (ret >= 0)
1846 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001847 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001848 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301849 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001850 if (ret < 0)
1851 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001852 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001853 strlist__delete(rawlist);
1854
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001855 if (ret < 0) {
1856 strlist__delete(sl);
1857 return NULL;
1858 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001859 return sl;
1860}
1861
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301862static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001863{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001864 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301865 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001866
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001867 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301868 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001869 return -EINVAL;
1870 }
1871
Masami Hiramatsufa282442009-12-08 17:03:23 -05001872 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001873 if (!probe_event_dry_run) {
1874 ret = write(fd, buf, strlen(buf));
1875 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001876 pr_warning("Failed to write event: %s\n",
1877 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001878 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001880 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001881}
1882
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001883static int get_new_event_name(char *buf, size_t len, const char *base,
1884 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001885{
1886 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001887
1888 /* Try no suffix */
1889 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001890 if (ret < 0) {
1891 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1892 return ret;
1893 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001894 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001896
Masami Hiramatsud761b082009-12-15 10:32:25 -05001897 if (!allow_suffix) {
1898 pr_warning("Error: event \"%s\" already exists. "
1899 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001900 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001901 }
1902
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001903 /* Try to add suffix */
1904 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001905 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001906 if (ret < 0) {
1907 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1908 return ret;
1909 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001910 if (!strlist__has_entry(namelist, buf))
1911 break;
1912 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001913 if (i == MAX_EVENT_INDEX) {
1914 pr_warning("Too many events are on the same function.\n");
1915 ret = -ERANGE;
1916 }
1917
1918 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001919}
1920
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301921static int __add_probe_trace_events(struct perf_probe_event *pev,
1922 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001923 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001924{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001925 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301926 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927 char buf[64];
1928 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001929 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001930
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301931 if (pev->uprobes)
1932 fd = open_uprobe_events(true);
1933 else
1934 fd = open_kprobe_events(true);
1935
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001936 if (fd < 0)
1937 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001938 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301939 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001940 if (!namelist) {
1941 pr_debug("Failed to get current event list.\n");
1942 return -EIO;
1943 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001944
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001945 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301946 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001947 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001948 tev = &tevs[i];
1949 if (pev->event)
1950 event = pev->event;
1951 else
1952 if (pev->point.function)
1953 event = pev->point.function;
1954 else
1955 event = tev->point.symbol;
1956 if (pev->group)
1957 group = pev->group;
1958 else
1959 group = PERFPROBE_GROUP;
1960
1961 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001962 ret = get_new_event_name(buf, 64, event,
1963 namelist, allow_suffix);
1964 if (ret < 0)
1965 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001966 event = buf;
1967
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001968 tev->event = strdup(event);
1969 tev->group = strdup(group);
1970 if (tev->event == NULL || tev->group == NULL) {
1971 ret = -ENOMEM;
1972 break;
1973 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301974 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001975 if (ret < 0)
1976 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001977 /* Add added event name to namelist */
1978 strlist__add(namelist, event);
1979
1980 /* Trick here - save current event/group */
1981 event = pev->event;
1982 group = pev->group;
1983 pev->event = tev->event;
1984 pev->group = tev->group;
1985 show_perf_probe_event(pev);
1986 /* Trick here - restore current event/group */
1987 pev->event = (char *)event;
1988 pev->group = (char *)group;
1989
1990 /*
1991 * Probes after the first probe which comes from same
1992 * user input are always allowed to add suffix, because
1993 * there might be several addresses corresponding to
1994 * one code line.
1995 */
1996 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001997 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001998
1999 if (ret >= 0) {
2000 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302001 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002002 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2003 tev->event);
2004 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002005
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002006 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002007 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002008 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002009}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002010
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302011static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2012 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302013 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002014{
2015 struct symbol *sym;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002016 int ret, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302017 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002018
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002019 if (pev->uprobes && !pev->group) {
2020 /* Replace group name if not given */
2021 ret = convert_exec_to_group(target, &pev->group);
2022 if (ret != 0) {
2023 pr_warning("Failed to make a group name.\n");
2024 return ret;
2025 }
2026 }
2027
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002028 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302029 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002030 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002031 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002032
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002033 if (pev->uprobes) {
2034 ret = convert_name_to_addr(pev, target);
2035 if (ret < 0)
2036 return ret;
2037 }
2038
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002039 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302040 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002041 if (tev == NULL)
2042 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002043
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002044 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002045 tev->point.symbol = strdup(pev->point.function);
2046 if (tev->point.symbol == NULL) {
2047 ret = -ENOMEM;
2048 goto error;
2049 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002050
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302051 if (target) {
2052 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08002053 if (tev->point.module == NULL) {
2054 ret = -ENOMEM;
2055 goto error;
2056 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002057 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002058
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002059 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09002060 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002061 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302062 tev->uprobes = pev->uprobes;
2063
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002064 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302065 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002066 * tev->nargs);
2067 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002068 ret = -ENOMEM;
2069 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002070 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002071 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002072 if (pev->args[i].name) {
2073 tev->args[i].name = strdup(pev->args[i].name);
2074 if (tev->args[i].name == NULL) {
2075 ret = -ENOMEM;
2076 goto error;
2077 }
2078 }
2079 tev->args[i].value = strdup(pev->args[i].var);
2080 if (tev->args[i].value == NULL) {
2081 ret = -ENOMEM;
2082 goto error;
2083 }
2084 if (pev->args[i].type) {
2085 tev->args[i].type = strdup(pev->args[i].type);
2086 if (tev->args[i].type == NULL) {
2087 ret = -ENOMEM;
2088 goto error;
2089 }
2090 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002091 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002092 }
2093
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302094 if (pev->uprobes)
2095 return 1;
2096
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002097 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002098 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002099 if (!sym) {
2100 pr_warning("Kernel symbol \'%s\' not found.\n",
2101 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002102 ret = -ENOENT;
2103 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05302104 } else if (tev->point.offset > sym->end - sym->start) {
2105 pr_warning("Offset specified is greater than size of %s\n",
2106 tev->point.symbol);
2107 ret = -ENOENT;
2108 goto error;
2109
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002110 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002111
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002112 return 1;
2113error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302114 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002115 free(tev);
2116 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002117 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002118}
2119
2120struct __event_package {
2121 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302122 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002123 int ntevs;
2124};
2125
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002126int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302127 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002128{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002129 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002130 struct __event_package *pkgs;
2131
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302132 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002133 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302134
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002135 if (pkgs == NULL)
2136 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002137
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302138 if (!pevs->uprobes)
2139 /* Init vmlinux path */
2140 ret = init_vmlinux();
2141 else
2142 ret = init_user_exec();
2143
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002144 if (ret < 0) {
2145 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002146 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002147 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002148
2149 /* Loop 1: convert all events */
2150 for (i = 0; i < npevs; i++) {
2151 pkgs[i].pev = &pevs[i];
2152 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302153 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002154 &pkgs[i].tevs,
2155 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302156 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002157 if (ret < 0)
2158 goto end;
2159 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002160 }
2161
2162 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002163 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302164 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002165 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002166 if (ret < 0)
2167 break;
2168 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002170 /* Loop 3: cleanup and free trace events */
2171 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002172 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302173 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002174 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002175 }
2176 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002177
2178 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002179}
2180
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302181static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002182{
2183 char *p;
2184 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002186
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302187 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002188 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2189 if (ret < 0)
2190 goto error;
2191
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002192 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002193 if (!p) {
2194 pr_debug("Internal error: %s should have ':' but not.\n",
2195 ent->s);
2196 ret = -ENOTSUP;
2197 goto error;
2198 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002199 *p = '/';
2200
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002201 pr_debug("Writing event: %s\n", buf);
2202 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002203 if (ret < 0) {
2204 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002205 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002206 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302208 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209 return 0;
2210error:
2211 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2212 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002213}
2214
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215static int del_trace_probe_event(int fd, const char *buf,
2216 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002217{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002218 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002220
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002221 if (strpbrk(buf, "*?")) { /* Glob-exp */
2222 strlist__for_each_safe(ent, n, namelist)
2223 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302224 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002225 if (ret < 0)
2226 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002227 strlist__remove(namelist, ent);
2228 }
2229 } else {
2230 ent = strlist__find(namelist, buf);
2231 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302232 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002233 if (ret >= 0)
2234 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002235 }
2236 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237
2238 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002239}
2240
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002241int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002242{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302243 int ret = -1, ufd = -1, kfd = -1;
2244 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002245 const char *group, *event;
2246 char *p, *str;
2247 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302248 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249
Masami Hiramatsufa282442009-12-08 17:03:23 -05002250 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302251 kfd = open_kprobe_events(true);
2252 if (kfd < 0)
2253 return kfd;
2254
2255 namelist = get_probe_trace_event_names(kfd, true);
2256 ufd = open_uprobe_events(true);
2257
2258 if (ufd >= 0)
2259 unamelist = get_probe_trace_event_names(ufd, true);
2260
2261 if (namelist == NULL && unamelist == NULL)
2262 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002263
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002264 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002265 str = strdup(ent->s);
2266 if (str == NULL) {
2267 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302268 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002269 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002270 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002271 p = strchr(str, ':');
2272 if (p) {
2273 group = str;
2274 *p = '\0';
2275 event = p + 1;
2276 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002277 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002278 event = str;
2279 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302280
2281 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2282 if (ret < 0) {
2283 pr_err("Failed to copy event.");
2284 free(str);
2285 goto error;
2286 }
2287
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002288 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302289
2290 if (namelist)
2291 ret = del_trace_probe_event(kfd, buf, namelist);
2292
2293 if (unamelist && ret != 0)
2294 ret = del_trace_probe_event(ufd, buf, unamelist);
2295
2296 if (ret != 0)
2297 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2298
Masami Hiramatsufa282442009-12-08 17:03:23 -05002299 free(str);
2300 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302301
2302error:
2303 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302304 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302305 close(kfd);
2306 }
2307
2308 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302309 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302310 close(ufd);
2311 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002312
2313 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002314}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302315
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002316/* TODO: don't use a global variable for filter ... */
2317static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002318
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002319/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002320 * If a symbol corresponds to a function with global binding and
2321 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002322 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002323static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002324 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002325{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002326 if (sym->binding == STB_GLOBAL &&
2327 strfilter__compare(available_func_filter, sym->name))
2328 return 0;
2329 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002330}
2331
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302332static int __show_available_funcs(struct map *map)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002333{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002334 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002335 pr_err("Failed to load map.\n");
2336 return -EINVAL;
2337 }
2338 if (!dso__sorted_by_name(map->dso, map->type))
2339 dso__sort_by_name(map->dso, map->type);
2340
2341 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2342 return 0;
2343}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302344
2345static int available_kernel_funcs(const char *module)
2346{
2347 struct map *map;
2348 int ret;
2349
2350 ret = init_vmlinux();
2351 if (ret < 0)
2352 return ret;
2353
2354 map = kernel_get_module_map(module);
2355 if (!map) {
2356 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2357 return -EINVAL;
2358 }
2359 return __show_available_funcs(map);
2360}
2361
2362static int available_user_funcs(const char *target)
2363{
2364 struct map *map;
2365 int ret;
2366
2367 ret = init_user_exec();
2368 if (ret < 0)
2369 return ret;
2370
2371 map = dso__new_map(target);
2372 ret = __show_available_funcs(map);
2373 dso__delete(map->dso);
2374 map__delete(map);
2375 return ret;
2376}
2377
2378int show_available_funcs(const char *target, struct strfilter *_filter,
2379 bool user)
2380{
2381 setup_pager();
2382 available_func_filter = _filter;
2383
2384 if (!user)
2385 return available_kernel_funcs(target);
2386
2387 return available_user_funcs(target);
2388}
2389
2390/*
2391 * uprobe_events only accepts address:
2392 * Convert function and any offset to address
2393 */
2394static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2395{
2396 struct perf_probe_point *pp = &pev->point;
2397 struct symbol *sym;
2398 struct map *map = NULL;
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002399 char *function = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302400 int ret = -EINVAL;
2401 unsigned long long vaddr = 0;
2402
2403 if (!pp->function) {
2404 pr_warning("No function specified for uprobes");
2405 goto out;
2406 }
2407
2408 function = strdup(pp->function);
2409 if (!function) {
2410 pr_warning("Failed to allocate memory by strdup.\n");
2411 ret = -ENOMEM;
2412 goto out;
2413 }
2414
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002415 map = dso__new_map(exec);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302416 if (!map) {
2417 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2418 goto out;
2419 }
2420 available_func_filter = strfilter__new(function, NULL);
2421 if (map__load(map, filter_available_functions)) {
2422 pr_err("Failed to load map.\n");
2423 goto out;
2424 }
2425
2426 sym = map__find_symbol_by_name(map, function, NULL);
2427 if (!sym) {
2428 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2429 goto out;
2430 }
2431
2432 if (map->start > sym->start)
2433 vaddr = map->start;
2434 vaddr += sym->start + pp->offset + map->pgoff;
2435 pp->offset = 0;
2436
2437 if (!pev->event) {
2438 pev->event = function;
2439 function = NULL;
2440 }
2441 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002442 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302443
2444 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002445 exec_copy = strdup(exec);
2446 if (!exec_copy) {
2447 ret = -ENOMEM;
2448 pr_warning("Failed to copy exec string.\n");
2449 goto out;
2450 }
2451
2452 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302453 if (ptr1) {
2454 ptr2 = strpbrk(ptr1, "-._");
2455 if (ptr2)
2456 *ptr2 = '\0';
2457 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2458 ptr1);
2459 free(ptr1);
2460 }
David Ahern1fb89442012-09-08 09:06:51 -06002461 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302462 }
2463 free(pp->function);
2464 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2465 if (!pp->function) {
2466 ret = -ENOMEM;
2467 pr_warning("Failed to allocate memory by zalloc.\n");
2468 goto out;
2469 }
2470 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2471 ret = 0;
2472
2473out:
2474 if (map) {
2475 dso__delete(map->dso);
2476 map__delete(map);
2477 }
2478 if (function)
2479 free(function);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302480 return ret;
2481}