blob: 72b56aef105ea321b4aa3d0859a7c93e28cd1930 [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;
359 free(tevs[i].point.symbol);
360 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
404 if (tmp)
405 free(tmp);
406
407 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900408}
409
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300410/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530411static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900412 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530413 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300414{
415 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530416 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900417 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300418
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530419 dinfo = open_debuginfo(target);
420
Masami Hiramatsuff741782011-06-27 16:27:39 +0900421 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400422 if (need_dwarf) {
423 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900424 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400425 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900426 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300427 return 0;
428 }
429
Masami Hiramatsuff741782011-06-27 16:27:39 +0900430 /* Searching trace events corresponding to a probe event */
431 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
432
433 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300434
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400435 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530436 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000437 if (target) {
438 if (pev->uprobes)
439 ret = add_exec_to_probe_trace_events(*tevs,
440 ntevs, target);
441 else
442 ret = add_module_to_probe_trace_events(*tevs,
443 ntevs, target);
444 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900445 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400446 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300447
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400448 if (ntevs == 0) { /* No error but failed to find probe point. */
449 pr_warning("Probe point '%s' not found.\n",
450 synthesize_perf_probe_point(&pev->point));
451 return -ENOENT;
452 }
453 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400454 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
455 if (ntevs == -EBADF) {
456 pr_warning("Warning: No dwarf info found in the vmlinux - "
457 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
458 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900459 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400460 return 0;
461 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300462 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400463 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300464}
465
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900466/*
467 * Find a src file from a DWARF tag path. Prepend optional source path prefix
468 * and chop off leading directories that do not exist. Result is passed back as
469 * a newly allocated path on success.
470 * Return 0 if file was found and readable, -errno otherwise.
471 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900472static int get_real_path(const char *raw_path, const char *comp_dir,
473 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900474{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900475 const char *prefix = symbol_conf.source_prefix;
476
477 if (!prefix) {
478 if (raw_path[0] != '/' && comp_dir)
479 /* If not an absolute path, try to use comp_dir */
480 prefix = comp_dir;
481 else {
482 if (access(raw_path, R_OK) == 0) {
483 *new_path = strdup(raw_path);
484 return 0;
485 } else
486 return -errno;
487 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900488 }
489
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900490 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900491 if (!*new_path)
492 return -ENOMEM;
493
494 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900495 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900496
497 if (access(*new_path, R_OK) == 0)
498 return 0;
499
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900500 if (!symbol_conf.source_prefix)
501 /* In case of searching comp_dir, don't retry */
502 return -errno;
503
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900504 switch (errno) {
505 case ENAMETOOLONG:
506 case ENOENT:
507 case EROFS:
508 case EFAULT:
509 raw_path = strchr(++raw_path, '/');
510 if (!raw_path) {
511 free(*new_path);
512 *new_path = NULL;
513 return -ENOENT;
514 }
515 continue;
516
517 default:
518 free(*new_path);
519 *new_path = NULL;
520 return -errno;
521 }
522 }
523}
524
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300525#define LINEBUF_SIZE 256
526#define NR_ADDITIONAL_LINES 2
527
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100528static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300529{
530 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100531 const char *color = show_num ? "" : PERF_COLOR_BLUE;
532 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300533
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100534 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300535 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
536 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100537 if (skip)
538 continue;
539 if (!prefix) {
540 prefix = show_num ? "%7d " : " ";
541 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300542 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100543 color_fprintf(stdout, color, "%s", buf);
544
545 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400546
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100547 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300548error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100549 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100550 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100551 return -1;
552 }
553 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300554}
555
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100556static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
557{
558 int rv = __show_one_line(fp, l, skip, show_num);
559 if (rv == 0) {
560 pr_warning("Source file is shorter than expected.\n");
561 rv = -1;
562 }
563 return rv;
564}
565
566#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
567#define show_one_line(f,l) _show_one_line(f,l,false,false)
568#define skip_one_line(f,l) _show_one_line(f,l,true,false)
569#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
570
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300571/*
572 * Show line-range always requires debuginfo to find source file and
573 * line number.
574 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900575int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300576{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400577 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300578 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900579 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300580 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900581 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900582 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300583
584 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400585 ret = init_vmlinux();
586 if (ret < 0)
587 return ret;
588
Masami Hiramatsuff741782011-06-27 16:27:39 +0900589 dinfo = open_debuginfo(module);
590 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400591 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900592 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400593 }
594
Masami Hiramatsuff741782011-06-27 16:27:39 +0900595 ret = debuginfo__find_line_range(dinfo, lr);
596 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400597 if (ret == 0) {
598 pr_warning("Specified source line is not found.\n");
599 return -ENOENT;
600 } else if (ret < 0) {
601 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
602 return ret;
603 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300604
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900605 /* Convert source file path */
606 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900607 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900608 free(tmp); /* Free old path */
609 if (ret < 0) {
610 pr_warning("Failed to find source file. (%d)\n", ret);
611 return ret;
612 }
613
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300614 setup_pager();
615
616 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900617 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300618 lr->start - lr->offset);
619 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100620 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300621
622 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400623 if (fp == NULL) {
624 pr_warning("Failed to open %s: %s\n", lr->path,
625 strerror(errno));
626 return -errno;
627 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300628 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100629 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100630 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100631 if (ret < 0)
632 goto end;
633 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300634
635 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100636 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100637 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100638 if (ret < 0)
639 goto end;
640 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100641 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400642 if (ret < 0)
643 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300644 }
645
646 if (lr->end == INT_MAX)
647 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100648 while (l <= lr->end) {
649 ret = show_one_line_or_eof(fp, l++ - lr->offset);
650 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100651 break;
652 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400653end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300654 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400655 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300656}
657
Masami Hiramatsuff741782011-06-27 16:27:39 +0900658static int show_available_vars_at(struct debuginfo *dinfo,
659 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900660 int max_vls, struct strfilter *_filter,
661 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900662{
663 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900664 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900665 struct str_node *node;
666 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900667 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900668
669 buf = synthesize_perf_probe_point(&pev->point);
670 if (!buf)
671 return -EINVAL;
672 pr_debug("Searching variables at %s\n", buf);
673
Masami Hiramatsuff741782011-06-27 16:27:39 +0900674 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
675 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900676 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900677 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900678 goto end;
679 }
680 /* Some variables are found */
681 fprintf(stdout, "Available variables at %s\n", buf);
682 for (i = 0; i < ret; i++) {
683 vl = &vls[i];
684 /*
685 * A probe point might be converted to
686 * several trace points.
687 */
688 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
689 vl->point.offset);
690 free(vl->point.symbol);
691 nvars = 0;
692 if (vl->vars) {
693 strlist__for_each(node, vl->vars) {
694 var = strchr(node->s, '\t') + 1;
695 if (strfilter__compare(_filter, var)) {
696 fprintf(stdout, "\t\t%s\n", node->s);
697 nvars++;
698 }
699 }
700 strlist__delete(vl->vars);
701 }
702 if (nvars == 0)
703 fprintf(stdout, "\t\t(No matched variables)\n");
704 }
705 free(vls);
706end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900707 free(buf);
708 return ret;
709}
710
711/* Show available variables on given probe point */
712int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900713 int max_vls, const char *module,
714 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900715{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900716 int i, ret = 0;
717 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900718
719 ret = init_vmlinux();
720 if (ret < 0)
721 return ret;
722
Masami Hiramatsuff741782011-06-27 16:27:39 +0900723 dinfo = open_debuginfo(module);
724 if (!dinfo) {
725 pr_warning("Failed to open debuginfo file.\n");
726 return -ENOENT;
727 }
728
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900729 setup_pager();
730
Masami Hiramatsuff741782011-06-27 16:27:39 +0900731 for (i = 0; i < npevs && ret >= 0; i++)
732 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900733 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900734
735 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900736 return ret;
737}
738
Ingo Molnar89fe8082013-09-30 12:07:11 +0200739#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300740
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530741static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900742 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900744 struct symbol *sym;
745
746 sym = __find_kernel_function_by_name(tp->symbol, NULL);
747 if (!sym) {
748 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
749 return -ENOENT;
750 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400751
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530752 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300753}
754
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530755static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300756 struct probe_trace_event **tevs __maybe_unused,
757 int max_tevs __maybe_unused, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300758{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400759 if (perf_probe_event_need_dwarf(pev)) {
760 pr_warning("Debuginfo-analysis is not supported.\n");
761 return -ENOSYS;
762 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530763
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300764 return 0;
765}
766
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300767int show_line_range(struct line_range *lr __maybe_unused,
768 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300769{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400770 pr_warning("Debuginfo-analysis is not supported.\n");
771 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300772}
773
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300774int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
775 int npevs __maybe_unused, int max_vls __maybe_unused,
776 const char *module __maybe_unused,
777 struct strfilter *filter __maybe_unused,
778 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900779{
780 pr_warning("Debuginfo-analysis is not supported.\n");
781 return -ENOSYS;
782}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400783#endif
784
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100785static int parse_line_num(char **ptr, int *val, const char *what)
786{
787 const char *start = *ptr;
788
789 errno = 0;
790 *val = strtol(*ptr, ptr, 0);
791 if (errno || *ptr == start) {
792 semantic_error("'%s' is not a valid number.\n", what);
793 return -EINVAL;
794 }
795 return 0;
796}
797
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100798/*
799 * Stuff 'lr' according to the line range described by 'arg'.
800 * The line range syntax is described by:
801 *
802 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900803 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100804 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400805int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500806{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900807 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100808 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100809
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100810 if (!name)
811 return -ENOMEM;
812
813 lr->start = 0;
814 lr->end = INT_MAX;
815
816 range = strchr(name, ':');
817 if (range) {
818 *range++ = '\0';
819
820 err = parse_line_num(&range, &lr->start, "start line");
821 if (err)
822 goto err;
823
824 if (*range == '+' || *range == '-') {
825 const char c = *range++;
826
827 err = parse_line_num(&range, &lr->end, "end line");
828 if (err)
829 goto err;
830
831 if (c == '+') {
832 lr->end += lr->start;
833 /*
834 * Adjust the number of lines here.
835 * If the number of lines == 1, the
836 * the end of line should be equal to
837 * the start of line.
838 */
839 lr->end--;
840 }
841 }
842
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400843 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100844
845 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400846 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500847 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400848 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100849 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400850 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100851 if (*range != '\0') {
852 semantic_error("Tailing with invalid str '%s'.\n", range);
853 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400854 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400855 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400856
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900857 file = strchr(name, '@');
858 if (file) {
859 *file = '\0';
860 lr->file = strdup(++file);
861 if (lr->file == NULL) {
862 err = -ENOMEM;
863 goto err;
864 }
865 lr->function = name;
866 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100867 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500868 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100869 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400870
871 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100872err:
873 free(name);
874 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500875}
876
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500877/* Check the name is good for event/group */
878static bool check_event_name(const char *name)
879{
880 if (!isalpha(*name) && *name != '_')
881 return false;
882 while (*++name != '\0') {
883 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
884 return false;
885 }
886 return true;
887}
888
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500889/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500891{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400892 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500893 char *ptr, *tmp;
894 char c, nc = 0;
895 /*
896 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500897 * perf probe [EVENT=]SRC[:LN|;PTN]
898 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500899 *
900 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500901 */
902
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500903 ptr = strpbrk(arg, ";=@+%");
904 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500905 *ptr = '\0';
906 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400907 if (strchr(arg, ':')) {
908 semantic_error("Group name is not supported yet.\n");
909 return -ENOTSUP;
910 }
911 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500912 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400913 "follow C symbol-naming rule.\n", arg);
914 return -EINVAL;
915 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400916 pev->event = strdup(arg);
917 if (pev->event == NULL)
918 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400919 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500920 arg = tmp;
921 }
922
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500923 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500924 if (ptr) {
925 nc = *ptr;
926 *ptr++ = '\0';
927 }
928
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400929 tmp = strdup(arg);
930 if (tmp == NULL)
931 return -ENOMEM;
932
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500933 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400934 if (strchr(tmp, '.')) /* File */
935 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500936 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400937 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500938
939 /* Parse other options */
940 while (ptr) {
941 arg = ptr;
942 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500943 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400944 pp->lazy_line = strdup(arg);
945 if (pp->lazy_line == NULL)
946 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500947 break;
948 }
949 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500950 if (ptr) {
951 nc = *ptr;
952 *ptr++ = '\0';
953 }
954 switch (c) {
955 case ':': /* Line number */
956 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400957 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500958 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400959 " in line number.\n");
960 return -EINVAL;
961 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500962 break;
963 case '+': /* Byte offset from a symbol */
964 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400965 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500966 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400967 " in offset.\n");
968 return -EINVAL;
969 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500970 break;
971 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 if (pp->file) {
973 semantic_error("SRC@SRC is not allowed.\n");
974 return -EINVAL;
975 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400976 pp->file = strdup(arg);
977 if (pp->file == NULL)
978 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500979 break;
980 case '%': /* Probe places */
981 if (strcmp(arg, "return") == 0) {
982 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400983 } else { /* Others not supported yet */
984 semantic_error("%%%s is not supported.\n", arg);
985 return -ENOTSUP;
986 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500987 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400988 default: /* Buggy case */
989 pr_err("This program has a bug at %s:%d.\n",
990 __FILE__, __LINE__);
991 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500992 break;
993 }
994 }
995
996 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400997 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900998 semantic_error("Lazy pattern can't be used with"
999 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001000 return -EINVAL;
1001 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001002
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001003 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001004 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001005 return -EINVAL;
1006 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001007
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001008 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001009 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001010 return -EINVAL;
1011 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001012
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001013 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001014 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001015 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001016 return -EINVAL;
1017 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001018
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001019 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001020 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001021 return -EINVAL;
1022 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001023
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001024 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001025 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001026 return -EINVAL;
1027 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001028
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001029 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001030 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001031 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001032 return -EINVAL;
1033 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001034
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001035 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001036 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1037 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001039}
1040
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001041/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001042static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001043{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001044 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001045 struct perf_probe_arg_field **fieldp;
1046
1047 pr_debug("parsing arg: %s into ", str);
1048
Masami Hiramatsu48481932010-04-12 13:16:53 -04001049 tmp = strchr(str, '=');
1050 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001051 arg->name = strndup(str, tmp - str);
1052 if (arg->name == NULL)
1053 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001054 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001055 str = tmp + 1;
1056 }
1057
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001058 tmp = strchr(str, ':');
1059 if (tmp) { /* Type setting */
1060 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001061 arg->type = strdup(tmp + 1);
1062 if (arg->type == NULL)
1063 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001064 pr_debug("type:%s ", arg->type);
1065 }
1066
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001067 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001068 if (!is_c_varname(str) || !tmp) {
1069 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001070 arg->var = strdup(str);
1071 if (arg->var == NULL)
1072 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001073 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001074 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001075 }
1076
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001077 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001078 arg->var = strndup(str, tmp - str);
1079 if (arg->var == NULL)
1080 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001081 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001082 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001083 fieldp = &arg->field;
1084
1085 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001086 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1087 if (*fieldp == NULL)
1088 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001089 if (*tmp == '[') { /* Array */
1090 str = tmp;
1091 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001092 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001093 if (*tmp != ']' || tmp == str + 1) {
1094 semantic_error("Array index must be a"
1095 " number.\n");
1096 return -EINVAL;
1097 }
1098 tmp++;
1099 if (*tmp == '\0')
1100 tmp = NULL;
1101 } else { /* Structure */
1102 if (*tmp == '.') {
1103 str = tmp + 1;
1104 (*fieldp)->ref = false;
1105 } else if (tmp[1] == '>') {
1106 str = tmp + 2;
1107 (*fieldp)->ref = true;
1108 } else {
1109 semantic_error("Argument parse error: %s\n",
1110 str);
1111 return -EINVAL;
1112 }
1113 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001114 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001115 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001116 (*fieldp)->name = strndup(str, tmp - str);
1117 if ((*fieldp)->name == NULL)
1118 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001119 if (*str != '[')
1120 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001121 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1122 fieldp = &(*fieldp)->next;
1123 }
1124 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001125 (*fieldp)->name = strdup(str);
1126 if ((*fieldp)->name == NULL)
1127 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001128 if (*str != '[')
1129 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001130 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001131
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001132 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001133 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001134 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001135 if (arg->name == NULL)
1136 return -ENOMEM;
1137 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001138 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001139}
1140
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001141/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001142int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001143{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001144 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001145 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001146
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001147 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001148 if (!argv) {
1149 pr_debug("Failed to split arguments.\n");
1150 return -ENOMEM;
1151 }
1152 if (argc - 1 > MAX_PROBE_ARGS) {
1153 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1154 ret = -ERANGE;
1155 goto out;
1156 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001158 ret = parse_perf_probe_point(argv[0], pev);
1159 if (ret < 0)
1160 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001161
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001162 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001163 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001164 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1165 if (pev->args == NULL) {
1166 ret = -ENOMEM;
1167 goto out;
1168 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001169 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1170 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1171 if (ret >= 0 &&
1172 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001173 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174 " kretprobe.\n");
1175 ret = -EINVAL;
1176 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001177 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001178out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001179 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001180
1181 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001182}
1183
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184/* Return true if this perf_probe_event requires debuginfo */
1185bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001187 int i;
1188
1189 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1190 return true;
1191
1192 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001193 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001194 return true;
1195
1196 return false;
1197}
1198
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301199/* Parse probe_events event into struct probe_point */
1200static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001201 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001202{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301203 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001204 char pr;
1205 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001206 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001207 int ret, i, argc;
1208 char **argv;
1209
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301210 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001211 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212 if (!argv) {
1213 pr_debug("Failed to split arguments.\n");
1214 return -ENOMEM;
1215 }
1216 if (argc < 2) {
1217 semantic_error("Too few probe arguments.\n");
1218 ret = -ERANGE;
1219 goto out;
1220 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001221
1222 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001223 argv0_str = strdup(argv[0]);
1224 if (argv0_str == NULL) {
1225 ret = -ENOMEM;
1226 goto out;
1227 }
1228 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1229 fmt2_str = strtok_r(NULL, "/", &fmt);
1230 fmt3_str = strtok_r(NULL, " \t", &fmt);
1231 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1232 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001233 semantic_error("Failed to parse event name: %s\n", argv[0]);
1234 ret = -EINVAL;
1235 goto out;
1236 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001237 pr = fmt1_str[0];
1238 tev->group = strdup(fmt2_str);
1239 tev->event = strdup(fmt3_str);
1240 if (tev->group == NULL || tev->event == NULL) {
1241 ret = -ENOMEM;
1242 goto out;
1243 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001245
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001246 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001247
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001248 /* Scan module name(if there), function name and offset */
1249 p = strchr(argv[1], ':');
1250 if (p) {
1251 tp->module = strndup(argv[1], p - argv[1]);
1252 p++;
1253 } else
1254 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001255 fmt1_str = strtok_r(p, "+", &fmt);
1256 tp->symbol = strdup(fmt1_str);
1257 if (tp->symbol == NULL) {
1258 ret = -ENOMEM;
1259 goto out;
1260 }
1261 fmt2_str = strtok_r(NULL, "", &fmt);
1262 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001263 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001264 else
1265 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301268 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001269 if (tev->args == NULL) {
1270 ret = -ENOMEM;
1271 goto out;
1272 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001273 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001274 p = strchr(argv[i + 2], '=');
1275 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001276 *p++ = '\0';
1277 else
1278 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001279 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001280 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001281 tev->args[i].value = strdup(p);
1282 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1283 ret = -ENOMEM;
1284 goto out;
1285 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001286 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 ret = 0;
1288out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001289 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001290 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001292}
1293
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001294/* Compose only probe arg */
1295int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1296{
1297 struct perf_probe_arg_field *field = pa->field;
1298 int ret;
1299 char *tmp = buf;
1300
Masami Hiramatsu48481932010-04-12 13:16:53 -04001301 if (pa->name && pa->var)
1302 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1303 else
1304 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001305 if (ret <= 0)
1306 goto error;
1307 tmp += ret;
1308 len -= ret;
1309
1310 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001311 if (field->name[0] == '[')
1312 ret = e_snprintf(tmp, len, "%s", field->name);
1313 else
1314 ret = e_snprintf(tmp, len, "%s%s",
1315 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001316 if (ret <= 0)
1317 goto error;
1318 tmp += ret;
1319 len -= ret;
1320 field = field->next;
1321 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001322
1323 if (pa->type) {
1324 ret = e_snprintf(tmp, len, ":%s", pa->type);
1325 if (ret <= 0)
1326 goto error;
1327 tmp += ret;
1328 len -= ret;
1329 }
1330
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001331 return tmp - buf;
1332error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001333 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001334 strerror(-ret));
1335 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001336}
1337
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001338/* Compose only probe point (not argument) */
1339static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001340{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001341 char *buf, *tmp;
1342 char offs[32] = "", line[32] = "", file[32] = "";
1343 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001344
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001345 buf = zalloc(MAX_CMDLEN);
1346 if (buf == NULL) {
1347 ret = -ENOMEM;
1348 goto error;
1349 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001350 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001351 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001352 if (ret <= 0)
1353 goto error;
1354 }
1355 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001356 ret = e_snprintf(line, 32, ":%d", pp->line);
1357 if (ret <= 0)
1358 goto error;
1359 }
1360 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001361 tmp = pp->file;
1362 len = strlen(tmp);
1363 if (len > 30) {
1364 tmp = strchr(pp->file + len - 30, '/');
1365 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1366 }
1367 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001368 if (ret <= 0)
1369 goto error;
1370 }
1371
1372 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001373 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1374 offs, pp->retprobe ? "%return" : "", line,
1375 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001377 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001378 if (ret <= 0)
1379 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001380
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001381 return buf;
1382error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001383 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001385 if (buf)
1386 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001388}
1389
1390#if 0
1391char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1392{
1393 char *buf;
1394 int i, len, ret;
1395
1396 buf = synthesize_perf_probe_point(&pev->point);
1397 if (!buf)
1398 return NULL;
1399
1400 len = strlen(buf);
1401 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001402 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001403 pev->args[i].name);
1404 if (ret <= 0) {
1405 free(buf);
1406 return NULL;
1407 }
1408 len += ret;
1409 }
1410
1411 return buf;
1412}
1413#endif
1414
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301415static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001416 char **buf, size_t *buflen,
1417 int depth)
1418{
1419 int ret;
1420 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301421 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001422 buflen, depth + 1);
1423 if (depth < 0)
1424 goto out;
1425 }
1426
1427 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1428 if (ret < 0)
1429 depth = ret;
1430 else {
1431 *buf += ret;
1432 *buflen -= ret;
1433 }
1434out:
1435 return depth;
1436
1437}
1438
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301439static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001440 char *buf, size_t buflen)
1441{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301442 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001443 int ret, depth = 0;
1444 char *tmp = buf;
1445
1446 /* Argument name or separator */
1447 if (arg->name)
1448 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1449 else
1450 ret = e_snprintf(buf, buflen, " ");
1451 if (ret < 0)
1452 return ret;
1453 buf += ret;
1454 buflen -= ret;
1455
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001456 /* Special case: @XXX */
1457 if (arg->value[0] == '@' && arg->ref)
1458 ref = ref->next;
1459
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001460 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001461 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301462 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001463 &buflen, 1);
1464 if (depth < 0)
1465 return depth;
1466 }
1467
1468 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001469 if (arg->value[0] == '@' && arg->ref)
1470 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1471 arg->ref->offset);
1472 else
1473 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001474 if (ret < 0)
1475 return ret;
1476 buf += ret;
1477 buflen -= ret;
1478
1479 /* Closing */
1480 while (depth--) {
1481 ret = e_snprintf(buf, buflen, ")");
1482 if (ret < 0)
1483 return ret;
1484 buf += ret;
1485 buflen -= ret;
1486 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001487 /* Print argument type */
1488 if (arg->type) {
1489 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1490 if (ret <= 0)
1491 return ret;
1492 buf += ret;
1493 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494
1495 return buf - tmp;
1496}
1497
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301498char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001499{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301500 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001501 char *buf;
1502 int i, len, ret;
1503
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001504 buf = zalloc(MAX_CMDLEN);
1505 if (buf == NULL)
1506 return NULL;
1507
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301508 if (tev->uprobes)
1509 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1510 tp->retprobe ? 'r' : 'p',
1511 tev->group, tev->event,
1512 tp->module, tp->symbol);
1513 else
1514 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1515 tp->retprobe ? 'r' : 'p',
1516 tev->group, tev->event,
1517 tp->module ?: "", tp->module ? ":" : "",
1518 tp->symbol, tp->offset);
1519
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 if (len <= 0)
1521 goto error;
1522
1523 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301524 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001525 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001526 if (ret <= 0)
1527 goto error;
1528 len += ret;
1529 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001530
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001531 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001532error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001533 free(buf);
1534 return NULL;
1535}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001536
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301537static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301538 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001539{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001540 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001542
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001543 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001544 pev->event = strdup(tev->event);
1545 pev->group = strdup(tev->group);
1546 if (pev->event == NULL || pev->group == NULL)
1547 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001548
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001549 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301550 if (is_kprobe)
1551 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1552 else
1553 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1554
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001555 if (ret < 0)
1556 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001557
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001558 /* Convert trace_arg to probe_arg */
1559 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001560 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1561 if (pev->args == NULL)
1562 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001563 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001564 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001565 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001566 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301567 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001568 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001569 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001570 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001571 if (pev->args[i].name == NULL && ret >= 0)
1572 ret = -ENOMEM;
1573 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574
1575 if (ret < 0)
1576 clear_perf_probe_event(pev);
1577
1578 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001579}
1580
1581void clear_perf_probe_event(struct perf_probe_event *pev)
1582{
1583 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001584 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001585 int i;
1586
1587 if (pev->event)
1588 free(pev->event);
1589 if (pev->group)
1590 free(pev->group);
1591 if (pp->file)
1592 free(pp->file);
1593 if (pp->function)
1594 free(pp->function);
1595 if (pp->lazy_line)
1596 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001597 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 if (pev->args[i].name)
1599 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001600 if (pev->args[i].var)
1601 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001602 if (pev->args[i].type)
1603 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001604 field = pev->args[i].field;
1605 while (field) {
1606 next = field->next;
1607 if (field->name)
1608 free(field->name);
1609 free(field);
1610 field = next;
1611 }
1612 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 if (pev->args)
1614 free(pev->args);
1615 memset(pev, 0, sizeof(*pev));
1616}
1617
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301618static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001619{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301620 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621 int i;
1622
1623 if (tev->event)
1624 free(tev->event);
1625 if (tev->group)
1626 free(tev->group);
1627 if (tev->point.symbol)
1628 free(tev->point.symbol);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001629 if (tev->point.module)
1630 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001631 for (i = 0; i < tev->nargs; i++) {
1632 if (tev->args[i].name)
1633 free(tev->args[i].name);
1634 if (tev->args[i].value)
1635 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001636 if (tev->args[i].type)
1637 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 ref = tev->args[i].ref;
1639 while (ref) {
1640 next = ref->next;
1641 free(ref);
1642 ref = next;
1643 }
1644 }
1645 if (tev->args)
1646 free(tev->args);
1647 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001648}
1649
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301650static void print_warn_msg(const char *file, bool is_kprobe)
1651{
1652
1653 if (errno == ENOENT) {
1654 const char *config;
1655
1656 if (!is_kprobe)
1657 config = "CONFIG_UPROBE_EVENTS";
1658 else
1659 config = "CONFIG_KPROBE_EVENTS";
1660
1661 pr_warning("%s file does not exist - please rebuild kernel"
1662 " with %s.\n", file, config);
1663 } else
1664 pr_warning("Failed to open %s file: %s\n", file,
1665 strerror(errno));
1666}
1667
1668static int open_probe_events(const char *trace_file, bool readwrite,
1669 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001670{
1671 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001672 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001673 int ret;
1674
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001675 __debugfs = debugfs_find_mountpoint();
1676 if (__debugfs == NULL) {
1677 pr_warning("Debugfs is not mounted.\n");
1678 return -ENOENT;
1679 }
1680
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301681 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001682 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001683 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001684 if (readwrite && !probe_event_dry_run)
1685 ret = open(buf, O_RDWR, O_APPEND);
1686 else
1687 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001688
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301689 if (ret < 0)
1690 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001691 }
1692 return ret;
1693}
1694
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301695static int open_kprobe_events(bool readwrite)
1696{
1697 return open_probe_events("tracing/kprobe_events", readwrite, true);
1698}
1699
1700static int open_uprobe_events(bool readwrite)
1701{
1702 return open_probe_events("tracing/uprobe_events", readwrite, false);
1703}
1704
1705/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301706static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001707{
1708 int ret, idx;
1709 FILE *fp;
1710 char buf[MAX_CMDLEN];
1711 char *p;
1712 struct strlist *sl;
1713
1714 sl = strlist__new(true, NULL);
1715
1716 fp = fdopen(dup(fd), "r");
1717 while (!feof(fp)) {
1718 p = fgets(buf, MAX_CMDLEN, fp);
1719 if (!p)
1720 break;
1721
1722 idx = strlen(p) - 1;
1723 if (p[idx] == '\n')
1724 p[idx] = '\0';
1725 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001726 if (ret < 0) {
1727 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1728 strlist__delete(sl);
1729 return NULL;
1730 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001731 }
1732 fclose(fp);
1733
1734 return sl;
1735}
1736
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001737/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001738static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001739{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001740 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001741 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001742 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001743
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744 /* Synthesize only event probe point */
1745 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001746 if (!place)
1747 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748
1749 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001750 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001751 return ret;
1752
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001753 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001754
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001755 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001756 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001757 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001758 ret = synthesize_perf_probe_arg(&pev->args[i],
1759 buf, 128);
1760 if (ret < 0)
1761 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001762 printf(" %s", buf);
1763 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001764 }
1765 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001767 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001768}
1769
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301770static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301772 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301773 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001775 struct strlist *rawlist;
1776 struct str_node *ent;
1777
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778 memset(&tev, 0, sizeof(tev));
1779 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001780
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301781 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001782 if (!rawlist)
1783 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001784
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001785 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301786 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001787 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301788 ret = convert_to_perf_probe_event(&tev, &pev,
1789 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001790 if (ret >= 0)
1791 ret = show_perf_probe_event(&pev);
1792 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001793 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301794 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001795 if (ret < 0)
1796 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001797 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001798 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001799
1800 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001801}
1802
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301803/* List up current perf-probe events */
1804int show_perf_probe_events(void)
1805{
1806 int fd, ret;
1807
1808 setup_pager();
1809 fd = open_kprobe_events(false);
1810
1811 if (fd < 0)
1812 return fd;
1813
1814 ret = init_vmlinux();
1815 if (ret < 0)
1816 return ret;
1817
1818 ret = __show_perf_probe_events(fd, true);
1819 close(fd);
1820
1821 fd = open_uprobe_events(false);
1822 if (fd >= 0) {
1823 ret = __show_perf_probe_events(fd, false);
1824 close(fd);
1825 }
1826
1827 return ret;
1828}
1829
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001830/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301831static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001832{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001833 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001834 struct strlist *sl, *rawlist;
1835 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301836 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001837 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001838
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001839 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301840 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001841 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001842 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301843 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001844 if (ret < 0)
1845 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001846 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001847 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1848 tev.event);
1849 if (ret >= 0)
1850 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001851 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001852 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301853 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001854 if (ret < 0)
1855 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001856 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001857 strlist__delete(rawlist);
1858
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001859 if (ret < 0) {
1860 strlist__delete(sl);
1861 return NULL;
1862 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001863 return sl;
1864}
1865
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301866static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001867{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001868 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301869 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001870
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001871 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301872 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001873 return -EINVAL;
1874 }
1875
Masami Hiramatsufa282442009-12-08 17:03:23 -05001876 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001877 if (!probe_event_dry_run) {
1878 ret = write(fd, buf, strlen(buf));
1879 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001880 pr_warning("Failed to write event: %s\n",
1881 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001882 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001884 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001885}
1886
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001887static int get_new_event_name(char *buf, size_t len, const char *base,
1888 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001889{
1890 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001891
1892 /* Try no suffix */
1893 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001894 if (ret < 0) {
1895 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1896 return ret;
1897 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001898 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001899 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001900
Masami Hiramatsud761b082009-12-15 10:32:25 -05001901 if (!allow_suffix) {
1902 pr_warning("Error: event \"%s\" already exists. "
1903 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001904 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001905 }
1906
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001907 /* Try to add suffix */
1908 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001909 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001910 if (ret < 0) {
1911 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1912 return ret;
1913 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001914 if (!strlist__has_entry(namelist, buf))
1915 break;
1916 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001917 if (i == MAX_EVENT_INDEX) {
1918 pr_warning("Too many events are on the same function.\n");
1919 ret = -ERANGE;
1920 }
1921
1922 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001923}
1924
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301925static int __add_probe_trace_events(struct perf_probe_event *pev,
1926 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001927 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001928{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001929 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301930 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001931 char buf[64];
1932 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001933 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001934
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301935 if (pev->uprobes)
1936 fd = open_uprobe_events(true);
1937 else
1938 fd = open_kprobe_events(true);
1939
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001940 if (fd < 0)
1941 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001942 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301943 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001944 if (!namelist) {
1945 pr_debug("Failed to get current event list.\n");
1946 return -EIO;
1947 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001948
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001949 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301950 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001951 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001952 tev = &tevs[i];
1953 if (pev->event)
1954 event = pev->event;
1955 else
1956 if (pev->point.function)
1957 event = pev->point.function;
1958 else
1959 event = tev->point.symbol;
1960 if (pev->group)
1961 group = pev->group;
1962 else
1963 group = PERFPROBE_GROUP;
1964
1965 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001966 ret = get_new_event_name(buf, 64, event,
1967 namelist, allow_suffix);
1968 if (ret < 0)
1969 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001970 event = buf;
1971
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001972 tev->event = strdup(event);
1973 tev->group = strdup(group);
1974 if (tev->event == NULL || tev->group == NULL) {
1975 ret = -ENOMEM;
1976 break;
1977 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301978 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001979 if (ret < 0)
1980 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001981 /* Add added event name to namelist */
1982 strlist__add(namelist, event);
1983
1984 /* Trick here - save current event/group */
1985 event = pev->event;
1986 group = pev->group;
1987 pev->event = tev->event;
1988 pev->group = tev->group;
1989 show_perf_probe_event(pev);
1990 /* Trick here - restore current event/group */
1991 pev->event = (char *)event;
1992 pev->group = (char *)group;
1993
1994 /*
1995 * Probes after the first probe which comes from same
1996 * user input are always allowed to add suffix, because
1997 * there might be several addresses corresponding to
1998 * one code line.
1999 */
2000 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002001 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002002
2003 if (ret >= 0) {
2004 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302005 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002006 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2007 tev->event);
2008 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002009
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002010 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002011 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002012 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002013}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002014
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302015static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2016 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302017 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002018{
2019 struct symbol *sym;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002020 int ret, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302021 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002022
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002023 if (pev->uprobes && !pev->group) {
2024 /* Replace group name if not given */
2025 ret = convert_exec_to_group(target, &pev->group);
2026 if (ret != 0) {
2027 pr_warning("Failed to make a group name.\n");
2028 return ret;
2029 }
2030 }
2031
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002032 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302033 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002034 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002035 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002036
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002037 if (pev->uprobes) {
2038 ret = convert_name_to_addr(pev, target);
2039 if (ret < 0)
2040 return ret;
2041 }
2042
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002043 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302044 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002045 if (tev == NULL)
2046 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002047
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002048 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002049 tev->point.symbol = strdup(pev->point.function);
2050 if (tev->point.symbol == NULL) {
2051 ret = -ENOMEM;
2052 goto error;
2053 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002054
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302055 if (target) {
2056 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08002057 if (tev->point.module == NULL) {
2058 ret = -ENOMEM;
2059 goto error;
2060 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002061 }
Jovi Zhangce27a442011-07-25 22:08:08 +08002062
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002063 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09002064 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002065 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302066 tev->uprobes = pev->uprobes;
2067
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002068 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302069 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002070 * tev->nargs);
2071 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002072 ret = -ENOMEM;
2073 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002074 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002075 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002076 if (pev->args[i].name) {
2077 tev->args[i].name = strdup(pev->args[i].name);
2078 if (tev->args[i].name == NULL) {
2079 ret = -ENOMEM;
2080 goto error;
2081 }
2082 }
2083 tev->args[i].value = strdup(pev->args[i].var);
2084 if (tev->args[i].value == NULL) {
2085 ret = -ENOMEM;
2086 goto error;
2087 }
2088 if (pev->args[i].type) {
2089 tev->args[i].type = strdup(pev->args[i].type);
2090 if (tev->args[i].type == NULL) {
2091 ret = -ENOMEM;
2092 goto error;
2093 }
2094 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04002095 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002096 }
2097
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302098 if (pev->uprobes)
2099 return 1;
2100
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002101 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002102 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002103 if (!sym) {
2104 pr_warning("Kernel symbol \'%s\' not found.\n",
2105 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002106 ret = -ENOENT;
2107 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05302108 } else if (tev->point.offset > sym->end - sym->start) {
2109 pr_warning("Offset specified is greater than size of %s\n",
2110 tev->point.symbol);
2111 ret = -ENOENT;
2112 goto error;
2113
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002114 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002115
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002116 return 1;
2117error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302118 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002119 free(tev);
2120 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002121 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002122}
2123
2124struct __event_package {
2125 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302126 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002127 int ntevs;
2128};
2129
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002130int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302131 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002132{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002133 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002134 struct __event_package *pkgs;
2135
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302136 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002137 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302138
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002139 if (pkgs == NULL)
2140 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002141
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302142 if (!pevs->uprobes)
2143 /* Init vmlinux path */
2144 ret = init_vmlinux();
2145 else
2146 ret = init_user_exec();
2147
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002148 if (ret < 0) {
2149 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002150 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002151 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002152
2153 /* Loop 1: convert all events */
2154 for (i = 0; i < npevs; i++) {
2155 pkgs[i].pev = &pevs[i];
2156 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302157 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002158 &pkgs[i].tevs,
2159 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302160 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002161 if (ret < 0)
2162 goto end;
2163 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002164 }
2165
2166 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002167 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302168 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002170 if (ret < 0)
2171 break;
2172 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002173end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002174 /* Loop 3: cleanup and free trace events */
2175 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002176 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302177 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002178 free(pkgs[i].tevs);
2179 }
2180 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002181
2182 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002183}
2184
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302185static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002186{
2187 char *p;
2188 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002190
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302191 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002192 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2193 if (ret < 0)
2194 goto error;
2195
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002196 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002197 if (!p) {
2198 pr_debug("Internal error: %s should have ':' but not.\n",
2199 ent->s);
2200 ret = -ENOTSUP;
2201 goto error;
2202 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002203 *p = '/';
2204
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002205 pr_debug("Writing event: %s\n", buf);
2206 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002207 if (ret < 0) {
2208 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002210 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002211
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302212 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002213 return 0;
2214error:
2215 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2216 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002217}
2218
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219static int del_trace_probe_event(int fd, const char *buf,
2220 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002221{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002222 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302223 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002224
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002225 if (strpbrk(buf, "*?")) { /* Glob-exp */
2226 strlist__for_each_safe(ent, n, namelist)
2227 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302228 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002229 if (ret < 0)
2230 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002231 strlist__remove(namelist, ent);
2232 }
2233 } else {
2234 ent = strlist__find(namelist, buf);
2235 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302236 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237 if (ret >= 0)
2238 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002239 }
2240 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002241
2242 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002243}
2244
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002245int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002246{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302247 int ret = -1, ufd = -1, kfd = -1;
2248 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002249 const char *group, *event;
2250 char *p, *str;
2251 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302252 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002253
Masami Hiramatsufa282442009-12-08 17:03:23 -05002254 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302255 kfd = open_kprobe_events(true);
2256 if (kfd < 0)
2257 return kfd;
2258
2259 namelist = get_probe_trace_event_names(kfd, true);
2260 ufd = open_uprobe_events(true);
2261
2262 if (ufd >= 0)
2263 unamelist = get_probe_trace_event_names(ufd, true);
2264
2265 if (namelist == NULL && unamelist == NULL)
2266 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002267
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002268 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002269 str = strdup(ent->s);
2270 if (str == NULL) {
2271 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302272 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002273 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002274 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002275 p = strchr(str, ':');
2276 if (p) {
2277 group = str;
2278 *p = '\0';
2279 event = p + 1;
2280 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002281 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002282 event = str;
2283 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302284
2285 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2286 if (ret < 0) {
2287 pr_err("Failed to copy event.");
2288 free(str);
2289 goto error;
2290 }
2291
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002292 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302293
2294 if (namelist)
2295 ret = del_trace_probe_event(kfd, buf, namelist);
2296
2297 if (unamelist && ret != 0)
2298 ret = del_trace_probe_event(ufd, buf, unamelist);
2299
2300 if (ret != 0)
2301 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2302
Masami Hiramatsufa282442009-12-08 17:03:23 -05002303 free(str);
2304 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302305
2306error:
2307 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302308 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302309 close(kfd);
2310 }
2311
2312 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302313 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302314 close(ufd);
2315 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002316
2317 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002318}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302319
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002320/* TODO: don't use a global variable for filter ... */
2321static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002322
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002323/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002324 * If a symbol corresponds to a function with global binding and
2325 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002326 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002327static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002328 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002329{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002330 if (sym->binding == STB_GLOBAL &&
2331 strfilter__compare(available_func_filter, sym->name))
2332 return 0;
2333 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002334}
2335
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302336static int __show_available_funcs(struct map *map)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002337{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002338 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002339 pr_err("Failed to load map.\n");
2340 return -EINVAL;
2341 }
2342 if (!dso__sorted_by_name(map->dso, map->type))
2343 dso__sort_by_name(map->dso, map->type);
2344
2345 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2346 return 0;
2347}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302348
2349static int available_kernel_funcs(const char *module)
2350{
2351 struct map *map;
2352 int ret;
2353
2354 ret = init_vmlinux();
2355 if (ret < 0)
2356 return ret;
2357
2358 map = kernel_get_module_map(module);
2359 if (!map) {
2360 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2361 return -EINVAL;
2362 }
2363 return __show_available_funcs(map);
2364}
2365
2366static int available_user_funcs(const char *target)
2367{
2368 struct map *map;
2369 int ret;
2370
2371 ret = init_user_exec();
2372 if (ret < 0)
2373 return ret;
2374
2375 map = dso__new_map(target);
2376 ret = __show_available_funcs(map);
2377 dso__delete(map->dso);
2378 map__delete(map);
2379 return ret;
2380}
2381
2382int show_available_funcs(const char *target, struct strfilter *_filter,
2383 bool user)
2384{
2385 setup_pager();
2386 available_func_filter = _filter;
2387
2388 if (!user)
2389 return available_kernel_funcs(target);
2390
2391 return available_user_funcs(target);
2392}
2393
2394/*
2395 * uprobe_events only accepts address:
2396 * Convert function and any offset to address
2397 */
2398static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2399{
2400 struct perf_probe_point *pp = &pev->point;
2401 struct symbol *sym;
2402 struct map *map = NULL;
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002403 char *function = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302404 int ret = -EINVAL;
2405 unsigned long long vaddr = 0;
2406
2407 if (!pp->function) {
2408 pr_warning("No function specified for uprobes");
2409 goto out;
2410 }
2411
2412 function = strdup(pp->function);
2413 if (!function) {
2414 pr_warning("Failed to allocate memory by strdup.\n");
2415 ret = -ENOMEM;
2416 goto out;
2417 }
2418
Masami Hiramatsu8a613d42013-12-26 05:41:50 +00002419 map = dso__new_map(exec);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302420 if (!map) {
2421 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2422 goto out;
2423 }
2424 available_func_filter = strfilter__new(function, NULL);
2425 if (map__load(map, filter_available_functions)) {
2426 pr_err("Failed to load map.\n");
2427 goto out;
2428 }
2429
2430 sym = map__find_symbol_by_name(map, function, NULL);
2431 if (!sym) {
2432 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2433 goto out;
2434 }
2435
2436 if (map->start > sym->start)
2437 vaddr = map->start;
2438 vaddr += sym->start + pp->offset + map->pgoff;
2439 pp->offset = 0;
2440
2441 if (!pev->event) {
2442 pev->event = function;
2443 function = NULL;
2444 }
2445 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002446 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302447
2448 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002449 exec_copy = strdup(exec);
2450 if (!exec_copy) {
2451 ret = -ENOMEM;
2452 pr_warning("Failed to copy exec string.\n");
2453 goto out;
2454 }
2455
2456 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302457 if (ptr1) {
2458 ptr2 = strpbrk(ptr1, "-._");
2459 if (ptr2)
2460 *ptr2 = '\0';
2461 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2462 ptr1);
2463 free(ptr1);
2464 }
David Ahern1fb89442012-09-08 09:06:51 -06002465 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302466 }
2467 free(pp->function);
2468 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2469 if (!pp->function) {
2470 ret = -ENOMEM;
2471 pr_warning("Failed to allocate memory by zalloc.\n");
2472 goto out;
2473 }
2474 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2475 ret = 0;
2476
2477out:
2478 if (map) {
2479 dso__delete(map->dso);
2480 map__delete(map);
2481 }
2482 if (function)
2483 free(function);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302484 return ret;
2485}