blob: 9c6989ca2bea0242edcfed3f75803dd4e923068d [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 Petkov85c66be2013-02-20 16:32:30 +010043#include <lk/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) {
157 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
158 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
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530175static int init_user_exec(void)
176{
177 int ret = 0;
178
179 symbol_conf.try_vmlinux_path = false;
180 symbol_conf.sort_by_name = true;
181 ret = symbol__init();
182
183 if (ret < 0)
184 pr_debug("Failed to init symbol map.\n");
185
186 return ret;
187}
188
189static int convert_to_perf_probe_point(struct probe_trace_point *tp,
190 struct perf_probe_point *pp)
191{
192 pp->function = strdup(tp->symbol);
193
194 if (pp->function == NULL)
195 return -ENOMEM;
196
197 pp->offset = tp->offset;
198 pp->retprobe = tp->retprobe;
199
200 return 0;
201}
202
Ingo Molnar89fe8082013-09-30 12:07:11 +0200203#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900204/* Open new debuginfo of given module */
205static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900206{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900207 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900208
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900209 /* A file path -- this is an offline module */
210 if (module && strchr(module, '/'))
211 path = module;
212 else {
213 path = kernel_get_module_path(module);
214
215 if (!path) {
216 pr_err("Failed to find path of %s module.\n",
217 module ?: "kernel");
218 return NULL;
219 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900220 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900221 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400222}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300223
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530224/*
225 * Convert trace point to probe point with debuginfo
226 * Currently only handles kprobes.
227 */
228static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900229 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300230{
231 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900232 struct map *map;
233 u64 addr;
234 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900235 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300236
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900237 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300238 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900239 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200240 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900241 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900242
243 dinfo = debuginfo__new_online_kernel(addr);
244 if (dinfo) {
245 ret = debuginfo__find_probe_point(dinfo,
246 (unsigned long)addr, pp);
247 debuginfo__delete(dinfo);
248 } else {
249 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
250 addr);
251 ret = -ENOENT;
252 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300253 }
254 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400255 pr_debug("Failed to find corresponding probes from "
256 "debuginfo. Use kprobe event information.\n");
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530257 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300258 }
259 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400260
261 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300262}
263
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900264static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
265 int ntevs, const char *module)
266{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900267 int i, ret = 0;
268 char *tmp;
269
270 if (!module)
271 return 0;
272
273 tmp = strrchr(module, '/');
274 if (tmp) {
275 /* This is a module path -- get the module name */
276 module = strdup(tmp + 1);
277 if (!module)
278 return -ENOMEM;
279 tmp = strchr(module, '.');
280 if (tmp)
281 *tmp = '\0';
282 tmp = (char *)module; /* For free() */
283 }
284
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900285 for (i = 0; i < ntevs; i++) {
286 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900287 if (!tevs[i].point.module) {
288 ret = -ENOMEM;
289 break;
290 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900291 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900292
293 if (tmp)
294 free(tmp);
295
296 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900297}
298
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300299/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530300static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900301 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530302 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300303{
304 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530305 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900306 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300307
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530308 if (pev->uprobes) {
309 if (need_dwarf) {
310 pr_warning("Debuginfo-analysis is not yet supported"
311 " with -x/--exec option.\n");
312 return -ENOSYS;
313 }
314 return convert_name_to_addr(pev, target);
315 }
316
317 dinfo = open_debuginfo(target);
318
Masami Hiramatsuff741782011-06-27 16:27:39 +0900319 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400320 if (need_dwarf) {
321 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900322 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400323 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900324 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300325 return 0;
326 }
327
Masami Hiramatsuff741782011-06-27 16:27:39 +0900328 /* Searching trace events corresponding to a probe event */
329 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
330
331 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300332
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400333 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530334 pr_debug("find %d probe_trace_events.\n", ntevs);
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530335 if (target)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900336 ret = add_module_to_probe_trace_events(*tevs, ntevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530337 target);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900338 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400339 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300340
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400341 if (ntevs == 0) { /* No error but failed to find probe point. */
342 pr_warning("Probe point '%s' not found.\n",
343 synthesize_perf_probe_point(&pev->point));
344 return -ENOENT;
345 }
346 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400347 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
348 if (ntevs == -EBADF) {
349 pr_warning("Warning: No dwarf info found in the vmlinux - "
350 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
351 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900352 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400353 return 0;
354 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300355 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400356 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300357}
358
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900359/*
360 * Find a src file from a DWARF tag path. Prepend optional source path prefix
361 * and chop off leading directories that do not exist. Result is passed back as
362 * a newly allocated path on success.
363 * Return 0 if file was found and readable, -errno otherwise.
364 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900365static int get_real_path(const char *raw_path, const char *comp_dir,
366 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900367{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900368 const char *prefix = symbol_conf.source_prefix;
369
370 if (!prefix) {
371 if (raw_path[0] != '/' && comp_dir)
372 /* If not an absolute path, try to use comp_dir */
373 prefix = comp_dir;
374 else {
375 if (access(raw_path, R_OK) == 0) {
376 *new_path = strdup(raw_path);
377 return 0;
378 } else
379 return -errno;
380 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900381 }
382
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900383 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900384 if (!*new_path)
385 return -ENOMEM;
386
387 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900388 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900389
390 if (access(*new_path, R_OK) == 0)
391 return 0;
392
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900393 if (!symbol_conf.source_prefix)
394 /* In case of searching comp_dir, don't retry */
395 return -errno;
396
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900397 switch (errno) {
398 case ENAMETOOLONG:
399 case ENOENT:
400 case EROFS:
401 case EFAULT:
402 raw_path = strchr(++raw_path, '/');
403 if (!raw_path) {
404 free(*new_path);
405 *new_path = NULL;
406 return -ENOENT;
407 }
408 continue;
409
410 default:
411 free(*new_path);
412 *new_path = NULL;
413 return -errno;
414 }
415 }
416}
417
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300418#define LINEBUF_SIZE 256
419#define NR_ADDITIONAL_LINES 2
420
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100421static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300422{
423 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100424 const char *color = show_num ? "" : PERF_COLOR_BLUE;
425 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300426
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100427 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300428 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
429 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100430 if (skip)
431 continue;
432 if (!prefix) {
433 prefix = show_num ? "%7d " : " ";
434 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300435 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100436 color_fprintf(stdout, color, "%s", buf);
437
438 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400439
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100440 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300441error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100442 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100443 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100444 return -1;
445 }
446 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300447}
448
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100449static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
450{
451 int rv = __show_one_line(fp, l, skip, show_num);
452 if (rv == 0) {
453 pr_warning("Source file is shorter than expected.\n");
454 rv = -1;
455 }
456 return rv;
457}
458
459#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
460#define show_one_line(f,l) _show_one_line(f,l,false,false)
461#define skip_one_line(f,l) _show_one_line(f,l,true,false)
462#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
463
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300464/*
465 * Show line-range always requires debuginfo to find source file and
466 * line number.
467 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900468int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300469{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400470 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300471 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900472 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300473 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900474 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900475 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300476
477 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400478 ret = init_vmlinux();
479 if (ret < 0)
480 return ret;
481
Masami Hiramatsuff741782011-06-27 16:27:39 +0900482 dinfo = open_debuginfo(module);
483 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400484 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900485 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400486 }
487
Masami Hiramatsuff741782011-06-27 16:27:39 +0900488 ret = debuginfo__find_line_range(dinfo, lr);
489 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400490 if (ret == 0) {
491 pr_warning("Specified source line is not found.\n");
492 return -ENOENT;
493 } else if (ret < 0) {
494 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
495 return ret;
496 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300497
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900498 /* Convert source file path */
499 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900500 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900501 free(tmp); /* Free old path */
502 if (ret < 0) {
503 pr_warning("Failed to find source file. (%d)\n", ret);
504 return ret;
505 }
506
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300507 setup_pager();
508
509 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900510 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300511 lr->start - lr->offset);
512 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100513 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300514
515 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400516 if (fp == NULL) {
517 pr_warning("Failed to open %s: %s\n", lr->path,
518 strerror(errno));
519 return -errno;
520 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300521 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100522 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100523 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100524 if (ret < 0)
525 goto end;
526 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300527
528 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100529 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100530 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100531 if (ret < 0)
532 goto end;
533 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100534 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400535 if (ret < 0)
536 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300537 }
538
539 if (lr->end == INT_MAX)
540 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100541 while (l <= lr->end) {
542 ret = show_one_line_or_eof(fp, l++ - lr->offset);
543 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100544 break;
545 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400546end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300547 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300549}
550
Masami Hiramatsuff741782011-06-27 16:27:39 +0900551static int show_available_vars_at(struct debuginfo *dinfo,
552 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900553 int max_vls, struct strfilter *_filter,
554 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900555{
556 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900557 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900558 struct str_node *node;
559 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900560 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900561
562 buf = synthesize_perf_probe_point(&pev->point);
563 if (!buf)
564 return -EINVAL;
565 pr_debug("Searching variables at %s\n", buf);
566
Masami Hiramatsuff741782011-06-27 16:27:39 +0900567 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
568 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900569 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900570 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900571 goto end;
572 }
573 /* Some variables are found */
574 fprintf(stdout, "Available variables at %s\n", buf);
575 for (i = 0; i < ret; i++) {
576 vl = &vls[i];
577 /*
578 * A probe point might be converted to
579 * several trace points.
580 */
581 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
582 vl->point.offset);
583 free(vl->point.symbol);
584 nvars = 0;
585 if (vl->vars) {
586 strlist__for_each(node, vl->vars) {
587 var = strchr(node->s, '\t') + 1;
588 if (strfilter__compare(_filter, var)) {
589 fprintf(stdout, "\t\t%s\n", node->s);
590 nvars++;
591 }
592 }
593 strlist__delete(vl->vars);
594 }
595 if (nvars == 0)
596 fprintf(stdout, "\t\t(No matched variables)\n");
597 }
598 free(vls);
599end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900600 free(buf);
601 return ret;
602}
603
604/* Show available variables on given probe point */
605int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900606 int max_vls, const char *module,
607 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900608{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900609 int i, ret = 0;
610 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900611
612 ret = init_vmlinux();
613 if (ret < 0)
614 return ret;
615
Masami Hiramatsuff741782011-06-27 16:27:39 +0900616 dinfo = open_debuginfo(module);
617 if (!dinfo) {
618 pr_warning("Failed to open debuginfo file.\n");
619 return -ENOENT;
620 }
621
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900622 setup_pager();
623
Masami Hiramatsuff741782011-06-27 16:27:39 +0900624 for (i = 0; i < npevs && ret >= 0; i++)
625 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900626 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900627
628 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900629 return ret;
630}
631
Ingo Molnar89fe8082013-09-30 12:07:11 +0200632#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300633
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530634static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900635 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300636{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900637 struct symbol *sym;
638
639 sym = __find_kernel_function_by_name(tp->symbol, NULL);
640 if (!sym) {
641 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
642 return -ENOENT;
643 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400644
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530645 return convert_to_perf_probe_point(tp, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300646}
647
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530648static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300649 struct probe_trace_event **tevs __maybe_unused,
650 int max_tevs __maybe_unused, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300651{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400652 if (perf_probe_event_need_dwarf(pev)) {
653 pr_warning("Debuginfo-analysis is not supported.\n");
654 return -ENOSYS;
655 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530656
657 if (pev->uprobes)
658 return convert_name_to_addr(pev, target);
659
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300660 return 0;
661}
662
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300663int show_line_range(struct line_range *lr __maybe_unused,
664 const char *module __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300665{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400666 pr_warning("Debuginfo-analysis is not supported.\n");
667 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300668}
669
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300670int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
671 int npevs __maybe_unused, int max_vls __maybe_unused,
672 const char *module __maybe_unused,
673 struct strfilter *filter __maybe_unused,
674 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900675{
676 pr_warning("Debuginfo-analysis is not supported.\n");
677 return -ENOSYS;
678}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400679#endif
680
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100681static int parse_line_num(char **ptr, int *val, const char *what)
682{
683 const char *start = *ptr;
684
685 errno = 0;
686 *val = strtol(*ptr, ptr, 0);
687 if (errno || *ptr == start) {
688 semantic_error("'%s' is not a valid number.\n", what);
689 return -EINVAL;
690 }
691 return 0;
692}
693
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100694/*
695 * Stuff 'lr' according to the line range described by 'arg'.
696 * The line range syntax is described by:
697 *
698 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900699 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100700 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400701int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500702{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900703 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100704 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100705
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100706 if (!name)
707 return -ENOMEM;
708
709 lr->start = 0;
710 lr->end = INT_MAX;
711
712 range = strchr(name, ':');
713 if (range) {
714 *range++ = '\0';
715
716 err = parse_line_num(&range, &lr->start, "start line");
717 if (err)
718 goto err;
719
720 if (*range == '+' || *range == '-') {
721 const char c = *range++;
722
723 err = parse_line_num(&range, &lr->end, "end line");
724 if (err)
725 goto err;
726
727 if (c == '+') {
728 lr->end += lr->start;
729 /*
730 * Adjust the number of lines here.
731 * If the number of lines == 1, the
732 * the end of line should be equal to
733 * the start of line.
734 */
735 lr->end--;
736 }
737 }
738
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400739 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100740
741 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400742 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500743 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400744 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100745 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400746 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100747 if (*range != '\0') {
748 semantic_error("Tailing with invalid str '%s'.\n", range);
749 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400750 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400751 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400752
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900753 file = strchr(name, '@');
754 if (file) {
755 *file = '\0';
756 lr->file = strdup(++file);
757 if (lr->file == NULL) {
758 err = -ENOMEM;
759 goto err;
760 }
761 lr->function = name;
762 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100763 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500764 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100765 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766
767 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100768err:
769 free(name);
770 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500771}
772
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500773/* Check the name is good for event/group */
774static bool check_event_name(const char *name)
775{
776 if (!isalpha(*name) && *name != '_')
777 return false;
778 while (*++name != '\0') {
779 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
780 return false;
781 }
782 return true;
783}
784
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500785/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400786static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500787{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400788 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500789 char *ptr, *tmp;
790 char c, nc = 0;
791 /*
792 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500793 * perf probe [EVENT=]SRC[:LN|;PTN]
794 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500795 *
796 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500797 */
798
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500799 ptr = strpbrk(arg, ";=@+%");
800 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500801 *ptr = '\0';
802 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400803 if (strchr(arg, ':')) {
804 semantic_error("Group name is not supported yet.\n");
805 return -ENOTSUP;
806 }
807 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500808 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400809 "follow C symbol-naming rule.\n", arg);
810 return -EINVAL;
811 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400812 pev->event = strdup(arg);
813 if (pev->event == NULL)
814 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400815 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500816 arg = tmp;
817 }
818
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500819 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500820 if (ptr) {
821 nc = *ptr;
822 *ptr++ = '\0';
823 }
824
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400825 tmp = strdup(arg);
826 if (tmp == NULL)
827 return -ENOMEM;
828
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500829 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400830 if (strchr(tmp, '.')) /* File */
831 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500832 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400833 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500834
835 /* Parse other options */
836 while (ptr) {
837 arg = ptr;
838 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500839 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400840 pp->lazy_line = strdup(arg);
841 if (pp->lazy_line == NULL)
842 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500843 break;
844 }
845 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500846 if (ptr) {
847 nc = *ptr;
848 *ptr++ = '\0';
849 }
850 switch (c) {
851 case ':': /* Line number */
852 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400853 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500854 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855 " in line number.\n");
856 return -EINVAL;
857 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500858 break;
859 case '+': /* Byte offset from a symbol */
860 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500862 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400863 " in offset.\n");
864 return -EINVAL;
865 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500866 break;
867 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400868 if (pp->file) {
869 semantic_error("SRC@SRC is not allowed.\n");
870 return -EINVAL;
871 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400872 pp->file = strdup(arg);
873 if (pp->file == NULL)
874 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500875 break;
876 case '%': /* Probe places */
877 if (strcmp(arg, "return") == 0) {
878 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400879 } else { /* Others not supported yet */
880 semantic_error("%%%s is not supported.\n", arg);
881 return -ENOTSUP;
882 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500883 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884 default: /* Buggy case */
885 pr_err("This program has a bug at %s:%d.\n",
886 __FILE__, __LINE__);
887 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500888 break;
889 }
890 }
891
892 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400893 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900894 semantic_error("Lazy pattern can't be used with"
895 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400896 return -EINVAL;
897 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500898
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400899 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900900 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400901 return -EINVAL;
902 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500903
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400904 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900905 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400906 return -EINVAL;
907 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500908
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400909 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500910 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900911 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400912 return -EINVAL;
913 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500914
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400915 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900916 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400917 return -EINVAL;
918 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500919
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400920 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900921 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400922 return -EINVAL;
923 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500924
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400925 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500926 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900927 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400928 return -EINVAL;
929 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500930
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400931 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500932 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
933 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400934 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500935}
936
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400937/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400938static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400939{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400940 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400941 struct perf_probe_arg_field **fieldp;
942
943 pr_debug("parsing arg: %s into ", str);
944
Masami Hiramatsu48481932010-04-12 13:16:53 -0400945 tmp = strchr(str, '=');
946 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400947 arg->name = strndup(str, tmp - str);
948 if (arg->name == NULL)
949 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400950 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400951 str = tmp + 1;
952 }
953
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400954 tmp = strchr(str, ':');
955 if (tmp) { /* Type setting */
956 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400957 arg->type = strdup(tmp + 1);
958 if (arg->type == NULL)
959 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400960 pr_debug("type:%s ", arg->type);
961 }
962
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400963 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400964 if (!is_c_varname(str) || !tmp) {
965 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400966 arg->var = strdup(str);
967 if (arg->var == NULL)
968 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400969 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400970 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400971 }
972
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400973 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400974 arg->var = strndup(str, tmp - str);
975 if (arg->var == NULL)
976 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400977 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400978 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400979 fieldp = &arg->field;
980
981 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400982 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
983 if (*fieldp == NULL)
984 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400985 if (*tmp == '[') { /* Array */
986 str = tmp;
987 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400988 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400989 if (*tmp != ']' || tmp == str + 1) {
990 semantic_error("Array index must be a"
991 " number.\n");
992 return -EINVAL;
993 }
994 tmp++;
995 if (*tmp == '\0')
996 tmp = NULL;
997 } else { /* Structure */
998 if (*tmp == '.') {
999 str = tmp + 1;
1000 (*fieldp)->ref = false;
1001 } else if (tmp[1] == '>') {
1002 str = tmp + 2;
1003 (*fieldp)->ref = true;
1004 } else {
1005 semantic_error("Argument parse error: %s\n",
1006 str);
1007 return -EINVAL;
1008 }
1009 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001010 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001011 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001012 (*fieldp)->name = strndup(str, tmp - str);
1013 if ((*fieldp)->name == NULL)
1014 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001015 if (*str != '[')
1016 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001017 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1018 fieldp = &(*fieldp)->next;
1019 }
1020 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001021 (*fieldp)->name = strdup(str);
1022 if ((*fieldp)->name == NULL)
1023 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001024 if (*str != '[')
1025 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001026 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001027
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001028 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001029 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001030 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001031 if (arg->name == NULL)
1032 return -ENOMEM;
1033 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001034 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001035}
1036
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001037/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001039{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001040 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001041 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001042
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001043 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001044 if (!argv) {
1045 pr_debug("Failed to split arguments.\n");
1046 return -ENOMEM;
1047 }
1048 if (argc - 1 > MAX_PROBE_ARGS) {
1049 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1050 ret = -ERANGE;
1051 goto out;
1052 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001053 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001054 ret = parse_perf_probe_point(argv[0], pev);
1055 if (ret < 0)
1056 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001057
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001058 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001059 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001060 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1061 if (pev->args == NULL) {
1062 ret = -ENOMEM;
1063 goto out;
1064 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001065 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1066 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1067 if (ret >= 0 &&
1068 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001069 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001070 " kretprobe.\n");
1071 ret = -EINVAL;
1072 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001073 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001074out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001075 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001076
1077 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001078}
1079
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001080/* Return true if this perf_probe_event requires debuginfo */
1081bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001082{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001083 int i;
1084
1085 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1086 return true;
1087
1088 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001089 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001090 return true;
1091
1092 return false;
1093}
1094
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301095/* Parse probe_events event into struct probe_point */
1096static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001097 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001098{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301099 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001100 char pr;
1101 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001102 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001103 int ret, i, argc;
1104 char **argv;
1105
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301106 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001107 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 if (!argv) {
1109 pr_debug("Failed to split arguments.\n");
1110 return -ENOMEM;
1111 }
1112 if (argc < 2) {
1113 semantic_error("Too few probe arguments.\n");
1114 ret = -ERANGE;
1115 goto out;
1116 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001117
1118 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001119 argv0_str = strdup(argv[0]);
1120 if (argv0_str == NULL) {
1121 ret = -ENOMEM;
1122 goto out;
1123 }
1124 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1125 fmt2_str = strtok_r(NULL, "/", &fmt);
1126 fmt3_str = strtok_r(NULL, " \t", &fmt);
1127 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1128 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001129 semantic_error("Failed to parse event name: %s\n", argv[0]);
1130 ret = -EINVAL;
1131 goto out;
1132 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001133 pr = fmt1_str[0];
1134 tev->group = strdup(fmt2_str);
1135 tev->event = strdup(fmt3_str);
1136 if (tev->group == NULL || tev->event == NULL) {
1137 ret = -ENOMEM;
1138 goto out;
1139 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001140 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001141
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001142 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001143
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001144 /* Scan module name(if there), function name and offset */
1145 p = strchr(argv[1], ':');
1146 if (p) {
1147 tp->module = strndup(argv[1], p - argv[1]);
1148 p++;
1149 } else
1150 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001151 fmt1_str = strtok_r(p, "+", &fmt);
1152 tp->symbol = strdup(fmt1_str);
1153 if (tp->symbol == NULL) {
1154 ret = -ENOMEM;
1155 goto out;
1156 }
1157 fmt2_str = strtok_r(NULL, "", &fmt);
1158 if (fmt2_str == NULL)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001159 tp->offset = 0;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001160 else
1161 tp->offset = strtoul(fmt2_str, NULL, 10);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001162
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001163 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301164 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001165 if (tev->args == NULL) {
1166 ret = -ENOMEM;
1167 goto out;
1168 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001170 p = strchr(argv[i + 2], '=');
1171 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001172 *p++ = '\0';
1173 else
1174 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001175 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001176 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001177 tev->args[i].value = strdup(p);
1178 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1179 ret = -ENOMEM;
1180 goto out;
1181 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001182 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001183 ret = 0;
1184out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001185 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001188}
1189
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001190/* Compose only probe arg */
1191int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1192{
1193 struct perf_probe_arg_field *field = pa->field;
1194 int ret;
1195 char *tmp = buf;
1196
Masami Hiramatsu48481932010-04-12 13:16:53 -04001197 if (pa->name && pa->var)
1198 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1199 else
1200 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001201 if (ret <= 0)
1202 goto error;
1203 tmp += ret;
1204 len -= ret;
1205
1206 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001207 if (field->name[0] == '[')
1208 ret = e_snprintf(tmp, len, "%s", field->name);
1209 else
1210 ret = e_snprintf(tmp, len, "%s%s",
1211 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001212 if (ret <= 0)
1213 goto error;
1214 tmp += ret;
1215 len -= ret;
1216 field = field->next;
1217 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001218
1219 if (pa->type) {
1220 ret = e_snprintf(tmp, len, ":%s", pa->type);
1221 if (ret <= 0)
1222 goto error;
1223 tmp += ret;
1224 len -= ret;
1225 }
1226
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001227 return tmp - buf;
1228error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001229 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 strerror(-ret));
1231 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001232}
1233
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001234/* Compose only probe point (not argument) */
1235static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001236{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001237 char *buf, *tmp;
1238 char offs[32] = "", line[32] = "", file[32] = "";
1239 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001240
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001241 buf = zalloc(MAX_CMDLEN);
1242 if (buf == NULL) {
1243 ret = -ENOMEM;
1244 goto error;
1245 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001246 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001247 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001248 if (ret <= 0)
1249 goto error;
1250 }
1251 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001252 ret = e_snprintf(line, 32, ":%d", pp->line);
1253 if (ret <= 0)
1254 goto error;
1255 }
1256 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001257 tmp = pp->file;
1258 len = strlen(tmp);
1259 if (len > 30) {
1260 tmp = strchr(pp->file + len - 30, '/');
1261 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1262 }
1263 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001264 if (ret <= 0)
1265 goto error;
1266 }
1267
1268 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001269 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1270 offs, pp->retprobe ? "%return" : "", line,
1271 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001272 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001273 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001274 if (ret <= 0)
1275 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001276
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 return buf;
1278error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001279 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001281 if (buf)
1282 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001283 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284}
1285
1286#if 0
1287char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1288{
1289 char *buf;
1290 int i, len, ret;
1291
1292 buf = synthesize_perf_probe_point(&pev->point);
1293 if (!buf)
1294 return NULL;
1295
1296 len = strlen(buf);
1297 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001298 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001299 pev->args[i].name);
1300 if (ret <= 0) {
1301 free(buf);
1302 return NULL;
1303 }
1304 len += ret;
1305 }
1306
1307 return buf;
1308}
1309#endif
1310
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301311static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001312 char **buf, size_t *buflen,
1313 int depth)
1314{
1315 int ret;
1316 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301317 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001318 buflen, depth + 1);
1319 if (depth < 0)
1320 goto out;
1321 }
1322
1323 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1324 if (ret < 0)
1325 depth = ret;
1326 else {
1327 *buf += ret;
1328 *buflen -= ret;
1329 }
1330out:
1331 return depth;
1332
1333}
1334
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301335static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001336 char *buf, size_t buflen)
1337{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301338 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001339 int ret, depth = 0;
1340 char *tmp = buf;
1341
1342 /* Argument name or separator */
1343 if (arg->name)
1344 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1345 else
1346 ret = e_snprintf(buf, buflen, " ");
1347 if (ret < 0)
1348 return ret;
1349 buf += ret;
1350 buflen -= ret;
1351
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001352 /* Special case: @XXX */
1353 if (arg->value[0] == '@' && arg->ref)
1354 ref = ref->next;
1355
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001356 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001357 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301358 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359 &buflen, 1);
1360 if (depth < 0)
1361 return depth;
1362 }
1363
1364 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001365 if (arg->value[0] == '@' && arg->ref)
1366 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1367 arg->ref->offset);
1368 else
1369 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001370 if (ret < 0)
1371 return ret;
1372 buf += ret;
1373 buflen -= ret;
1374
1375 /* Closing */
1376 while (depth--) {
1377 ret = e_snprintf(buf, buflen, ")");
1378 if (ret < 0)
1379 return ret;
1380 buf += ret;
1381 buflen -= ret;
1382 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001383 /* Print argument type */
1384 if (arg->type) {
1385 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1386 if (ret <= 0)
1387 return ret;
1388 buf += ret;
1389 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390
1391 return buf - tmp;
1392}
1393
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301394char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001395{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301396 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001397 char *buf;
1398 int i, len, ret;
1399
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001400 buf = zalloc(MAX_CMDLEN);
1401 if (buf == NULL)
1402 return NULL;
1403
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301404 if (tev->uprobes)
1405 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1406 tp->retprobe ? 'r' : 'p',
1407 tev->group, tev->event,
1408 tp->module, tp->symbol);
1409 else
1410 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1411 tp->retprobe ? 'r' : 'p',
1412 tev->group, tev->event,
1413 tp->module ?: "", tp->module ? ":" : "",
1414 tp->symbol, tp->offset);
1415
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001416 if (len <= 0)
1417 goto error;
1418
1419 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301420 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001422 if (ret <= 0)
1423 goto error;
1424 len += ret;
1425 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001426
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001427 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001428error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 free(buf);
1430 return NULL;
1431}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001432
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301433static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301434 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001435{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001436 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001438
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001439 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001440 pev->event = strdup(tev->event);
1441 pev->group = strdup(tev->group);
1442 if (pev->event == NULL || pev->group == NULL)
1443 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001444
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001445 /* Convert trace_point to probe_point */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301446 if (is_kprobe)
1447 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1448 else
1449 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1450
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 if (ret < 0)
1452 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001453
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001454 /* Convert trace_arg to probe_arg */
1455 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001456 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1457 if (pev->args == NULL)
1458 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001459 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001460 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001461 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001462 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301463 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001465 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001466 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001467 if (pev->args[i].name == NULL && ret >= 0)
1468 ret = -ENOMEM;
1469 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470
1471 if (ret < 0)
1472 clear_perf_probe_event(pev);
1473
1474 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475}
1476
1477void clear_perf_probe_event(struct perf_probe_event *pev)
1478{
1479 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001480 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 int i;
1482
1483 if (pev->event)
1484 free(pev->event);
1485 if (pev->group)
1486 free(pev->group);
1487 if (pp->file)
1488 free(pp->file);
1489 if (pp->function)
1490 free(pp->function);
1491 if (pp->lazy_line)
1492 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001493 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494 if (pev->args[i].name)
1495 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001496 if (pev->args[i].var)
1497 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001498 if (pev->args[i].type)
1499 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001500 field = pev->args[i].field;
1501 while (field) {
1502 next = field->next;
1503 if (field->name)
1504 free(field->name);
1505 free(field);
1506 field = next;
1507 }
1508 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509 if (pev->args)
1510 free(pev->args);
1511 memset(pev, 0, sizeof(*pev));
1512}
1513
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301514static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301516 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001517 int i;
1518
1519 if (tev->event)
1520 free(tev->event);
1521 if (tev->group)
1522 free(tev->group);
1523 if (tev->point.symbol)
1524 free(tev->point.symbol);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001525 if (tev->point.module)
1526 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001527 for (i = 0; i < tev->nargs; i++) {
1528 if (tev->args[i].name)
1529 free(tev->args[i].name);
1530 if (tev->args[i].value)
1531 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001532 if (tev->args[i].type)
1533 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001534 ref = tev->args[i].ref;
1535 while (ref) {
1536 next = ref->next;
1537 free(ref);
1538 ref = next;
1539 }
1540 }
1541 if (tev->args)
1542 free(tev->args);
1543 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001544}
1545
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301546static void print_warn_msg(const char *file, bool is_kprobe)
1547{
1548
1549 if (errno == ENOENT) {
1550 const char *config;
1551
1552 if (!is_kprobe)
1553 config = "CONFIG_UPROBE_EVENTS";
1554 else
1555 config = "CONFIG_KPROBE_EVENTS";
1556
1557 pr_warning("%s file does not exist - please rebuild kernel"
1558 " with %s.\n", file, config);
1559 } else
1560 pr_warning("Failed to open %s file: %s\n", file,
1561 strerror(errno));
1562}
1563
1564static int open_probe_events(const char *trace_file, bool readwrite,
1565 bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001566{
1567 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001568 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001569 int ret;
1570
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001571 __debugfs = debugfs_find_mountpoint();
1572 if (__debugfs == NULL) {
1573 pr_warning("Debugfs is not mounted.\n");
1574 return -ENOENT;
1575 }
1576
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301577 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001578 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001579 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001580 if (readwrite && !probe_event_dry_run)
1581 ret = open(buf, O_RDWR, O_APPEND);
1582 else
1583 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001584
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301585 if (ret < 0)
1586 print_warn_msg(buf, is_kprobe);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587 }
1588 return ret;
1589}
1590
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301591static int open_kprobe_events(bool readwrite)
1592{
1593 return open_probe_events("tracing/kprobe_events", readwrite, true);
1594}
1595
1596static int open_uprobe_events(bool readwrite)
1597{
1598 return open_probe_events("tracing/uprobe_events", readwrite, false);
1599}
1600
1601/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301602static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001603{
1604 int ret, idx;
1605 FILE *fp;
1606 char buf[MAX_CMDLEN];
1607 char *p;
1608 struct strlist *sl;
1609
1610 sl = strlist__new(true, NULL);
1611
1612 fp = fdopen(dup(fd), "r");
1613 while (!feof(fp)) {
1614 p = fgets(buf, MAX_CMDLEN, fp);
1615 if (!p)
1616 break;
1617
1618 idx = strlen(p) - 1;
1619 if (p[idx] == '\n')
1620 p[idx] = '\0';
1621 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001622 if (ret < 0) {
1623 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1624 strlist__delete(sl);
1625 return NULL;
1626 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001627 }
1628 fclose(fp);
1629
1630 return sl;
1631}
1632
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001633/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001634static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001635{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001636 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001637 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001639
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001640 /* Synthesize only event probe point */
1641 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001642 if (!place)
1643 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001644
1645 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001646 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001647 return ret;
1648
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001649 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001650
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001652 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001653 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001654 ret = synthesize_perf_probe_arg(&pev->args[i],
1655 buf, 128);
1656 if (ret < 0)
1657 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001658 printf(" %s", buf);
1659 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001660 }
1661 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001662 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001663 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001664}
1665
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301666static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001667{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301668 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301669 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001670 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001671 struct strlist *rawlist;
1672 struct str_node *ent;
1673
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001674 memset(&tev, 0, sizeof(tev));
1675 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001676
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301677 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001678 if (!rawlist)
1679 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001680
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001681 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301682 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001683 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301684 ret = convert_to_perf_probe_event(&tev, &pev,
1685 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001686 if (ret >= 0)
1687 ret = show_perf_probe_event(&pev);
1688 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001689 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301690 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001691 if (ret < 0)
1692 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001693 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001694 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001695
1696 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001697}
1698
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301699/* List up current perf-probe events */
1700int show_perf_probe_events(void)
1701{
1702 int fd, ret;
1703
1704 setup_pager();
1705 fd = open_kprobe_events(false);
1706
1707 if (fd < 0)
1708 return fd;
1709
1710 ret = init_vmlinux();
1711 if (ret < 0)
1712 return ret;
1713
1714 ret = __show_perf_probe_events(fd, true);
1715 close(fd);
1716
1717 fd = open_uprobe_events(false);
1718 if (fd >= 0) {
1719 ret = __show_perf_probe_events(fd, false);
1720 close(fd);
1721 }
1722
1723 return ret;
1724}
1725
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001726/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301727static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001728{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001729 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001730 struct strlist *sl, *rawlist;
1731 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301732 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001733 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001734
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301736 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001737 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001738 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301739 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001740 if (ret < 0)
1741 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001742 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001743 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1744 tev.event);
1745 if (ret >= 0)
1746 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001747 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001748 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301749 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001750 if (ret < 0)
1751 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001752 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001753 strlist__delete(rawlist);
1754
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001755 if (ret < 0) {
1756 strlist__delete(sl);
1757 return NULL;
1758 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001759 return sl;
1760}
1761
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301762static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001763{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001764 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301765 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001766
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001767 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301768 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001769 return -EINVAL;
1770 }
1771
Masami Hiramatsufa282442009-12-08 17:03:23 -05001772 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001773 if (!probe_event_dry_run) {
1774 ret = write(fd, buf, strlen(buf));
1775 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001776 pr_warning("Failed to write event: %s\n",
1777 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001778 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001779 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001780 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001781}
1782
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783static int get_new_event_name(char *buf, size_t len, const char *base,
1784 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001785{
1786 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001787
1788 /* Try no suffix */
1789 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001790 if (ret < 0) {
1791 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1792 return ret;
1793 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001794 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001795 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001796
Masami Hiramatsud761b082009-12-15 10:32:25 -05001797 if (!allow_suffix) {
1798 pr_warning("Error: event \"%s\" already exists. "
1799 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001800 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001801 }
1802
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001803 /* Try to add suffix */
1804 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001805 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001806 if (ret < 0) {
1807 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1808 return ret;
1809 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001810 if (!strlist__has_entry(namelist, buf))
1811 break;
1812 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001813 if (i == MAX_EVENT_INDEX) {
1814 pr_warning("Too many events are on the same function.\n");
1815 ret = -ERANGE;
1816 }
1817
1818 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001819}
1820
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301821static int __add_probe_trace_events(struct perf_probe_event *pev,
1822 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001823 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001824{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001825 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301826 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827 char buf[64];
1828 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001829 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001830
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301831 if (pev->uprobes)
1832 fd = open_uprobe_events(true);
1833 else
1834 fd = open_kprobe_events(true);
1835
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001836 if (fd < 0)
1837 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001838 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301839 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001840 if (!namelist) {
1841 pr_debug("Failed to get current event list.\n");
1842 return -EIO;
1843 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001844
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001845 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301846 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001847 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001848 tev = &tevs[i];
1849 if (pev->event)
1850 event = pev->event;
1851 else
1852 if (pev->point.function)
1853 event = pev->point.function;
1854 else
1855 event = tev->point.symbol;
1856 if (pev->group)
1857 group = pev->group;
1858 else
1859 group = PERFPROBE_GROUP;
1860
1861 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001862 ret = get_new_event_name(buf, 64, event,
1863 namelist, allow_suffix);
1864 if (ret < 0)
1865 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866 event = buf;
1867
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001868 tev->event = strdup(event);
1869 tev->group = strdup(group);
1870 if (tev->event == NULL || tev->group == NULL) {
1871 ret = -ENOMEM;
1872 break;
1873 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301874 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875 if (ret < 0)
1876 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001877 /* Add added event name to namelist */
1878 strlist__add(namelist, event);
1879
1880 /* Trick here - save current event/group */
1881 event = pev->event;
1882 group = pev->group;
1883 pev->event = tev->event;
1884 pev->group = tev->group;
1885 show_perf_probe_event(pev);
1886 /* Trick here - restore current event/group */
1887 pev->event = (char *)event;
1888 pev->group = (char *)group;
1889
1890 /*
1891 * Probes after the first probe which comes from same
1892 * user input are always allowed to add suffix, because
1893 * there might be several addresses corresponding to
1894 * one code line.
1895 */
1896 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001897 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001898
1899 if (ret >= 0) {
1900 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301901 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001902 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1903 tev->event);
1904 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001905
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001906 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001907 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001908 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001909}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001910
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301911static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1912 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301913 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001914{
1915 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001916 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301917 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001919 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301920 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001921 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001922 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001923
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001924 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301925 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001926 if (tev == NULL)
1927 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001928
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001930 tev->point.symbol = strdup(pev->point.function);
1931 if (tev->point.symbol == NULL) {
1932 ret = -ENOMEM;
1933 goto error;
1934 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001935
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301936 if (target) {
1937 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08001938 if (tev->point.module == NULL) {
1939 ret = -ENOMEM;
1940 goto error;
1941 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001942 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001943
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001944 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001945 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001946 tev->nargs = pev->nargs;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301947 tev->uprobes = pev->uprobes;
1948
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001949 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301950 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001951 * tev->nargs);
1952 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001953 ret = -ENOMEM;
1954 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001955 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001956 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001957 if (pev->args[i].name) {
1958 tev->args[i].name = strdup(pev->args[i].name);
1959 if (tev->args[i].name == NULL) {
1960 ret = -ENOMEM;
1961 goto error;
1962 }
1963 }
1964 tev->args[i].value = strdup(pev->args[i].var);
1965 if (tev->args[i].value == NULL) {
1966 ret = -ENOMEM;
1967 goto error;
1968 }
1969 if (pev->args[i].type) {
1970 tev->args[i].type = strdup(pev->args[i].type);
1971 if (tev->args[i].type == NULL) {
1972 ret = -ENOMEM;
1973 goto error;
1974 }
1975 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001976 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001977 }
1978
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301979 if (pev->uprobes)
1980 return 1;
1981
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001982 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001983 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001984 if (!sym) {
1985 pr_warning("Kernel symbol \'%s\' not found.\n",
1986 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001987 ret = -ENOENT;
1988 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05301989 } else if (tev->point.offset > sym->end - sym->start) {
1990 pr_warning("Offset specified is greater than size of %s\n",
1991 tev->point.symbol);
1992 ret = -ENOENT;
1993 goto error;
1994
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001995 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001996
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001997 return 1;
1998error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301999 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002000 free(tev);
2001 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002002 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002003}
2004
2005struct __event_package {
2006 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302007 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002008 int ntevs;
2009};
2010
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002011int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302012 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002013{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002014 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002015 struct __event_package *pkgs;
2016
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302017 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002018 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302019
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002020 if (pkgs == NULL)
2021 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002022
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302023 if (!pevs->uprobes)
2024 /* Init vmlinux path */
2025 ret = init_vmlinux();
2026 else
2027 ret = init_user_exec();
2028
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002029 if (ret < 0) {
2030 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002031 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002032 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002033
2034 /* Loop 1: convert all events */
2035 for (i = 0; i < npevs; i++) {
2036 pkgs[i].pev = &pevs[i];
2037 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302038 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002039 &pkgs[i].tevs,
2040 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302041 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002042 if (ret < 0)
2043 goto end;
2044 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002045 }
2046
2047 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002048 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302049 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002050 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002051 if (ret < 0)
2052 break;
2053 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002054end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002055 /* Loop 3: cleanup and free trace events */
2056 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002057 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302058 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002059 free(pkgs[i].tevs);
2060 }
2061 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002062
2063 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002064}
2065
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302066static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002067{
2068 char *p;
2069 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002070 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002071
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302072 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002073 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2074 if (ret < 0)
2075 goto error;
2076
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002077 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002078 if (!p) {
2079 pr_debug("Internal error: %s should have ':' but not.\n",
2080 ent->s);
2081 ret = -ENOTSUP;
2082 goto error;
2083 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002084 *p = '/';
2085
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002086 pr_debug("Writing event: %s\n", buf);
2087 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002088 if (ret < 0) {
2089 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002090 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002091 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002092
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05302093 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002094 return 0;
2095error:
2096 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2097 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002098}
2099
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302100static int del_trace_probe_event(int fd, const char *buf,
2101 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002102{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002103 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302104 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002105
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002106 if (strpbrk(buf, "*?")) { /* Glob-exp */
2107 strlist__for_each_safe(ent, n, namelist)
2108 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302109 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002110 if (ret < 0)
2111 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002112 strlist__remove(namelist, ent);
2113 }
2114 } else {
2115 ent = strlist__find(namelist, buf);
2116 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302117 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002118 if (ret >= 0)
2119 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002120 }
2121 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002122
2123 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002124}
2125
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002126int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002127{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302128 int ret = -1, ufd = -1, kfd = -1;
2129 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002130 const char *group, *event;
2131 char *p, *str;
2132 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302133 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002134
Masami Hiramatsufa282442009-12-08 17:03:23 -05002135 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302136 kfd = open_kprobe_events(true);
2137 if (kfd < 0)
2138 return kfd;
2139
2140 namelist = get_probe_trace_event_names(kfd, true);
2141 ufd = open_uprobe_events(true);
2142
2143 if (ufd >= 0)
2144 unamelist = get_probe_trace_event_names(ufd, true);
2145
2146 if (namelist == NULL && unamelist == NULL)
2147 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002148
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002149 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002150 str = strdup(ent->s);
2151 if (str == NULL) {
2152 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302153 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002154 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002155 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002156 p = strchr(str, ':');
2157 if (p) {
2158 group = str;
2159 *p = '\0';
2160 event = p + 1;
2161 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002162 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002163 event = str;
2164 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302165
2166 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2167 if (ret < 0) {
2168 pr_err("Failed to copy event.");
2169 free(str);
2170 goto error;
2171 }
2172
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002173 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302174
2175 if (namelist)
2176 ret = del_trace_probe_event(kfd, buf, namelist);
2177
2178 if (unamelist && ret != 0)
2179 ret = del_trace_probe_event(ufd, buf, unamelist);
2180
2181 if (ret != 0)
2182 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2183
Masami Hiramatsufa282442009-12-08 17:03:23 -05002184 free(str);
2185 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302186
2187error:
2188 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302189 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302190 close(kfd);
2191 }
2192
2193 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302194 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302195 close(ufd);
2196 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002197
2198 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002199}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302200
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002201/* TODO: don't use a global variable for filter ... */
2202static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002203
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002204/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002205 * If a symbol corresponds to a function with global binding and
2206 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002207 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002208static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002209 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002210{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002211 if (sym->binding == STB_GLOBAL &&
2212 strfilter__compare(available_func_filter, sym->name))
2213 return 0;
2214 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002215}
2216
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217static int __show_available_funcs(struct map *map)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002218{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002219 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002220 pr_err("Failed to load map.\n");
2221 return -EINVAL;
2222 }
2223 if (!dso__sorted_by_name(map->dso, map->type))
2224 dso__sort_by_name(map->dso, map->type);
2225
2226 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2227 return 0;
2228}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302229
2230static int available_kernel_funcs(const char *module)
2231{
2232 struct map *map;
2233 int ret;
2234
2235 ret = init_vmlinux();
2236 if (ret < 0)
2237 return ret;
2238
2239 map = kernel_get_module_map(module);
2240 if (!map) {
2241 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2242 return -EINVAL;
2243 }
2244 return __show_available_funcs(map);
2245}
2246
2247static int available_user_funcs(const char *target)
2248{
2249 struct map *map;
2250 int ret;
2251
2252 ret = init_user_exec();
2253 if (ret < 0)
2254 return ret;
2255
2256 map = dso__new_map(target);
2257 ret = __show_available_funcs(map);
2258 dso__delete(map->dso);
2259 map__delete(map);
2260 return ret;
2261}
2262
2263int show_available_funcs(const char *target, struct strfilter *_filter,
2264 bool user)
2265{
2266 setup_pager();
2267 available_func_filter = _filter;
2268
2269 if (!user)
2270 return available_kernel_funcs(target);
2271
2272 return available_user_funcs(target);
2273}
2274
2275/*
2276 * uprobe_events only accepts address:
2277 * Convert function and any offset to address
2278 */
2279static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2280{
2281 struct perf_probe_point *pp = &pev->point;
2282 struct symbol *sym;
2283 struct map *map = NULL;
2284 char *function = NULL, *name = NULL;
2285 int ret = -EINVAL;
2286 unsigned long long vaddr = 0;
2287
2288 if (!pp->function) {
2289 pr_warning("No function specified for uprobes");
2290 goto out;
2291 }
2292
2293 function = strdup(pp->function);
2294 if (!function) {
2295 pr_warning("Failed to allocate memory by strdup.\n");
2296 ret = -ENOMEM;
2297 goto out;
2298 }
2299
2300 name = realpath(exec, NULL);
2301 if (!name) {
2302 pr_warning("Cannot find realpath for %s.\n", exec);
2303 goto out;
2304 }
2305 map = dso__new_map(name);
2306 if (!map) {
2307 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2308 goto out;
2309 }
2310 available_func_filter = strfilter__new(function, NULL);
2311 if (map__load(map, filter_available_functions)) {
2312 pr_err("Failed to load map.\n");
2313 goto out;
2314 }
2315
2316 sym = map__find_symbol_by_name(map, function, NULL);
2317 if (!sym) {
2318 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2319 goto out;
2320 }
2321
2322 if (map->start > sym->start)
2323 vaddr = map->start;
2324 vaddr += sym->start + pp->offset + map->pgoff;
2325 pp->offset = 0;
2326
2327 if (!pev->event) {
2328 pev->event = function;
2329 function = NULL;
2330 }
2331 if (!pev->group) {
David Ahern1fb89442012-09-08 09:06:51 -06002332 char *ptr1, *ptr2, *exec_copy;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302333
2334 pev->group = zalloc(sizeof(char *) * 64);
David Ahern1fb89442012-09-08 09:06:51 -06002335 exec_copy = strdup(exec);
2336 if (!exec_copy) {
2337 ret = -ENOMEM;
2338 pr_warning("Failed to copy exec string.\n");
2339 goto out;
2340 }
2341
2342 ptr1 = strdup(basename(exec_copy));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302343 if (ptr1) {
2344 ptr2 = strpbrk(ptr1, "-._");
2345 if (ptr2)
2346 *ptr2 = '\0';
2347 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2348 ptr1);
2349 free(ptr1);
2350 }
David Ahern1fb89442012-09-08 09:06:51 -06002351 free(exec_copy);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302352 }
2353 free(pp->function);
2354 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2355 if (!pp->function) {
2356 ret = -ENOMEM;
2357 pr_warning("Failed to allocate memory by zalloc.\n");
2358 goto out;
2359 }
2360 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2361 ret = 0;
2362
2363out:
2364 if (map) {
2365 dso__delete(map->dso);
2366 map__delete(map);
2367 }
2368 if (function)
2369 free(function);
2370 if (name)
2371 free(name);
2372 return ret;
2373}