blob: 86ed8580c3cbc71da06cd0f366051e0a30607c75 [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);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030075static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
83 if (symbol_conf.vmlinux_name == NULL)
84 symbol_conf.try_vmlinux_path = true;
85 else
86 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040087 ret = symbol__init();
88 if (ret < 0) {
89 pr_debug("Failed to init symbol map.\n");
90 goto out;
91 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040092
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090093 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094 if (ret < 0)
95 goto out;
96
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090098 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090099 goto out;
100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900107static struct symbol *__find_kernel_function_by_name(const char *name,
108 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400109{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110 return machine__find_kernel_function_by_name(&machine, name, mapp,
111 NULL);
112}
113
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900114static struct map *kernel_get_module_map(const char *module)
115{
116 struct rb_node *nd;
117 struct map_groups *grp = &machine.kmaps;
118
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900119 /* A file path -- this is an offline module */
120 if (module && strchr(module, '/'))
121 return machine__new_module(&machine, 0, module);
122
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900123 if (!module)
124 module = "kernel";
125
126 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
127 struct map *pos = rb_entry(nd, struct map, rb_node);
128 if (strncmp(pos->dso->short_name + 1, module,
129 pos->dso->short_name_len - 2) == 0) {
130 return pos;
131 }
132 }
133 return NULL;
134}
135
136static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900137{
138 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100139 struct map *map;
140 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900141
142 if (module) {
143 list_for_each_entry(dso, &machine.kernel_dsos, node) {
144 if (strncmp(dso->short_name + 1, module,
145 dso->short_name_len - 2) == 0)
146 goto found;
147 }
148 pr_debug("Failed to find module %s.\n", module);
149 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100150 }
151
152 map = machine.vmlinux_maps[MAP__FUNCTION];
153 dso = map->dso;
154
155 vmlinux_name = symbol_conf.vmlinux_name;
156 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300157 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100158 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900159 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100160 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900161 pr_debug("Failed to load kernel map.\n");
162 return NULL;
163 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400164 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900165found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900166 return dso;
167}
168
169const char *kernel_get_module_path(const char *module)
170{
171 struct dso *dso = kernel_get_module_dso(module);
172 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900173}
174
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000175/* Copied from unwind.c */
176static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
177 GElf_Shdr *shp, const char *name)
178{
179 Elf_Scn *sec = NULL;
180
181 while ((sec = elf_nextscn(elf, sec)) != NULL) {
182 char *str;
183
184 gelf_getshdr(sec, shp);
185 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
186 if (!strcmp(name, str))
187 break;
188 }
189
190 return sec;
191}
192
193static int get_text_start_address(const char *exec, unsigned long *address)
194{
195 Elf *elf;
196 GElf_Ehdr ehdr;
197 GElf_Shdr shdr;
198 int fd, ret = -ENOENT;
199
200 fd = open(exec, O_RDONLY);
201 if (fd < 0)
202 return -errno;
203
204 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
205 if (elf == NULL)
206 return -EINVAL;
207
208 if (gelf_getehdr(elf, &ehdr) == NULL)
209 goto out;
210
211 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text"))
212 goto out;
213
214 *address = shdr.sh_addr - shdr.sh_offset;
215 ret = 0;
216out:
217 elf_end(elf);
218 return ret;
219}
220
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530221static int init_user_exec(void)
222{
223 int ret = 0;
224
225 symbol_conf.try_vmlinux_path = false;
226 symbol_conf.sort_by_name = true;
227 ret = symbol__init();
228
229 if (ret < 0)
230 pr_debug("Failed to init symbol map.\n");
231
232 return ret;
233}
234
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000235static int convert_exec_to_group(const char *exec, char **result)
236{
237 char *ptr1, *ptr2, *exec_copy;
238 char buf[64];
239 int ret;
240
241 exec_copy = strdup(exec);
242 if (!exec_copy)
243 return -ENOMEM;
244
245 ptr1 = basename(exec_copy);
246 if (!ptr1) {
247 ret = -EINVAL;
248 goto out;
249 }
250
251 ptr2 = strpbrk(ptr1, "-._");
252 if (ptr2)
253 *ptr2 = '\0';
254 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
255 if (ret < 0)
256 goto out;
257
258 *result = strdup(buf);
259 ret = *result ? 0 : -ENOMEM;
260
261out:
262 free(exec_copy);
263 return ret;
264}
265
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530266static int convert_to_perf_probe_point(struct probe_trace_point *tp,
267 struct perf_probe_point *pp)
268{
269 pp->function = strdup(tp->symbol);
270
271 if (pp->function == NULL)
272 return -ENOMEM;
273
274 pp->offset = tp->offset;
275 pp->retprobe = tp->retprobe;
276
277 return 0;
278}
279
Ingo Molnar89fe8082013-09-30 12:07:11 +0200280#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900281/* Open new debuginfo of given module */
282static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900283{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900284 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900285
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900286 /* A file path -- this is an offline module */
287 if (module && strchr(module, '/'))
288 path = module;
289 else {
290 path = kernel_get_module_path(module);
291
292 if (!path) {
293 pr_err("Failed to find path of %s module.\n",
294 module ?: "kernel");
295 return NULL;
296 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900297 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900298 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400299}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300300
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530301/*
302 * Convert trace point to probe point with debuginfo
303 * Currently only handles kprobes.
304 */
305static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900306 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300307{
308 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900309 struct map *map;
310 u64 addr;
311 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900312 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300313
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900314 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300315 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900316 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200317 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900318 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900319
320 dinfo = debuginfo__new_online_kernel(addr);
321 if (dinfo) {
322 ret = debuginfo__find_probe_point(dinfo,
323 (unsigned long)addr, pp);
324 debuginfo__delete(dinfo);
325 } else {
326 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
327 addr);
328 ret = -ENOENT;
329 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300330 }
331 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400332 pr_debug("Failed to find corresponding probes from "
333 "debuginfo. Use kprobe event information.\n");
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530334 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300335 }
336 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400337
338 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300339}
340
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000341static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
342 int ntevs, const char *exec)
343{
344 int i, ret = 0;
345 unsigned long offset, stext = 0;
346 char buf[32];
347
348 if (!exec)
349 return 0;
350
351 ret = get_text_start_address(exec, &stext);
352 if (ret < 0)
353 return ret;
354
355 for (i = 0; i < ntevs && ret >= 0; i++) {
356 offset = tevs[i].point.address - stext;
357 offset += tevs[i].point.offset;
358 tevs[i].point.offset = 0;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300359 zfree(&tevs[i].point.symbol);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000360 ret = e_snprintf(buf, 32, "0x%lx", offset);
361 if (ret < 0)
362 break;
363 tevs[i].point.module = strdup(exec);
364 tevs[i].point.symbol = strdup(buf);
365 if (!tevs[i].point.symbol || !tevs[i].point.module) {
366 ret = -ENOMEM;
367 break;
368 }
369 tevs[i].uprobes = true;
370 }
371
372 return ret;
373}
374
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900375static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
376 int ntevs, const char *module)
377{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900378 int i, ret = 0;
379 char *tmp;
380
381 if (!module)
382 return 0;
383
384 tmp = strrchr(module, '/');
385 if (tmp) {
386 /* This is a module path -- get the module name */
387 module = strdup(tmp + 1);
388 if (!module)
389 return -ENOMEM;
390 tmp = strchr(module, '.');
391 if (tmp)
392 *tmp = '\0';
393 tmp = (char *)module; /* For free() */
394 }
395
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900396 for (i = 0; i < ntevs; i++) {
397 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900398 if (!tevs[i].point.module) {
399 ret = -ENOMEM;
400 break;
401 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900402 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900403
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300404 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900405 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900406}
407
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300408/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530409static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900410 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530411 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300412{
413 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530414 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900415 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300416
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530417 dinfo = open_debuginfo(target);
418
Masami Hiramatsuff741782011-06-27 16:27:39 +0900419 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400420 if (need_dwarf) {
421 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900422 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400423 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900424 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300425 return 0;
426 }
427
Masami Hiramatsuff741782011-06-27 16:27:39 +0900428 /* Searching trace events corresponding to a probe event */
429 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
430
431 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300432
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400433 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530434 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000435 if (target) {
436 if (pev->uprobes)
437 ret = add_exec_to_probe_trace_events(*tevs,
438 ntevs, target);
439 else
440 ret = add_module_to_probe_trace_events(*tevs,
441 ntevs, target);
442 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900443 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400444 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300445
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400446 if (ntevs == 0) { /* No error but failed to find probe point. */
447 pr_warning("Probe point '%s' not found.\n",
448 synthesize_perf_probe_point(&pev->point));
449 return -ENOENT;
450 }
451 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400452 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
453 if (ntevs == -EBADF) {
454 pr_warning("Warning: No dwarf info found in the vmlinux - "
455 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
456 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900457 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400458 return 0;
459 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300460 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400461 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300462}
463
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900464/*
465 * Find a src file from a DWARF tag path. Prepend optional source path prefix
466 * and chop off leading directories that do not exist. Result is passed back as
467 * a newly allocated path on success.
468 * Return 0 if file was found and readable, -errno otherwise.
469 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900470static int get_real_path(const char *raw_path, const char *comp_dir,
471 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900472{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900473 const char *prefix = symbol_conf.source_prefix;
474
475 if (!prefix) {
476 if (raw_path[0] != '/' && comp_dir)
477 /* If not an absolute path, try to use comp_dir */
478 prefix = comp_dir;
479 else {
480 if (access(raw_path, R_OK) == 0) {
481 *new_path = strdup(raw_path);
482 return 0;
483 } else
484 return -errno;
485 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900486 }
487
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900488 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900489 if (!*new_path)
490 return -ENOMEM;
491
492 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900493 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900494
495 if (access(*new_path, R_OK) == 0)
496 return 0;
497
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900498 if (!symbol_conf.source_prefix)
499 /* In case of searching comp_dir, don't retry */
500 return -errno;
501
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900502 switch (errno) {
503 case ENAMETOOLONG:
504 case ENOENT:
505 case EROFS:
506 case EFAULT:
507 raw_path = strchr(++raw_path, '/');
508 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300509 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900510 return -ENOENT;
511 }
512 continue;
513
514 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300515 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900516 return -errno;
517 }
518 }
519}
520
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300521#define LINEBUF_SIZE 256
522#define NR_ADDITIONAL_LINES 2
523
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100524static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300525{
526 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100527 const char *color = show_num ? "" : PERF_COLOR_BLUE;
528 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300529
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100530 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300531 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
532 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100533 if (skip)
534 continue;
535 if (!prefix) {
536 prefix = show_num ? "%7d " : " ";
537 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300538 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100539 color_fprintf(stdout, color, "%s", buf);
540
541 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400542
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100543 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300544error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100545 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100546 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100547 return -1;
548 }
549 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300550}
551
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100552static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
553{
554 int rv = __show_one_line(fp, l, skip, show_num);
555 if (rv == 0) {
556 pr_warning("Source file is shorter than expected.\n");
557 rv = -1;
558 }
559 return rv;
560}
561
562#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
563#define show_one_line(f,l) _show_one_line(f,l,false,false)
564#define skip_one_line(f,l) _show_one_line(f,l,true,false)
565#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
566
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300567/*
568 * Show line-range always requires debuginfo to find source file and
569 * line number.
570 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900571int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300572{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400573 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300574 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900575 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300576 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900577 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900578 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300579
580 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400581 ret = init_vmlinux();
582 if (ret < 0)
583 return ret;
584
Masami Hiramatsuff741782011-06-27 16:27:39 +0900585 dinfo = open_debuginfo(module);
586 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400587 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900588 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400589 }
590
Masami Hiramatsuff741782011-06-27 16:27:39 +0900591 ret = debuginfo__find_line_range(dinfo, lr);
592 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400593 if (ret == 0) {
594 pr_warning("Specified source line is not found.\n");
595 return -ENOENT;
596 } else if (ret < 0) {
597 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
598 return ret;
599 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300600
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900601 /* Convert source file path */
602 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900603 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900604 free(tmp); /* Free old path */
605 if (ret < 0) {
606 pr_warning("Failed to find source file. (%d)\n", ret);
607 return ret;
608 }
609
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300610 setup_pager();
611
612 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900613 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300614 lr->start - lr->offset);
615 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100616 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300617
618 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400619 if (fp == NULL) {
620 pr_warning("Failed to open %s: %s\n", lr->path,
621 strerror(errno));
622 return -errno;
623 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300624 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100625 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100626 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100627 if (ret < 0)
628 goto end;
629 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300630
631 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100632 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100633 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100634 if (ret < 0)
635 goto end;
636 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100637 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400638 if (ret < 0)
639 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300640 }
641
642 if (lr->end == INT_MAX)
643 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100644 while (l <= lr->end) {
645 ret = show_one_line_or_eof(fp, l++ - lr->offset);
646 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100647 break;
648 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400649end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300650 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400651 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300652}
653
Masami Hiramatsuff741782011-06-27 16:27:39 +0900654static int show_available_vars_at(struct debuginfo *dinfo,
655 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900656 int max_vls, struct strfilter *_filter,
657 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900658{
659 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900660 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900661 struct str_node *node;
662 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900663 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900664
665 buf = synthesize_perf_probe_point(&pev->point);
666 if (!buf)
667 return -EINVAL;
668 pr_debug("Searching variables at %s\n", buf);
669
Masami Hiramatsuff741782011-06-27 16:27:39 +0900670 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
671 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900672 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900673 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900674 goto end;
675 }
676 /* Some variables are found */
677 fprintf(stdout, "Available variables at %s\n", buf);
678 for (i = 0; i < ret; i++) {
679 vl = &vls[i];
680 /*
681 * A probe point might be converted to
682 * several trace points.
683 */
684 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
685 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300686 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900687 nvars = 0;
688 if (vl->vars) {
689 strlist__for_each(node, vl->vars) {
690 var = strchr(node->s, '\t') + 1;
691 if (strfilter__compare(_filter, var)) {
692 fprintf(stdout, "\t\t%s\n", node->s);
693 nvars++;
694 }
695 }
696 strlist__delete(vl->vars);
697 }
698 if (nvars == 0)
699 fprintf(stdout, "\t\t(No matched variables)\n");
700 }
701 free(vls);
702end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900703 free(buf);
704 return ret;
705}
706
707/* Show available variables on given probe point */
708int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900709 int max_vls, const char *module,
710 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900711{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900712 int i, ret = 0;
713 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900714
715 ret = init_vmlinux();
716 if (ret < 0)
717 return ret;
718
Masami Hiramatsuff741782011-06-27 16:27:39 +0900719 dinfo = open_debuginfo(module);
720 if (!dinfo) {
721 pr_warning("Failed to open debuginfo file.\n");
722 return -ENOENT;
723 }
724
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900725 setup_pager();
726
Masami Hiramatsuff741782011-06-27 16:27:39 +0900727 for (i = 0; i < npevs && ret >= 0; i++)
728 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900729 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730
731 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900732 return ret;
733}
734
Ingo Molnar89fe8082013-09-30 12:07:11 +0200735#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300736
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530737static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900738 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300739{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900740 struct symbol *sym;
741
742 sym = __find_kernel_function_by_name(tp->symbol, NULL);
743 if (!sym) {
744 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
745 return -ENOENT;
746 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400747
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530748 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300749}
750
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530751static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300752 struct probe_trace_event **tevs __maybe_unused,
753 int max_tevs __maybe_unused, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300754{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400755 if (perf_probe_event_need_dwarf(pev)) {
756 pr_warning("Debuginfo-analysis is not supported.\n");
757 return -ENOSYS;
758 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530759
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300760 return 0;
761}
762
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300763int show_line_range(struct line_range *lr __maybe_unused,
764 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300765{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766 pr_warning("Debuginfo-analysis is not supported.\n");
767 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300768}
769
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300770int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
771 int npevs __maybe_unused, int max_vls __maybe_unused,
772 const char *module __maybe_unused,
773 struct strfilter *filter __maybe_unused,
774 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900775{
776 pr_warning("Debuginfo-analysis is not supported.\n");
777 return -ENOSYS;
778}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400779#endif
780
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100781static int parse_line_num(char **ptr, int *val, const char *what)
782{
783 const char *start = *ptr;
784
785 errno = 0;
786 *val = strtol(*ptr, ptr, 0);
787 if (errno || *ptr == start) {
788 semantic_error("'%s' is not a valid number.\n", what);
789 return -EINVAL;
790 }
791 return 0;
792}
793
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100794/*
795 * Stuff 'lr' according to the line range described by 'arg'.
796 * The line range syntax is described by:
797 *
798 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900799 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100800 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400801int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500802{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900803 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100804 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100805
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100806 if (!name)
807 return -ENOMEM;
808
809 lr->start = 0;
810 lr->end = INT_MAX;
811
812 range = strchr(name, ':');
813 if (range) {
814 *range++ = '\0';
815
816 err = parse_line_num(&range, &lr->start, "start line");
817 if (err)
818 goto err;
819
820 if (*range == '+' || *range == '-') {
821 const char c = *range++;
822
823 err = parse_line_num(&range, &lr->end, "end line");
824 if (err)
825 goto err;
826
827 if (c == '+') {
828 lr->end += lr->start;
829 /*
830 * Adjust the number of lines here.
831 * If the number of lines == 1, the
832 * the end of line should be equal to
833 * the start of line.
834 */
835 lr->end--;
836 }
837 }
838
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400839 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100840
841 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400842 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500843 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100845 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100847 if (*range != '\0') {
848 semantic_error("Tailing with invalid str '%s'.\n", range);
849 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400850 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400851 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400852
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900853 file = strchr(name, '@');
854 if (file) {
855 *file = '\0';
856 lr->file = strdup(++file);
857 if (lr->file == NULL) {
858 err = -ENOMEM;
859 goto err;
860 }
861 lr->function = name;
862 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100863 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500864 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100865 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400866
867 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100868err:
869 free(name);
870 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500871}
872
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500873/* Check the name is good for event/group */
874static bool check_event_name(const char *name)
875{
876 if (!isalpha(*name) && *name != '_')
877 return false;
878 while (*++name != '\0') {
879 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
880 return false;
881 }
882 return true;
883}
884
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500885/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400886static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500887{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400888 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500889 char *ptr, *tmp;
890 char c, nc = 0;
891 /*
892 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500893 * perf probe [EVENT=]SRC[:LN|;PTN]
894 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500895 *
896 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500897 */
898
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500899 ptr = strpbrk(arg, ";=@+%");
900 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500901 *ptr = '\0';
902 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400903 if (strchr(arg, ':')) {
904 semantic_error("Group name is not supported yet.\n");
905 return -ENOTSUP;
906 }
907 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500908 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400909 "follow C symbol-naming rule.\n", arg);
910 return -EINVAL;
911 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400912 pev->event = strdup(arg);
913 if (pev->event == NULL)
914 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400915 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500916 arg = tmp;
917 }
918
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500919 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500920 if (ptr) {
921 nc = *ptr;
922 *ptr++ = '\0';
923 }
924
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400925 tmp = strdup(arg);
926 if (tmp == NULL)
927 return -ENOMEM;
928
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500929 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400930 if (strchr(tmp, '.')) /* File */
931 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500932 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400933 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500934
935 /* Parse other options */
936 while (ptr) {
937 arg = ptr;
938 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500939 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400940 pp->lazy_line = strdup(arg);
941 if (pp->lazy_line == NULL)
942 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500943 break;
944 }
945 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500946 if (ptr) {
947 nc = *ptr;
948 *ptr++ = '\0';
949 }
950 switch (c) {
951 case ':': /* Line number */
952 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400953 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500954 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400955 " in line number.\n");
956 return -EINVAL;
957 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500958 break;
959 case '+': /* Byte offset from a symbol */
960 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400961 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500962 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400963 " in offset.\n");
964 return -EINVAL;
965 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500966 break;
967 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400968 if (pp->file) {
969 semantic_error("SRC@SRC is not allowed.\n");
970 return -EINVAL;
971 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400972 pp->file = strdup(arg);
973 if (pp->file == NULL)
974 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500975 break;
976 case '%': /* Probe places */
977 if (strcmp(arg, "return") == 0) {
978 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400979 } else { /* Others not supported yet */
980 semantic_error("%%%s is not supported.\n", arg);
981 return -ENOTSUP;
982 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500983 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400984 default: /* Buggy case */
985 pr_err("This program has a bug at %s:%d.\n",
986 __FILE__, __LINE__);
987 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500988 break;
989 }
990 }
991
992 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400993 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900994 semantic_error("Lazy pattern can't be used with"
995 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400996 return -EINVAL;
997 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500998
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400999 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001000 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001001 return -EINVAL;
1002 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001003
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001004 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001005 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001006 return -EINVAL;
1007 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001008
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001009 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001010 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001011 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001012 return -EINVAL;
1013 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001014
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001015 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001016 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001017 return -EINVAL;
1018 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001019
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001020 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001021 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001022 return -EINVAL;
1023 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001024
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001025 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001026 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001027 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001028 return -EINVAL;
1029 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001030
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001031 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001032 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1033 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001034 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001035}
1036
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001037/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001039{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001040 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001041 struct perf_probe_arg_field **fieldp;
1042
1043 pr_debug("parsing arg: %s into ", str);
1044
Masami Hiramatsu48481932010-04-12 13:16:53 -04001045 tmp = strchr(str, '=');
1046 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001047 arg->name = strndup(str, tmp - str);
1048 if (arg->name == NULL)
1049 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001050 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001051 str = tmp + 1;
1052 }
1053
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001054 tmp = strchr(str, ':');
1055 if (tmp) { /* Type setting */
1056 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001057 arg->type = strdup(tmp + 1);
1058 if (arg->type == NULL)
1059 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001060 pr_debug("type:%s ", arg->type);
1061 }
1062
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001063 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001064 if (!is_c_varname(str) || !tmp) {
1065 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001066 arg->var = strdup(str);
1067 if (arg->var == NULL)
1068 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001069 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001070 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001071 }
1072
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001073 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001074 arg->var = strndup(str, tmp - str);
1075 if (arg->var == NULL)
1076 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001077 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001078 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001079 fieldp = &arg->field;
1080
1081 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001082 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1083 if (*fieldp == NULL)
1084 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001085 if (*tmp == '[') { /* Array */
1086 str = tmp;
1087 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001088 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001089 if (*tmp != ']' || tmp == str + 1) {
1090 semantic_error("Array index must be a"
1091 " number.\n");
1092 return -EINVAL;
1093 }
1094 tmp++;
1095 if (*tmp == '\0')
1096 tmp = NULL;
1097 } else { /* Structure */
1098 if (*tmp == '.') {
1099 str = tmp + 1;
1100 (*fieldp)->ref = false;
1101 } else if (tmp[1] == '>') {
1102 str = tmp + 2;
1103 (*fieldp)->ref = true;
1104 } else {
1105 semantic_error("Argument parse error: %s\n",
1106 str);
1107 return -EINVAL;
1108 }
1109 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001111 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001112 (*fieldp)->name = strndup(str, tmp - str);
1113 if ((*fieldp)->name == NULL)
1114 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001115 if (*str != '[')
1116 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001117 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1118 fieldp = &(*fieldp)->next;
1119 }
1120 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001121 (*fieldp)->name = strdup(str);
1122 if ((*fieldp)->name == NULL)
1123 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001124 if (*str != '[')
1125 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001126 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001127
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001128 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001129 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001130 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001131 if (arg->name == NULL)
1132 return -ENOMEM;
1133 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001134 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001135}
1136
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001137/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001138int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001139{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001140 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001141 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001142
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001143 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144 if (!argv) {
1145 pr_debug("Failed to split arguments.\n");
1146 return -ENOMEM;
1147 }
1148 if (argc - 1 > MAX_PROBE_ARGS) {
1149 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1150 ret = -ERANGE;
1151 goto out;
1152 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001153 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001154 ret = parse_perf_probe_point(argv[0], pev);
1155 if (ret < 0)
1156 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001158 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001159 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001160 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1161 if (pev->args == NULL) {
1162 ret = -ENOMEM;
1163 goto out;
1164 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1166 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1167 if (ret >= 0 &&
1168 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 " kretprobe.\n");
1171 ret = -EINVAL;
1172 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001173 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001175 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001176
1177 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001178}
1179
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001180/* Return true if this perf_probe_event requires debuginfo */
1181bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001182{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 int i;
1184
1185 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1186 return true;
1187
1188 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001189 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001190 return true;
1191
1192 return false;
1193}
1194
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301195/* Parse probe_events event into struct probe_point */
1196static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001197 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001198{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301199 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001200 char pr;
1201 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001202 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001203 int ret, i, argc;
1204 char **argv;
1205
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301206 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001208 if (!argv) {
1209 pr_debug("Failed to split arguments.\n");
1210 return -ENOMEM;
1211 }
1212 if (argc < 2) {
1213 semantic_error("Too few probe arguments.\n");
1214 ret = -ERANGE;
1215 goto out;
1216 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001217
1218 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001219 argv0_str = strdup(argv[0]);
1220 if (argv0_str == NULL) {
1221 ret = -ENOMEM;
1222 goto out;
1223 }
1224 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1225 fmt2_str = strtok_r(NULL, "/", &fmt);
1226 fmt3_str = strtok_r(NULL, " \t", &fmt);
1227 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1228 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 semantic_error("Failed to parse event name: %s\n", argv[0]);
1230 ret = -EINVAL;
1231 goto out;
1232 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001233 pr = fmt1_str[0];
1234 tev->group = strdup(fmt2_str);
1235 tev->event = strdup(fmt3_str);
1236 if (tev->group == NULL || tev->event == NULL) {
1237 ret = -ENOMEM;
1238 goto out;
1239 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001240 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001241
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001242 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001243
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001244 /* Scan module name(if there), function name and offset */
1245 p = strchr(argv[1], ':');
1246 if (p) {
1247 tp->module = strndup(argv[1], p - argv[1]);
1248 p++;
1249 } else
1250 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001251 fmt1_str = strtok_r(p, "+", &fmt);
1252 tp->symbol = strdup(fmt1_str);
1253 if (tp->symbol == NULL) {
1254 ret = -ENOMEM;
1255 goto out;
1256 }
1257 fmt2_str = strtok_r(NULL, "", &fmt);
1258 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001259 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001260 else
1261 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001262
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001263 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301264 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001265 if (tev->args == NULL) {
1266 ret = -ENOMEM;
1267 goto out;
1268 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001270 p = strchr(argv[i + 2], '=');
1271 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001272 *p++ = '\0';
1273 else
1274 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001275 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001276 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001277 tev->args[i].value = strdup(p);
1278 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1279 ret = -ENOMEM;
1280 goto out;
1281 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001282 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001283 ret = 0;
1284out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001285 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001286 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001288}
1289
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001290/* Compose only probe arg */
1291int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1292{
1293 struct perf_probe_arg_field *field = pa->field;
1294 int ret;
1295 char *tmp = buf;
1296
Masami Hiramatsu48481932010-04-12 13:16:53 -04001297 if (pa->name && pa->var)
1298 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1299 else
1300 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001301 if (ret <= 0)
1302 goto error;
1303 tmp += ret;
1304 len -= ret;
1305
1306 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001307 if (field->name[0] == '[')
1308 ret = e_snprintf(tmp, len, "%s", field->name);
1309 else
1310 ret = e_snprintf(tmp, len, "%s%s",
1311 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001312 if (ret <= 0)
1313 goto error;
1314 tmp += ret;
1315 len -= ret;
1316 field = field->next;
1317 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001318
1319 if (pa->type) {
1320 ret = e_snprintf(tmp, len, ":%s", pa->type);
1321 if (ret <= 0)
1322 goto error;
1323 tmp += ret;
1324 len -= ret;
1325 }
1326
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001327 return tmp - buf;
1328error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001329 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001330 strerror(-ret));
1331 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001332}
1333
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001334/* Compose only probe point (not argument) */
1335static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001336{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001337 char *buf, *tmp;
1338 char offs[32] = "", line[32] = "", file[32] = "";
1339 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001340
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001341 buf = zalloc(MAX_CMDLEN);
1342 if (buf == NULL) {
1343 ret = -ENOMEM;
1344 goto error;
1345 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001346 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001347 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001348 if (ret <= 0)
1349 goto error;
1350 }
1351 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001352 ret = e_snprintf(line, 32, ":%d", pp->line);
1353 if (ret <= 0)
1354 goto error;
1355 }
1356 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001357 tmp = pp->file;
1358 len = strlen(tmp);
1359 if (len > 30) {
1360 tmp = strchr(pp->file + len - 30, '/');
1361 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1362 }
1363 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001364 if (ret <= 0)
1365 goto error;
1366 }
1367
1368 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001369 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1370 offs, pp->retprobe ? "%return" : "", line,
1371 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001372 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001373 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001374 if (ret <= 0)
1375 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001377 return buf;
1378error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001379 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 strerror(-ret));
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001381 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001382 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001383}
1384
1385#if 0
1386char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1387{
1388 char *buf;
1389 int i, len, ret;
1390
1391 buf = synthesize_perf_probe_point(&pev->point);
1392 if (!buf)
1393 return NULL;
1394
1395 len = strlen(buf);
1396 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001397 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398 pev->args[i].name);
1399 if (ret <= 0) {
1400 free(buf);
1401 return NULL;
1402 }
1403 len += ret;
1404 }
1405
1406 return buf;
1407}
1408#endif
1409
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301410static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001411 char **buf, size_t *buflen,
1412 int depth)
1413{
1414 int ret;
1415 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301416 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001417 buflen, depth + 1);
1418 if (depth < 0)
1419 goto out;
1420 }
1421
1422 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1423 if (ret < 0)
1424 depth = ret;
1425 else {
1426 *buf += ret;
1427 *buflen -= ret;
1428 }
1429out:
1430 return depth;
1431
1432}
1433
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301434static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001435 char *buf, size_t buflen)
1436{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301437 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001438 int ret, depth = 0;
1439 char *tmp = buf;
1440
1441 /* Argument name or separator */
1442 if (arg->name)
1443 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1444 else
1445 ret = e_snprintf(buf, buflen, " ");
1446 if (ret < 0)
1447 return ret;
1448 buf += ret;
1449 buflen -= ret;
1450
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001451 /* Special case: @XXX */
1452 if (arg->value[0] == '@' && arg->ref)
1453 ref = ref->next;
1454
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001455 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001456 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301457 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001458 &buflen, 1);
1459 if (depth < 0)
1460 return depth;
1461 }
1462
1463 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001464 if (arg->value[0] == '@' && arg->ref)
1465 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1466 arg->ref->offset);
1467 else
1468 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001469 if (ret < 0)
1470 return ret;
1471 buf += ret;
1472 buflen -= ret;
1473
1474 /* Closing */
1475 while (depth--) {
1476 ret = e_snprintf(buf, buflen, ")");
1477 if (ret < 0)
1478 return ret;
1479 buf += ret;
1480 buflen -= ret;
1481 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001482 /* Print argument type */
1483 if (arg->type) {
1484 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1485 if (ret <= 0)
1486 return ret;
1487 buf += ret;
1488 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001489
1490 return buf - tmp;
1491}
1492
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301493char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301495 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496 char *buf;
1497 int i, len, ret;
1498
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001499 buf = zalloc(MAX_CMDLEN);
1500 if (buf == NULL)
1501 return NULL;
1502
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301503 if (tev->uprobes)
1504 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1505 tp->retprobe ? 'r' : 'p',
1506 tev->group, tev->event,
1507 tp->module, tp->symbol);
1508 else
1509 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1510 tp->retprobe ? 'r' : 'p',
1511 tev->group, tev->event,
1512 tp->module ?: "", tp->module ? ":" : "",
1513 tp->symbol, tp->offset);
1514
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 if (len <= 0)
1516 goto error;
1517
1518 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301519 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001521 if (ret <= 0)
1522 goto error;
1523 len += ret;
1524 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001525
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001527error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001528 free(buf);
1529 return NULL;
1530}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001531
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301532static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301533 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001534{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001535 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001536 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001537
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001538 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001539 pev->event = strdup(tev->event);
1540 pev->group = strdup(tev->group);
1541 if (pev->event == NULL || pev->group == NULL)
1542 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001543
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001544 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301545 if (is_kprobe)
1546 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1547 else
1548 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1549
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550 if (ret < 0)
1551 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001552
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001553 /* Convert trace_arg to probe_arg */
1554 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001555 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1556 if (pev->args == NULL)
1557 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001558 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001559 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001560 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001561 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301562 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001563 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001564 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001565 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001566 if (pev->args[i].name == NULL && ret >= 0)
1567 ret = -ENOMEM;
1568 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001569
1570 if (ret < 0)
1571 clear_perf_probe_event(pev);
1572
1573 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001574}
1575
1576void clear_perf_probe_event(struct perf_probe_event *pev)
1577{
1578 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001579 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001580 int i;
1581
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001582 free(pev->event);
1583 free(pev->group);
1584 free(pp->file);
1585 free(pp->function);
1586 free(pp->lazy_line);
1587
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001588 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001589 free(pev->args[i].name);
1590 free(pev->args[i].var);
1591 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001592 field = pev->args[i].field;
1593 while (field) {
1594 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001595 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001596 free(field);
1597 field = next;
1598 }
1599 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001600 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001601 memset(pev, 0, sizeof(*pev));
1602}
1603
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301604static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301606 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001607 int i;
1608
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001609 free(tev->event);
1610 free(tev->group);
1611 free(tev->point.symbol);
1612 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001614 free(tev->args[i].name);
1615 free(tev->args[i].value);
1616 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001617 ref = tev->args[i].ref;
1618 while (ref) {
1619 next = ref->next;
1620 free(ref);
1621 ref = next;
1622 }
1623 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001624 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001626}
1627
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301628static void print_warn_msg(const char *file, bool is_kprobe)
1629{
1630
1631 if (errno == ENOENT) {
1632 const char *config;
1633
1634 if (!is_kprobe)
1635 config = "CONFIG_UPROBE_EVENTS";
1636 else
1637 config = "CONFIG_KPROBE_EVENTS";
1638
1639 pr_warning("%s file does not exist - please rebuild kernel"
1640 " with %s.\n", file, config);
1641 } else
1642 pr_warning("Failed to open %s file: %s\n", file,
1643 strerror(errno));
1644}
1645
1646static int open_probe_events(const char *trace_file, bool readwrite,
1647 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001648{
1649 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001650 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001651 int ret;
1652
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001653 __debugfs = debugfs_find_mountpoint();
1654 if (__debugfs == NULL) {
1655 pr_warning("Debugfs is not mounted.\n");
1656 return -ENOENT;
1657 }
1658
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301659 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001660 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001661 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001662 if (readwrite && !probe_event_dry_run)
1663 ret = open(buf, O_RDWR, O_APPEND);
1664 else
1665 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001666
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301667 if (ret < 0)
1668 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001669 }
1670 return ret;
1671}
1672
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301673static int open_kprobe_events(bool readwrite)
1674{
1675 return open_probe_events("tracing/kprobe_events", readwrite, true);
1676}
1677
1678static int open_uprobe_events(bool readwrite)
1679{
1680 return open_probe_events("tracing/uprobe_events", readwrite, false);
1681}
1682
1683/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301684static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001685{
1686 int ret, idx;
1687 FILE *fp;
1688 char buf[MAX_CMDLEN];
1689 char *p;
1690 struct strlist *sl;
1691
1692 sl = strlist__new(true, NULL);
1693
1694 fp = fdopen(dup(fd), "r");
1695 while (!feof(fp)) {
1696 p = fgets(buf, MAX_CMDLEN, fp);
1697 if (!p)
1698 break;
1699
1700 idx = strlen(p) - 1;
1701 if (p[idx] == '\n')
1702 p[idx] = '\0';
1703 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001704 if (ret < 0) {
1705 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1706 strlist__delete(sl);
1707 return NULL;
1708 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001709 }
1710 fclose(fp);
1711
1712 return sl;
1713}
1714
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001715/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001716static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001717{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001718 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001719 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001720 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001721
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001722 /* Synthesize only event probe point */
1723 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001724 if (!place)
1725 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001726
1727 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001728 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001729 return ret;
1730
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001731 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001732
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001734 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001735 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736 ret = synthesize_perf_probe_arg(&pev->args[i],
1737 buf, 128);
1738 if (ret < 0)
1739 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001740 printf(" %s", buf);
1741 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001742 }
1743 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001745 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001746}
1747
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301748static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001749{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301750 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301751 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001752 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001753 struct strlist *rawlist;
1754 struct str_node *ent;
1755
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001756 memset(&tev, 0, sizeof(tev));
1757 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001758
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301759 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001760 if (!rawlist)
1761 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001762
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001763 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301764 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001765 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301766 ret = convert_to_perf_probe_event(&tev, &pev,
1767 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001768 if (ret >= 0)
1769 ret = show_perf_probe_event(&pev);
1770 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301772 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001773 if (ret < 0)
1774 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001775 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001776 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001777
1778 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001779}
1780
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301781/* List up current perf-probe events */
1782int show_perf_probe_events(void)
1783{
1784 int fd, ret;
1785
1786 setup_pager();
1787 fd = open_kprobe_events(false);
1788
1789 if (fd < 0)
1790 return fd;
1791
1792 ret = init_vmlinux();
1793 if (ret < 0)
1794 return ret;
1795
1796 ret = __show_perf_probe_events(fd, true);
1797 close(fd);
1798
1799 fd = open_uprobe_events(false);
1800 if (fd >= 0) {
1801 ret = __show_perf_probe_events(fd, false);
1802 close(fd);
1803 }
1804
1805 return ret;
1806}
1807
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001808/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301809static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001810{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001811 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001812 struct strlist *sl, *rawlist;
1813 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301814 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001815 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001816
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001817 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301818 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001819 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001820 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301821 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001822 if (ret < 0)
1823 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001824 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001825 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1826 tev.event);
1827 if (ret >= 0)
1828 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001829 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001830 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301831 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001832 if (ret < 0)
1833 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001834 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001835 strlist__delete(rawlist);
1836
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001837 if (ret < 0) {
1838 strlist__delete(sl);
1839 return NULL;
1840 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001841 return sl;
1842}
1843
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301844static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001845{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001846 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301847 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001848
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301850 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001851 return -EINVAL;
1852 }
1853
Masami Hiramatsufa282442009-12-08 17:03:23 -05001854 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001855 if (!probe_event_dry_run) {
1856 ret = write(fd, buf, strlen(buf));
1857 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001858 pr_warning("Failed to write event: %s\n",
1859 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001860 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001862 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001863}
1864
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865static int get_new_event_name(char *buf, size_t len, const char *base,
1866 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001867{
1868 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001869
1870 /* Try no suffix */
1871 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001872 if (ret < 0) {
1873 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1874 return ret;
1875 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001876 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001877 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001878
Masami Hiramatsud761b082009-12-15 10:32:25 -05001879 if (!allow_suffix) {
1880 pr_warning("Error: event \"%s\" already exists. "
1881 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001882 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001883 }
1884
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001885 /* Try to add suffix */
1886 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001887 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001888 if (ret < 0) {
1889 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1890 return ret;
1891 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001892 if (!strlist__has_entry(namelist, buf))
1893 break;
1894 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895 if (i == MAX_EVENT_INDEX) {
1896 pr_warning("Too many events are on the same function.\n");
1897 ret = -ERANGE;
1898 }
1899
1900 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001901}
1902
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301903static int __add_probe_trace_events(struct perf_probe_event *pev,
1904 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001905 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001906{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001907 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301908 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001909 char buf[64];
1910 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001911 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001912
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301913 if (pev->uprobes)
1914 fd = open_uprobe_events(true);
1915 else
1916 fd = open_kprobe_events(true);
1917
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001918 if (fd < 0)
1919 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001920 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301921 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001922 if (!namelist) {
1923 pr_debug("Failed to get current event list.\n");
1924 return -EIO;
1925 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001926
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001927 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301928 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001929 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001930 tev = &tevs[i];
1931 if (pev->event)
1932 event = pev->event;
1933 else
1934 if (pev->point.function)
1935 event = pev->point.function;
1936 else
1937 event = tev->point.symbol;
1938 if (pev->group)
1939 group = pev->group;
1940 else
1941 group = PERFPROBE_GROUP;
1942
1943 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001944 ret = get_new_event_name(buf, 64, event,
1945 namelist, allow_suffix);
1946 if (ret < 0)
1947 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001948 event = buf;
1949
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001950 tev->event = strdup(event);
1951 tev->group = strdup(group);
1952 if (tev->event == NULL || tev->group == NULL) {
1953 ret = -ENOMEM;
1954 break;
1955 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301956 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001957 if (ret < 0)
1958 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001959 /* Add added event name to namelist */
1960 strlist__add(namelist, event);
1961
1962 /* Trick here - save current event/group */
1963 event = pev->event;
1964 group = pev->group;
1965 pev->event = tev->event;
1966 pev->group = tev->group;
1967 show_perf_probe_event(pev);
1968 /* Trick here - restore current event/group */
1969 pev->event = (char *)event;
1970 pev->group = (char *)group;
1971
1972 /*
1973 * Probes after the first probe which comes from same
1974 * user input are always allowed to add suffix, because
1975 * there might be several addresses corresponding to
1976 * one code line.
1977 */
1978 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001979 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001980
1981 if (ret >= 0) {
1982 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301983 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001984 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1985 tev->event);
1986 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001987
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001988 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001989 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001990 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001991}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001992
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301993static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1994 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301995 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001996{
1997 struct symbol *sym;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00001998 int ret, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301999 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002000
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002001 if (pev->uprobes && !pev->group) {
2002 /* Replace group name if not given */
2003 ret = convert_exec_to_group(target, &pev->group);
2004 if (ret != 0) {
2005 pr_warning("Failed to make a group name.\n");
2006 return ret;
2007 }
2008 }
2009
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002010 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302011 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002012 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002013 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002014
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002015 if (pev->uprobes) {
2016 ret = convert_name_to_addr(pev, target);
2017 if (ret < 0)
2018 return ret;
2019 }
2020
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002021 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302022 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002023 if (tev == NULL)
2024 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002025
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002026 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002027 tev->point.symbol = strdup(pev->point.function);
2028 if (tev->point.symbol == NULL) {
2029 ret = -ENOMEM;
2030 goto error;
2031 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002032
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302033 if (target) {
2034 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08002035 if (tev->point.module == NULL) {
2036 ret = -ENOMEM;
2037 goto error;
2038 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002039 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002040
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002041 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09002042 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002043 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302044 tev->uprobes = pev->uprobes;
2045
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002046 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302047 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002048 * tev->nargs);
2049 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002050 ret = -ENOMEM;
2051 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002052 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002053 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002054 if (pev->args[i].name) {
2055 tev->args[i].name = strdup(pev->args[i].name);
2056 if (tev->args[i].name == NULL) {
2057 ret = -ENOMEM;
2058 goto error;
2059 }
2060 }
2061 tev->args[i].value = strdup(pev->args[i].var);
2062 if (tev->args[i].value == NULL) {
2063 ret = -ENOMEM;
2064 goto error;
2065 }
2066 if (pev->args[i].type) {
2067 tev->args[i].type = strdup(pev->args[i].type);
2068 if (tev->args[i].type == NULL) {
2069 ret = -ENOMEM;
2070 goto error;
2071 }
2072 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002073 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002074 }
2075
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302076 if (pev->uprobes)
2077 return 1;
2078
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002079 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002080 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002081 if (!sym) {
2082 pr_warning("Kernel symbol \'%s\' not found.\n",
2083 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002084 ret = -ENOENT;
2085 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05302086 } else if (tev->point.offset > sym->end - sym->start) {
2087 pr_warning("Offset specified is greater than size of %s\n",
2088 tev->point.symbol);
2089 ret = -ENOENT;
2090 goto error;
2091
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002092 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002093
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002094 return 1;
2095error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302096 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002097 free(tev);
2098 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002099 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002100}
2101
2102struct __event_package {
2103 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302104 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002105 int ntevs;
2106};
2107
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002108int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302109 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002110{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002111 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002112 struct __event_package *pkgs;
2113
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302114 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002115 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302116
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002117 if (pkgs == NULL)
2118 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002119
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302120 if (!pevs->uprobes)
2121 /* Init vmlinux path */
2122 ret = init_vmlinux();
2123 else
2124 ret = init_user_exec();
2125
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002126 if (ret < 0) {
2127 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002129 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002130
2131 /* Loop 1: convert all events */
2132 for (i = 0; i < npevs; i++) {
2133 pkgs[i].pev = &pevs[i];
2134 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302135 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002136 &pkgs[i].tevs,
2137 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302138 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002139 if (ret < 0)
2140 goto end;
2141 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002142 }
2143
2144 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002145 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302146 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002147 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002148 if (ret < 0)
2149 break;
2150 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002151end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002152 /* Loop 3: cleanup and free trace events */
2153 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002154 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302155 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002156 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002157 }
2158 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002159
2160 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002161}
2162
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302163static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002164{
2165 char *p;
2166 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002167 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002168
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302169 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002170 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2171 if (ret < 0)
2172 goto error;
2173
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002174 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002175 if (!p) {
2176 pr_debug("Internal error: %s should have ':' but not.\n",
2177 ent->s);
2178 ret = -ENOTSUP;
2179 goto error;
2180 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002181 *p = '/';
2182
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002183 pr_debug("Writing event: %s\n", buf);
2184 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002185 if (ret < 0) {
2186 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002187 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002188 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002189
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302190 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002191 return 0;
2192error:
2193 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2194 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002195}
2196
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302197static int del_trace_probe_event(int fd, const char *buf,
2198 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002199{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002200 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302201 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002202
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002203 if (strpbrk(buf, "*?")) { /* Glob-exp */
2204 strlist__for_each_safe(ent, n, namelist)
2205 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302206 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207 if (ret < 0)
2208 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002209 strlist__remove(namelist, ent);
2210 }
2211 } else {
2212 ent = strlist__find(namelist, buf);
2213 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302214 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002215 if (ret >= 0)
2216 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002217 }
2218 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002219
2220 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002221}
2222
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002223int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002224{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302225 int ret = -1, ufd = -1, kfd = -1;
2226 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002227 const char *group, *event;
2228 char *p, *str;
2229 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302230 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002231
Masami Hiramatsufa282442009-12-08 17:03:23 -05002232 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302233 kfd = open_kprobe_events(true);
2234 if (kfd < 0)
2235 return kfd;
2236
2237 namelist = get_probe_trace_event_names(kfd, true);
2238 ufd = open_uprobe_events(true);
2239
2240 if (ufd >= 0)
2241 unamelist = get_probe_trace_event_names(ufd, true);
2242
2243 if (namelist == NULL && unamelist == NULL)
2244 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002245
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002246 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002247 str = strdup(ent->s);
2248 if (str == NULL) {
2249 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302250 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002251 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002252 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002253 p = strchr(str, ':');
2254 if (p) {
2255 group = str;
2256 *p = '\0';
2257 event = p + 1;
2258 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002259 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002260 event = str;
2261 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302262
2263 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2264 if (ret < 0) {
2265 pr_err("Failed to copy event.");
2266 free(str);
2267 goto error;
2268 }
2269
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002270 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302271
2272 if (namelist)
2273 ret = del_trace_probe_event(kfd, buf, namelist);
2274
2275 if (unamelist && ret != 0)
2276 ret = del_trace_probe_event(ufd, buf, unamelist);
2277
2278 if (ret != 0)
2279 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2280
Masami Hiramatsufa282442009-12-08 17:03:23 -05002281 free(str);
2282 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302283
2284error:
2285 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302286 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302287 close(kfd);
2288 }
2289
2290 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302291 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302292 close(ufd);
2293 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002294
2295 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002296}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302297
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002298/* TODO: don't use a global variable for filter ... */
2299static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002300
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002301/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002302 * If a symbol corresponds to a function with global binding and
2303 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002304 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002305static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002306 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002307{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002308 if (sym->binding == STB_GLOBAL &&
2309 strfilter__compare(available_func_filter, sym->name))
2310 return 0;
2311 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002312}
2313
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302314static int __show_available_funcs(struct map *map)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002315{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002316 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002317 pr_err("Failed to load map.\n");
2318 return -EINVAL;
2319 }
2320 if (!dso__sorted_by_name(map->dso, map->type))
2321 dso__sort_by_name(map->dso, map->type);
2322
2323 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2324 return 0;
2325}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302326
2327static int available_kernel_funcs(const char *module)
2328{
2329 struct map *map;
2330 int ret;
2331
2332 ret = init_vmlinux();
2333 if (ret < 0)
2334 return ret;
2335
2336 map = kernel_get_module_map(module);
2337 if (!map) {
2338 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2339 return -EINVAL;
2340 }
2341 return __show_available_funcs(map);
2342}
2343
2344static int available_user_funcs(const char *target)
2345{
2346 struct map *map;
2347 int ret;
2348
2349 ret = init_user_exec();
2350 if (ret < 0)
2351 return ret;
2352
2353 map = dso__new_map(target);
2354 ret = __show_available_funcs(map);
2355 dso__delete(map->dso);
2356 map__delete(map);
2357 return ret;
2358}
2359
2360int show_available_funcs(const char *target, struct strfilter *_filter,
2361 bool user)
2362{
2363 setup_pager();
2364 available_func_filter = _filter;
2365
2366 if (!user)
2367 return available_kernel_funcs(target);
2368
2369 return available_user_funcs(target);
2370}
2371
2372/*
2373 * uprobe_events only accepts address:
2374 * Convert function and any offset to address
2375 */
2376static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2377{
2378 struct perf_probe_point *pp = &pev->point;
2379 struct symbol *sym;
2380 struct map *map = NULL;
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002381 char *function = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302382 int ret = -EINVAL;
2383 unsigned long long vaddr = 0;
2384
2385 if (!pp->function) {
2386 pr_warning("No function specified for uprobes");
2387 goto out;
2388 }
2389
2390 function = strdup(pp->function);
2391 if (!function) {
2392 pr_warning("Failed to allocate memory by strdup.\n");
2393 ret = -ENOMEM;
2394 goto out;
2395 }
2396
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002397 map = dso__new_map(exec);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302398 if (!map) {
2399 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2400 goto out;
2401 }
2402 available_func_filter = strfilter__new(function, NULL);
2403 if (map__load(map, filter_available_functions)) {
2404 pr_err("Failed to load map.\n");
2405 goto out;
2406 }
2407
2408 sym = map__find_symbol_by_name(map, function, NULL);
2409 if (!sym) {
2410 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2411 goto out;
2412 }
2413
2414 if (map->start > sym->start)
2415 vaddr = map->start;
2416 vaddr += sym->start + pp->offset + map->pgoff;
2417 pp->offset = 0;
2418
2419 if (!pev->event) {
2420 pev->event = function;
2421 function = NULL;
2422 }
2423 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002424 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302425
2426 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002427 exec_copy = strdup(exec);
2428 if (!exec_copy) {
2429 ret = -ENOMEM;
2430 pr_warning("Failed to copy exec string.\n");
2431 goto out;
2432 }
2433
2434 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302435 if (ptr1) {
2436 ptr2 = strpbrk(ptr1, "-._");
2437 if (ptr2)
2438 *ptr2 = '\0';
2439 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2440 ptr1);
2441 free(ptr1);
2442 }
David Ahern1fb89442012-09-08 09:06:51 -06002443 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302444 }
2445 free(pp->function);
2446 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2447 if (!pp->function) {
2448 ret = -ENOMEM;
2449 pr_warning("Failed to allocate memory by zalloc.\n");
2450 goto out;
2451 }
2452 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2453 ret = 0;
2454
2455out:
2456 if (map) {
2457 dso__delete(map->dso);
2458 map__delete(map);
2459 }
2460 if (function)
2461 free(function);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302462 return ret;
2463}