blob: e33554a562b36fb59c95d935258d525a0e810dac [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 Hiramatsue1c01d62009-11-30 19:20:05 -050037#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050038#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050039#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050040#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050041#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040042#include "symbol.h"
43#include "thread.h"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040044#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030045#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048
49#define MAX_CMDLEN 256
50#define MAX_PROBE_ARGS 128
51#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu146a1432010-04-12 13:17:42 -040055#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050056
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030073static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030074static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090076/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040077static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079 int ret;
80
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040081 symbol_conf.sort_by_name = true;
82 if (symbol_conf.vmlinux_name == NULL)
83 symbol_conf.try_vmlinux_path = true;
84 else
85 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040086 ret = symbol__init();
87 if (ret < 0) {
88 pr_debug("Failed to init symbol map.\n");
89 goto out;
90 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040091
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090092 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030093 if (ret < 0)
94 goto out;
95
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090096 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090097 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090098 goto out;
99 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400100out:
101 if (ret < 0)
102 pr_warning("Failed to init vmlinux path.\n");
103 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400104}
105
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900106static struct symbol *__find_kernel_function_by_name(const char *name,
107 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400108{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900109 return machine__find_kernel_function_by_name(&machine, name, mapp,
110 NULL);
111}
112
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900113static struct map *kernel_get_module_map(const char *module)
114{
115 struct rb_node *nd;
116 struct map_groups *grp = &machine.kmaps;
117
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900118 /* A file path -- this is an offline module */
119 if (module && strchr(module, '/'))
120 return machine__new_module(&machine, 0, module);
121
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900122 if (!module)
123 module = "kernel";
124
125 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
126 struct map *pos = rb_entry(nd, struct map, rb_node);
127 if (strncmp(pos->dso->short_name + 1, module,
128 pos->dso->short_name_len - 2) == 0) {
129 return pos;
130 }
131 }
132 return NULL;
133}
134
135static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900136{
137 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100138 struct map *map;
139 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900140
141 if (module) {
142 list_for_each_entry(dso, &machine.kernel_dsos, node) {
143 if (strncmp(dso->short_name + 1, module,
144 dso->short_name_len - 2) == 0)
145 goto found;
146 }
147 pr_debug("Failed to find module %s.\n", module);
148 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100149 }
150
151 map = machine.vmlinux_maps[MAP__FUNCTION];
152 dso = map->dso;
153
154 vmlinux_name = symbol_conf.vmlinux_name;
155 if (vmlinux_name) {
156 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
157 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900158 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100159 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900160 pr_debug("Failed to load kernel map.\n");
161 return NULL;
162 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400163 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900164found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900165 return dso;
166}
167
168const char *kernel_get_module_path(const char *module)
169{
170 struct dso *dso = kernel_get_module_dso(module);
171 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900172}
173
174#ifdef DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900175/* Open new debuginfo of given module */
176static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900177{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900178 const char *path;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900179
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900180 /* A file path -- this is an offline module */
181 if (module && strchr(module, '/'))
182 path = module;
183 else {
184 path = kernel_get_module_path(module);
185
186 if (!path) {
187 pr_err("Failed to find path of %s module.\n",
188 module ?: "kernel");
189 return NULL;
190 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900191 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900192 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400193}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300194
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530195/*
196 * Convert trace point to probe point with debuginfo
197 * Currently only handles kprobes.
198 */
199static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900200 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300201{
202 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900203 struct map *map;
204 u64 addr;
205 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900206 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300207
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900208 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300209 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900210 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200211 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900212 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900213
214 dinfo = debuginfo__new_online_kernel(addr);
215 if (dinfo) {
216 ret = debuginfo__find_probe_point(dinfo,
217 (unsigned long)addr, pp);
218 debuginfo__delete(dinfo);
219 } else {
220 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
221 addr);
222 ret = -ENOENT;
223 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300224 }
225 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400226 pr_debug("Failed to find corresponding probes from "
227 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400228 pp->function = strdup(tp->symbol);
229 if (pp->function == NULL)
230 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300231 pp->offset = tp->offset;
232 }
233 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400234
235 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300236}
237
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900238static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
239 int ntevs, const char *module)
240{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900241 int i, ret = 0;
242 char *tmp;
243
244 if (!module)
245 return 0;
246
247 tmp = strrchr(module, '/');
248 if (tmp) {
249 /* This is a module path -- get the module name */
250 module = strdup(tmp + 1);
251 if (!module)
252 return -ENOMEM;
253 tmp = strchr(module, '.');
254 if (tmp)
255 *tmp = '\0';
256 tmp = (char *)module; /* For free() */
257 }
258
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900259 for (i = 0; i < ntevs; i++) {
260 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900261 if (!tevs[i].point.module) {
262 ret = -ENOMEM;
263 break;
264 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900265 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900266
267 if (tmp)
268 free(tmp);
269
270 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900271}
272
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300273/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530274static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900275 struct probe_trace_event **tevs,
276 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300277{
278 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900279 struct debuginfo *dinfo = open_debuginfo(module);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900280 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300281
Masami Hiramatsuff741782011-06-27 16:27:39 +0900282 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400283 if (need_dwarf) {
284 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900285 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400286 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900287 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300288 return 0;
289 }
290
Masami Hiramatsuff741782011-06-27 16:27:39 +0900291 /* Searching trace events corresponding to a probe event */
292 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
293
294 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300295
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400296 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530297 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900298 if (module)
299 ret = add_module_to_probe_trace_events(*tevs, ntevs,
300 module);
301 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400302 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300303
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400304 if (ntevs == 0) { /* No error but failed to find probe point. */
305 pr_warning("Probe point '%s' not found.\n",
306 synthesize_perf_probe_point(&pev->point));
307 return -ENOENT;
308 }
309 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400310 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
311 if (ntevs == -EBADF) {
312 pr_warning("Warning: No dwarf info found in the vmlinux - "
313 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
314 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900315 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400316 return 0;
317 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300318 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400319 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300320}
321
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900322/*
323 * Find a src file from a DWARF tag path. Prepend optional source path prefix
324 * and chop off leading directories that do not exist. Result is passed back as
325 * a newly allocated path on success.
326 * Return 0 if file was found and readable, -errno otherwise.
327 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900328static int get_real_path(const char *raw_path, const char *comp_dir,
329 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900330{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900331 const char *prefix = symbol_conf.source_prefix;
332
333 if (!prefix) {
334 if (raw_path[0] != '/' && comp_dir)
335 /* If not an absolute path, try to use comp_dir */
336 prefix = comp_dir;
337 else {
338 if (access(raw_path, R_OK) == 0) {
339 *new_path = strdup(raw_path);
340 return 0;
341 } else
342 return -errno;
343 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900344 }
345
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900346 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900347 if (!*new_path)
348 return -ENOMEM;
349
350 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900351 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900352
353 if (access(*new_path, R_OK) == 0)
354 return 0;
355
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900356 if (!symbol_conf.source_prefix)
357 /* In case of searching comp_dir, don't retry */
358 return -errno;
359
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900360 switch (errno) {
361 case ENAMETOOLONG:
362 case ENOENT:
363 case EROFS:
364 case EFAULT:
365 raw_path = strchr(++raw_path, '/');
366 if (!raw_path) {
367 free(*new_path);
368 *new_path = NULL;
369 return -ENOENT;
370 }
371 continue;
372
373 default:
374 free(*new_path);
375 *new_path = NULL;
376 return -errno;
377 }
378 }
379}
380
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300381#define LINEBUF_SIZE 256
382#define NR_ADDITIONAL_LINES 2
383
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100384static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300385{
386 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100387 const char *color = show_num ? "" : PERF_COLOR_BLUE;
388 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300389
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100390 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300391 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
392 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100393 if (skip)
394 continue;
395 if (!prefix) {
396 prefix = show_num ? "%7d " : " ";
397 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300398 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100399 color_fprintf(stdout, color, "%s", buf);
400
401 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400402
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100403 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300404error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100405 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100406 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100407 return -1;
408 }
409 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300410}
411
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100412static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
413{
414 int rv = __show_one_line(fp, l, skip, show_num);
415 if (rv == 0) {
416 pr_warning("Source file is shorter than expected.\n");
417 rv = -1;
418 }
419 return rv;
420}
421
422#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
423#define show_one_line(f,l) _show_one_line(f,l,false,false)
424#define skip_one_line(f,l) _show_one_line(f,l,true,false)
425#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
426
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300427/*
428 * Show line-range always requires debuginfo to find source file and
429 * line number.
430 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900431int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300432{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400433 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300434 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900435 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300436 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900437 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900438 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300439
440 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400441 ret = init_vmlinux();
442 if (ret < 0)
443 return ret;
444
Masami Hiramatsuff741782011-06-27 16:27:39 +0900445 dinfo = open_debuginfo(module);
446 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400447 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900448 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400449 }
450
Masami Hiramatsuff741782011-06-27 16:27:39 +0900451 ret = debuginfo__find_line_range(dinfo, lr);
452 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400453 if (ret == 0) {
454 pr_warning("Specified source line is not found.\n");
455 return -ENOENT;
456 } else if (ret < 0) {
457 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
458 return ret;
459 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300460
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900461 /* Convert source file path */
462 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900463 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900464 free(tmp); /* Free old path */
465 if (ret < 0) {
466 pr_warning("Failed to find source file. (%d)\n", ret);
467 return ret;
468 }
469
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300470 setup_pager();
471
472 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900473 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300474 lr->start - lr->offset);
475 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100476 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300477
478 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400479 if (fp == NULL) {
480 pr_warning("Failed to open %s: %s\n", lr->path,
481 strerror(errno));
482 return -errno;
483 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300484 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100485 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100486 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100487 if (ret < 0)
488 goto end;
489 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300490
491 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100492 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100493 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100494 if (ret < 0)
495 goto end;
496 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100497 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400498 if (ret < 0)
499 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300500 }
501
502 if (lr->end == INT_MAX)
503 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100504 while (l <= lr->end) {
505 ret = show_one_line_or_eof(fp, l++ - lr->offset);
506 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100507 break;
508 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400509end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300510 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400511 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300512}
513
Masami Hiramatsuff741782011-06-27 16:27:39 +0900514static int show_available_vars_at(struct debuginfo *dinfo,
515 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900516 int max_vls, struct strfilter *_filter,
517 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900518{
519 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900520 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900521 struct str_node *node;
522 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900523 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900524
525 buf = synthesize_perf_probe_point(&pev->point);
526 if (!buf)
527 return -EINVAL;
528 pr_debug("Searching variables at %s\n", buf);
529
Masami Hiramatsuff741782011-06-27 16:27:39 +0900530 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
531 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900532 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900533 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900534 goto end;
535 }
536 /* Some variables are found */
537 fprintf(stdout, "Available variables at %s\n", buf);
538 for (i = 0; i < ret; i++) {
539 vl = &vls[i];
540 /*
541 * A probe point might be converted to
542 * several trace points.
543 */
544 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
545 vl->point.offset);
546 free(vl->point.symbol);
547 nvars = 0;
548 if (vl->vars) {
549 strlist__for_each(node, vl->vars) {
550 var = strchr(node->s, '\t') + 1;
551 if (strfilter__compare(_filter, var)) {
552 fprintf(stdout, "\t\t%s\n", node->s);
553 nvars++;
554 }
555 }
556 strlist__delete(vl->vars);
557 }
558 if (nvars == 0)
559 fprintf(stdout, "\t\t(No matched variables)\n");
560 }
561 free(vls);
562end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900563 free(buf);
564 return ret;
565}
566
567/* Show available variables on given probe point */
568int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900569 int max_vls, const char *module,
570 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900571{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900572 int i, ret = 0;
573 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900574
575 ret = init_vmlinux();
576 if (ret < 0)
577 return ret;
578
Masami Hiramatsuff741782011-06-27 16:27:39 +0900579 dinfo = open_debuginfo(module);
580 if (!dinfo) {
581 pr_warning("Failed to open debuginfo file.\n");
582 return -ENOENT;
583 }
584
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900585 setup_pager();
586
Masami Hiramatsuff741782011-06-27 16:27:39 +0900587 for (i = 0; i < npevs && ret >= 0; i++)
588 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900589 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900590
591 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900592 return ret;
593}
594
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300595#else /* !DWARF_SUPPORT */
596
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530597static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900598 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300599{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900600 struct symbol *sym;
601
602 sym = __find_kernel_function_by_name(tp->symbol, NULL);
603 if (!sym) {
604 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
605 return -ENOENT;
606 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400607 pp->function = strdup(tp->symbol);
608 if (pp->function == NULL)
609 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300610 pp->offset = tp->offset;
611 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400612
613 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300614}
615
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530616static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
617 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900618 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300619{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400620 if (perf_probe_event_need_dwarf(pev)) {
621 pr_warning("Debuginfo-analysis is not supported.\n");
622 return -ENOSYS;
623 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300624 return 0;
625}
626
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900627int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300628{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629 pr_warning("Debuginfo-analysis is not supported.\n");
630 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300631}
632
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900633int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900634 int npevs __unused, int max_vls __unused,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900635 const char *module __unused,
636 struct strfilter *filter __unused,
637 bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900638{
639 pr_warning("Debuginfo-analysis is not supported.\n");
640 return -ENOSYS;
641}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400642#endif
643
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100644static int parse_line_num(char **ptr, int *val, const char *what)
645{
646 const char *start = *ptr;
647
648 errno = 0;
649 *val = strtol(*ptr, ptr, 0);
650 if (errno || *ptr == start) {
651 semantic_error("'%s' is not a valid number.\n", what);
652 return -EINVAL;
653 }
654 return 0;
655}
656
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100657/*
658 * Stuff 'lr' according to the line range described by 'arg'.
659 * The line range syntax is described by:
660 *
661 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900662 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100663 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400664int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500665{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900666 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100667 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100668
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100669 if (!name)
670 return -ENOMEM;
671
672 lr->start = 0;
673 lr->end = INT_MAX;
674
675 range = strchr(name, ':');
676 if (range) {
677 *range++ = '\0';
678
679 err = parse_line_num(&range, &lr->start, "start line");
680 if (err)
681 goto err;
682
683 if (*range == '+' || *range == '-') {
684 const char c = *range++;
685
686 err = parse_line_num(&range, &lr->end, "end line");
687 if (err)
688 goto err;
689
690 if (c == '+') {
691 lr->end += lr->start;
692 /*
693 * Adjust the number of lines here.
694 * If the number of lines == 1, the
695 * the end of line should be equal to
696 * the start of line.
697 */
698 lr->end--;
699 }
700 }
701
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400702 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100703
704 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400705 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500706 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100708 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400709 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100710 if (*range != '\0') {
711 semantic_error("Tailing with invalid str '%s'.\n", range);
712 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400713 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400714 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400715
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900716 file = strchr(name, '@');
717 if (file) {
718 *file = '\0';
719 lr->file = strdup(++file);
720 if (lr->file == NULL) {
721 err = -ENOMEM;
722 goto err;
723 }
724 lr->function = name;
725 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100726 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500727 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100728 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400729
730 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100731err:
732 free(name);
733 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500734}
735
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500736/* Check the name is good for event/group */
737static bool check_event_name(const char *name)
738{
739 if (!isalpha(*name) && *name != '_')
740 return false;
741 while (*++name != '\0') {
742 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
743 return false;
744 }
745 return true;
746}
747
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500748/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400749static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500750{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400751 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500752 char *ptr, *tmp;
753 char c, nc = 0;
754 /*
755 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500756 * perf probe [EVENT=]SRC[:LN|;PTN]
757 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500758 *
759 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500760 */
761
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500762 ptr = strpbrk(arg, ";=@+%");
763 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500764 *ptr = '\0';
765 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766 if (strchr(arg, ':')) {
767 semantic_error("Group name is not supported yet.\n");
768 return -ENOTSUP;
769 }
770 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500771 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400772 "follow C symbol-naming rule.\n", arg);
773 return -EINVAL;
774 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400775 pev->event = strdup(arg);
776 if (pev->event == NULL)
777 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400778 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500779 arg = tmp;
780 }
781
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500782 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500783 if (ptr) {
784 nc = *ptr;
785 *ptr++ = '\0';
786 }
787
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400788 tmp = strdup(arg);
789 if (tmp == NULL)
790 return -ENOMEM;
791
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500792 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400793 if (strchr(tmp, '.')) /* File */
794 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500795 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400796 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500797
798 /* Parse other options */
799 while (ptr) {
800 arg = ptr;
801 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500802 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400803 pp->lazy_line = strdup(arg);
804 if (pp->lazy_line == NULL)
805 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500806 break;
807 }
808 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500809 if (ptr) {
810 nc = *ptr;
811 *ptr++ = '\0';
812 }
813 switch (c) {
814 case ':': /* Line number */
815 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400816 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500817 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400818 " in line number.\n");
819 return -EINVAL;
820 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500821 break;
822 case '+': /* Byte offset from a symbol */
823 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400824 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500825 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400826 " in offset.\n");
827 return -EINVAL;
828 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500829 break;
830 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 if (pp->file) {
832 semantic_error("SRC@SRC is not allowed.\n");
833 return -EINVAL;
834 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400835 pp->file = strdup(arg);
836 if (pp->file == NULL)
837 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500838 break;
839 case '%': /* Probe places */
840 if (strcmp(arg, "return") == 0) {
841 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842 } else { /* Others not supported yet */
843 semantic_error("%%%s is not supported.\n", arg);
844 return -ENOTSUP;
845 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500846 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400847 default: /* Buggy case */
848 pr_err("This program has a bug at %s:%d.\n",
849 __FILE__, __LINE__);
850 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500851 break;
852 }
853 }
854
855 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400856 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900857 semantic_error("Lazy pattern can't be used with"
858 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400859 return -EINVAL;
860 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500861
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400862 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900863 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400864 return -EINVAL;
865 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500866
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400867 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900868 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869 return -EINVAL;
870 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500871
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400872 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500873 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900874 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400875 return -EINVAL;
876 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500877
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400878 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900879 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400880 return -EINVAL;
881 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500882
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400883 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900884 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400885 return -EINVAL;
886 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500887
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400888 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500889 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900890 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400891 return -EINVAL;
892 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500893
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400894 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500895 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
896 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400897 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500898}
899
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400900/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400901static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400902{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400903 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400904 struct perf_probe_arg_field **fieldp;
905
906 pr_debug("parsing arg: %s into ", str);
907
Masami Hiramatsu48481932010-04-12 13:16:53 -0400908 tmp = strchr(str, '=');
909 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400910 arg->name = strndup(str, tmp - str);
911 if (arg->name == NULL)
912 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400913 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400914 str = tmp + 1;
915 }
916
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400917 tmp = strchr(str, ':');
918 if (tmp) { /* Type setting */
919 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400920 arg->type = strdup(tmp + 1);
921 if (arg->type == NULL)
922 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400923 pr_debug("type:%s ", arg->type);
924 }
925
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400926 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400927 if (!is_c_varname(str) || !tmp) {
928 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400929 arg->var = strdup(str);
930 if (arg->var == NULL)
931 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400932 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400933 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400934 }
935
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400936 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400937 arg->var = strndup(str, tmp - str);
938 if (arg->var == NULL)
939 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400940 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400941 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400942 fieldp = &arg->field;
943
944 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400945 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
946 if (*fieldp == NULL)
947 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400948 if (*tmp == '[') { /* Array */
949 str = tmp;
950 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400951 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400952 if (*tmp != ']' || tmp == str + 1) {
953 semantic_error("Array index must be a"
954 " number.\n");
955 return -EINVAL;
956 }
957 tmp++;
958 if (*tmp == '\0')
959 tmp = NULL;
960 } else { /* Structure */
961 if (*tmp == '.') {
962 str = tmp + 1;
963 (*fieldp)->ref = false;
964 } else if (tmp[1] == '>') {
965 str = tmp + 2;
966 (*fieldp)->ref = true;
967 } else {
968 semantic_error("Argument parse error: %s\n",
969 str);
970 return -EINVAL;
971 }
972 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400974 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400975 (*fieldp)->name = strndup(str, tmp - str);
976 if ((*fieldp)->name == NULL)
977 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400978 if (*str != '[')
979 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400980 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
981 fieldp = &(*fieldp)->next;
982 }
983 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400984 (*fieldp)->name = strdup(str);
985 if ((*fieldp)->name == NULL)
986 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400987 if (*str != '[')
988 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400989 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400990
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400991 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400992 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400993 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400994 if (arg->name == NULL)
995 return -ENOMEM;
996 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400997 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400998}
999
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001001int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001002{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001003 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001004 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001005
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001006 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001007 if (!argv) {
1008 pr_debug("Failed to split arguments.\n");
1009 return -ENOMEM;
1010 }
1011 if (argc - 1 > MAX_PROBE_ARGS) {
1012 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1013 ret = -ERANGE;
1014 goto out;
1015 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001016 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001017 ret = parse_perf_probe_point(argv[0], pev);
1018 if (ret < 0)
1019 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001020
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001021 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001022 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001023 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1024 if (pev->args == NULL) {
1025 ret = -ENOMEM;
1026 goto out;
1027 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001028 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1029 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1030 if (ret >= 0 &&
1031 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001032 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 " kretprobe.\n");
1034 ret = -EINVAL;
1035 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001036 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001037out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001038 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001039
1040 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001041}
1042
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001043/* Return true if this perf_probe_event requires debuginfo */
1044bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001045{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001046 int i;
1047
1048 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1049 return true;
1050
1051 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001052 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001053 return true;
1054
1055 return false;
1056}
1057
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301058/* Parse probe_events event into struct probe_point */
1059static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001060 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001061{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301062 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001063 char pr;
1064 char *p;
1065 int ret, i, argc;
1066 char **argv;
1067
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301068 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001069 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001070 if (!argv) {
1071 pr_debug("Failed to split arguments.\n");
1072 return -ENOMEM;
1073 }
1074 if (argc < 2) {
1075 semantic_error("Too few probe arguments.\n");
1076 ret = -ERANGE;
1077 goto out;
1078 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001079
1080 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +08001081 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082 &pr, (float *)(void *)&tev->group,
1083 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 if (ret != 3) {
1085 semantic_error("Failed to parse event name: %s\n", argv[0]);
1086 ret = -EINVAL;
1087 goto out;
1088 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001089 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001090
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001091 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001092
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001093 /* Scan module name(if there), function name and offset */
1094 p = strchr(argv[1], ':');
1095 if (p) {
1096 tp->module = strndup(argv[1], p - argv[1]);
1097 p++;
1098 } else
1099 p = argv[1];
1100 ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001101 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001102 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001103 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001104
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001105 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301106 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001107 if (tev->args == NULL) {
1108 ret = -ENOMEM;
1109 goto out;
1110 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001111 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001112 p = strchr(argv[i + 2], '=');
1113 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001114 *p++ = '\0';
1115 else
1116 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001117 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001118 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001119 tev->args[i].value = strdup(p);
1120 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1121 ret = -ENOMEM;
1122 goto out;
1123 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001124 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001125 ret = 0;
1126out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001127 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001128 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001129}
1130
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001131/* Compose only probe arg */
1132int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1133{
1134 struct perf_probe_arg_field *field = pa->field;
1135 int ret;
1136 char *tmp = buf;
1137
Masami Hiramatsu48481932010-04-12 13:16:53 -04001138 if (pa->name && pa->var)
1139 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1140 else
1141 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001142 if (ret <= 0)
1143 goto error;
1144 tmp += ret;
1145 len -= ret;
1146
1147 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001148 if (field->name[0] == '[')
1149 ret = e_snprintf(tmp, len, "%s", field->name);
1150 else
1151 ret = e_snprintf(tmp, len, "%s%s",
1152 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001153 if (ret <= 0)
1154 goto error;
1155 tmp += ret;
1156 len -= ret;
1157 field = field->next;
1158 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001159
1160 if (pa->type) {
1161 ret = e_snprintf(tmp, len, ":%s", pa->type);
1162 if (ret <= 0)
1163 goto error;
1164 tmp += ret;
1165 len -= ret;
1166 }
1167
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001168 return tmp - buf;
1169error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001170 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001171 strerror(-ret));
1172 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001173}
1174
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001175/* Compose only probe point (not argument) */
1176static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001177{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001178 char *buf, *tmp;
1179 char offs[32] = "", line[32] = "", file[32] = "";
1180 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001181
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001182 buf = zalloc(MAX_CMDLEN);
1183 if (buf == NULL) {
1184 ret = -ENOMEM;
1185 goto error;
1186 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001187 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001188 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001189 if (ret <= 0)
1190 goto error;
1191 }
1192 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001193 ret = e_snprintf(line, 32, ":%d", pp->line);
1194 if (ret <= 0)
1195 goto error;
1196 }
1197 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001198 tmp = pp->file;
1199 len = strlen(tmp);
1200 if (len > 30) {
1201 tmp = strchr(pp->file + len - 30, '/');
1202 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1203 }
1204 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001205 if (ret <= 0)
1206 goto error;
1207 }
1208
1209 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001210 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1211 offs, pp->retprobe ? "%return" : "", line,
1212 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001213 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001214 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001215 if (ret <= 0)
1216 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001217
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001218 return buf;
1219error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001220 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001221 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001222 if (buf)
1223 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001225}
1226
1227#if 0
1228char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1229{
1230 char *buf;
1231 int i, len, ret;
1232
1233 buf = synthesize_perf_probe_point(&pev->point);
1234 if (!buf)
1235 return NULL;
1236
1237 len = strlen(buf);
1238 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001239 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001240 pev->args[i].name);
1241 if (ret <= 0) {
1242 free(buf);
1243 return NULL;
1244 }
1245 len += ret;
1246 }
1247
1248 return buf;
1249}
1250#endif
1251
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301252static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001253 char **buf, size_t *buflen,
1254 int depth)
1255{
1256 int ret;
1257 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301258 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001259 buflen, depth + 1);
1260 if (depth < 0)
1261 goto out;
1262 }
1263
1264 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1265 if (ret < 0)
1266 depth = ret;
1267 else {
1268 *buf += ret;
1269 *buflen -= ret;
1270 }
1271out:
1272 return depth;
1273
1274}
1275
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301276static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 char *buf, size_t buflen)
1278{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301279 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001280 int ret, depth = 0;
1281 char *tmp = buf;
1282
1283 /* Argument name or separator */
1284 if (arg->name)
1285 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1286 else
1287 ret = e_snprintf(buf, buflen, " ");
1288 if (ret < 0)
1289 return ret;
1290 buf += ret;
1291 buflen -= ret;
1292
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001293 /* Special case: @XXX */
1294 if (arg->value[0] == '@' && arg->ref)
1295 ref = ref->next;
1296
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001297 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001298 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301299 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001300 &buflen, 1);
1301 if (depth < 0)
1302 return depth;
1303 }
1304
1305 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001306 if (arg->value[0] == '@' && arg->ref)
1307 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1308 arg->ref->offset);
1309 else
1310 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001311 if (ret < 0)
1312 return ret;
1313 buf += ret;
1314 buflen -= ret;
1315
1316 /* Closing */
1317 while (depth--) {
1318 ret = e_snprintf(buf, buflen, ")");
1319 if (ret < 0)
1320 return ret;
1321 buf += ret;
1322 buflen -= ret;
1323 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001324 /* Print argument type */
1325 if (arg->type) {
1326 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1327 if (ret <= 0)
1328 return ret;
1329 buf += ret;
1330 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001331
1332 return buf - tmp;
1333}
1334
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301335char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001336{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301337 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001338 char *buf;
1339 int i, len, ret;
1340
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001341 buf = zalloc(MAX_CMDLEN);
1342 if (buf == NULL)
1343 return NULL;
1344
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001345 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001346 tp->retprobe ? 'r' : 'p',
1347 tev->group, tev->event,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001348 tp->module ?: "", tp->module ? ":" : "",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001349 tp->symbol, tp->offset);
1350 if (len <= 0)
1351 goto error;
1352
1353 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301354 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001355 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001356 if (ret <= 0)
1357 goto error;
1358 len += ret;
1359 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001360
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001361 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001362error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001363 free(buf);
1364 return NULL;
1365}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001366
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301367static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001368 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001369{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001370 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001372
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001373 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001374 pev->event = strdup(tev->event);
1375 pev->group = strdup(tev->group);
1376 if (pev->event == NULL || pev->group == NULL)
1377 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001378
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001379 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301380 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 if (ret < 0)
1382 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001383
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001384 /* Convert trace_arg to probe_arg */
1385 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001386 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1387 if (pev->args == NULL)
1388 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001389 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001391 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001392 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301393 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001394 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001395 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001396 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001397 if (pev->args[i].name == NULL && ret >= 0)
1398 ret = -ENOMEM;
1399 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400
1401 if (ret < 0)
1402 clear_perf_probe_event(pev);
1403
1404 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001405}
1406
1407void clear_perf_probe_event(struct perf_probe_event *pev)
1408{
1409 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001410 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001411 int i;
1412
1413 if (pev->event)
1414 free(pev->event);
1415 if (pev->group)
1416 free(pev->group);
1417 if (pp->file)
1418 free(pp->file);
1419 if (pp->function)
1420 free(pp->function);
1421 if (pp->lazy_line)
1422 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001423 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001424 if (pev->args[i].name)
1425 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001426 if (pev->args[i].var)
1427 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001428 if (pev->args[i].type)
1429 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001430 field = pev->args[i].field;
1431 while (field) {
1432 next = field->next;
1433 if (field->name)
1434 free(field->name);
1435 free(field);
1436 field = next;
1437 }
1438 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001439 if (pev->args)
1440 free(pev->args);
1441 memset(pev, 0, sizeof(*pev));
1442}
1443
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301444static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001445{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301446 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001447 int i;
1448
1449 if (tev->event)
1450 free(tev->event);
1451 if (tev->group)
1452 free(tev->group);
1453 if (tev->point.symbol)
1454 free(tev->point.symbol);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001455 if (tev->point.module)
1456 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001457 for (i = 0; i < tev->nargs; i++) {
1458 if (tev->args[i].name)
1459 free(tev->args[i].name);
1460 if (tev->args[i].value)
1461 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001462 if (tev->args[i].type)
1463 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 ref = tev->args[i].ref;
1465 while (ref) {
1466 next = ref->next;
1467 free(ref);
1468 ref = next;
1469 }
1470 }
1471 if (tev->args)
1472 free(tev->args);
1473 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474}
1475
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001476static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001477{
1478 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001479 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001480 int ret;
1481
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001482 __debugfs = debugfs_find_mountpoint();
1483 if (__debugfs == NULL) {
1484 pr_warning("Debugfs is not mounted.\n");
1485 return -ENOENT;
1486 }
1487
1488 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001489 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001490 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001491 if (readwrite && !probe_event_dry_run)
1492 ret = open(buf, O_RDWR, O_APPEND);
1493 else
1494 ret = open(buf, O_RDONLY, 0);
1495 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001496
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001497 if (ret < 0) {
1498 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 pr_warning("kprobe_events file does not exist - please"
1500 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001501 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001502 pr_warning("Failed to open kprobe_events file: %s\n",
1503 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001504 }
1505 return ret;
1506}
1507
1508/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301509static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001510{
1511 int ret, idx;
1512 FILE *fp;
1513 char buf[MAX_CMDLEN];
1514 char *p;
1515 struct strlist *sl;
1516
1517 sl = strlist__new(true, NULL);
1518
1519 fp = fdopen(dup(fd), "r");
1520 while (!feof(fp)) {
1521 p = fgets(buf, MAX_CMDLEN, fp);
1522 if (!p)
1523 break;
1524
1525 idx = strlen(p) - 1;
1526 if (p[idx] == '\n')
1527 p[idx] = '\0';
1528 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 if (ret < 0) {
1530 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1531 strlist__delete(sl);
1532 return NULL;
1533 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001534 }
1535 fclose(fp);
1536
1537 return sl;
1538}
1539
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001540/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001542{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001543 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001544 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001545 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001546
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001547 /* Synthesize only event probe point */
1548 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001549 if (!place)
1550 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001551
1552 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001553 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 return ret;
1555
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001556 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001557
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001558 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001559 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001560 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001561 ret = synthesize_perf_probe_arg(&pev->args[i],
1562 buf, 128);
1563 if (ret < 0)
1564 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001565 printf(" %s", buf);
1566 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001567 }
1568 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001569 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001570 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001571}
1572
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001573/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001575{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301577 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001578 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001579 struct strlist *rawlist;
1580 struct str_node *ent;
1581
Masami Hiramatsu72041332010-01-05 17:47:10 -05001582 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001583 ret = init_vmlinux();
1584 if (ret < 0)
1585 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001586
1587 memset(&tev, 0, sizeof(tev));
1588 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001589
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001590 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001591 if (fd < 0)
1592 return fd;
1593
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301594 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001595 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001596 if (!rawlist)
1597 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001598
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001599 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301600 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 if (ret >= 0) {
1602 ret = convert_to_perf_probe_event(&tev, &pev);
1603 if (ret >= 0)
1604 ret = show_perf_probe_event(&pev);
1605 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001606 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301607 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001608 if (ret < 0)
1609 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001610 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001611 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001612
1613 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614}
1615
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001616/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301617static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001618{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001619 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001620 struct strlist *sl, *rawlist;
1621 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301622 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001623 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001624
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301626 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001627 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001628 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301629 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001630 if (ret < 0)
1631 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001632 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001633 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1634 tev.event);
1635 if (ret >= 0)
1636 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001637 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001638 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301639 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001640 if (ret < 0)
1641 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001642 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001643 strlist__delete(rawlist);
1644
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001645 if (ret < 0) {
1646 strlist__delete(sl);
1647 return NULL;
1648 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001649 return sl;
1650}
1651
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301652static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001653{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001654 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301655 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001656
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001657 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301658 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001659 return -EINVAL;
1660 }
1661
Masami Hiramatsufa282442009-12-08 17:03:23 -05001662 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001663 if (!probe_event_dry_run) {
1664 ret = write(fd, buf, strlen(buf));
1665 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001666 pr_warning("Failed to write event: %s\n",
1667 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001668 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001669 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001670 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001671}
1672
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001673static int get_new_event_name(char *buf, size_t len, const char *base,
1674 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001675{
1676 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001677
1678 /* Try no suffix */
1679 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001680 if (ret < 0) {
1681 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1682 return ret;
1683 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001684 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001685 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001686
Masami Hiramatsud761b082009-12-15 10:32:25 -05001687 if (!allow_suffix) {
1688 pr_warning("Error: event \"%s\" already exists. "
1689 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001690 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001691 }
1692
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001693 /* Try to add suffix */
1694 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001695 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001696 if (ret < 0) {
1697 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1698 return ret;
1699 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001700 if (!strlist__has_entry(namelist, buf))
1701 break;
1702 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001703 if (i == MAX_EVENT_INDEX) {
1704 pr_warning("Too many events are on the same function.\n");
1705 ret = -ERANGE;
1706 }
1707
1708 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001709}
1710
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301711static int __add_probe_trace_events(struct perf_probe_event *pev,
1712 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001713 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001714{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001715 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301716 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001717 char buf[64];
1718 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001719 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001720
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001721 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001722 if (fd < 0)
1723 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001724 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301725 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001726 if (!namelist) {
1727 pr_debug("Failed to get current event list.\n");
1728 return -EIO;
1729 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001730
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001731 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001732 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001733 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734 tev = &tevs[i];
1735 if (pev->event)
1736 event = pev->event;
1737 else
1738 if (pev->point.function)
1739 event = pev->point.function;
1740 else
1741 event = tev->point.symbol;
1742 if (pev->group)
1743 group = pev->group;
1744 else
1745 group = PERFPROBE_GROUP;
1746
1747 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001748 ret = get_new_event_name(buf, 64, event,
1749 namelist, allow_suffix);
1750 if (ret < 0)
1751 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001752 event = buf;
1753
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001754 tev->event = strdup(event);
1755 tev->group = strdup(group);
1756 if (tev->event == NULL || tev->group == NULL) {
1757 ret = -ENOMEM;
1758 break;
1759 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301760 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001761 if (ret < 0)
1762 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001763 /* Add added event name to namelist */
1764 strlist__add(namelist, event);
1765
1766 /* Trick here - save current event/group */
1767 event = pev->event;
1768 group = pev->group;
1769 pev->event = tev->event;
1770 pev->group = tev->group;
1771 show_perf_probe_event(pev);
1772 /* Trick here - restore current event/group */
1773 pev->event = (char *)event;
1774 pev->group = (char *)group;
1775
1776 /*
1777 * Probes after the first probe which comes from same
1778 * user input are always allowed to add suffix, because
1779 * there might be several addresses corresponding to
1780 * one code line.
1781 */
1782 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001783 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001784
1785 if (ret >= 0) {
1786 /* Show how to use the event. */
1787 printf("\nYou can now use it on all perf tools, such as:\n\n");
1788 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1789 tev->event);
1790 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001791
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001792 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001793 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001794 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001795}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001796
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301797static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1798 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001799 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001800{
1801 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001802 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301803 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001804
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001805 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001806 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001807 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001808 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001809
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001810 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301811 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001812 if (tev == NULL)
1813 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001814
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001815 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001816 tev->point.symbol = strdup(pev->point.function);
1817 if (tev->point.symbol == NULL) {
1818 ret = -ENOMEM;
1819 goto error;
1820 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001821
1822 if (module) {
1823 tev->point.module = strdup(module);
1824 if (tev->point.module == NULL) {
1825 ret = -ENOMEM;
1826 goto error;
1827 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001828 }
Jovi Zhangce27a442011-07-25 22:08:08 +08001829
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001830 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001831 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001832 tev->nargs = pev->nargs;
1833 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301834 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001835 * tev->nargs);
1836 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001837 ret = -ENOMEM;
1838 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001839 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001840 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001841 if (pev->args[i].name) {
1842 tev->args[i].name = strdup(pev->args[i].name);
1843 if (tev->args[i].name == NULL) {
1844 ret = -ENOMEM;
1845 goto error;
1846 }
1847 }
1848 tev->args[i].value = strdup(pev->args[i].var);
1849 if (tev->args[i].value == NULL) {
1850 ret = -ENOMEM;
1851 goto error;
1852 }
1853 if (pev->args[i].type) {
1854 tev->args[i].type = strdup(pev->args[i].type);
1855 if (tev->args[i].type == NULL) {
1856 ret = -ENOMEM;
1857 goto error;
1858 }
1859 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001860 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001861 }
1862
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001863 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001864 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865 if (!sym) {
1866 pr_warning("Kernel symbol \'%s\' not found.\n",
1867 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001868 ret = -ENOENT;
1869 goto error;
Prashanth Nageshappa1c1bc922012-02-28 09:43:01 +05301870 } else if (tev->point.offset > sym->end - sym->start) {
1871 pr_warning("Offset specified is greater than size of %s\n",
1872 tev->point.symbol);
1873 ret = -ENOENT;
1874 goto error;
1875
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001876 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001877
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001878 return 1;
1879error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301880 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001881 free(tev);
1882 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001883 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884}
1885
1886struct __event_package {
1887 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301888 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889 int ntevs;
1890};
1891
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001893 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001894{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001896 struct __event_package *pkgs;
1897
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001898 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1899 if (pkgs == NULL)
1900 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001901
1902 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001903 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001904 if (ret < 0) {
1905 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001906 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001907 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908
1909 /* Loop 1: convert all events */
1910 for (i = 0; i < npevs; i++) {
1911 pkgs[i].pev = &pevs[i];
1912 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301913 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001914 &pkgs[i].tevs,
1915 max_tevs,
1916 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001917 if (ret < 0)
1918 goto end;
1919 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001920 }
1921
1922 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001923 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301924 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001925 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001926 if (ret < 0)
1927 break;
1928 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001929end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001930 /* Loop 3: cleanup and free trace events */
1931 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001932 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301933 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001934 free(pkgs[i].tevs);
1935 }
1936 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001937
1938 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001939}
1940
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301941static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001942{
1943 char *p;
1944 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001945 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001946
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301947 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001948 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1949 if (ret < 0)
1950 goto error;
1951
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001952 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001953 if (!p) {
1954 pr_debug("Internal error: %s should have ':' but not.\n",
1955 ent->s);
1956 ret = -ENOTSUP;
1957 goto error;
1958 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001959 *p = '/';
1960
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001961 pr_debug("Writing event: %s\n", buf);
1962 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09001963 if (ret < 0) {
1964 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001965 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09001966 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001967
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001968 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001969 return 0;
1970error:
1971 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1972 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001973}
1974
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301975static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001976 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001977{
1978 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001979 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001980 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001981
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001982 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1983 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001984 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001985 return ret;
1986 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001987
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001988 if (strpbrk(buf, "*?")) { /* Glob-exp */
1989 strlist__for_each_safe(ent, n, namelist)
1990 if (strglobmatch(ent->s, buf)) {
1991 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301992 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001993 if (ret < 0)
1994 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001995 strlist__remove(namelist, ent);
1996 }
1997 } else {
1998 ent = strlist__find(namelist, buf);
1999 if (ent) {
2000 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302001 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002002 if (ret >= 0)
2003 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002004 }
2005 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002006 if (found == 0 && ret >= 0)
2007 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2008
2009 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002010}
2011
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002012int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002013{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002014 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002015 const char *group, *event;
2016 char *p, *str;
2017 struct str_node *ent;
2018 struct strlist *namelist;
2019
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002020 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002021 if (fd < 0)
2022 return fd;
2023
Masami Hiramatsufa282442009-12-08 17:03:23 -05002024 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302025 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002026 if (namelist == NULL)
2027 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002028
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002029 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002030 str = strdup(ent->s);
2031 if (str == NULL) {
2032 ret = -ENOMEM;
2033 break;
2034 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002035 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002036 p = strchr(str, ':');
2037 if (p) {
2038 group = str;
2039 *p = '\0';
2040 event = p + 1;
2041 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002042 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002043 event = str;
2044 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002045 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302046 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002047 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002048 if (ret < 0)
2049 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002050 }
2051 strlist__delete(namelist);
2052 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002053
2054 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002055}
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002056/* TODO: don't use a global variable for filter ... */
2057static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002058
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002059/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002060 * If a symbol corresponds to a function with global binding and
2061 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002062 */
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002063static int filter_available_functions(struct map *map __unused,
2064 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002065{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002066 if (sym->binding == STB_GLOBAL &&
2067 strfilter__compare(available_func_filter, sym->name))
2068 return 0;
2069 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002070}
2071
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002072int show_available_funcs(const char *module, struct strfilter *_filter)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002073{
2074 struct map *map;
2075 int ret;
2076
2077 setup_pager();
2078
2079 ret = init_vmlinux();
2080 if (ret < 0)
2081 return ret;
2082
2083 map = kernel_get_module_map(module);
2084 if (!map) {
2085 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2086 return -EINVAL;
2087 }
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002088 available_func_filter = _filter;
2089 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002090 pr_err("Failed to load map.\n");
2091 return -EINVAL;
2092 }
2093 if (!dso__sorted_by_name(map->dso, map->type))
2094 dso__sort_by_name(map->dso, map->type);
2095
2096 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2097 return 0;
2098}