blob: 8a8ee64e72d14e52a12d6e26915950cbd9969649 [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"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040043#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030044#include "trace-event.h" /* For __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"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047
48#define MAX_CMDLEN 256
49#define MAX_PROBE_ARGS 128
50#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);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030073static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040074
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090075/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040076static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078 int ret;
79
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080 symbol_conf.sort_by_name = true;
81 if (symbol_conf.vmlinux_name == NULL)
82 symbol_conf.try_vmlinux_path = true;
83 else
84 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040085 ret = symbol__init();
86 if (ret < 0) {
87 pr_debug("Failed to init symbol map.\n");
88 goto out;
89 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040090
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090091 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030092 if (ret < 0)
93 goto out;
94
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090095 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090096 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 goto out;
98 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040099out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400103}
104
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900105static struct symbol *__find_kernel_function_by_name(const char *name,
106 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400107{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900108 return machine__find_kernel_function_by_name(&machine, name, mapp,
109 NULL);
110}
111
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900112static struct map *kernel_get_module_map(const char *module)
113{
114 struct rb_node *nd;
115 struct map_groups *grp = &machine.kmaps;
116
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900117 /* A file path -- this is an offline module */
118 if (module && strchr(module, '/'))
119 return machine__new_module(&machine, 0, module);
120
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900121 if (!module)
122 module = "kernel";
123
124 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
125 struct map *pos = rb_entry(nd, struct map, rb_node);
126 if (strncmp(pos->dso->short_name + 1, module,
127 pos->dso->short_name_len - 2) == 0) {
128 return pos;
129 }
130 }
131 return NULL;
132}
133
134static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900135{
136 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100137 struct map *map;
138 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900139
140 if (module) {
141 list_for_each_entry(dso, &machine.kernel_dsos, node) {
142 if (strncmp(dso->short_name + 1, module,
143 dso->short_name_len - 2) == 0)
144 goto found;
145 }
146 pr_debug("Failed to find module %s.\n", module);
147 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100148 }
149
150 map = machine.vmlinux_maps[MAP__FUNCTION];
151 dso = map->dso;
152
153 vmlinux_name = symbol_conf.vmlinux_name;
154 if (vmlinux_name) {
155 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
156 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900157 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100158 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900159 pr_debug("Failed to load kernel map.\n");
160 return NULL;
161 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400162 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900163found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900164 return dso;
165}
166
167const char *kernel_get_module_path(const char *module)
168{
169 struct dso *dso = kernel_get_module_dso(module);
170 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900171}
172
173#ifdef DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900174/* Open new debuginfo of given module */
175static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900176{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900177 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900178
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900179 /* A file path -- this is an offline module */
180 if (module && strchr(module, '/'))
181 path = module;
182 else {
183 path = kernel_get_module_path(module);
184
185 if (!path) {
186 pr_err("Failed to find path of %s module.\n",
187 module ?: "kernel");
188 return NULL;
189 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900190 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900191 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400192}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300193
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530194/*
195 * Convert trace point to probe point with debuginfo
196 * Currently only handles kprobes.
197 */
198static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900199 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300200{
201 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900202 struct map *map;
203 u64 addr;
204 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900205 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300206
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900207 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300208 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900209 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200210 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900211 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900212
213 dinfo = debuginfo__new_online_kernel(addr);
214 if (dinfo) {
215 ret = debuginfo__find_probe_point(dinfo,
216 (unsigned long)addr, pp);
217 debuginfo__delete(dinfo);
218 } else {
219 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
220 addr);
221 ret = -ENOENT;
222 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300223 }
224 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400225 pr_debug("Failed to find corresponding probes from "
226 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400227 pp->function = strdup(tp->symbol);
228 if (pp->function == NULL)
229 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300230 pp->offset = tp->offset;
231 }
232 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400233
234 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300235}
236
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900237static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
238 int ntevs, const char *module)
239{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900240 int i, ret = 0;
241 char *tmp;
242
243 if (!module)
244 return 0;
245
246 tmp = strrchr(module, '/');
247 if (tmp) {
248 /* This is a module path -- get the module name */
249 module = strdup(tmp + 1);
250 if (!module)
251 return -ENOMEM;
252 tmp = strchr(module, '.');
253 if (tmp)
254 *tmp = '\0';
255 tmp = (char *)module; /* For free() */
256 }
257
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900258 for (i = 0; i < ntevs; i++) {
259 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900260 if (!tevs[i].point.module) {
261 ret = -ENOMEM;
262 break;
263 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900264 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900265
266 if (tmp)
267 free(tmp);
268
269 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900270}
271
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300272/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530273static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900274 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530275 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300276{
277 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530278 struct debuginfo *dinfo = open_debuginfo(target);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900279 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300280
Masami Hiramatsuff741782011-06-27 16:27:39 +0900281 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400282 if (need_dwarf) {
283 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900284 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400285 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900286 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300287 return 0;
288 }
289
Masami Hiramatsuff741782011-06-27 16:27:39 +0900290 /* Searching trace events corresponding to a probe event */
291 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
292
293 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300294
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400295 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530296 pr_debug("find %d probe_trace_events.\n", ntevs);
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530297 if (target)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900298 ret = add_module_to_probe_trace_events(*tevs, ntevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530299 target);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900300 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400301 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300302
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400303 if (ntevs == 0) { /* No error but failed to find probe point. */
304 pr_warning("Probe point '%s' not found.\n",
305 synthesize_perf_probe_point(&pev->point));
306 return -ENOENT;
307 }
308 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400309 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
310 if (ntevs == -EBADF) {
311 pr_warning("Warning: No dwarf info found in the vmlinux - "
312 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
313 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900314 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400315 return 0;
316 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300317 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400318 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300319}
320
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900321/*
322 * Find a src file from a DWARF tag path. Prepend optional source path prefix
323 * and chop off leading directories that do not exist. Result is passed back as
324 * a newly allocated path on success.
325 * Return 0 if file was found and readable, -errno otherwise.
326 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900327static int get_real_path(const char *raw_path, const char *comp_dir,
328 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900329{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900330 const char *prefix = symbol_conf.source_prefix;
331
332 if (!prefix) {
333 if (raw_path[0] != '/' && comp_dir)
334 /* If not an absolute path, try to use comp_dir */
335 prefix = comp_dir;
336 else {
337 if (access(raw_path, R_OK) == 0) {
338 *new_path = strdup(raw_path);
339 return 0;
340 } else
341 return -errno;
342 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900343 }
344
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900345 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900346 if (!*new_path)
347 return -ENOMEM;
348
349 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900350 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900351
352 if (access(*new_path, R_OK) == 0)
353 return 0;
354
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900355 if (!symbol_conf.source_prefix)
356 /* In case of searching comp_dir, don't retry */
357 return -errno;
358
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900359 switch (errno) {
360 case ENAMETOOLONG:
361 case ENOENT:
362 case EROFS:
363 case EFAULT:
364 raw_path = strchr(++raw_path, '/');
365 if (!raw_path) {
366 free(*new_path);
367 *new_path = NULL;
368 return -ENOENT;
369 }
370 continue;
371
372 default:
373 free(*new_path);
374 *new_path = NULL;
375 return -errno;
376 }
377 }
378}
379
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300380#define LINEBUF_SIZE 256
381#define NR_ADDITIONAL_LINES 2
382
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100383static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300384{
385 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100386 const char *color = show_num ? "" : PERF_COLOR_BLUE;
387 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300388
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100389 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300390 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
391 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100392 if (skip)
393 continue;
394 if (!prefix) {
395 prefix = show_num ? "%7d " : " ";
396 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300397 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100398 color_fprintf(stdout, color, "%s", buf);
399
400 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400401
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100402 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300403error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100404 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100405 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100406 return -1;
407 }
408 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300409}
410
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100411static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
412{
413 int rv = __show_one_line(fp, l, skip, show_num);
414 if (rv == 0) {
415 pr_warning("Source file is shorter than expected.\n");
416 rv = -1;
417 }
418 return rv;
419}
420
421#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
422#define show_one_line(f,l) _show_one_line(f,l,false,false)
423#define skip_one_line(f,l) _show_one_line(f,l,true,false)
424#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
425
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300426/*
427 * Show line-range always requires debuginfo to find source file and
428 * line number.
429 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900430int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300431{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400432 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300433 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900434 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300435 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900436 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900437 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300438
439 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400440 ret = init_vmlinux();
441 if (ret < 0)
442 return ret;
443
Masami Hiramatsuff741782011-06-27 16:27:39 +0900444 dinfo = open_debuginfo(module);
445 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400446 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900447 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400448 }
449
Masami Hiramatsuff741782011-06-27 16:27:39 +0900450 ret = debuginfo__find_line_range(dinfo, lr);
451 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400452 if (ret == 0) {
453 pr_warning("Specified source line is not found.\n");
454 return -ENOENT;
455 } else if (ret < 0) {
456 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
457 return ret;
458 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300459
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900460 /* Convert source file path */
461 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900462 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900463 free(tmp); /* Free old path */
464 if (ret < 0) {
465 pr_warning("Failed to find source file. (%d)\n", ret);
466 return ret;
467 }
468
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300469 setup_pager();
470
471 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900472 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300473 lr->start - lr->offset);
474 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100475 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300476
477 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400478 if (fp == NULL) {
479 pr_warning("Failed to open %s: %s\n", lr->path,
480 strerror(errno));
481 return -errno;
482 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300483 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100484 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100485 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100486 if (ret < 0)
487 goto end;
488 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300489
490 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100491 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100492 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100493 if (ret < 0)
494 goto end;
495 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100496 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400497 if (ret < 0)
498 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300499 }
500
501 if (lr->end == INT_MAX)
502 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100503 while (l <= lr->end) {
504 ret = show_one_line_or_eof(fp, l++ - lr->offset);
505 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100506 break;
507 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400508end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300509 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400510 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300511}
512
Masami Hiramatsuff741782011-06-27 16:27:39 +0900513static int show_available_vars_at(struct debuginfo *dinfo,
514 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900515 int max_vls, struct strfilter *_filter,
516 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900517{
518 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900519 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900520 struct str_node *node;
521 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900522 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900523
524 buf = synthesize_perf_probe_point(&pev->point);
525 if (!buf)
526 return -EINVAL;
527 pr_debug("Searching variables at %s\n", buf);
528
Masami Hiramatsuff741782011-06-27 16:27:39 +0900529 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
530 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900531 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900532 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900533 goto end;
534 }
535 /* Some variables are found */
536 fprintf(stdout, "Available variables at %s\n", buf);
537 for (i = 0; i < ret; i++) {
538 vl = &vls[i];
539 /*
540 * A probe point might be converted to
541 * several trace points.
542 */
543 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
544 vl->point.offset);
545 free(vl->point.symbol);
546 nvars = 0;
547 if (vl->vars) {
548 strlist__for_each(node, vl->vars) {
549 var = strchr(node->s, '\t') + 1;
550 if (strfilter__compare(_filter, var)) {
551 fprintf(stdout, "\t\t%s\n", node->s);
552 nvars++;
553 }
554 }
555 strlist__delete(vl->vars);
556 }
557 if (nvars == 0)
558 fprintf(stdout, "\t\t(No matched variables)\n");
559 }
560 free(vls);
561end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900562 free(buf);
563 return ret;
564}
565
566/* Show available variables on given probe point */
567int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900568 int max_vls, const char *module,
569 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900570{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900571 int i, ret = 0;
572 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900573
574 ret = init_vmlinux();
575 if (ret < 0)
576 return ret;
577
Masami Hiramatsuff741782011-06-27 16:27:39 +0900578 dinfo = open_debuginfo(module);
579 if (!dinfo) {
580 pr_warning("Failed to open debuginfo file.\n");
581 return -ENOENT;
582 }
583
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900584 setup_pager();
585
Masami Hiramatsuff741782011-06-27 16:27:39 +0900586 for (i = 0; i < npevs && ret >= 0; i++)
587 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900588 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900589
590 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900591 return ret;
592}
593
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300594#else /* !DWARF_SUPPORT */
595
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530596static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900597 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300598{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900599 struct symbol *sym;
600
601 sym = __find_kernel_function_by_name(tp->symbol, NULL);
602 if (!sym) {
603 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
604 return -ENOENT;
605 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400606 pp->function = strdup(tp->symbol);
607 if (pp->function == NULL)
608 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300609 pp->offset = tp->offset;
610 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400611
612 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300613}
614
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530615static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
616 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900617 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300618{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400619 if (perf_probe_event_need_dwarf(pev)) {
620 pr_warning("Debuginfo-analysis is not supported.\n");
621 return -ENOSYS;
622 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300623 return 0;
624}
625
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900626int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300627{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400628 pr_warning("Debuginfo-analysis is not supported.\n");
629 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300630}
631
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900632int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900633 int npevs __unused, int max_vls __unused,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900634 const char *module __unused,
635 struct strfilter *filter __unused,
636 bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900637{
638 pr_warning("Debuginfo-analysis is not supported.\n");
639 return -ENOSYS;
640}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400641#endif
642
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100643static int parse_line_num(char **ptr, int *val, const char *what)
644{
645 const char *start = *ptr;
646
647 errno = 0;
648 *val = strtol(*ptr, ptr, 0);
649 if (errno || *ptr == start) {
650 semantic_error("'%s' is not a valid number.\n", what);
651 return -EINVAL;
652 }
653 return 0;
654}
655
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100656/*
657 * Stuff 'lr' according to the line range described by 'arg'.
658 * The line range syntax is described by:
659 *
660 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900661 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100662 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400663int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500664{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900665 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100666 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100667
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100668 if (!name)
669 return -ENOMEM;
670
671 lr->start = 0;
672 lr->end = INT_MAX;
673
674 range = strchr(name, ':');
675 if (range) {
676 *range++ = '\0';
677
678 err = parse_line_num(&range, &lr->start, "start line");
679 if (err)
680 goto err;
681
682 if (*range == '+' || *range == '-') {
683 const char c = *range++;
684
685 err = parse_line_num(&range, &lr->end, "end line");
686 if (err)
687 goto err;
688
689 if (c == '+') {
690 lr->end += lr->start;
691 /*
692 * Adjust the number of lines here.
693 * If the number of lines == 1, the
694 * the end of line should be equal to
695 * the start of line.
696 */
697 lr->end--;
698 }
699 }
700
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400701 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100702
703 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400704 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500705 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400706 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100707 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400708 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100709 if (*range != '\0') {
710 semantic_error("Tailing with invalid str '%s'.\n", range);
711 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400712 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400713 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400714
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900715 file = strchr(name, '@');
716 if (file) {
717 *file = '\0';
718 lr->file = strdup(++file);
719 if (lr->file == NULL) {
720 err = -ENOMEM;
721 goto err;
722 }
723 lr->function = name;
724 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100725 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500726 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100727 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400728
729 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100730err:
731 free(name);
732 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500733}
734
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500735/* Check the name is good for event/group */
736static bool check_event_name(const char *name)
737{
738 if (!isalpha(*name) && *name != '_')
739 return false;
740 while (*++name != '\0') {
741 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
742 return false;
743 }
744 return true;
745}
746
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500747/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400748static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500749{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400750 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500751 char *ptr, *tmp;
752 char c, nc = 0;
753 /*
754 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500755 * perf probe [EVENT=]SRC[:LN|;PTN]
756 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500757 *
758 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500759 */
760
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500761 ptr = strpbrk(arg, ";=@+%");
762 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500763 *ptr = '\0';
764 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400765 if (strchr(arg, ':')) {
766 semantic_error("Group name is not supported yet.\n");
767 return -ENOTSUP;
768 }
769 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500770 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 "follow C symbol-naming rule.\n", arg);
772 return -EINVAL;
773 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400774 pev->event = strdup(arg);
775 if (pev->event == NULL)
776 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400777 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500778 arg = tmp;
779 }
780
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500781 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500782 if (ptr) {
783 nc = *ptr;
784 *ptr++ = '\0';
785 }
786
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400787 tmp = strdup(arg);
788 if (tmp == NULL)
789 return -ENOMEM;
790
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500791 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400792 if (strchr(tmp, '.')) /* File */
793 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500794 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400795 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500796
797 /* Parse other options */
798 while (ptr) {
799 arg = ptr;
800 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500801 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400802 pp->lazy_line = strdup(arg);
803 if (pp->lazy_line == NULL)
804 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500805 break;
806 }
807 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500808 if (ptr) {
809 nc = *ptr;
810 *ptr++ = '\0';
811 }
812 switch (c) {
813 case ':': /* Line number */
814 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400815 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500816 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400817 " in line number.\n");
818 return -EINVAL;
819 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500820 break;
821 case '+': /* Byte offset from a symbol */
822 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400823 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500824 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400825 " in offset.\n");
826 return -EINVAL;
827 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500828 break;
829 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400830 if (pp->file) {
831 semantic_error("SRC@SRC is not allowed.\n");
832 return -EINVAL;
833 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400834 pp->file = strdup(arg);
835 if (pp->file == NULL)
836 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500837 break;
838 case '%': /* Probe places */
839 if (strcmp(arg, "return") == 0) {
840 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400841 } else { /* Others not supported yet */
842 semantic_error("%%%s is not supported.\n", arg);
843 return -ENOTSUP;
844 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500845 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 default: /* Buggy case */
847 pr_err("This program has a bug at %s:%d.\n",
848 __FILE__, __LINE__);
849 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500850 break;
851 }
852 }
853
854 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900856 semantic_error("Lazy pattern can't be used with"
857 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400858 return -EINVAL;
859 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500860
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900862 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400863 return -EINVAL;
864 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500865
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400866 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900867 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400868 return -EINVAL;
869 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500870
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400871 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500872 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900873 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400874 return -EINVAL;
875 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500876
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400877 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900878 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400879 return -EINVAL;
880 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500881
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400882 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900883 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884 return -EINVAL;
885 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500886
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400887 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500888 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900889 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890 return -EINVAL;
891 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500892
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400893 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500894 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
895 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400896 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500897}
898
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400899/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400900static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400901{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400902 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400903 struct perf_probe_arg_field **fieldp;
904
905 pr_debug("parsing arg: %s into ", str);
906
Masami Hiramatsu48481932010-04-12 13:16:53 -0400907 tmp = strchr(str, '=');
908 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400909 arg->name = strndup(str, tmp - str);
910 if (arg->name == NULL)
911 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400912 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400913 str = tmp + 1;
914 }
915
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400916 tmp = strchr(str, ':');
917 if (tmp) { /* Type setting */
918 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400919 arg->type = strdup(tmp + 1);
920 if (arg->type == NULL)
921 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400922 pr_debug("type:%s ", arg->type);
923 }
924
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400925 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400926 if (!is_c_varname(str) || !tmp) {
927 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400928 arg->var = strdup(str);
929 if (arg->var == NULL)
930 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400931 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400932 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400933 }
934
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400935 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400936 arg->var = strndup(str, tmp - str);
937 if (arg->var == NULL)
938 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400939 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400940 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400941 fieldp = &arg->field;
942
943 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400944 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
945 if (*fieldp == NULL)
946 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400947 if (*tmp == '[') { /* Array */
948 str = tmp;
949 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400950 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400951 if (*tmp != ']' || tmp == str + 1) {
952 semantic_error("Array index must be a"
953 " number.\n");
954 return -EINVAL;
955 }
956 tmp++;
957 if (*tmp == '\0')
958 tmp = NULL;
959 } else { /* Structure */
960 if (*tmp == '.') {
961 str = tmp + 1;
962 (*fieldp)->ref = false;
963 } else if (tmp[1] == '>') {
964 str = tmp + 2;
965 (*fieldp)->ref = true;
966 } else {
967 semantic_error("Argument parse error: %s\n",
968 str);
969 return -EINVAL;
970 }
971 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400973 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400974 (*fieldp)->name = strndup(str, tmp - str);
975 if ((*fieldp)->name == NULL)
976 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400977 if (*str != '[')
978 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400979 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
980 fieldp = &(*fieldp)->next;
981 }
982 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400983 (*fieldp)->name = strdup(str);
984 if ((*fieldp)->name == NULL)
985 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400986 if (*str != '[')
987 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400988 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400989
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400990 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400991 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400992 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400993 if (arg->name == NULL)
994 return -ENOMEM;
995 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400996 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400997}
998
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400999/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001000int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001001{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001002 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001003 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001004
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001005 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001006 if (!argv) {
1007 pr_debug("Failed to split arguments.\n");
1008 return -ENOMEM;
1009 }
1010 if (argc - 1 > MAX_PROBE_ARGS) {
1011 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1012 ret = -ERANGE;
1013 goto out;
1014 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001015 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001016 ret = parse_perf_probe_point(argv[0], pev);
1017 if (ret < 0)
1018 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001019
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001020 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001021 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001022 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1023 if (pev->args == NULL) {
1024 ret = -ENOMEM;
1025 goto out;
1026 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001027 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1028 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1029 if (ret >= 0 &&
1030 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001031 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001032 " kretprobe.\n");
1033 ret = -EINVAL;
1034 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001035 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001036out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001037 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038
1039 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001040}
1041
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001042/* Return true if this perf_probe_event requires debuginfo */
1043bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001044{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001045 int i;
1046
1047 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1048 return true;
1049
1050 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001051 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001052 return true;
1053
1054 return false;
1055}
1056
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301057/* Parse probe_events event into struct probe_point */
1058static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001059 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001060{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301061 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001062 char pr;
1063 char *p;
1064 int ret, i, argc;
1065 char **argv;
1066
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301067 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001068 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001069 if (!argv) {
1070 pr_debug("Failed to split arguments.\n");
1071 return -ENOMEM;
1072 }
1073 if (argc < 2) {
1074 semantic_error("Too few probe arguments.\n");
1075 ret = -ERANGE;
1076 goto out;
1077 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001078
1079 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +08001080 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001081 &pr, (float *)(void *)&tev->group,
1082 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001083 if (ret != 3) {
1084 semantic_error("Failed to parse event name: %s\n", argv[0]);
1085 ret = -EINVAL;
1086 goto out;
1087 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001088 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001089
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001090 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001091
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001092 /* Scan module name(if there), function name and offset */
1093 p = strchr(argv[1], ':');
1094 if (p) {
1095 tp->module = strndup(argv[1], p - argv[1]);
1096 p++;
1097 } else
1098 p = argv[1];
1099 ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001100 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001101 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001102 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001103
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001104 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301105 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001106 if (tev->args == NULL) {
1107 ret = -ENOMEM;
1108 goto out;
1109 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001110 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001111 p = strchr(argv[i + 2], '=');
1112 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001113 *p++ = '\0';
1114 else
1115 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001116 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001117 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001118 tev->args[i].value = strdup(p);
1119 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1120 ret = -ENOMEM;
1121 goto out;
1122 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001123 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001124 ret = 0;
1125out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001126 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001127 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001128}
1129
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001130/* Compose only probe arg */
1131int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1132{
1133 struct perf_probe_arg_field *field = pa->field;
1134 int ret;
1135 char *tmp = buf;
1136
Masami Hiramatsu48481932010-04-12 13:16:53 -04001137 if (pa->name && pa->var)
1138 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1139 else
1140 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001141 if (ret <= 0)
1142 goto error;
1143 tmp += ret;
1144 len -= ret;
1145
1146 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001147 if (field->name[0] == '[')
1148 ret = e_snprintf(tmp, len, "%s", field->name);
1149 else
1150 ret = e_snprintf(tmp, len, "%s%s",
1151 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001152 if (ret <= 0)
1153 goto error;
1154 tmp += ret;
1155 len -= ret;
1156 field = field->next;
1157 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001158
1159 if (pa->type) {
1160 ret = e_snprintf(tmp, len, ":%s", pa->type);
1161 if (ret <= 0)
1162 goto error;
1163 tmp += ret;
1164 len -= ret;
1165 }
1166
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001167 return tmp - buf;
1168error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001169 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 strerror(-ret));
1171 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001172}
1173
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001174/* Compose only probe point (not argument) */
1175static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001176{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001177 char *buf, *tmp;
1178 char offs[32] = "", line[32] = "", file[32] = "";
1179 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001180
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001181 buf = zalloc(MAX_CMDLEN);
1182 if (buf == NULL) {
1183 ret = -ENOMEM;
1184 goto error;
1185 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001187 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001188 if (ret <= 0)
1189 goto error;
1190 }
1191 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001192 ret = e_snprintf(line, 32, ":%d", pp->line);
1193 if (ret <= 0)
1194 goto error;
1195 }
1196 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001197 tmp = pp->file;
1198 len = strlen(tmp);
1199 if (len > 30) {
1200 tmp = strchr(pp->file + len - 30, '/');
1201 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1202 }
1203 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001204 if (ret <= 0)
1205 goto error;
1206 }
1207
1208 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001209 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1210 offs, pp->retprobe ? "%return" : "", line,
1211 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001212 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001213 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001214 if (ret <= 0)
1215 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001216
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001217 return buf;
1218error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001219 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001221 if (buf)
1222 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001224}
1225
1226#if 0
1227char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1228{
1229 char *buf;
1230 int i, len, ret;
1231
1232 buf = synthesize_perf_probe_point(&pev->point);
1233 if (!buf)
1234 return NULL;
1235
1236 len = strlen(buf);
1237 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001238 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001239 pev->args[i].name);
1240 if (ret <= 0) {
1241 free(buf);
1242 return NULL;
1243 }
1244 len += ret;
1245 }
1246
1247 return buf;
1248}
1249#endif
1250
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301251static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252 char **buf, size_t *buflen,
1253 int depth)
1254{
1255 int ret;
1256 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301257 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001258 buflen, depth + 1);
1259 if (depth < 0)
1260 goto out;
1261 }
1262
1263 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1264 if (ret < 0)
1265 depth = ret;
1266 else {
1267 *buf += ret;
1268 *buflen -= ret;
1269 }
1270out:
1271 return depth;
1272
1273}
1274
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301275static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001276 char *buf, size_t buflen)
1277{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301278 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001279 int ret, depth = 0;
1280 char *tmp = buf;
1281
1282 /* Argument name or separator */
1283 if (arg->name)
1284 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1285 else
1286 ret = e_snprintf(buf, buflen, " ");
1287 if (ret < 0)
1288 return ret;
1289 buf += ret;
1290 buflen -= ret;
1291
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001292 /* Special case: @XXX */
1293 if (arg->value[0] == '@' && arg->ref)
1294 ref = ref->next;
1295
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001296 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001297 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301298 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001299 &buflen, 1);
1300 if (depth < 0)
1301 return depth;
1302 }
1303
1304 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001305 if (arg->value[0] == '@' && arg->ref)
1306 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1307 arg->ref->offset);
1308 else
1309 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001310 if (ret < 0)
1311 return ret;
1312 buf += ret;
1313 buflen -= ret;
1314
1315 /* Closing */
1316 while (depth--) {
1317 ret = e_snprintf(buf, buflen, ")");
1318 if (ret < 0)
1319 return ret;
1320 buf += ret;
1321 buflen -= ret;
1322 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001323 /* Print argument type */
1324 if (arg->type) {
1325 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1326 if (ret <= 0)
1327 return ret;
1328 buf += ret;
1329 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001330
1331 return buf - tmp;
1332}
1333
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301334char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001335{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301336 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001337 char *buf;
1338 int i, len, ret;
1339
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001340 buf = zalloc(MAX_CMDLEN);
1341 if (buf == NULL)
1342 return NULL;
1343
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001344 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001345 tp->retprobe ? 'r' : 'p',
1346 tev->group, tev->event,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001347 tp->module ?: "", tp->module ? ":" : "",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001348 tp->symbol, tp->offset);
1349 if (len <= 0)
1350 goto error;
1351
1352 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301353 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001354 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001355 if (ret <= 0)
1356 goto error;
1357 len += ret;
1358 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001359
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001360 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001361error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001362 free(buf);
1363 return NULL;
1364}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001365
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301366static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001367 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001368{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001369 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001371
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001372 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001373 pev->event = strdup(tev->event);
1374 pev->group = strdup(tev->group);
1375 if (pev->event == NULL || pev->group == NULL)
1376 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001377
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001378 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301379 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 if (ret < 0)
1381 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001382
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001383 /* Convert trace_arg to probe_arg */
1384 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001385 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1386 if (pev->args == NULL)
1387 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001388 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001389 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001390 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301392 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001394 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001395 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001396 if (pev->args[i].name == NULL && ret >= 0)
1397 ret = -ENOMEM;
1398 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399
1400 if (ret < 0)
1401 clear_perf_probe_event(pev);
1402
1403 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404}
1405
1406void clear_perf_probe_event(struct perf_probe_event *pev)
1407{
1408 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001409 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001410 int i;
1411
1412 if (pev->event)
1413 free(pev->event);
1414 if (pev->group)
1415 free(pev->group);
1416 if (pp->file)
1417 free(pp->file);
1418 if (pp->function)
1419 free(pp->function);
1420 if (pp->lazy_line)
1421 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001422 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001423 if (pev->args[i].name)
1424 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001425 if (pev->args[i].var)
1426 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001427 if (pev->args[i].type)
1428 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001429 field = pev->args[i].field;
1430 while (field) {
1431 next = field->next;
1432 if (field->name)
1433 free(field->name);
1434 free(field);
1435 field = next;
1436 }
1437 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001438 if (pev->args)
1439 free(pev->args);
1440 memset(pev, 0, sizeof(*pev));
1441}
1442
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301443static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001444{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301445 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001446 int i;
1447
1448 if (tev->event)
1449 free(tev->event);
1450 if (tev->group)
1451 free(tev->group);
1452 if (tev->point.symbol)
1453 free(tev->point.symbol);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001454 if (tev->point.module)
1455 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001456 for (i = 0; i < tev->nargs; i++) {
1457 if (tev->args[i].name)
1458 free(tev->args[i].name);
1459 if (tev->args[i].value)
1460 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001461 if (tev->args[i].type)
1462 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001463 ref = tev->args[i].ref;
1464 while (ref) {
1465 next = ref->next;
1466 free(ref);
1467 ref = next;
1468 }
1469 }
1470 if (tev->args)
1471 free(tev->args);
1472 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001473}
1474
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001475static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001476{
1477 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001478 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001479 int ret;
1480
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001481 __debugfs = debugfs_find_mountpoint();
1482 if (__debugfs == NULL) {
1483 pr_warning("Debugfs is not mounted.\n");
1484 return -ENOENT;
1485 }
1486
1487 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001489 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001490 if (readwrite && !probe_event_dry_run)
1491 ret = open(buf, O_RDWR, O_APPEND);
1492 else
1493 ret = open(buf, O_RDONLY, 0);
1494 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001495
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001496 if (ret < 0) {
1497 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001498 pr_warning("kprobe_events file does not exist - please"
1499 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001500 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501 pr_warning("Failed to open kprobe_events file: %s\n",
1502 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001503 }
1504 return ret;
1505}
1506
1507/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301508static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001509{
1510 int ret, idx;
1511 FILE *fp;
1512 char buf[MAX_CMDLEN];
1513 char *p;
1514 struct strlist *sl;
1515
1516 sl = strlist__new(true, NULL);
1517
1518 fp = fdopen(dup(fd), "r");
1519 while (!feof(fp)) {
1520 p = fgets(buf, MAX_CMDLEN, fp);
1521 if (!p)
1522 break;
1523
1524 idx = strlen(p) - 1;
1525 if (p[idx] == '\n')
1526 p[idx] = '\0';
1527 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 if (ret < 0) {
1529 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1530 strlist__delete(sl);
1531 return NULL;
1532 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001533 }
1534 fclose(fp);
1535
1536 return sl;
1537}
1538
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001539/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001540static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001541{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001542 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001543 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001545
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 /* Synthesize only event probe point */
1547 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548 if (!place)
1549 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001550
1551 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001552 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001553 return ret;
1554
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001555 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001556
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001557 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001558 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001559 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001560 ret = synthesize_perf_probe_arg(&pev->args[i],
1561 buf, 128);
1562 if (ret < 0)
1563 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001564 printf(" %s", buf);
1565 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001566 }
1567 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001568 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001569 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001570}
1571
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001572/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001573int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001574{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001575 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301576 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001578 struct strlist *rawlist;
1579 struct str_node *ent;
1580
Masami Hiramatsu72041332010-01-05 17:47:10 -05001581 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 ret = init_vmlinux();
1583 if (ret < 0)
1584 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001585
1586 memset(&tev, 0, sizeof(tev));
1587 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001588
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001589 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001590 if (fd < 0)
1591 return fd;
1592
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301593 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001594 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001595 if (!rawlist)
1596 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001597
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001598 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301599 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001600 if (ret >= 0) {
1601 ret = convert_to_perf_probe_event(&tev, &pev);
1602 if (ret >= 0)
1603 ret = show_perf_probe_event(&pev);
1604 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301606 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001607 if (ret < 0)
1608 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001609 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001610 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001611
1612 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001613}
1614
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001615/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301616static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001617{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001618 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001619 struct strlist *sl, *rawlist;
1620 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301621 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001622 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001623
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001624 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301625 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001626 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001627 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301628 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001629 if (ret < 0)
1630 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001631 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001632 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1633 tev.event);
1634 if (ret >= 0)
1635 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001636 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001637 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301638 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001639 if (ret < 0)
1640 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001641 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001642 strlist__delete(rawlist);
1643
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001644 if (ret < 0) {
1645 strlist__delete(sl);
1646 return NULL;
1647 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001648 return sl;
1649}
1650
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001652{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001653 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301654 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001655
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001656 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301657 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001658 return -EINVAL;
1659 }
1660
Masami Hiramatsufa282442009-12-08 17:03:23 -05001661 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001662 if (!probe_event_dry_run) {
1663 ret = write(fd, buf, strlen(buf));
1664 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001665 pr_warning("Failed to write event: %s\n",
1666 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001667 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001668 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001669 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001670}
1671
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001672static int get_new_event_name(char *buf, size_t len, const char *base,
1673 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001674{
1675 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001676
1677 /* Try no suffix */
1678 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001679 if (ret < 0) {
1680 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1681 return ret;
1682 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001683 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001684 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001685
Masami Hiramatsud761b082009-12-15 10:32:25 -05001686 if (!allow_suffix) {
1687 pr_warning("Error: event \"%s\" already exists. "
1688 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001689 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001690 }
1691
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001692 /* Try to add suffix */
1693 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001694 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001695 if (ret < 0) {
1696 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1697 return ret;
1698 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001699 if (!strlist__has_entry(namelist, buf))
1700 break;
1701 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001702 if (i == MAX_EVENT_INDEX) {
1703 pr_warning("Too many events are on the same function.\n");
1704 ret = -ERANGE;
1705 }
1706
1707 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001708}
1709
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301710static int __add_probe_trace_events(struct perf_probe_event *pev,
1711 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001712 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001713{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001714 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301715 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716 char buf[64];
1717 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001718 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001719
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001720 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001721 if (fd < 0)
1722 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001723 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301724 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001725 if (!namelist) {
1726 pr_debug("Failed to get current event list.\n");
1727 return -EIO;
1728 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001729
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001730 ret = 0;
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301731 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001732 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 tev = &tevs[i];
1734 if (pev->event)
1735 event = pev->event;
1736 else
1737 if (pev->point.function)
1738 event = pev->point.function;
1739 else
1740 event = tev->point.symbol;
1741 if (pev->group)
1742 group = pev->group;
1743 else
1744 group = PERFPROBE_GROUP;
1745
1746 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001747 ret = get_new_event_name(buf, 64, event,
1748 namelist, allow_suffix);
1749 if (ret < 0)
1750 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001751 event = buf;
1752
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001753 tev->event = strdup(event);
1754 tev->group = strdup(group);
1755 if (tev->event == NULL || tev->group == NULL) {
1756 ret = -ENOMEM;
1757 break;
1758 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301759 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001760 if (ret < 0)
1761 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762 /* Add added event name to namelist */
1763 strlist__add(namelist, event);
1764
1765 /* Trick here - save current event/group */
1766 event = pev->event;
1767 group = pev->group;
1768 pev->event = tev->event;
1769 pev->group = tev->group;
1770 show_perf_probe_event(pev);
1771 /* Trick here - restore current event/group */
1772 pev->event = (char *)event;
1773 pev->group = (char *)group;
1774
1775 /*
1776 * Probes after the first probe which comes from same
1777 * user input are always allowed to add suffix, because
1778 * there might be several addresses corresponding to
1779 * one code line.
1780 */
1781 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001782 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783
1784 if (ret >= 0) {
1785 /* Show how to use the event. */
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301786 printf("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001787 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1788 tev->event);
1789 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001790
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001791 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001792 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001793 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001794}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001795
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301796static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1797 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301798 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001799{
1800 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001801 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301802 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001803
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001804 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301805 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001806 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001807 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001808
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001809 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301810 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001811 if (tev == NULL)
1812 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001813
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001814 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001815 tev->point.symbol = strdup(pev->point.function);
1816 if (tev->point.symbol == NULL) {
1817 ret = -ENOMEM;
1818 goto error;
1819 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001820
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301821 if (target) {
1822 tev->point.module = strdup(target);
Jovi Zhangce27a442011-07-25 22:08:08 +08001823 if (tev->point.module == NULL) {
1824 ret = -ENOMEM;
1825 goto error;
1826 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001827 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001828
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001829 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001830 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831 tev->nargs = pev->nargs;
1832 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301833 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001834 * tev->nargs);
1835 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 ret = -ENOMEM;
1837 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001838 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001839 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001840 if (pev->args[i].name) {
1841 tev->args[i].name = strdup(pev->args[i].name);
1842 if (tev->args[i].name == NULL) {
1843 ret = -ENOMEM;
1844 goto error;
1845 }
1846 }
1847 tev->args[i].value = strdup(pev->args[i].var);
1848 if (tev->args[i].value == NULL) {
1849 ret = -ENOMEM;
1850 goto error;
1851 }
1852 if (pev->args[i].type) {
1853 tev->args[i].type = strdup(pev->args[i].type);
1854 if (tev->args[i].type == NULL) {
1855 ret = -ENOMEM;
1856 goto error;
1857 }
1858 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001859 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001860 }
1861
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001862 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001863 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001864 if (!sym) {
1865 pr_warning("Kernel symbol \'%s\' not found.\n",
1866 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001867 ret = -ENOENT;
1868 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05301869 } else if (tev->point.offset > sym->end - sym->start) {
1870 pr_warning("Offset specified is greater than size of %s\n",
1871 tev->point.symbol);
1872 ret = -ENOENT;
1873 goto error;
1874
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001875 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001876
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001877 return 1;
1878error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301879 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001880 free(tev);
1881 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001882 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883}
1884
1885struct __event_package {
1886 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301887 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888 int ntevs;
1889};
1890
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001891int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301892 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001894 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001895 struct __event_package *pkgs;
1896
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001897 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1898 if (pkgs == NULL)
1899 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900
1901 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001902 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001903 if (ret < 0) {
1904 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001905 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001906 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001907
1908 /* Loop 1: convert all events */
1909 for (i = 0; i < npevs; i++) {
1910 pkgs[i].pev = &pevs[i];
1911 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301912 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001913 &pkgs[i].tevs,
1914 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05301915 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001916 if (ret < 0)
1917 goto end;
1918 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001919 }
1920
1921 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001922 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301923 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001924 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001925 if (ret < 0)
1926 break;
1927 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001928end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001929 /* Loop 3: cleanup and free trace events */
1930 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001931 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301932 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001933 free(pkgs[i].tevs);
1934 }
1935 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001936
1937 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001938}
1939
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301940static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001941{
1942 char *p;
1943 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001944 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001945
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301946 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001947 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1948 if (ret < 0)
1949 goto error;
1950
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001951 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001952 if (!p) {
1953 pr_debug("Internal error: %s should have ':' but not.\n",
1954 ent->s);
1955 ret = -ENOTSUP;
1956 goto error;
1957 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001958 *p = '/';
1959
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001960 pr_debug("Writing event: %s\n", buf);
1961 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09001962 if (ret < 0) {
1963 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001964 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09001965 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001966
Srikar Dronamrajua844d1e2012-01-20 17:43:54 +05301967 printf("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001968 return 0;
1969error:
1970 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1971 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001972}
1973
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301974static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001975 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001976{
1977 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001978 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001979 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001980
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001981 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1982 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001983 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001984 return ret;
1985 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001986
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001987 if (strpbrk(buf, "*?")) { /* Glob-exp */
1988 strlist__for_each_safe(ent, n, namelist)
1989 if (strglobmatch(ent->s, buf)) {
1990 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301991 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001992 if (ret < 0)
1993 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001994 strlist__remove(namelist, ent);
1995 }
1996 } else {
1997 ent = strlist__find(namelist, buf);
1998 if (ent) {
1999 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302000 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002001 if (ret >= 0)
2002 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002003 }
2004 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002005 if (found == 0 && ret >= 0)
2006 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2007
2008 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002009}
2010
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002011int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002012{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002013 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002014 const char *group, *event;
2015 char *p, *str;
2016 struct str_node *ent;
2017 struct strlist *namelist;
2018
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002019 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002020 if (fd < 0)
2021 return fd;
2022
Masami Hiramatsufa282442009-12-08 17:03:23 -05002023 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302024 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002025 if (namelist == NULL)
2026 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002027
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002028 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002029 str = strdup(ent->s);
2030 if (str == NULL) {
2031 ret = -ENOMEM;
2032 break;
2033 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002034 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002035 p = strchr(str, ':');
2036 if (p) {
2037 group = str;
2038 *p = '\0';
2039 event = p + 1;
2040 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002041 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002042 event = str;
2043 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002044 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302045 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002046 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002047 if (ret < 0)
2048 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002049 }
2050 strlist__delete(namelist);
2051 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002052
2053 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002054}
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002055/* TODO: don't use a global variable for filter ... */
2056static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002057
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002058/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002059 * If a symbol corresponds to a function with global binding and
2060 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002061 */
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002062static int filter_available_functions(struct map *map __unused,
2063 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002064{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002065 if (sym->binding == STB_GLOBAL &&
2066 strfilter__compare(available_func_filter, sym->name))
2067 return 0;
2068 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002069}
2070
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302071int show_available_funcs(const char *target, struct strfilter *_filter)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002072{
2073 struct map *map;
2074 int ret;
2075
2076 setup_pager();
2077
2078 ret = init_vmlinux();
2079 if (ret < 0)
2080 return ret;
2081
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302082 map = kernel_get_module_map(target);
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002083 if (!map) {
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302084 pr_err("Failed to find %s map.\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002085 return -EINVAL;
2086 }
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002087 available_func_filter = _filter;
2088 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002089 pr_err("Failed to load map.\n");
2090 return -EINVAL;
2091 }
2092 if (!dso__sorted_by_name(map->dso, map->type))
2093 dso__sort_by_name(map->dso, map->type);
2094
2095 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2096 return 0;
2097}