blob: 369ddc64bbb6a6d81aea0b9f92075fe5315af178 [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
22#define _GNU_SOURCE
23#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050032#include <stdarg.h>
33#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090034#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050035
36#undef _GNU_SOURCE
Masami Hiramatsu31facc52010-03-16 18:05:30 -040037#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "event.h"
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050039#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050040#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050041#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050042#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050043#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040044#include "symbol.h"
45#include "thread.h"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040046#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030047#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040049#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050
51#define MAX_CMDLEN 256
52#define MAX_PROBE_ARGS 128
53#define PERFPROBE_GROUP "probe"
54
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055bool probe_event_dry_run; /* Dry run flag */
56
Masami Hiramatsu146a1432010-04-12 13:17:42 -040057#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050058
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059/* If there is no space to write, returns -E2BIG. */
60static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050061 __attribute__((format(printf, 3, 4)));
62
63static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050064{
65 int ret;
66 va_list ap;
67 va_start(ap, format);
68 ret = vsnprintf(str, size, format, ap);
69 va_end(ap);
70 if (ret >= (int)size)
71 ret = -E2BIG;
72 return ret;
73}
74
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030075static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030076static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090078/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040088 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040093
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090094 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030095 if (ret < 0)
96 goto out;
97
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090098 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090099 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 goto out;
101 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400102out:
103 if (ret < 0)
104 pr_warning("Failed to init vmlinux path.\n");
105 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106}
107
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900108static struct symbol *__find_kernel_function_by_name(const char *name,
109 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400110{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900111 return machine__find_kernel_function_by_name(&machine, name, mapp,
112 NULL);
113}
114
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900115static struct map *kernel_get_module_map(const char *module)
116{
117 struct rb_node *nd;
118 struct map_groups *grp = &machine.kmaps;
119
120 if (!module)
121 module = "kernel";
122
123 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
124 struct map *pos = rb_entry(nd, struct map, rb_node);
125 if (strncmp(pos->dso->short_name + 1, module,
126 pos->dso->short_name_len - 2) == 0) {
127 return pos;
128 }
129 }
130 return NULL;
131}
132
133static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900134{
135 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100136 struct map *map;
137 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900138
139 if (module) {
140 list_for_each_entry(dso, &machine.kernel_dsos, node) {
141 if (strncmp(dso->short_name + 1, module,
142 dso->short_name_len - 2) == 0)
143 goto found;
144 }
145 pr_debug("Failed to find module %s.\n", module);
146 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100147 }
148
149 map = machine.vmlinux_maps[MAP__FUNCTION];
150 dso = map->dso;
151
152 vmlinux_name = symbol_conf.vmlinux_name;
153 if (vmlinux_name) {
154 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
155 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900156 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100157 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900158 pr_debug("Failed to load kernel map.\n");
159 return NULL;
160 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400161 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900162found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163 return dso;
164}
165
166const char *kernel_get_module_path(const char *module)
167{
168 struct dso *dso = kernel_get_module_dso(module);
169 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900170}
171
172#ifdef DWARF_SUPPORT
173static int open_vmlinux(const char *module)
174{
175 const char *path = kernel_get_module_path(module);
176 if (!path) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900177 pr_err("Failed to find path of %s module.\n",
178 module ?: "kernel");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900179 return -ENOENT;
180 }
181 pr_debug("Try to open %s\n", path);
182 return open(path, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400183}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300184
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530185/*
186 * Convert trace point to probe point with debuginfo
187 * Currently only handles kprobes.
188 */
189static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900190 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300191{
192 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900193 struct map *map;
194 u64 addr;
195 int ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300196
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900197 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300198 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900199 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200200 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900201 tp->offset, addr);
202 ret = find_perf_probe_point((unsigned long)addr, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300203 }
204 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400205 pr_debug("Failed to find corresponding probes from "
206 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400207 pp->function = strdup(tp->symbol);
208 if (pp->function == NULL)
209 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300210 pp->offset = tp->offset;
211 }
212 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400213
214 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300215}
216
217/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530218static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
219 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900220 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300221{
222 bool need_dwarf = perf_probe_event_need_dwarf(pev);
223 int fd, ntevs;
224
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900225 fd = open_vmlinux(module);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300226 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400227 if (need_dwarf) {
228 pr_warning("Failed to open debuginfo file.\n");
229 return fd;
230 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300231 pr_debug("Could not open vmlinux. Try to use symbols.\n");
232 return 0;
233 }
234
235 /* Searching trace events corresponding to probe event */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530236 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300237 close(fd);
238
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400239 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530240 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300241 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400242 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300243
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400244 if (ntevs == 0) { /* No error but failed to find probe point. */
245 pr_warning("Probe point '%s' not found.\n",
246 synthesize_perf_probe_point(&pev->point));
247 return -ENOENT;
248 }
249 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400250 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
251 if (ntevs == -EBADF) {
252 pr_warning("Warning: No dwarf info found in the vmlinux - "
253 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
254 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900255 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400256 return 0;
257 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300258 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400259 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300260}
261
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900262/*
263 * Find a src file from a DWARF tag path. Prepend optional source path prefix
264 * and chop off leading directories that do not exist. Result is passed back as
265 * a newly allocated path on success.
266 * Return 0 if file was found and readable, -errno otherwise.
267 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900268static int get_real_path(const char *raw_path, const char *comp_dir,
269 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900270{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900271 const char *prefix = symbol_conf.source_prefix;
272
273 if (!prefix) {
274 if (raw_path[0] != '/' && comp_dir)
275 /* If not an absolute path, try to use comp_dir */
276 prefix = comp_dir;
277 else {
278 if (access(raw_path, R_OK) == 0) {
279 *new_path = strdup(raw_path);
280 return 0;
281 } else
282 return -errno;
283 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900284 }
285
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900286 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900287 if (!*new_path)
288 return -ENOMEM;
289
290 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900291 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900292
293 if (access(*new_path, R_OK) == 0)
294 return 0;
295
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900296 if (!symbol_conf.source_prefix)
297 /* In case of searching comp_dir, don't retry */
298 return -errno;
299
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900300 switch (errno) {
301 case ENAMETOOLONG:
302 case ENOENT:
303 case EROFS:
304 case EFAULT:
305 raw_path = strchr(++raw_path, '/');
306 if (!raw_path) {
307 free(*new_path);
308 *new_path = NULL;
309 return -ENOENT;
310 }
311 continue;
312
313 default:
314 free(*new_path);
315 *new_path = NULL;
316 return -errno;
317 }
318 }
319}
320
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300321#define LINEBUF_SIZE 256
322#define NR_ADDITIONAL_LINES 2
323
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100324static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300325{
326 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100327 const char *color = show_num ? "" : PERF_COLOR_BLUE;
328 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300329
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100330 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300331 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
332 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100333 if (skip)
334 continue;
335 if (!prefix) {
336 prefix = show_num ? "%7d " : " ";
337 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300338 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100339 color_fprintf(stdout, color, "%s", buf);
340
341 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400342
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100343 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300344error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100345 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100346 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100347 return -1;
348 }
349 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300350}
351
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100352static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
353{
354 int rv = __show_one_line(fp, l, skip, show_num);
355 if (rv == 0) {
356 pr_warning("Source file is shorter than expected.\n");
357 rv = -1;
358 }
359 return rv;
360}
361
362#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
363#define show_one_line(f,l) _show_one_line(f,l,false,false)
364#define skip_one_line(f,l) _show_one_line(f,l,true,false)
365#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
366
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300367/*
368 * Show line-range always requires debuginfo to find source file and
369 * line number.
370 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900371int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300372{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400373 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300374 struct line_node *ln;
375 FILE *fp;
376 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900377 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300378
379 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400380 ret = init_vmlinux();
381 if (ret < 0)
382 return ret;
383
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900384 fd = open_vmlinux(module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400385 if (fd < 0) {
386 pr_warning("Failed to open debuginfo file.\n");
387 return fd;
388 }
389
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300390 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300391 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400392 if (ret == 0) {
393 pr_warning("Specified source line is not found.\n");
394 return -ENOENT;
395 } else if (ret < 0) {
396 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
397 return ret;
398 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300399
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900400 /* Convert source file path */
401 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900402 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900403 free(tmp); /* Free old path */
404 if (ret < 0) {
405 pr_warning("Failed to find source file. (%d)\n", ret);
406 return ret;
407 }
408
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300409 setup_pager();
410
411 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900412 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300413 lr->start - lr->offset);
414 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100415 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300416
417 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400418 if (fp == NULL) {
419 pr_warning("Failed to open %s: %s\n", lr->path,
420 strerror(errno));
421 return -errno;
422 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300423 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100424 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100425 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100426 if (ret < 0)
427 goto end;
428 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300429
430 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100431 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100432 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100433 if (ret < 0)
434 goto end;
435 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100436 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400437 if (ret < 0)
438 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300439 }
440
441 if (lr->end == INT_MAX)
442 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100443 while (l <= lr->end) {
444 ret = show_one_line_or_eof(fp, l++ - lr->offset);
445 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100446 break;
447 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400448end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300449 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400450 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300451}
452
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900453static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900454 int max_vls, struct strfilter *_filter,
455 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900456{
457 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900458 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900459 struct str_node *node;
460 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900461 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900462
463 buf = synthesize_perf_probe_point(&pev->point);
464 if (!buf)
465 return -EINVAL;
466 pr_debug("Searching variables at %s\n", buf);
467
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900468 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900469 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900470 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900471 goto end;
472 }
473 /* Some variables are found */
474 fprintf(stdout, "Available variables at %s\n", buf);
475 for (i = 0; i < ret; i++) {
476 vl = &vls[i];
477 /*
478 * A probe point might be converted to
479 * several trace points.
480 */
481 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
482 vl->point.offset);
483 free(vl->point.symbol);
484 nvars = 0;
485 if (vl->vars) {
486 strlist__for_each(node, vl->vars) {
487 var = strchr(node->s, '\t') + 1;
488 if (strfilter__compare(_filter, var)) {
489 fprintf(stdout, "\t\t%s\n", node->s);
490 nvars++;
491 }
492 }
493 strlist__delete(vl->vars);
494 }
495 if (nvars == 0)
496 fprintf(stdout, "\t\t(No matched variables)\n");
497 }
498 free(vls);
499end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900500 free(buf);
501 return ret;
502}
503
504/* Show available variables on given probe point */
505int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900506 int max_vls, const char *module,
507 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900508{
509 int i, fd, ret = 0;
510
511 ret = init_vmlinux();
512 if (ret < 0)
513 return ret;
514
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900515 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900516 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900517 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900518 return fd;
519 }
520
521 setup_pager();
522
523 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900524 ret = show_available_vars_at(fd, &pevs[i], max_vls, _filter,
525 externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900526
527 close(fd);
528 return ret;
529}
530
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300531#else /* !DWARF_SUPPORT */
532
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530533static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900534 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300535{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900536 struct symbol *sym;
537
538 sym = __find_kernel_function_by_name(tp->symbol, NULL);
539 if (!sym) {
540 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
541 return -ENOENT;
542 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400543 pp->function = strdup(tp->symbol);
544 if (pp->function == NULL)
545 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300546 pp->offset = tp->offset;
547 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548
549 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300550}
551
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530552static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
553 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900554 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300555{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400556 if (perf_probe_event_need_dwarf(pev)) {
557 pr_warning("Debuginfo-analysis is not supported.\n");
558 return -ENOSYS;
559 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300560 return 0;
561}
562
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900563int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300564{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400565 pr_warning("Debuginfo-analysis is not supported.\n");
566 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300567}
568
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900569int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900570 int npevs __unused, int max_vls __unused,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900571 const char *module __unused,
572 struct strfilter *filter __unused,
573 bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900574{
575 pr_warning("Debuginfo-analysis is not supported.\n");
576 return -ENOSYS;
577}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400578#endif
579
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100580static int parse_line_num(char **ptr, int *val, const char *what)
581{
582 const char *start = *ptr;
583
584 errno = 0;
585 *val = strtol(*ptr, ptr, 0);
586 if (errno || *ptr == start) {
587 semantic_error("'%s' is not a valid number.\n", what);
588 return -EINVAL;
589 }
590 return 0;
591}
592
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100593/*
594 * Stuff 'lr' according to the line range described by 'arg'.
595 * The line range syntax is described by:
596 *
597 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900598 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100599 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400600int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500601{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900602 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100603 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100604
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100605 if (!name)
606 return -ENOMEM;
607
608 lr->start = 0;
609 lr->end = INT_MAX;
610
611 range = strchr(name, ':');
612 if (range) {
613 *range++ = '\0';
614
615 err = parse_line_num(&range, &lr->start, "start line");
616 if (err)
617 goto err;
618
619 if (*range == '+' || *range == '-') {
620 const char c = *range++;
621
622 err = parse_line_num(&range, &lr->end, "end line");
623 if (err)
624 goto err;
625
626 if (c == '+') {
627 lr->end += lr->start;
628 /*
629 * Adjust the number of lines here.
630 * If the number of lines == 1, the
631 * the end of line should be equal to
632 * the start of line.
633 */
634 lr->end--;
635 }
636 }
637
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400638 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100639
640 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400641 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500642 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400643 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100644 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400645 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100646 if (*range != '\0') {
647 semantic_error("Tailing with invalid str '%s'.\n", range);
648 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400649 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400650 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400651
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900652 file = strchr(name, '@');
653 if (file) {
654 *file = '\0';
655 lr->file = strdup(++file);
656 if (lr->file == NULL) {
657 err = -ENOMEM;
658 goto err;
659 }
660 lr->function = name;
661 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100662 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500663 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100664 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400665
666 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100667err:
668 free(name);
669 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500670}
671
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500672/* Check the name is good for event/group */
673static bool check_event_name(const char *name)
674{
675 if (!isalpha(*name) && *name != '_')
676 return false;
677 while (*++name != '\0') {
678 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
679 return false;
680 }
681 return true;
682}
683
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500684/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400685static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500686{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400687 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500688 char *ptr, *tmp;
689 char c, nc = 0;
690 /*
691 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500692 * perf probe [EVENT=]SRC[:LN|;PTN]
693 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500694 *
695 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500696 */
697
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500698 ptr = strpbrk(arg, ";=@+%");
699 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500700 *ptr = '\0';
701 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 if (strchr(arg, ':')) {
703 semantic_error("Group name is not supported yet.\n");
704 return -ENOTSUP;
705 }
706 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500707 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400708 "follow C symbol-naming rule.\n", arg);
709 return -EINVAL;
710 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400711 pev->event = strdup(arg);
712 if (pev->event == NULL)
713 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400714 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500715 arg = tmp;
716 }
717
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500718 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500719 if (ptr) {
720 nc = *ptr;
721 *ptr++ = '\0';
722 }
723
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400724 tmp = strdup(arg);
725 if (tmp == NULL)
726 return -ENOMEM;
727
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500728 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400729 if (strchr(tmp, '.')) /* File */
730 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500731 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400732 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500733
734 /* Parse other options */
735 while (ptr) {
736 arg = ptr;
737 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500738 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400739 pp->lazy_line = strdup(arg);
740 if (pp->lazy_line == NULL)
741 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500742 break;
743 }
744 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500745 if (ptr) {
746 nc = *ptr;
747 *ptr++ = '\0';
748 }
749 switch (c) {
750 case ':': /* Line number */
751 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400752 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500753 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400754 " in line number.\n");
755 return -EINVAL;
756 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500757 break;
758 case '+': /* Byte offset from a symbol */
759 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400760 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500761 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400762 " in offset.\n");
763 return -EINVAL;
764 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500765 break;
766 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400767 if (pp->file) {
768 semantic_error("SRC@SRC is not allowed.\n");
769 return -EINVAL;
770 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400771 pp->file = strdup(arg);
772 if (pp->file == NULL)
773 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500774 break;
775 case '%': /* Probe places */
776 if (strcmp(arg, "return") == 0) {
777 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400778 } else { /* Others not supported yet */
779 semantic_error("%%%s is not supported.\n", arg);
780 return -ENOTSUP;
781 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500782 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400783 default: /* Buggy case */
784 pr_err("This program has a bug at %s:%d.\n",
785 __FILE__, __LINE__);
786 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500787 break;
788 }
789 }
790
791 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400792 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900793 semantic_error("Lazy pattern can't be used with"
794 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400795 return -EINVAL;
796 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500797
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900799 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400800 return -EINVAL;
801 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500802
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400803 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900804 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400805 return -EINVAL;
806 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500807
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400808 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500809 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900810 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400811 return -EINVAL;
812 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500813
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400814 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900815 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400816 return -EINVAL;
817 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500818
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400819 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900820 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400821 return -EINVAL;
822 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500823
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400824 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500825 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900826 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 return -EINVAL;
828 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500829
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400830 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500831 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
832 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400833 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500834}
835
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400836/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400837static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400838{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400839 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400840 struct perf_probe_arg_field **fieldp;
841
842 pr_debug("parsing arg: %s into ", str);
843
Masami Hiramatsu48481932010-04-12 13:16:53 -0400844 tmp = strchr(str, '=');
845 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400846 arg->name = strndup(str, tmp - str);
847 if (arg->name == NULL)
848 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400849 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400850 str = tmp + 1;
851 }
852
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400853 tmp = strchr(str, ':');
854 if (tmp) { /* Type setting */
855 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400856 arg->type = strdup(tmp + 1);
857 if (arg->type == NULL)
858 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400859 pr_debug("type:%s ", arg->type);
860 }
861
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400862 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400863 if (!is_c_varname(str) || !tmp) {
864 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400865 arg->var = strdup(str);
866 if (arg->var == NULL)
867 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400868 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400870 }
871
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400872 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400873 arg->var = strndup(str, tmp - str);
874 if (arg->var == NULL)
875 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400876 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400877 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400878 fieldp = &arg->field;
879
880 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400881 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
882 if (*fieldp == NULL)
883 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400884 if (*tmp == '[') { /* Array */
885 str = tmp;
886 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400887 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400888 if (*tmp != ']' || tmp == str + 1) {
889 semantic_error("Array index must be a"
890 " number.\n");
891 return -EINVAL;
892 }
893 tmp++;
894 if (*tmp == '\0')
895 tmp = NULL;
896 } else { /* Structure */
897 if (*tmp == '.') {
898 str = tmp + 1;
899 (*fieldp)->ref = false;
900 } else if (tmp[1] == '>') {
901 str = tmp + 2;
902 (*fieldp)->ref = true;
903 } else {
904 semantic_error("Argument parse error: %s\n",
905 str);
906 return -EINVAL;
907 }
908 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400909 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400910 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400911 (*fieldp)->name = strndup(str, tmp - str);
912 if ((*fieldp)->name == NULL)
913 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400914 if (*str != '[')
915 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400916 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
917 fieldp = &(*fieldp)->next;
918 }
919 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400920 (*fieldp)->name = strdup(str);
921 if ((*fieldp)->name == NULL)
922 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400923 if (*str != '[')
924 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400925 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400926
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400927 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400928 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400929 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400930 if (arg->name == NULL)
931 return -ENOMEM;
932 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400933 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400934}
935
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400936/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400937int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500938{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500939 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400940 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500941
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400942 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400943 if (!argv) {
944 pr_debug("Failed to split arguments.\n");
945 return -ENOMEM;
946 }
947 if (argc - 1 > MAX_PROBE_ARGS) {
948 semantic_error("Too many probe arguments (%d).\n", argc - 1);
949 ret = -ERANGE;
950 goto out;
951 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500952 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400953 ret = parse_perf_probe_point(argv[0], pev);
954 if (ret < 0)
955 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500956
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500957 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400958 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400959 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
960 if (pev->args == NULL) {
961 ret = -ENOMEM;
962 goto out;
963 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400964 for (i = 0; i < pev->nargs && ret >= 0; i++) {
965 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
966 if (ret >= 0 &&
967 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400968 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400969 " kretprobe.\n");
970 ret = -EINVAL;
971 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500972 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500974 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400975
976 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500977}
978
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400979/* Return true if this perf_probe_event requires debuginfo */
980bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500981{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982 int i;
983
984 if (pev->point.file || pev->point.line || pev->point.lazy_line)
985 return true;
986
987 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400988 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400989 return true;
990
991 return false;
992}
993
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530994/* Parse probe_events event into struct probe_point */
995static int parse_probe_trace_command(const char *cmd,
996 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400997{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530998 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500999 char pr;
1000 char *p;
1001 int ret, i, argc;
1002 char **argv;
1003
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301004 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001005 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001006 if (!argv) {
1007 pr_debug("Failed to split arguments.\n");
1008 return -ENOMEM;
1009 }
1010 if (argc < 2) {
1011 semantic_error("Too few probe arguments.\n");
1012 ret = -ERANGE;
1013 goto out;
1014 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001015
1016 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +08001017 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001018 &pr, (float *)(void *)&tev->group,
1019 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001020 if (ret != 3) {
1021 semantic_error("Failed to parse event name: %s\n", argv[0]);
1022 ret = -EINVAL;
1023 goto out;
1024 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001025 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001026
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001027 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001028
1029 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001030 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
1031 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001032 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001033 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001034
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001035 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301036 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001037 if (tev->args == NULL) {
1038 ret = -ENOMEM;
1039 goto out;
1040 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001041 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001042 p = strchr(argv[i + 2], '=');
1043 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001044 *p++ = '\0';
1045 else
1046 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001047 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001048 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001049 tev->args[i].value = strdup(p);
1050 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1051 ret = -ENOMEM;
1052 goto out;
1053 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001054 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001055 ret = 0;
1056out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001057 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001058 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001059}
1060
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001061/* Compose only probe arg */
1062int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1063{
1064 struct perf_probe_arg_field *field = pa->field;
1065 int ret;
1066 char *tmp = buf;
1067
Masami Hiramatsu48481932010-04-12 13:16:53 -04001068 if (pa->name && pa->var)
1069 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1070 else
1071 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001072 if (ret <= 0)
1073 goto error;
1074 tmp += ret;
1075 len -= ret;
1076
1077 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001078 if (field->name[0] == '[')
1079 ret = e_snprintf(tmp, len, "%s", field->name);
1080 else
1081 ret = e_snprintf(tmp, len, "%s%s",
1082 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001083 if (ret <= 0)
1084 goto error;
1085 tmp += ret;
1086 len -= ret;
1087 field = field->next;
1088 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001089
1090 if (pa->type) {
1091 ret = e_snprintf(tmp, len, ":%s", pa->type);
1092 if (ret <= 0)
1093 goto error;
1094 tmp += ret;
1095 len -= ret;
1096 }
1097
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001098 return tmp - buf;
1099error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001100 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001101 strerror(-ret));
1102 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001103}
1104
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001105/* Compose only probe point (not argument) */
1106static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001107{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001108 char *buf, *tmp;
1109 char offs[32] = "", line[32] = "", file[32] = "";
1110 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001111
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001112 buf = zalloc(MAX_CMDLEN);
1113 if (buf == NULL) {
1114 ret = -ENOMEM;
1115 goto error;
1116 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001117 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001118 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001119 if (ret <= 0)
1120 goto error;
1121 }
1122 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001123 ret = e_snprintf(line, 32, ":%d", pp->line);
1124 if (ret <= 0)
1125 goto error;
1126 }
1127 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001128 tmp = pp->file;
1129 len = strlen(tmp);
1130 if (len > 30) {
1131 tmp = strchr(pp->file + len - 30, '/');
1132 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1133 }
1134 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001135 if (ret <= 0)
1136 goto error;
1137 }
1138
1139 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001140 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1141 offs, pp->retprobe ? "%return" : "", line,
1142 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001143 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001144 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001145 if (ret <= 0)
1146 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001147
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001148 return buf;
1149error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001150 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001151 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001152 if (buf)
1153 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001154 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001155}
1156
1157#if 0
1158char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1159{
1160 char *buf;
1161 int i, len, ret;
1162
1163 buf = synthesize_perf_probe_point(&pev->point);
1164 if (!buf)
1165 return NULL;
1166
1167 len = strlen(buf);
1168 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001169 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001170 pev->args[i].name);
1171 if (ret <= 0) {
1172 free(buf);
1173 return NULL;
1174 }
1175 len += ret;
1176 }
1177
1178 return buf;
1179}
1180#endif
1181
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301182static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 char **buf, size_t *buflen,
1184 int depth)
1185{
1186 int ret;
1187 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301188 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001189 buflen, depth + 1);
1190 if (depth < 0)
1191 goto out;
1192 }
1193
1194 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1195 if (ret < 0)
1196 depth = ret;
1197 else {
1198 *buf += ret;
1199 *buflen -= ret;
1200 }
1201out:
1202 return depth;
1203
1204}
1205
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301206static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 char *buf, size_t buflen)
1208{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301209 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001210 int ret, depth = 0;
1211 char *tmp = buf;
1212
1213 /* Argument name or separator */
1214 if (arg->name)
1215 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1216 else
1217 ret = e_snprintf(buf, buflen, " ");
1218 if (ret < 0)
1219 return ret;
1220 buf += ret;
1221 buflen -= ret;
1222
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001223 /* Special case: @XXX */
1224 if (arg->value[0] == '@' && arg->ref)
1225 ref = ref->next;
1226
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001227 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001228 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301229 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001230 &buflen, 1);
1231 if (depth < 0)
1232 return depth;
1233 }
1234
1235 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001236 if (arg->value[0] == '@' && arg->ref)
1237 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1238 arg->ref->offset);
1239 else
1240 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001241 if (ret < 0)
1242 return ret;
1243 buf += ret;
1244 buflen -= ret;
1245
1246 /* Closing */
1247 while (depth--) {
1248 ret = e_snprintf(buf, buflen, ")");
1249 if (ret < 0)
1250 return ret;
1251 buf += ret;
1252 buflen -= ret;
1253 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001254 /* Print argument type */
1255 if (arg->type) {
1256 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1257 if (ret <= 0)
1258 return ret;
1259 buf += ret;
1260 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001261
1262 return buf - tmp;
1263}
1264
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301265char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001266{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301267 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268 char *buf;
1269 int i, len, ret;
1270
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001271 buf = zalloc(MAX_CMDLEN);
1272 if (buf == NULL)
1273 return NULL;
1274
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001275 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1276 tp->retprobe ? 'r' : 'p',
1277 tev->group, tev->event,
1278 tp->symbol, tp->offset);
1279 if (len <= 0)
1280 goto error;
1281
1282 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301283 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001285 if (ret <= 0)
1286 goto error;
1287 len += ret;
1288 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001289
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001290 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001291error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001292 free(buf);
1293 return NULL;
1294}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001295
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301296static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001297 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001298{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001299 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001300 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001301
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001302 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001303 pev->event = strdup(tev->event);
1304 pev->group = strdup(tev->group);
1305 if (pev->event == NULL || pev->group == NULL)
1306 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001307
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001308 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301309 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001310 if (ret < 0)
1311 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001312
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001313 /* Convert trace_arg to probe_arg */
1314 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001315 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1316 if (pev->args == NULL)
1317 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001318 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001319 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001321 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301322 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001323 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001324 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001325 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001326 if (pev->args[i].name == NULL && ret >= 0)
1327 ret = -ENOMEM;
1328 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001329
1330 if (ret < 0)
1331 clear_perf_probe_event(pev);
1332
1333 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001334}
1335
1336void clear_perf_probe_event(struct perf_probe_event *pev)
1337{
1338 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001339 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001340 int i;
1341
1342 if (pev->event)
1343 free(pev->event);
1344 if (pev->group)
1345 free(pev->group);
1346 if (pp->file)
1347 free(pp->file);
1348 if (pp->function)
1349 free(pp->function);
1350 if (pp->lazy_line)
1351 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001352 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 if (pev->args[i].name)
1354 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001355 if (pev->args[i].var)
1356 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001357 if (pev->args[i].type)
1358 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001359 field = pev->args[i].field;
1360 while (field) {
1361 next = field->next;
1362 if (field->name)
1363 free(field->name);
1364 free(field);
1365 field = next;
1366 }
1367 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001368 if (pev->args)
1369 free(pev->args);
1370 memset(pev, 0, sizeof(*pev));
1371}
1372
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301373static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001374{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301375 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001376 int i;
1377
1378 if (tev->event)
1379 free(tev->event);
1380 if (tev->group)
1381 free(tev->group);
1382 if (tev->point.symbol)
1383 free(tev->point.symbol);
1384 for (i = 0; i < tev->nargs; i++) {
1385 if (tev->args[i].name)
1386 free(tev->args[i].name);
1387 if (tev->args[i].value)
1388 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001389 if (tev->args[i].type)
1390 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391 ref = tev->args[i].ref;
1392 while (ref) {
1393 next = ref->next;
1394 free(ref);
1395 ref = next;
1396 }
1397 }
1398 if (tev->args)
1399 free(tev->args);
1400 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401}
1402
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001403static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001404{
1405 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001406 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001407 int ret;
1408
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001409 __debugfs = debugfs_find_mountpoint();
1410 if (__debugfs == NULL) {
1411 pr_warning("Debugfs is not mounted.\n");
1412 return -ENOENT;
1413 }
1414
1415 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001417 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 if (readwrite && !probe_event_dry_run)
1419 ret = open(buf, O_RDWR, O_APPEND);
1420 else
1421 ret = open(buf, O_RDONLY, 0);
1422 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001423
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001424 if (ret < 0) {
1425 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001426 pr_warning("kprobe_events file does not exist - please"
1427 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001428 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 pr_warning("Failed to open kprobe_events file: %s\n",
1430 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001431 }
1432 return ret;
1433}
1434
1435/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301436static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001437{
1438 int ret, idx;
1439 FILE *fp;
1440 char buf[MAX_CMDLEN];
1441 char *p;
1442 struct strlist *sl;
1443
1444 sl = strlist__new(true, NULL);
1445
1446 fp = fdopen(dup(fd), "r");
1447 while (!feof(fp)) {
1448 p = fgets(buf, MAX_CMDLEN, fp);
1449 if (!p)
1450 break;
1451
1452 idx = strlen(p) - 1;
1453 if (p[idx] == '\n')
1454 p[idx] = '\0';
1455 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (ret < 0) {
1457 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1458 strlist__delete(sl);
1459 return NULL;
1460 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001461 }
1462 fclose(fp);
1463
1464 return sl;
1465}
1466
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001467/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001469{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001470 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001471 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001473
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001474 /* Synthesize only event probe point */
1475 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001476 if (!place)
1477 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001478
1479 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001480 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001481 return ret;
1482
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001483 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001484
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001485 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001486 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001487 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488 ret = synthesize_perf_probe_arg(&pev->args[i],
1489 buf, 128);
1490 if (ret < 0)
1491 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001492 printf(" %s", buf);
1493 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001494 }
1495 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001497 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001498}
1499
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001500/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301504 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001505 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001506 struct strlist *rawlist;
1507 struct str_node *ent;
1508
Masami Hiramatsu72041332010-01-05 17:47:10 -05001509 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 ret = init_vmlinux();
1511 if (ret < 0)
1512 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513
1514 memset(&tev, 0, sizeof(tev));
1515 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001516
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001517 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518 if (fd < 0)
1519 return fd;
1520
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301521 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001522 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 if (!rawlist)
1524 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001525
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001526 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301527 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 if (ret >= 0) {
1529 ret = convert_to_perf_probe_event(&tev, &pev);
1530 if (ret >= 0)
1531 ret = show_perf_probe_event(&pev);
1532 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001533 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301534 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535 if (ret < 0)
1536 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001537 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001538 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001539
1540 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541}
1542
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001543/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301544static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001545{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001546 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001547 struct strlist *sl, *rawlist;
1548 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301549 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001551
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001552 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301553 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001554 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001555 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301556 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001557 if (ret < 0)
1558 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001559 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001560 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1561 tev.event);
1562 if (ret >= 0)
1563 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001564 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001565 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301566 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001567 if (ret < 0)
1568 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001569 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001570 strlist__delete(rawlist);
1571
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001572 if (ret < 0) {
1573 strlist__delete(sl);
1574 return NULL;
1575 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001576 return sl;
1577}
1578
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301579static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001580{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001581 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301582 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001583
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001584 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301585 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001586 return -EINVAL;
1587 }
1588
Masami Hiramatsufa282442009-12-08 17:03:23 -05001589 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001590 if (!probe_event_dry_run) {
1591 ret = write(fd, buf, strlen(buf));
1592 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001593 pr_warning("Failed to write event: %s\n",
1594 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001595 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001596 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001597 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001598}
1599
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001600static int get_new_event_name(char *buf, size_t len, const char *base,
1601 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001602{
1603 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001604
1605 /* Try no suffix */
1606 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001607 if (ret < 0) {
1608 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1609 return ret;
1610 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001611 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001612 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001613
Masami Hiramatsud761b082009-12-15 10:32:25 -05001614 if (!allow_suffix) {
1615 pr_warning("Error: event \"%s\" already exists. "
1616 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001617 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001618 }
1619
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001620 /* Try to add suffix */
1621 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001622 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001623 if (ret < 0) {
1624 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1625 return ret;
1626 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001627 if (!strlist__has_entry(namelist, buf))
1628 break;
1629 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001630 if (i == MAX_EVENT_INDEX) {
1631 pr_warning("Too many events are on the same function.\n");
1632 ret = -ERANGE;
1633 }
1634
1635 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001636}
1637
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301638static int __add_probe_trace_events(struct perf_probe_event *pev,
1639 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001640 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001641{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001642 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301643 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001644 char buf[64];
1645 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001646 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001647
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001648 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001649 if (fd < 0)
1650 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001651 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301652 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 if (!namelist) {
1654 pr_debug("Failed to get current event list.\n");
1655 return -EIO;
1656 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001657
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001658 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001659 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001660 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 tev = &tevs[i];
1662 if (pev->event)
1663 event = pev->event;
1664 else
1665 if (pev->point.function)
1666 event = pev->point.function;
1667 else
1668 event = tev->point.symbol;
1669 if (pev->group)
1670 group = pev->group;
1671 else
1672 group = PERFPROBE_GROUP;
1673
1674 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001675 ret = get_new_event_name(buf, 64, event,
1676 namelist, allow_suffix);
1677 if (ret < 0)
1678 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001679 event = buf;
1680
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001681 tev->event = strdup(event);
1682 tev->group = strdup(group);
1683 if (tev->event == NULL || tev->group == NULL) {
1684 ret = -ENOMEM;
1685 break;
1686 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301687 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001688 if (ret < 0)
1689 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001690 /* Add added event name to namelist */
1691 strlist__add(namelist, event);
1692
1693 /* Trick here - save current event/group */
1694 event = pev->event;
1695 group = pev->group;
1696 pev->event = tev->event;
1697 pev->group = tev->group;
1698 show_perf_probe_event(pev);
1699 /* Trick here - restore current event/group */
1700 pev->event = (char *)event;
1701 pev->group = (char *)group;
1702
1703 /*
1704 * Probes after the first probe which comes from same
1705 * user input are always allowed to add suffix, because
1706 * there might be several addresses corresponding to
1707 * one code line.
1708 */
1709 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001710 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001711
1712 if (ret >= 0) {
1713 /* Show how to use the event. */
1714 printf("\nYou can now use it on all perf tools, such as:\n\n");
1715 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1716 tev->event);
1717 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001718
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001719 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001720 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001721 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001722}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001723
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301724static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1725 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001726 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001727{
1728 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001729 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301730 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001731
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001732 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001733 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001734 if (ret != 0)
1735 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001736
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001737 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301738 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001739 if (tev == NULL)
1740 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001741
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001742 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001743 tev->point.symbol = strdup(pev->point.function);
1744 if (tev->point.symbol == NULL) {
1745 ret = -ENOMEM;
1746 goto error;
1747 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001749 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001750 tev->nargs = pev->nargs;
1751 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301752 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001753 * tev->nargs);
1754 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001755 ret = -ENOMEM;
1756 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001757 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001758 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001759 if (pev->args[i].name) {
1760 tev->args[i].name = strdup(pev->args[i].name);
1761 if (tev->args[i].name == NULL) {
1762 ret = -ENOMEM;
1763 goto error;
1764 }
1765 }
1766 tev->args[i].value = strdup(pev->args[i].var);
1767 if (tev->args[i].value == NULL) {
1768 ret = -ENOMEM;
1769 goto error;
1770 }
1771 if (pev->args[i].type) {
1772 tev->args[i].type = strdup(pev->args[i].type);
1773 if (tev->args[i].type == NULL) {
1774 ret = -ENOMEM;
1775 goto error;
1776 }
1777 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001778 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001779 }
1780
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001781 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001782 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783 if (!sym) {
1784 pr_warning("Kernel symbol \'%s\' not found.\n",
1785 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001786 ret = -ENOENT;
1787 goto error;
1788 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001789
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001790 return 1;
1791error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301792 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001793 free(tev);
1794 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001795 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001796}
1797
1798struct __event_package {
1799 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301800 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001801 int ntevs;
1802};
1803
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001804int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001805 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001807 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001808 struct __event_package *pkgs;
1809
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001810 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1811 if (pkgs == NULL)
1812 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001813
1814 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001815 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001816 if (ret < 0) {
1817 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001818 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001819 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001820
1821 /* Loop 1: convert all events */
1822 for (i = 0; i < npevs; i++) {
1823 pkgs[i].pev = &pevs[i];
1824 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301825 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001826 &pkgs[i].tevs,
1827 max_tevs,
1828 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001829 if (ret < 0)
1830 goto end;
1831 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001832 }
1833
1834 /* Loop 2: add all events */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001835 for (i = 0; i < npevs && ret >= 0; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301836 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001837 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001838 if (ret < 0)
1839 break;
1840 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001841end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001842 /* Loop 3: cleanup and free trace events */
1843 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001844 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301845 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001846 free(pkgs[i].tevs);
1847 }
1848 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849
1850 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001851}
1852
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301853static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001854{
1855 char *p;
1856 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001857 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001858
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301859 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001860 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1861 if (ret < 0)
1862 goto error;
1863
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001864 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865 if (!p) {
1866 pr_debug("Internal error: %s should have ':' but not.\n",
1867 ent->s);
1868 ret = -ENOTSUP;
1869 goto error;
1870 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001871 *p = '/';
1872
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873 pr_debug("Writing event: %s\n", buf);
1874 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875 if (ret < 0)
1876 goto error;
1877
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001878 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001879 return 0;
1880error:
1881 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1882 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001883}
1884
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301885static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001886 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001887{
1888 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001889 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001890 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001891
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1893 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001894 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895 return ret;
1896 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001897
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001898 if (strpbrk(buf, "*?")) { /* Glob-exp */
1899 strlist__for_each_safe(ent, n, namelist)
1900 if (strglobmatch(ent->s, buf)) {
1901 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301902 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001903 if (ret < 0)
1904 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001905 strlist__remove(namelist, ent);
1906 }
1907 } else {
1908 ent = strlist__find(namelist, buf);
1909 if (ent) {
1910 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301911 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001912 if (ret >= 0)
1913 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001914 }
1915 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001916 if (found == 0 && ret >= 0)
1917 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1918
1919 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001920}
1921
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001922int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001923{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001924 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001925 const char *group, *event;
1926 char *p, *str;
1927 struct str_node *ent;
1928 struct strlist *namelist;
1929
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001930 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001931 if (fd < 0)
1932 return fd;
1933
Masami Hiramatsufa282442009-12-08 17:03:23 -05001934 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301935 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001936 if (namelist == NULL)
1937 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001938
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001939 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001940 str = strdup(ent->s);
1941 if (str == NULL) {
1942 ret = -ENOMEM;
1943 break;
1944 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001945 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001946 p = strchr(str, ':');
1947 if (p) {
1948 group = str;
1949 *p = '\0';
1950 event = p + 1;
1951 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001952 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001953 event = str;
1954 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001955 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301956 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001957 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001958 if (ret < 0)
1959 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001960 }
1961 strlist__delete(namelist);
1962 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001963
1964 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001965}
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001966/* TODO: don't use a global variable for filter ... */
1967static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001968
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001969/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001970 * If a symbol corresponds to a function with global binding and
1971 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001972 */
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001973static int filter_available_functions(struct map *map __unused,
1974 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001975{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001976 if (sym->binding == STB_GLOBAL &&
1977 strfilter__compare(available_func_filter, sym->name))
1978 return 0;
1979 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001980}
1981
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001982int show_available_funcs(const char *module, struct strfilter *_filter)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001983{
1984 struct map *map;
1985 int ret;
1986
1987 setup_pager();
1988
1989 ret = init_vmlinux();
1990 if (ret < 0)
1991 return ret;
1992
1993 map = kernel_get_module_map(module);
1994 if (!map) {
1995 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
1996 return -EINVAL;
1997 }
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001998 available_func_filter = _filter;
1999 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002000 pr_err("Failed to load map.\n");
2001 return -EINVAL;
2002 }
2003 if (!dso__sorted_by_name(map->dso, map->type))
2004 dso__sort_by_name(map->dso, map->type);
2005
2006 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2007 return 0;
2008}