blob: 920c1957d6d334852889cde9cbb052d4b3d68d3f [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
Masami Hiramatsuff741782011-06-27 16:27:39 +0900173/* Open new debuginfo of given module */
174static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900175{
176 const char *path = kernel_get_module_path(module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900177
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900178 if (!path) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900179 pr_err("Failed to find path of %s module.\n",
180 module ?: "kernel");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900181 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900182 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900183 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400184}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300185
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530186/*
187 * Convert trace point to probe point with debuginfo
188 * Currently only handles kprobes.
189 */
190static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900191 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300192{
193 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900194 struct map *map;
195 u64 addr;
196 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900197 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300198
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900199 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300200 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900201 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200202 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900203 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900204
205 dinfo = debuginfo__new_online_kernel(addr);
206 if (dinfo) {
207 ret = debuginfo__find_probe_point(dinfo,
208 (unsigned long)addr, pp);
209 debuginfo__delete(dinfo);
210 } else {
211 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
212 addr);
213 ret = -ENOENT;
214 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300215 }
216 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400217 pr_debug("Failed to find corresponding probes from "
218 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400219 pp->function = strdup(tp->symbol);
220 if (pp->function == NULL)
221 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300222 pp->offset = tp->offset;
223 }
224 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400225
226 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300227}
228
229/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530230static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
231 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900232 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300233{
234 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900235 struct debuginfo *dinfo = open_debuginfo(module);
236 int ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300237
Masami Hiramatsuff741782011-06-27 16:27:39 +0900238 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400239 if (need_dwarf) {
240 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900241 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400242 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900243 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300244 return 0;
245 }
246
Masami Hiramatsuff741782011-06-27 16:27:39 +0900247 /* Searching trace events corresponding to a probe event */
248 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
249
250 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300251
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400252 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530253 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300254 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400255 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300256
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400257 if (ntevs == 0) { /* No error but failed to find probe point. */
258 pr_warning("Probe point '%s' not found.\n",
259 synthesize_perf_probe_point(&pev->point));
260 return -ENOENT;
261 }
262 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400263 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
264 if (ntevs == -EBADF) {
265 pr_warning("Warning: No dwarf info found in the vmlinux - "
266 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
267 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900268 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400269 return 0;
270 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300271 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400272 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300273}
274
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900275/*
276 * Find a src file from a DWARF tag path. Prepend optional source path prefix
277 * and chop off leading directories that do not exist. Result is passed back as
278 * a newly allocated path on success.
279 * Return 0 if file was found and readable, -errno otherwise.
280 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900281static int get_real_path(const char *raw_path, const char *comp_dir,
282 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900283{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900284 const char *prefix = symbol_conf.source_prefix;
285
286 if (!prefix) {
287 if (raw_path[0] != '/' && comp_dir)
288 /* If not an absolute path, try to use comp_dir */
289 prefix = comp_dir;
290 else {
291 if (access(raw_path, R_OK) == 0) {
292 *new_path = strdup(raw_path);
293 return 0;
294 } else
295 return -errno;
296 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900297 }
298
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900299 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900300 if (!*new_path)
301 return -ENOMEM;
302
303 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900304 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900305
306 if (access(*new_path, R_OK) == 0)
307 return 0;
308
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900309 if (!symbol_conf.source_prefix)
310 /* In case of searching comp_dir, don't retry */
311 return -errno;
312
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900313 switch (errno) {
314 case ENAMETOOLONG:
315 case ENOENT:
316 case EROFS:
317 case EFAULT:
318 raw_path = strchr(++raw_path, '/');
319 if (!raw_path) {
320 free(*new_path);
321 *new_path = NULL;
322 return -ENOENT;
323 }
324 continue;
325
326 default:
327 free(*new_path);
328 *new_path = NULL;
329 return -errno;
330 }
331 }
332}
333
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300334#define LINEBUF_SIZE 256
335#define NR_ADDITIONAL_LINES 2
336
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100337static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300338{
339 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100340 const char *color = show_num ? "" : PERF_COLOR_BLUE;
341 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300342
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100343 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300344 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
345 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100346 if (skip)
347 continue;
348 if (!prefix) {
349 prefix = show_num ? "%7d " : " ";
350 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300351 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100352 color_fprintf(stdout, color, "%s", buf);
353
354 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400355
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100356 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300357error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100358 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100359 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100360 return -1;
361 }
362 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300363}
364
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100365static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
366{
367 int rv = __show_one_line(fp, l, skip, show_num);
368 if (rv == 0) {
369 pr_warning("Source file is shorter than expected.\n");
370 rv = -1;
371 }
372 return rv;
373}
374
375#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
376#define show_one_line(f,l) _show_one_line(f,l,false,false)
377#define skip_one_line(f,l) _show_one_line(f,l,true,false)
378#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
379
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300380/*
381 * Show line-range always requires debuginfo to find source file and
382 * line number.
383 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900384int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300385{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400386 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300387 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900388 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300389 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900390 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900391 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300392
393 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400394 ret = init_vmlinux();
395 if (ret < 0)
396 return ret;
397
Masami Hiramatsuff741782011-06-27 16:27:39 +0900398 dinfo = open_debuginfo(module);
399 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400400 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900401 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400402 }
403
Masami Hiramatsuff741782011-06-27 16:27:39 +0900404 ret = debuginfo__find_line_range(dinfo, lr);
405 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400406 if (ret == 0) {
407 pr_warning("Specified source line is not found.\n");
408 return -ENOENT;
409 } else if (ret < 0) {
410 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
411 return ret;
412 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300413
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900414 /* Convert source file path */
415 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900416 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900417 free(tmp); /* Free old path */
418 if (ret < 0) {
419 pr_warning("Failed to find source file. (%d)\n", ret);
420 return ret;
421 }
422
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300423 setup_pager();
424
425 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900426 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300427 lr->start - lr->offset);
428 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100429 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300430
431 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400432 if (fp == NULL) {
433 pr_warning("Failed to open %s: %s\n", lr->path,
434 strerror(errno));
435 return -errno;
436 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300437 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100438 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100439 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100440 if (ret < 0)
441 goto end;
442 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300443
444 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100445 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100446 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100447 if (ret < 0)
448 goto end;
449 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100450 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400451 if (ret < 0)
452 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300453 }
454
455 if (lr->end == INT_MAX)
456 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100457 while (l <= lr->end) {
458 ret = show_one_line_or_eof(fp, l++ - lr->offset);
459 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100460 break;
461 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400462end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300463 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400464 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300465}
466
Masami Hiramatsuff741782011-06-27 16:27:39 +0900467static int show_available_vars_at(struct debuginfo *dinfo,
468 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900469 int max_vls, struct strfilter *_filter,
470 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900471{
472 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900473 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900474 struct str_node *node;
475 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900476 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900477
478 buf = synthesize_perf_probe_point(&pev->point);
479 if (!buf)
480 return -EINVAL;
481 pr_debug("Searching variables at %s\n", buf);
482
Masami Hiramatsuff741782011-06-27 16:27:39 +0900483 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
484 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900485 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900486 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900487 goto end;
488 }
489 /* Some variables are found */
490 fprintf(stdout, "Available variables at %s\n", buf);
491 for (i = 0; i < ret; i++) {
492 vl = &vls[i];
493 /*
494 * A probe point might be converted to
495 * several trace points.
496 */
497 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
498 vl->point.offset);
499 free(vl->point.symbol);
500 nvars = 0;
501 if (vl->vars) {
502 strlist__for_each(node, vl->vars) {
503 var = strchr(node->s, '\t') + 1;
504 if (strfilter__compare(_filter, var)) {
505 fprintf(stdout, "\t\t%s\n", node->s);
506 nvars++;
507 }
508 }
509 strlist__delete(vl->vars);
510 }
511 if (nvars == 0)
512 fprintf(stdout, "\t\t(No matched variables)\n");
513 }
514 free(vls);
515end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900516 free(buf);
517 return ret;
518}
519
520/* Show available variables on given probe point */
521int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900522 int max_vls, const char *module,
523 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900524{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900525 int i, ret = 0;
526 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900527
528 ret = init_vmlinux();
529 if (ret < 0)
530 return ret;
531
Masami Hiramatsuff741782011-06-27 16:27:39 +0900532 dinfo = open_debuginfo(module);
533 if (!dinfo) {
534 pr_warning("Failed to open debuginfo file.\n");
535 return -ENOENT;
536 }
537
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900538 setup_pager();
539
Masami Hiramatsuff741782011-06-27 16:27:39 +0900540 for (i = 0; i < npevs && ret >= 0; i++)
541 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900542 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900543
544 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900545 return ret;
546}
547
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300548#else /* !DWARF_SUPPORT */
549
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530550static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900551 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300552{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900553 struct symbol *sym;
554
555 sym = __find_kernel_function_by_name(tp->symbol, NULL);
556 if (!sym) {
557 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
558 return -ENOENT;
559 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400560 pp->function = strdup(tp->symbol);
561 if (pp->function == NULL)
562 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300563 pp->offset = tp->offset;
564 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400565
566 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300567}
568
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530569static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
570 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900571 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300572{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400573 if (perf_probe_event_need_dwarf(pev)) {
574 pr_warning("Debuginfo-analysis is not supported.\n");
575 return -ENOSYS;
576 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300577 return 0;
578}
579
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900580int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300581{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400582 pr_warning("Debuginfo-analysis is not supported.\n");
583 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300584}
585
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900586int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900587 int npevs __unused, int max_vls __unused,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900588 const char *module __unused,
589 struct strfilter *filter __unused,
590 bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900591{
592 pr_warning("Debuginfo-analysis is not supported.\n");
593 return -ENOSYS;
594}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400595#endif
596
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100597static int parse_line_num(char **ptr, int *val, const char *what)
598{
599 const char *start = *ptr;
600
601 errno = 0;
602 *val = strtol(*ptr, ptr, 0);
603 if (errno || *ptr == start) {
604 semantic_error("'%s' is not a valid number.\n", what);
605 return -EINVAL;
606 }
607 return 0;
608}
609
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100610/*
611 * Stuff 'lr' according to the line range described by 'arg'.
612 * The line range syntax is described by:
613 *
614 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900615 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100616 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400617int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500618{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900619 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100620 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100621
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100622 if (!name)
623 return -ENOMEM;
624
625 lr->start = 0;
626 lr->end = INT_MAX;
627
628 range = strchr(name, ':');
629 if (range) {
630 *range++ = '\0';
631
632 err = parse_line_num(&range, &lr->start, "start line");
633 if (err)
634 goto err;
635
636 if (*range == '+' || *range == '-') {
637 const char c = *range++;
638
639 err = parse_line_num(&range, &lr->end, "end line");
640 if (err)
641 goto err;
642
643 if (c == '+') {
644 lr->end += lr->start;
645 /*
646 * Adjust the number of lines here.
647 * If the number of lines == 1, the
648 * the end of line should be equal to
649 * the start of line.
650 */
651 lr->end--;
652 }
653 }
654
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400655 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100656
657 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400658 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500659 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400660 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100661 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400662 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100663 if (*range != '\0') {
664 semantic_error("Tailing with invalid str '%s'.\n", range);
665 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400666 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400667 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400668
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900669 file = strchr(name, '@');
670 if (file) {
671 *file = '\0';
672 lr->file = strdup(++file);
673 if (lr->file == NULL) {
674 err = -ENOMEM;
675 goto err;
676 }
677 lr->function = name;
678 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100679 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500680 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100681 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400682
683 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100684err:
685 free(name);
686 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500687}
688
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500689/* Check the name is good for event/group */
690static bool check_event_name(const char *name)
691{
692 if (!isalpha(*name) && *name != '_')
693 return false;
694 while (*++name != '\0') {
695 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
696 return false;
697 }
698 return true;
699}
700
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500701/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500703{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400704 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500705 char *ptr, *tmp;
706 char c, nc = 0;
707 /*
708 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500709 * perf probe [EVENT=]SRC[:LN|;PTN]
710 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500711 *
712 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500713 */
714
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500715 ptr = strpbrk(arg, ";=@+%");
716 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500717 *ptr = '\0';
718 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400719 if (strchr(arg, ':')) {
720 semantic_error("Group name is not supported yet.\n");
721 return -ENOTSUP;
722 }
723 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500724 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400725 "follow C symbol-naming rule.\n", arg);
726 return -EINVAL;
727 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400728 pev->event = strdup(arg);
729 if (pev->event == NULL)
730 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400731 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500732 arg = tmp;
733 }
734
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500735 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500736 if (ptr) {
737 nc = *ptr;
738 *ptr++ = '\0';
739 }
740
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400741 tmp = strdup(arg);
742 if (tmp == NULL)
743 return -ENOMEM;
744
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500745 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400746 if (strchr(tmp, '.')) /* File */
747 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500748 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400749 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500750
751 /* Parse other options */
752 while (ptr) {
753 arg = ptr;
754 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500755 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400756 pp->lazy_line = strdup(arg);
757 if (pp->lazy_line == NULL)
758 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500759 break;
760 }
761 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500762 if (ptr) {
763 nc = *ptr;
764 *ptr++ = '\0';
765 }
766 switch (c) {
767 case ':': /* Line number */
768 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400769 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500770 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 " in line number.\n");
772 return -EINVAL;
773 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500774 break;
775 case '+': /* Byte offset from a symbol */
776 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400777 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500778 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400779 " in offset.\n");
780 return -EINVAL;
781 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500782 break;
783 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400784 if (pp->file) {
785 semantic_error("SRC@SRC is not allowed.\n");
786 return -EINVAL;
787 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400788 pp->file = strdup(arg);
789 if (pp->file == NULL)
790 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500791 break;
792 case '%': /* Probe places */
793 if (strcmp(arg, "return") == 0) {
794 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400795 } else { /* Others not supported yet */
796 semantic_error("%%%s is not supported.\n", arg);
797 return -ENOTSUP;
798 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500799 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400800 default: /* Buggy case */
801 pr_err("This program has a bug at %s:%d.\n",
802 __FILE__, __LINE__);
803 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500804 break;
805 }
806 }
807
808 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400809 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900810 semantic_error("Lazy pattern can't be used with"
811 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400812 return -EINVAL;
813 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500814
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400815 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900816 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400817 return -EINVAL;
818 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500819
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400820 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900821 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400822 return -EINVAL;
823 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500824
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400825 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500826 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900827 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400828 return -EINVAL;
829 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500830
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900832 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400833 return -EINVAL;
834 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500835
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400836 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900837 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838 return -EINVAL;
839 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500840
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400841 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500842 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900843 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844 return -EINVAL;
845 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500846
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400847 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500848 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
849 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400850 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500851}
852
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400853/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400854static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400855{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400856 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400857 struct perf_probe_arg_field **fieldp;
858
859 pr_debug("parsing arg: %s into ", str);
860
Masami Hiramatsu48481932010-04-12 13:16:53 -0400861 tmp = strchr(str, '=');
862 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400863 arg->name = strndup(str, tmp - str);
864 if (arg->name == NULL)
865 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400866 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400867 str = tmp + 1;
868 }
869
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400870 tmp = strchr(str, ':');
871 if (tmp) { /* Type setting */
872 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400873 arg->type = strdup(tmp + 1);
874 if (arg->type == NULL)
875 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400876 pr_debug("type:%s ", arg->type);
877 }
878
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400879 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400880 if (!is_c_varname(str) || !tmp) {
881 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400882 arg->var = strdup(str);
883 if (arg->var == NULL)
884 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400885 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400886 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400887 }
888
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400889 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400890 arg->var = strndup(str, tmp - str);
891 if (arg->var == NULL)
892 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400893 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400894 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400895 fieldp = &arg->field;
896
897 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400898 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
899 if (*fieldp == NULL)
900 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400901 if (*tmp == '[') { /* Array */
902 str = tmp;
903 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400904 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400905 if (*tmp != ']' || tmp == str + 1) {
906 semantic_error("Array index must be a"
907 " number.\n");
908 return -EINVAL;
909 }
910 tmp++;
911 if (*tmp == '\0')
912 tmp = NULL;
913 } else { /* Structure */
914 if (*tmp == '.') {
915 str = tmp + 1;
916 (*fieldp)->ref = false;
917 } else if (tmp[1] == '>') {
918 str = tmp + 2;
919 (*fieldp)->ref = true;
920 } else {
921 semantic_error("Argument parse error: %s\n",
922 str);
923 return -EINVAL;
924 }
925 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400926 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400927 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400928 (*fieldp)->name = strndup(str, tmp - str);
929 if ((*fieldp)->name == NULL)
930 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400931 if (*str != '[')
932 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400933 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
934 fieldp = &(*fieldp)->next;
935 }
936 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400937 (*fieldp)->name = strdup(str);
938 if ((*fieldp)->name == NULL)
939 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400940 if (*str != '[')
941 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400942 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400943
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400944 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400945 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400946 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400947 if (arg->name == NULL)
948 return -ENOMEM;
949 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400950 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400951}
952
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400953/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400954int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500955{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500956 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400957 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500958
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400960 if (!argv) {
961 pr_debug("Failed to split arguments.\n");
962 return -ENOMEM;
963 }
964 if (argc - 1 > MAX_PROBE_ARGS) {
965 semantic_error("Too many probe arguments (%d).\n", argc - 1);
966 ret = -ERANGE;
967 goto out;
968 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500969 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400970 ret = parse_perf_probe_point(argv[0], pev);
971 if (ret < 0)
972 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500973
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500974 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400975 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400976 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
977 if (pev->args == NULL) {
978 ret = -ENOMEM;
979 goto out;
980 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400981 for (i = 0; i < pev->nargs && ret >= 0; i++) {
982 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
983 if (ret >= 0 &&
984 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400985 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 " kretprobe.\n");
987 ret = -EINVAL;
988 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500989 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400990out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500991 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400992
993 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500994}
995
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400996/* Return true if this perf_probe_event requires debuginfo */
997bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500998{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400999 int i;
1000
1001 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1002 return true;
1003
1004 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001005 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001006 return true;
1007
1008 return false;
1009}
1010
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301011/* Parse probe_events event into struct probe_point */
1012static int parse_probe_trace_command(const char *cmd,
1013 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001014{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301015 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001016 char pr;
1017 char *p;
1018 int ret, i, argc;
1019 char **argv;
1020
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301021 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001022 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001023 if (!argv) {
1024 pr_debug("Failed to split arguments.\n");
1025 return -ENOMEM;
1026 }
1027 if (argc < 2) {
1028 semantic_error("Too few probe arguments.\n");
1029 ret = -ERANGE;
1030 goto out;
1031 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001032
1033 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +08001034 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001035 &pr, (float *)(void *)&tev->group,
1036 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001037 if (ret != 3) {
1038 semantic_error("Failed to parse event name: %s\n", argv[0]);
1039 ret = -EINVAL;
1040 goto out;
1041 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001042 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001043
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001044 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001045
1046 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001047 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
1048 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001049 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001050 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001051
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001052 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301053 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001054 if (tev->args == NULL) {
1055 ret = -ENOMEM;
1056 goto out;
1057 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001058 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001059 p = strchr(argv[i + 2], '=');
1060 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001061 *p++ = '\0';
1062 else
1063 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001064 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001065 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001066 tev->args[i].value = strdup(p);
1067 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1068 ret = -ENOMEM;
1069 goto out;
1070 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001071 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001072 ret = 0;
1073out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001074 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001075 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001076}
1077
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001078/* Compose only probe arg */
1079int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1080{
1081 struct perf_probe_arg_field *field = pa->field;
1082 int ret;
1083 char *tmp = buf;
1084
Masami Hiramatsu48481932010-04-12 13:16:53 -04001085 if (pa->name && pa->var)
1086 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1087 else
1088 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001089 if (ret <= 0)
1090 goto error;
1091 tmp += ret;
1092 len -= ret;
1093
1094 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001095 if (field->name[0] == '[')
1096 ret = e_snprintf(tmp, len, "%s", field->name);
1097 else
1098 ret = e_snprintf(tmp, len, "%s%s",
1099 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001100 if (ret <= 0)
1101 goto error;
1102 tmp += ret;
1103 len -= ret;
1104 field = field->next;
1105 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001106
1107 if (pa->type) {
1108 ret = e_snprintf(tmp, len, ":%s", pa->type);
1109 if (ret <= 0)
1110 goto error;
1111 tmp += ret;
1112 len -= ret;
1113 }
1114
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001115 return tmp - buf;
1116error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001117 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001118 strerror(-ret));
1119 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001120}
1121
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001122/* Compose only probe point (not argument) */
1123static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001124{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001125 char *buf, *tmp;
1126 char offs[32] = "", line[32] = "", file[32] = "";
1127 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001128
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001129 buf = zalloc(MAX_CMDLEN);
1130 if (buf == NULL) {
1131 ret = -ENOMEM;
1132 goto error;
1133 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001134 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001135 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001136 if (ret <= 0)
1137 goto error;
1138 }
1139 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001140 ret = e_snprintf(line, 32, ":%d", pp->line);
1141 if (ret <= 0)
1142 goto error;
1143 }
1144 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001145 tmp = pp->file;
1146 len = strlen(tmp);
1147 if (len > 30) {
1148 tmp = strchr(pp->file + len - 30, '/');
1149 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1150 }
1151 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001152 if (ret <= 0)
1153 goto error;
1154 }
1155
1156 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001157 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1158 offs, pp->retprobe ? "%return" : "", line,
1159 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001160 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001161 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001162 if (ret <= 0)
1163 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001164
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001165 return buf;
1166error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001167 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001168 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001169 if (buf)
1170 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001171 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001172}
1173
1174#if 0
1175char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1176{
1177 char *buf;
1178 int i, len, ret;
1179
1180 buf = synthesize_perf_probe_point(&pev->point);
1181 if (!buf)
1182 return NULL;
1183
1184 len = strlen(buf);
1185 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001187 pev->args[i].name);
1188 if (ret <= 0) {
1189 free(buf);
1190 return NULL;
1191 }
1192 len += ret;
1193 }
1194
1195 return buf;
1196}
1197#endif
1198
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301199static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001200 char **buf, size_t *buflen,
1201 int depth)
1202{
1203 int ret;
1204 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301205 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001206 buflen, depth + 1);
1207 if (depth < 0)
1208 goto out;
1209 }
1210
1211 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1212 if (ret < 0)
1213 depth = ret;
1214 else {
1215 *buf += ret;
1216 *buflen -= ret;
1217 }
1218out:
1219 return depth;
1220
1221}
1222
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301223static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001224 char *buf, size_t buflen)
1225{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301226 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001227 int ret, depth = 0;
1228 char *tmp = buf;
1229
1230 /* Argument name or separator */
1231 if (arg->name)
1232 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1233 else
1234 ret = e_snprintf(buf, buflen, " ");
1235 if (ret < 0)
1236 return ret;
1237 buf += ret;
1238 buflen -= ret;
1239
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001240 /* Special case: @XXX */
1241 if (arg->value[0] == '@' && arg->ref)
1242 ref = ref->next;
1243
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001245 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301246 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001247 &buflen, 1);
1248 if (depth < 0)
1249 return depth;
1250 }
1251
1252 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001253 if (arg->value[0] == '@' && arg->ref)
1254 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1255 arg->ref->offset);
1256 else
1257 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001258 if (ret < 0)
1259 return ret;
1260 buf += ret;
1261 buflen -= ret;
1262
1263 /* Closing */
1264 while (depth--) {
1265 ret = e_snprintf(buf, buflen, ")");
1266 if (ret < 0)
1267 return ret;
1268 buf += ret;
1269 buflen -= ret;
1270 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001271 /* Print argument type */
1272 if (arg->type) {
1273 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1274 if (ret <= 0)
1275 return ret;
1276 buf += ret;
1277 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278
1279 return buf - tmp;
1280}
1281
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301282char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001283{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301284 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001285 char *buf;
1286 int i, len, ret;
1287
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001288 buf = zalloc(MAX_CMDLEN);
1289 if (buf == NULL)
1290 return NULL;
1291
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001292 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1293 tp->retprobe ? 'r' : 'p',
1294 tev->group, tev->event,
1295 tp->symbol, tp->offset);
1296 if (len <= 0)
1297 goto error;
1298
1299 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301300 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001301 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001302 if (ret <= 0)
1303 goto error;
1304 len += ret;
1305 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001306
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001307 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001308error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001309 free(buf);
1310 return NULL;
1311}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001312
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301313static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001314 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001315{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001316 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001318
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001319 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 pev->event = strdup(tev->event);
1321 pev->group = strdup(tev->group);
1322 if (pev->event == NULL || pev->group == NULL)
1323 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001324
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001325 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301326 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 if (ret < 0)
1328 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001329
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001330 /* Convert trace_arg to probe_arg */
1331 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001332 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1333 if (pev->args == NULL)
1334 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001335 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001336 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001337 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001338 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301339 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001340 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001341 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001342 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001343 if (pev->args[i].name == NULL && ret >= 0)
1344 ret = -ENOMEM;
1345 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001346
1347 if (ret < 0)
1348 clear_perf_probe_event(pev);
1349
1350 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001351}
1352
1353void clear_perf_probe_event(struct perf_probe_event *pev)
1354{
1355 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001356 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001357 int i;
1358
1359 if (pev->event)
1360 free(pev->event);
1361 if (pev->group)
1362 free(pev->group);
1363 if (pp->file)
1364 free(pp->file);
1365 if (pp->function)
1366 free(pp->function);
1367 if (pp->lazy_line)
1368 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001369 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001370 if (pev->args[i].name)
1371 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001372 if (pev->args[i].var)
1373 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001374 if (pev->args[i].type)
1375 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001376 field = pev->args[i].field;
1377 while (field) {
1378 next = field->next;
1379 if (field->name)
1380 free(field->name);
1381 free(field);
1382 field = next;
1383 }
1384 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001385 if (pev->args)
1386 free(pev->args);
1387 memset(pev, 0, sizeof(*pev));
1388}
1389
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301390static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301392 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001393 int i;
1394
1395 if (tev->event)
1396 free(tev->event);
1397 if (tev->group)
1398 free(tev->group);
1399 if (tev->point.symbol)
1400 free(tev->point.symbol);
1401 for (i = 0; i < tev->nargs; i++) {
1402 if (tev->args[i].name)
1403 free(tev->args[i].name);
1404 if (tev->args[i].value)
1405 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001406 if (tev->args[i].type)
1407 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001408 ref = tev->args[i].ref;
1409 while (ref) {
1410 next = ref->next;
1411 free(ref);
1412 ref = next;
1413 }
1414 }
1415 if (tev->args)
1416 free(tev->args);
1417 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001418}
1419
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001420static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001421{
1422 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001423 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001424 int ret;
1425
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001426 __debugfs = debugfs_find_mountpoint();
1427 if (__debugfs == NULL) {
1428 pr_warning("Debugfs is not mounted.\n");
1429 return -ENOENT;
1430 }
1431
1432 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001434 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 if (readwrite && !probe_event_dry_run)
1436 ret = open(buf, O_RDWR, O_APPEND);
1437 else
1438 ret = open(buf, O_RDONLY, 0);
1439 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001440
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001441 if (ret < 0) {
1442 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443 pr_warning("kprobe_events file does not exist - please"
1444 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001445 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001446 pr_warning("Failed to open kprobe_events file: %s\n",
1447 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001448 }
1449 return ret;
1450}
1451
1452/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301453static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001454{
1455 int ret, idx;
1456 FILE *fp;
1457 char buf[MAX_CMDLEN];
1458 char *p;
1459 struct strlist *sl;
1460
1461 sl = strlist__new(true, NULL);
1462
1463 fp = fdopen(dup(fd), "r");
1464 while (!feof(fp)) {
1465 p = fgets(buf, MAX_CMDLEN, fp);
1466 if (!p)
1467 break;
1468
1469 idx = strlen(p) - 1;
1470 if (p[idx] == '\n')
1471 p[idx] = '\0';
1472 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001473 if (ret < 0) {
1474 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1475 strlist__delete(sl);
1476 return NULL;
1477 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478 }
1479 fclose(fp);
1480
1481 return sl;
1482}
1483
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001484/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001485static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001486{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001487 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001488 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001489 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001490
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001491 /* Synthesize only event probe point */
1492 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001493 if (!place)
1494 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001495
1496 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001497 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001498 return ret;
1499
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001500 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001501
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001502 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001503 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001504 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001505 ret = synthesize_perf_probe_arg(&pev->args[i],
1506 buf, 128);
1507 if (ret < 0)
1508 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001509 printf(" %s", buf);
1510 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001511 }
1512 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001514 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001515}
1516
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001517/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001519{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001520 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301521 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001522 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001523 struct strlist *rawlist;
1524 struct str_node *ent;
1525
Masami Hiramatsu72041332010-01-05 17:47:10 -05001526 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 ret = init_vmlinux();
1528 if (ret < 0)
1529 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001530
1531 memset(&tev, 0, sizeof(tev));
1532 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001533
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001534 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535 if (fd < 0)
1536 return fd;
1537
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301538 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001539 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001540 if (!rawlist)
1541 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001542
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001543 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301544 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001545 if (ret >= 0) {
1546 ret = convert_to_perf_probe_event(&tev, &pev);
1547 if (ret >= 0)
1548 ret = show_perf_probe_event(&pev);
1549 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001550 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301551 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001552 if (ret < 0)
1553 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001554 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001555 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001556
1557 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001558}
1559
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001560/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301561static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001562{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001563 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001564 struct strlist *sl, *rawlist;
1565 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301566 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001567 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001568
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001569 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301570 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001571 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001572 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301573 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 if (ret < 0)
1575 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001576 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001577 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1578 tev.event);
1579 if (ret >= 0)
1580 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001581 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301583 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001584 if (ret < 0)
1585 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001586 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001587 strlist__delete(rawlist);
1588
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001589 if (ret < 0) {
1590 strlist__delete(sl);
1591 return NULL;
1592 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001593 return sl;
1594}
1595
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301596static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001597{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001598 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301599 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001600
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301602 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001603 return -EINVAL;
1604 }
1605
Masami Hiramatsufa282442009-12-08 17:03:23 -05001606 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001607 if (!probe_event_dry_run) {
1608 ret = write(fd, buf, strlen(buf));
1609 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610 pr_warning("Failed to write event: %s\n",
1611 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001612 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001614 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001615}
1616
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001617static int get_new_event_name(char *buf, size_t len, const char *base,
1618 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001619{
1620 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001621
1622 /* Try no suffix */
1623 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001624 if (ret < 0) {
1625 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1626 return ret;
1627 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001628 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001629 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001630
Masami Hiramatsud761b082009-12-15 10:32:25 -05001631 if (!allow_suffix) {
1632 pr_warning("Error: event \"%s\" already exists. "
1633 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001634 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001635 }
1636
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001637 /* Try to add suffix */
1638 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001639 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001640 if (ret < 0) {
1641 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1642 return ret;
1643 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001644 if (!strlist__has_entry(namelist, buf))
1645 break;
1646 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001647 if (i == MAX_EVENT_INDEX) {
1648 pr_warning("Too many events are on the same function.\n");
1649 ret = -ERANGE;
1650 }
1651
1652 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001653}
1654
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301655static int __add_probe_trace_events(struct perf_probe_event *pev,
1656 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001657 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001658{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001659 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301660 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 char buf[64];
1662 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001663 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001664
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001665 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001666 if (fd < 0)
1667 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001668 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301669 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001670 if (!namelist) {
1671 pr_debug("Failed to get current event list.\n");
1672 return -EIO;
1673 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001674
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001675 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001676 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001677 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001678 tev = &tevs[i];
1679 if (pev->event)
1680 event = pev->event;
1681 else
1682 if (pev->point.function)
1683 event = pev->point.function;
1684 else
1685 event = tev->point.symbol;
1686 if (pev->group)
1687 group = pev->group;
1688 else
1689 group = PERFPROBE_GROUP;
1690
1691 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001692 ret = get_new_event_name(buf, 64, event,
1693 namelist, allow_suffix);
1694 if (ret < 0)
1695 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 event = buf;
1697
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001698 tev->event = strdup(event);
1699 tev->group = strdup(group);
1700 if (tev->event == NULL || tev->group == NULL) {
1701 ret = -ENOMEM;
1702 break;
1703 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301704 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001705 if (ret < 0)
1706 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707 /* Add added event name to namelist */
1708 strlist__add(namelist, event);
1709
1710 /* Trick here - save current event/group */
1711 event = pev->event;
1712 group = pev->group;
1713 pev->event = tev->event;
1714 pev->group = tev->group;
1715 show_perf_probe_event(pev);
1716 /* Trick here - restore current event/group */
1717 pev->event = (char *)event;
1718 pev->group = (char *)group;
1719
1720 /*
1721 * Probes after the first probe which comes from same
1722 * user input are always allowed to add suffix, because
1723 * there might be several addresses corresponding to
1724 * one code line.
1725 */
1726 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001727 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001728
1729 if (ret >= 0) {
1730 /* Show how to use the event. */
1731 printf("\nYou can now use it on all perf tools, such as:\n\n");
1732 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1733 tev->event);
1734 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001735
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001736 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001737 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001738 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001739}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001740
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301741static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1742 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001743 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001744{
1745 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001746 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301747 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001749 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001750 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001751 if (ret != 0)
1752 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001753
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001754 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301755 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001756 if (tev == NULL)
1757 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001758
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001759 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001760 tev->point.symbol = strdup(pev->point.function);
1761 if (tev->point.symbol == NULL) {
1762 ret = -ENOMEM;
1763 goto error;
1764 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001766 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001767 tev->nargs = pev->nargs;
1768 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301769 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001770 * tev->nargs);
1771 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001772 ret = -ENOMEM;
1773 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001774 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001775 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001776 if (pev->args[i].name) {
1777 tev->args[i].name = strdup(pev->args[i].name);
1778 if (tev->args[i].name == NULL) {
1779 ret = -ENOMEM;
1780 goto error;
1781 }
1782 }
1783 tev->args[i].value = strdup(pev->args[i].var);
1784 if (tev->args[i].value == NULL) {
1785 ret = -ENOMEM;
1786 goto error;
1787 }
1788 if (pev->args[i].type) {
1789 tev->args[i].type = strdup(pev->args[i].type);
1790 if (tev->args[i].type == NULL) {
1791 ret = -ENOMEM;
1792 goto error;
1793 }
1794 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001795 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001796 }
1797
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001798 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001799 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001800 if (!sym) {
1801 pr_warning("Kernel symbol \'%s\' not found.\n",
1802 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001803 ret = -ENOENT;
1804 goto error;
1805 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001806
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001807 return 1;
1808error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301809 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001810 free(tev);
1811 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001812 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001813}
1814
1815struct __event_package {
1816 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301817 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001818 int ntevs;
1819};
1820
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001821int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001822 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001823{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001824 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001825 struct __event_package *pkgs;
1826
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001827 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1828 if (pkgs == NULL)
1829 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001830
1831 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001832 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001833 if (ret < 0) {
1834 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001835 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001836 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001837
1838 /* Loop 1: convert all events */
1839 for (i = 0; i < npevs; i++) {
1840 pkgs[i].pev = &pevs[i];
1841 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301842 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001843 &pkgs[i].tevs,
1844 max_tevs,
1845 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001846 if (ret < 0)
1847 goto end;
1848 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001849 }
1850
1851 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001852 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301853 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001854 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001855 if (ret < 0)
1856 break;
1857 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001858end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001859 /* Loop 3: cleanup and free trace events */
1860 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001861 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301862 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001863 free(pkgs[i].tevs);
1864 }
1865 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001866
1867 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001868}
1869
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301870static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001871{
1872 char *p;
1873 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001874 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001875
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301876 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001877 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1878 if (ret < 0)
1879 goto error;
1880
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001881 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001882 if (!p) {
1883 pr_debug("Internal error: %s should have ':' but not.\n",
1884 ent->s);
1885 ret = -ENOTSUP;
1886 goto error;
1887 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001888 *p = '/';
1889
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890 pr_debug("Writing event: %s\n", buf);
1891 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892 if (ret < 0)
1893 goto error;
1894
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001895 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001896 return 0;
1897error:
1898 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1899 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001900}
1901
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301902static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001903 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001904{
1905 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001906 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001907 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001908
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001909 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1910 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001911 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001912 return ret;
1913 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001914
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001915 if (strpbrk(buf, "*?")) { /* Glob-exp */
1916 strlist__for_each_safe(ent, n, namelist)
1917 if (strglobmatch(ent->s, buf)) {
1918 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301919 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001920 if (ret < 0)
1921 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001922 strlist__remove(namelist, ent);
1923 }
1924 } else {
1925 ent = strlist__find(namelist, buf);
1926 if (ent) {
1927 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301928 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001929 if (ret >= 0)
1930 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001931 }
1932 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001933 if (found == 0 && ret >= 0)
1934 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1935
1936 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001937}
1938
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001939int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001940{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001941 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001942 const char *group, *event;
1943 char *p, *str;
1944 struct str_node *ent;
1945 struct strlist *namelist;
1946
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001947 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001948 if (fd < 0)
1949 return fd;
1950
Masami Hiramatsufa282442009-12-08 17:03:23 -05001951 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301952 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001953 if (namelist == NULL)
1954 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001955
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001956 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001957 str = strdup(ent->s);
1958 if (str == NULL) {
1959 ret = -ENOMEM;
1960 break;
1961 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001962 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001963 p = strchr(str, ':');
1964 if (p) {
1965 group = str;
1966 *p = '\0';
1967 event = p + 1;
1968 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001969 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001970 event = str;
1971 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001972 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301973 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001974 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001975 if (ret < 0)
1976 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001977 }
1978 strlist__delete(namelist);
1979 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001980
1981 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001982}
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001983/* TODO: don't use a global variable for filter ... */
1984static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001985
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001986/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001987 * If a symbol corresponds to a function with global binding and
1988 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001989 */
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001990static int filter_available_functions(struct map *map __unused,
1991 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001992{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001993 if (sym->binding == STB_GLOBAL &&
1994 strfilter__compare(available_func_filter, sym->name))
1995 return 0;
1996 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09001997}
1998
Masami Hiramatsu3c422582011-01-20 23:15:45 +09001999int show_available_funcs(const char *module, struct strfilter *_filter)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002000{
2001 struct map *map;
2002 int ret;
2003
2004 setup_pager();
2005
2006 ret = init_vmlinux();
2007 if (ret < 0)
2008 return ret;
2009
2010 map = kernel_get_module_map(module);
2011 if (!map) {
2012 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2013 return -EINVAL;
2014 }
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002015 available_func_filter = _filter;
2016 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002017 pr_err("Failed to load map.\n");
2018 return -EINVAL;
2019 }
2020 if (!dso__sorted_by_name(map->dso, map->type))
2021 dso__sort_by_name(map->dso, map->type);
2022
2023 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2024 return 0;
2025}