blob: 859d377a3df3d7128f0bbedc6edcfcd00e42586b [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)
412 fprintf(stdout, "<%s:%d>\n", lr->function,
413 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 Hiramatsufb8c5a52010-10-21 19:13:35 +0900454 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900455{
456 char *buf;
457 int ret, i;
458 struct str_node *node;
459 struct variable_list *vls = NULL, *vl;
460
461 buf = synthesize_perf_probe_point(&pev->point);
462 if (!buf)
463 return -EINVAL;
464 pr_debug("Searching variables at %s\n", buf);
465
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900466 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900467 if (ret > 0) {
468 /* Some variables were found */
469 fprintf(stdout, "Available variables at %s\n", buf);
470 for (i = 0; i < ret; i++) {
471 vl = &vls[i];
472 /*
473 * A probe point might be converted to
474 * several trace points.
475 */
476 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
477 vl->point.offset);
478 free(vl->point.symbol);
479 if (vl->vars) {
480 strlist__for_each(node, vl->vars)
481 fprintf(stdout, "\t\t%s\n", node->s);
482 strlist__delete(vl->vars);
483 } else
484 fprintf(stdout, "(No variables)\n");
485 }
486 free(vls);
487 } else
488 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
489
490 free(buf);
491 return ret;
492}
493
494/* Show available variables on given probe point */
495int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900496 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900497{
498 int i, fd, ret = 0;
499
500 ret = init_vmlinux();
501 if (ret < 0)
502 return ret;
503
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900504 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900505 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900506 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900507 return fd;
508 }
509
510 setup_pager();
511
512 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900513 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900514
515 close(fd);
516 return ret;
517}
518
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300519#else /* !DWARF_SUPPORT */
520
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530521static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900522 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300523{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900524 struct symbol *sym;
525
526 sym = __find_kernel_function_by_name(tp->symbol, NULL);
527 if (!sym) {
528 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
529 return -ENOENT;
530 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400531 pp->function = strdup(tp->symbol);
532 if (pp->function == NULL)
533 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300534 pp->offset = tp->offset;
535 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400536
537 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300538}
539
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530540static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
541 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900542 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300543{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400544 if (perf_probe_event_need_dwarf(pev)) {
545 pr_warning("Debuginfo-analysis is not supported.\n");
546 return -ENOSYS;
547 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300548 return 0;
549}
550
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900551int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300552{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400553 pr_warning("Debuginfo-analysis is not supported.\n");
554 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300555}
556
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900557int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900558 int npevs __unused, int max_vls __unused,
559 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900560{
561 pr_warning("Debuginfo-analysis is not supported.\n");
562 return -ENOSYS;
563}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400564#endif
565
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100566static int parse_line_num(char **ptr, int *val, const char *what)
567{
568 const char *start = *ptr;
569
570 errno = 0;
571 *val = strtol(*ptr, ptr, 0);
572 if (errno || *ptr == start) {
573 semantic_error("'%s' is not a valid number.\n", what);
574 return -EINVAL;
575 }
576 return 0;
577}
578
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100579/*
580 * Stuff 'lr' according to the line range described by 'arg'.
581 * The line range syntax is described by:
582 *
583 * SRC[:SLN[+NUM|-ELN]]
584 * FNC[:SLN[+NUM|-ELN]]
585 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400586int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500587{
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100588 char *range, *name = strdup(arg);
589 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100590
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100591 if (!name)
592 return -ENOMEM;
593
594 lr->start = 0;
595 lr->end = INT_MAX;
596
597 range = strchr(name, ':');
598 if (range) {
599 *range++ = '\0';
600
601 err = parse_line_num(&range, &lr->start, "start line");
602 if (err)
603 goto err;
604
605 if (*range == '+' || *range == '-') {
606 const char c = *range++;
607
608 err = parse_line_num(&range, &lr->end, "end line");
609 if (err)
610 goto err;
611
612 if (c == '+') {
613 lr->end += lr->start;
614 /*
615 * Adjust the number of lines here.
616 * If the number of lines == 1, the
617 * the end of line should be equal to
618 * the start of line.
619 */
620 lr->end--;
621 }
622 }
623
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400624 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100625
626 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400627 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500628 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100630 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400631 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100632 if (*range != '\0') {
633 semantic_error("Tailing with invalid str '%s'.\n", range);
634 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400635 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400636 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400637
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100638 if (strchr(name, '.'))
639 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500640 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100641 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400642
643 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100644err:
645 free(name);
646 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500647}
648
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500649/* Check the name is good for event/group */
650static bool check_event_name(const char *name)
651{
652 if (!isalpha(*name) && *name != '_')
653 return false;
654 while (*++name != '\0') {
655 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
656 return false;
657 }
658 return true;
659}
660
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500661/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400662static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500663{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400664 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500665 char *ptr, *tmp;
666 char c, nc = 0;
667 /*
668 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500669 * perf probe [EVENT=]SRC[:LN|;PTN]
670 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500671 *
672 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500673 */
674
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500675 ptr = strpbrk(arg, ";=@+%");
676 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500677 *ptr = '\0';
678 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400679 if (strchr(arg, ':')) {
680 semantic_error("Group name is not supported yet.\n");
681 return -ENOTSUP;
682 }
683 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500684 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400685 "follow C symbol-naming rule.\n", arg);
686 return -EINVAL;
687 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400688 pev->event = strdup(arg);
689 if (pev->event == NULL)
690 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400691 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500692 arg = tmp;
693 }
694
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500695 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500696 if (ptr) {
697 nc = *ptr;
698 *ptr++ = '\0';
699 }
700
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400701 tmp = strdup(arg);
702 if (tmp == NULL)
703 return -ENOMEM;
704
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500705 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400706 if (strchr(tmp, '.')) /* File */
707 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500708 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400709 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500710
711 /* Parse other options */
712 while (ptr) {
713 arg = ptr;
714 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500715 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400716 pp->lazy_line = strdup(arg);
717 if (pp->lazy_line == NULL)
718 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500719 break;
720 }
721 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500722 if (ptr) {
723 nc = *ptr;
724 *ptr++ = '\0';
725 }
726 switch (c) {
727 case ':': /* Line number */
728 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400729 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500730 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400731 " in line number.\n");
732 return -EINVAL;
733 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500734 break;
735 case '+': /* Byte offset from a symbol */
736 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400737 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500738 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400739 " in offset.\n");
740 return -EINVAL;
741 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500742 break;
743 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400744 if (pp->file) {
745 semantic_error("SRC@SRC is not allowed.\n");
746 return -EINVAL;
747 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400748 pp->file = strdup(arg);
749 if (pp->file == NULL)
750 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500751 break;
752 case '%': /* Probe places */
753 if (strcmp(arg, "return") == 0) {
754 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400755 } else { /* Others not supported yet */
756 semantic_error("%%%s is not supported.\n", arg);
757 return -ENOTSUP;
758 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500759 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400760 default: /* Buggy case */
761 pr_err("This program has a bug at %s:%d.\n",
762 __FILE__, __LINE__);
763 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500764 break;
765 }
766 }
767
768 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400769 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900770 semantic_error("Lazy pattern can't be used with"
771 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400772 return -EINVAL;
773 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500774
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400775 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900776 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400777 return -EINVAL;
778 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500779
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400780 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900781 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400782 return -EINVAL;
783 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500784
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400785 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500786 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900787 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400788 return -EINVAL;
789 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500790
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400791 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900792 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400793 return -EINVAL;
794 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500795
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400796 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900797 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 return -EINVAL;
799 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500800
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400801 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500802 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900803 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400804 return -EINVAL;
805 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500806
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400807 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500808 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
809 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400810 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500811}
812
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400813/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400814static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400815{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400816 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400817 struct perf_probe_arg_field **fieldp;
818
819 pr_debug("parsing arg: %s into ", str);
820
Masami Hiramatsu48481932010-04-12 13:16:53 -0400821 tmp = strchr(str, '=');
822 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400823 arg->name = strndup(str, tmp - str);
824 if (arg->name == NULL)
825 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400826 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400827 str = tmp + 1;
828 }
829
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400830 tmp = strchr(str, ':');
831 if (tmp) { /* Type setting */
832 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400833 arg->type = strdup(tmp + 1);
834 if (arg->type == NULL)
835 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400836 pr_debug("type:%s ", arg->type);
837 }
838
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400839 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400840 if (!is_c_varname(str) || !tmp) {
841 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400842 arg->var = strdup(str);
843 if (arg->var == NULL)
844 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400845 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400847 }
848
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400849 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400850 arg->var = strndup(str, tmp - str);
851 if (arg->var == NULL)
852 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400853 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400854 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400855 fieldp = &arg->field;
856
857 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400858 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
859 if (*fieldp == NULL)
860 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400861 if (*tmp == '[') { /* Array */
862 str = tmp;
863 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400864 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400865 if (*tmp != ']' || tmp == str + 1) {
866 semantic_error("Array index must be a"
867 " number.\n");
868 return -EINVAL;
869 }
870 tmp++;
871 if (*tmp == '\0')
872 tmp = NULL;
873 } else { /* Structure */
874 if (*tmp == '.') {
875 str = tmp + 1;
876 (*fieldp)->ref = false;
877 } else if (tmp[1] == '>') {
878 str = tmp + 2;
879 (*fieldp)->ref = true;
880 } else {
881 semantic_error("Argument parse error: %s\n",
882 str);
883 return -EINVAL;
884 }
885 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400886 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400887 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400888 (*fieldp)->name = strndup(str, tmp - str);
889 if ((*fieldp)->name == NULL)
890 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400891 if (*str != '[')
892 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400893 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
894 fieldp = &(*fieldp)->next;
895 }
896 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400897 (*fieldp)->name = strdup(str);
898 if ((*fieldp)->name == NULL)
899 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400900 if (*str != '[')
901 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400902 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400903
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400904 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400905 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400906 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400907 if (arg->name == NULL)
908 return -ENOMEM;
909 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400910 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400911}
912
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400913/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400914int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500915{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500916 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400917 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500918
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400919 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400920 if (!argv) {
921 pr_debug("Failed to split arguments.\n");
922 return -ENOMEM;
923 }
924 if (argc - 1 > MAX_PROBE_ARGS) {
925 semantic_error("Too many probe arguments (%d).\n", argc - 1);
926 ret = -ERANGE;
927 goto out;
928 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500929 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400930 ret = parse_perf_probe_point(argv[0], pev);
931 if (ret < 0)
932 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500933
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500934 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400935 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400936 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
937 if (pev->args == NULL) {
938 ret = -ENOMEM;
939 goto out;
940 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400941 for (i = 0; i < pev->nargs && ret >= 0; i++) {
942 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
943 if (ret >= 0 &&
944 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400945 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400946 " kretprobe.\n");
947 ret = -EINVAL;
948 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500949 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400950out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500951 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400952
953 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500954}
955
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400956/* Return true if this perf_probe_event requires debuginfo */
957bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500958{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 int i;
960
961 if (pev->point.file || pev->point.line || pev->point.lazy_line)
962 return true;
963
964 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400965 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400966 return true;
967
968 return false;
969}
970
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530971/* Parse probe_events event into struct probe_point */
972static int parse_probe_trace_command(const char *cmd,
973 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530975 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500976 char pr;
977 char *p;
978 int ret, i, argc;
979 char **argv;
980
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530981 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400983 if (!argv) {
984 pr_debug("Failed to split arguments.\n");
985 return -ENOMEM;
986 }
987 if (argc < 2) {
988 semantic_error("Too few probe arguments.\n");
989 ret = -ERANGE;
990 goto out;
991 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500992
993 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800994 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400995 &pr, (float *)(void *)&tev->group,
996 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400997 if (ret != 3) {
998 semantic_error("Failed to parse event name: %s\n", argv[0]);
999 ret = -EINVAL;
1000 goto out;
1001 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001002 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001003
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001004 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001005
1006 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001007 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
1008 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001009 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001010 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001011
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001012 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301013 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001014 if (tev->args == NULL) {
1015 ret = -ENOMEM;
1016 goto out;
1017 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001018 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001019 p = strchr(argv[i + 2], '=');
1020 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001021 *p++ = '\0';
1022 else
1023 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001024 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001025 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001026 tev->args[i].value = strdup(p);
1027 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1028 ret = -ENOMEM;
1029 goto out;
1030 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001031 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001032 ret = 0;
1033out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001034 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001035 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001036}
1037
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001038/* Compose only probe arg */
1039int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1040{
1041 struct perf_probe_arg_field *field = pa->field;
1042 int ret;
1043 char *tmp = buf;
1044
Masami Hiramatsu48481932010-04-12 13:16:53 -04001045 if (pa->name && pa->var)
1046 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1047 else
1048 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001049 if (ret <= 0)
1050 goto error;
1051 tmp += ret;
1052 len -= ret;
1053
1054 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001055 if (field->name[0] == '[')
1056 ret = e_snprintf(tmp, len, "%s", field->name);
1057 else
1058 ret = e_snprintf(tmp, len, "%s%s",
1059 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001060 if (ret <= 0)
1061 goto error;
1062 tmp += ret;
1063 len -= ret;
1064 field = field->next;
1065 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001066
1067 if (pa->type) {
1068 ret = e_snprintf(tmp, len, ":%s", pa->type);
1069 if (ret <= 0)
1070 goto error;
1071 tmp += ret;
1072 len -= ret;
1073 }
1074
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001075 return tmp - buf;
1076error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001077 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001078 strerror(-ret));
1079 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001080}
1081
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082/* Compose only probe point (not argument) */
1083static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001084{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001085 char *buf, *tmp;
1086 char offs[32] = "", line[32] = "", file[32] = "";
1087 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001088
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001089 buf = zalloc(MAX_CMDLEN);
1090 if (buf == NULL) {
1091 ret = -ENOMEM;
1092 goto error;
1093 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001094 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001095 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001096 if (ret <= 0)
1097 goto error;
1098 }
1099 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001100 ret = e_snprintf(line, 32, ":%d", pp->line);
1101 if (ret <= 0)
1102 goto error;
1103 }
1104 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001105 tmp = pp->file;
1106 len = strlen(tmp);
1107 if (len > 30) {
1108 tmp = strchr(pp->file + len - 30, '/');
1109 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1110 }
1111 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001112 if (ret <= 0)
1113 goto error;
1114 }
1115
1116 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001117 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1118 offs, pp->retprobe ? "%return" : "", line,
1119 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001120 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001121 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001122 if (ret <= 0)
1123 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001124
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001125 return buf;
1126error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001127 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001128 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001129 if (buf)
1130 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001131 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001132}
1133
1134#if 0
1135char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1136{
1137 char *buf;
1138 int i, len, ret;
1139
1140 buf = synthesize_perf_probe_point(&pev->point);
1141 if (!buf)
1142 return NULL;
1143
1144 len = strlen(buf);
1145 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001146 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001147 pev->args[i].name);
1148 if (ret <= 0) {
1149 free(buf);
1150 return NULL;
1151 }
1152 len += ret;
1153 }
1154
1155 return buf;
1156}
1157#endif
1158
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301159static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001160 char **buf, size_t *buflen,
1161 int depth)
1162{
1163 int ret;
1164 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301165 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001166 buflen, depth + 1);
1167 if (depth < 0)
1168 goto out;
1169 }
1170
1171 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1172 if (ret < 0)
1173 depth = ret;
1174 else {
1175 *buf += ret;
1176 *buflen -= ret;
1177 }
1178out:
1179 return depth;
1180
1181}
1182
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301183static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 char *buf, size_t buflen)
1185{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301186 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001187 int ret, depth = 0;
1188 char *tmp = buf;
1189
1190 /* Argument name or separator */
1191 if (arg->name)
1192 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1193 else
1194 ret = e_snprintf(buf, buflen, " ");
1195 if (ret < 0)
1196 return ret;
1197 buf += ret;
1198 buflen -= ret;
1199
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001200 /* Special case: @XXX */
1201 if (arg->value[0] == '@' && arg->ref)
1202 ref = ref->next;
1203
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001204 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001205 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301206 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 &buflen, 1);
1208 if (depth < 0)
1209 return depth;
1210 }
1211
1212 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001213 if (arg->value[0] == '@' && arg->ref)
1214 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1215 arg->ref->offset);
1216 else
1217 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001218 if (ret < 0)
1219 return ret;
1220 buf += ret;
1221 buflen -= ret;
1222
1223 /* Closing */
1224 while (depth--) {
1225 ret = e_snprintf(buf, buflen, ")");
1226 if (ret < 0)
1227 return ret;
1228 buf += ret;
1229 buflen -= ret;
1230 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001231 /* Print argument type */
1232 if (arg->type) {
1233 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1234 if (ret <= 0)
1235 return ret;
1236 buf += ret;
1237 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001238
1239 return buf - tmp;
1240}
1241
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301242char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001243{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301244 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001245 char *buf;
1246 int i, len, ret;
1247
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001248 buf = zalloc(MAX_CMDLEN);
1249 if (buf == NULL)
1250 return NULL;
1251
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1253 tp->retprobe ? 'r' : 'p',
1254 tev->group, tev->event,
1255 tp->symbol, tp->offset);
1256 if (len <= 0)
1257 goto error;
1258
1259 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301260 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001261 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001262 if (ret <= 0)
1263 goto error;
1264 len += ret;
1265 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001268error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 free(buf);
1270 return NULL;
1271}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001272
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301273static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001274 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001275{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001276 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001279 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001280 pev->event = strdup(tev->event);
1281 pev->group = strdup(tev->group);
1282 if (pev->event == NULL || pev->group == NULL)
1283 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001284
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001285 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301286 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 if (ret < 0)
1288 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001289
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001290 /* Convert trace_arg to probe_arg */
1291 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001292 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1293 if (pev->args == NULL)
1294 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001295 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001296 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001297 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001298 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301299 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001300 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001301 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001302 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001303 if (pev->args[i].name == NULL && ret >= 0)
1304 ret = -ENOMEM;
1305 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001306
1307 if (ret < 0)
1308 clear_perf_probe_event(pev);
1309
1310 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001311}
1312
1313void clear_perf_probe_event(struct perf_probe_event *pev)
1314{
1315 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001316 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001317 int i;
1318
1319 if (pev->event)
1320 free(pev->event);
1321 if (pev->group)
1322 free(pev->group);
1323 if (pp->file)
1324 free(pp->file);
1325 if (pp->function)
1326 free(pp->function);
1327 if (pp->lazy_line)
1328 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001329 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001330 if (pev->args[i].name)
1331 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001332 if (pev->args[i].var)
1333 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001334 if (pev->args[i].type)
1335 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001336 field = pev->args[i].field;
1337 while (field) {
1338 next = field->next;
1339 if (field->name)
1340 free(field->name);
1341 free(field);
1342 field = next;
1343 }
1344 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001345 if (pev->args)
1346 free(pev->args);
1347 memset(pev, 0, sizeof(*pev));
1348}
1349
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301350static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001351{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301352 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 int i;
1354
1355 if (tev->event)
1356 free(tev->event);
1357 if (tev->group)
1358 free(tev->group);
1359 if (tev->point.symbol)
1360 free(tev->point.symbol);
1361 for (i = 0; i < tev->nargs; i++) {
1362 if (tev->args[i].name)
1363 free(tev->args[i].name);
1364 if (tev->args[i].value)
1365 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001366 if (tev->args[i].type)
1367 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001368 ref = tev->args[i].ref;
1369 while (ref) {
1370 next = ref->next;
1371 free(ref);
1372 ref = next;
1373 }
1374 }
1375 if (tev->args)
1376 free(tev->args);
1377 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001378}
1379
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001380static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001381{
1382 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001383 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001384 int ret;
1385
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001386 __debugfs = debugfs_find_mountpoint();
1387 if (__debugfs == NULL) {
1388 pr_warning("Debugfs is not mounted.\n");
1389 return -ENOENT;
1390 }
1391
1392 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001394 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395 if (readwrite && !probe_event_dry_run)
1396 ret = open(buf, O_RDWR, O_APPEND);
1397 else
1398 ret = open(buf, O_RDONLY, 0);
1399 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001400
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401 if (ret < 0) {
1402 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 pr_warning("kprobe_events file does not exist - please"
1404 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001405 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001406 pr_warning("Failed to open kprobe_events file: %s\n",
1407 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001408 }
1409 return ret;
1410}
1411
1412/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301413static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001414{
1415 int ret, idx;
1416 FILE *fp;
1417 char buf[MAX_CMDLEN];
1418 char *p;
1419 struct strlist *sl;
1420
1421 sl = strlist__new(true, NULL);
1422
1423 fp = fdopen(dup(fd), "r");
1424 while (!feof(fp)) {
1425 p = fgets(buf, MAX_CMDLEN, fp);
1426 if (!p)
1427 break;
1428
1429 idx = strlen(p) - 1;
1430 if (p[idx] == '\n')
1431 p[idx] = '\0';
1432 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 if (ret < 0) {
1434 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1435 strlist__delete(sl);
1436 return NULL;
1437 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 }
1439 fclose(fp);
1440
1441 return sl;
1442}
1443
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001444/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001446{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001447 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001448 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001449 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001450
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001451 /* Synthesize only event probe point */
1452 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001453 if (!place)
1454 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001455
1456 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001457 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 return ret;
1459
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001460 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001461
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001462 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001463 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001464 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465 ret = synthesize_perf_probe_arg(&pev->args[i],
1466 buf, 128);
1467 if (ret < 0)
1468 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001469 printf(" %s", buf);
1470 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001471 }
1472 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001473 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001475}
1476
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001477/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001479{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301481 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001482 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001483 struct strlist *rawlist;
1484 struct str_node *ent;
1485
Masami Hiramatsu72041332010-01-05 17:47:10 -05001486 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 ret = init_vmlinux();
1488 if (ret < 0)
1489 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490
1491 memset(&tev, 0, sizeof(tev));
1492 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001493
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001494 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001495 if (fd < 0)
1496 return fd;
1497
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301498 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001499 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001500 if (!rawlist)
1501 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001503 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301504 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001505 if (ret >= 0) {
1506 ret = convert_to_perf_probe_event(&tev, &pev);
1507 if (ret >= 0)
1508 ret = show_perf_probe_event(&pev);
1509 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001510 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301511 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001512 if (ret < 0)
1513 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001514 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001515 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516
1517 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001518}
1519
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001520/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301521static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001522{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001523 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001524 struct strlist *sl, *rawlist;
1525 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301526 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001528
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001529 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301530 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001531 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001532 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301533 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 if (ret < 0)
1535 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001536 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001537 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1538 tev.event);
1539 if (ret >= 0)
1540 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001541 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001542 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301543 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001544 if (ret < 0)
1545 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001546 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001547 strlist__delete(rawlist);
1548
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001549 if (ret < 0) {
1550 strlist__delete(sl);
1551 return NULL;
1552 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001553 return sl;
1554}
1555
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301556static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001557{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001558 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301559 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001560
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001561 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301562 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001563 return -EINVAL;
1564 }
1565
Masami Hiramatsufa282442009-12-08 17:03:23 -05001566 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001567 if (!probe_event_dry_run) {
1568 ret = write(fd, buf, strlen(buf));
1569 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001570 pr_warning("Failed to write event: %s\n",
1571 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001572 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001575}
1576
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001577static int get_new_event_name(char *buf, size_t len, const char *base,
1578 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001579{
1580 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001581
1582 /* Try no suffix */
1583 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001584 if (ret < 0) {
1585 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1586 return ret;
1587 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001588 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001589 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001590
Masami Hiramatsud761b082009-12-15 10:32:25 -05001591 if (!allow_suffix) {
1592 pr_warning("Error: event \"%s\" already exists. "
1593 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001595 }
1596
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001597 /* Try to add suffix */
1598 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001599 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001600 if (ret < 0) {
1601 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1602 return ret;
1603 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001604 if (!strlist__has_entry(namelist, buf))
1605 break;
1606 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001607 if (i == MAX_EVENT_INDEX) {
1608 pr_warning("Too many events are on the same function.\n");
1609 ret = -ERANGE;
1610 }
1611
1612 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001613}
1614
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301615static int __add_probe_trace_events(struct perf_probe_event *pev,
1616 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001617 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001618{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001619 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301620 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621 char buf[64];
1622 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001623 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001624
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001625 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626 if (fd < 0)
1627 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001628 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301629 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001630 if (!namelist) {
1631 pr_debug("Failed to get current event list.\n");
1632 return -EIO;
1633 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001634
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001635 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001637 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 tev = &tevs[i];
1639 if (pev->event)
1640 event = pev->event;
1641 else
1642 if (pev->point.function)
1643 event = pev->point.function;
1644 else
1645 event = tev->point.symbol;
1646 if (pev->group)
1647 group = pev->group;
1648 else
1649 group = PERFPROBE_GROUP;
1650
1651 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001652 ret = get_new_event_name(buf, 64, event,
1653 namelist, allow_suffix);
1654 if (ret < 0)
1655 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001656 event = buf;
1657
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001658 tev->event = strdup(event);
1659 tev->group = strdup(group);
1660 if (tev->event == NULL || tev->group == NULL) {
1661 ret = -ENOMEM;
1662 break;
1663 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301664 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001665 if (ret < 0)
1666 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001667 /* Add added event name to namelist */
1668 strlist__add(namelist, event);
1669
1670 /* Trick here - save current event/group */
1671 event = pev->event;
1672 group = pev->group;
1673 pev->event = tev->event;
1674 pev->group = tev->group;
1675 show_perf_probe_event(pev);
1676 /* Trick here - restore current event/group */
1677 pev->event = (char *)event;
1678 pev->group = (char *)group;
1679
1680 /*
1681 * Probes after the first probe which comes from same
1682 * user input are always allowed to add suffix, because
1683 * there might be several addresses corresponding to
1684 * one code line.
1685 */
1686 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001687 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001688
1689 if (ret >= 0) {
1690 /* Show how to use the event. */
1691 printf("\nYou can now use it on all perf tools, such as:\n\n");
1692 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1693 tev->event);
1694 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001695
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001696 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001697 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001698 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001699}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001700
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301701static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1702 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001703 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001704{
1705 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001706 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301707 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001708
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001709 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001710 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001711 if (ret != 0)
1712 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001713
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301715 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001716 if (tev == NULL)
1717 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001718
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001720 tev->point.symbol = strdup(pev->point.function);
1721 if (tev->point.symbol == NULL) {
1722 ret = -ENOMEM;
1723 goto error;
1724 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001725 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001726 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727 tev->nargs = pev->nargs;
1728 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301729 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001730 * tev->nargs);
1731 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001732 ret = -ENOMEM;
1733 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001734 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001735 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001736 if (pev->args[i].name) {
1737 tev->args[i].name = strdup(pev->args[i].name);
1738 if (tev->args[i].name == NULL) {
1739 ret = -ENOMEM;
1740 goto error;
1741 }
1742 }
1743 tev->args[i].value = strdup(pev->args[i].var);
1744 if (tev->args[i].value == NULL) {
1745 ret = -ENOMEM;
1746 goto error;
1747 }
1748 if (pev->args[i].type) {
1749 tev->args[i].type = strdup(pev->args[i].type);
1750 if (tev->args[i].type == NULL) {
1751 ret = -ENOMEM;
1752 goto error;
1753 }
1754 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001755 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001756 }
1757
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001758 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001759 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001760 if (!sym) {
1761 pr_warning("Kernel symbol \'%s\' not found.\n",
1762 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001763 ret = -ENOENT;
1764 goto error;
1765 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001766
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001767 return 1;
1768error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301769 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001770 free(tev);
1771 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001772 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773}
1774
1775struct __event_package {
1776 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301777 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778 int ntevs;
1779};
1780
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001781int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001782 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001783{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001784 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001785 struct __event_package *pkgs;
1786
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001787 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1788 if (pkgs == NULL)
1789 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001790
1791 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001792 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001793 if (ret < 0) {
1794 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001795 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001796 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001797
1798 /* Loop 1: convert all events */
1799 for (i = 0; i < npevs; i++) {
1800 pkgs[i].pev = &pevs[i];
1801 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301802 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001803 &pkgs[i].tevs,
1804 max_tevs,
1805 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001806 if (ret < 0)
1807 goto end;
1808 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001809 }
1810
1811 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001812 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301813 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001814 pkgs[i].ntevs, force_add);
1815end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001816 /* Loop 3: cleanup and free trace events */
1817 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001818 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301819 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001820 free(pkgs[i].tevs);
1821 }
1822 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001823
1824 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001825}
1826
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301827static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001828{
1829 char *p;
1830 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001832
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301833 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001834 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1835 if (ret < 0)
1836 goto error;
1837
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001838 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001839 if (!p) {
1840 pr_debug("Internal error: %s should have ':' but not.\n",
1841 ent->s);
1842 ret = -ENOTSUP;
1843 goto error;
1844 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001845 *p = '/';
1846
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001847 pr_debug("Writing event: %s\n", buf);
1848 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849 if (ret < 0)
1850 goto error;
1851
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001852 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001853 return 0;
1854error:
1855 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1856 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001857}
1858
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301859static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001860 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001861{
1862 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001863 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001864 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001865
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001866 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1867 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001868 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001869 return ret;
1870 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001871
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001872 if (strpbrk(buf, "*?")) { /* Glob-exp */
1873 strlist__for_each_safe(ent, n, namelist)
1874 if (strglobmatch(ent->s, buf)) {
1875 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301876 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001877 if (ret < 0)
1878 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001879 strlist__remove(namelist, ent);
1880 }
1881 } else {
1882 ent = strlist__find(namelist, buf);
1883 if (ent) {
1884 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301885 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001886 if (ret >= 0)
1887 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001888 }
1889 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001890 if (found == 0 && ret >= 0)
1891 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1892
1893 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001894}
1895
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001896int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001897{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001898 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001899 const char *group, *event;
1900 char *p, *str;
1901 struct str_node *ent;
1902 struct strlist *namelist;
1903
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001904 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001905 if (fd < 0)
1906 return fd;
1907
Masami Hiramatsufa282442009-12-08 17:03:23 -05001908 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301909 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001910 if (namelist == NULL)
1911 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001912
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001913 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001914 str = strdup(ent->s);
1915 if (str == NULL) {
1916 ret = -ENOMEM;
1917 break;
1918 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001919 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001920 p = strchr(str, ':');
1921 if (p) {
1922 group = str;
1923 *p = '\0';
1924 event = p + 1;
1925 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001926 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001927 event = str;
1928 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001929 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301930 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001931 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001932 if (ret < 0)
1933 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001934 }
1935 strlist__delete(namelist);
1936 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001937
1938 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001939}
1940
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001941/*
1942 * If a symbol corresponds to a function with global binding return 0.
1943 * For all others return 1.
1944 */
1945static int filter_non_global_functions(struct map *map __unused,
1946 struct symbol *sym)
1947{
1948 if (sym->binding != STB_GLOBAL)
1949 return 1;
1950
1951 return 0;
1952}
1953
1954int show_available_funcs(const char *module)
1955{
1956 struct map *map;
1957 int ret;
1958
1959 setup_pager();
1960
1961 ret = init_vmlinux();
1962 if (ret < 0)
1963 return ret;
1964
1965 map = kernel_get_module_map(module);
1966 if (!map) {
1967 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
1968 return -EINVAL;
1969 }
1970 if (map__load(map, filter_non_global_functions)) {
1971 pr_err("Failed to load map.\n");
1972 return -EINVAL;
1973 }
1974 if (!dso__sorted_by_name(map->dso, map->type))
1975 dso__sort_by_name(map->dso, map->type);
1976
1977 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
1978 return 0;
1979}