blob: 4fb480367c3ec5e861827a2645f941f8ef595f2d [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
2 * probe-event.c : perf-probe definition to kprobe_events format converter
3 *
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 Hiramatsu50656ee2009-11-30 19:19:58 -050034
35#undef _GNU_SOURCE
Masami Hiramatsu31facc52010-03-16 18:05:30 -040036#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050037#include "event.h"
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050038#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050039#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050040#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050041#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050042#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040043#include "symbol.h"
44#include "thread.h"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040045#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030046#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040048#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
51#define MAX_PROBE_ARGS 128
52#define PERFPROBE_GROUP "probe"
53
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040054bool probe_event_dry_run; /* Dry run flag */
55
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050058/* If there is no space to write, returns -E2BIG. */
59static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050060 __attribute__((format(printf, 3, 4)));
61
62static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050063{
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
72}
73
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030074static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075static struct map_groups kmap_groups;
76static struct map *kmaps[MAP__NR_TYPES];
77
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030078/* Initialize symbol maps and path of vmlinux */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080081 struct dso *kernel;
Masami Hiramatsu146a1432010-04-12 13:17:42 -040082 int ret;
83
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084 symbol_conf.sort_by_name = true;
85 if (symbol_conf.vmlinux_name == NULL)
86 symbol_conf.try_vmlinux_path = true;
87 else
88 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040089 ret = symbol__init();
90 if (ret < 0) {
91 pr_debug("Failed to init symbol map.\n");
92 goto out;
93 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040094
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080095 kernel = dso__new_kernel(symbol_conf.vmlinux_name);
96 if (kernel == NULL)
97 die("Failed to create kernel dso.");
98
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040099 map_groups__init(&kmap_groups);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800100 ret = __map_groups__create_kernel_maps(&kmap_groups, kmaps, kernel);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101 if (ret < 0)
102 pr_debug("Failed to create kernel maps.\n");
103
104out:
105 if (ret < 0)
106 pr_warning("Failed to init vmlinux path.\n");
107 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400108}
109
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300110#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400111static int open_vmlinux(void)
112{
113 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
114 pr_debug("Failed to load kernel map.\n");
115 return -EINVAL;
116 }
117 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
118 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
119}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300120
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400121/* Convert trace point to probe point with debuginfo */
122static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
123 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300124{
125 struct symbol *sym;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400126 int fd, ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300127
128 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
129 tp->symbol, NULL);
130 if (sym) {
131 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400132 if (fd >= 0) {
133 ret = find_perf_probe_point(fd,
134 sym->start + tp->offset, pp);
135 close(fd);
136 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300137 }
138 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400139 pr_debug("Failed to find corresponding probes from "
140 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400141 pp->function = strdup(tp->symbol);
142 if (pp->function == NULL)
143 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300144 pp->offset = tp->offset;
145 }
146 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400147
148 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300149}
150
151/* Try to find perf_probe_event with debuginfo */
152static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
153 struct kprobe_trace_event **tevs)
154{
155 bool need_dwarf = perf_probe_event_need_dwarf(pev);
156 int fd, ntevs;
157
158 fd = open_vmlinux();
159 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400160 if (need_dwarf) {
161 pr_warning("Failed to open debuginfo file.\n");
162 return fd;
163 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300164 pr_debug("Could not open vmlinux. Try to use symbols.\n");
165 return 0;
166 }
167
168 /* Searching trace events corresponding to probe event */
169 ntevs = find_kprobe_trace_events(fd, pev, tevs);
170 close(fd);
171
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400172 if (ntevs > 0) { /* Succeeded to find trace events */
173 pr_debug("find %d kprobe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300174 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400175 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300176
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400177 if (ntevs == 0) { /* No error but failed to find probe point. */
178 pr_warning("Probe point '%s' not found.\n",
179 synthesize_perf_probe_point(&pev->point));
180 return -ENOENT;
181 }
182 /* Error path : ntevs < 0 */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300183 if (need_dwarf) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400184 if (ntevs == -EBADF)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300185 pr_warning("No dwarf info found in the vmlinux - "
186 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400187 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300188 }
189 pr_debug("An error occurred in debuginfo analysis."
190 " Try to use symbols.\n");
191 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300192}
193
194#define LINEBUF_SIZE 256
195#define NR_ADDITIONAL_LINES 2
196
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400197static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300198{
199 char buf[LINEBUF_SIZE];
200 const char *color = PERF_COLOR_BLUE;
201
202 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
203 goto error;
204 if (!skip) {
205 if (show_num)
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400206 fprintf(stdout, "%7d %s", l, buf);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300207 else
208 color_fprintf(stdout, color, " %s", buf);
209 }
210
211 while (strlen(buf) == LINEBUF_SIZE - 1 &&
212 buf[LINEBUF_SIZE - 2] != '\n') {
213 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
214 goto error;
215 if (!skip) {
216 if (show_num)
217 fprintf(stdout, "%s", buf);
218 else
219 color_fprintf(stdout, color, "%s", buf);
220 }
221 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400222
223 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300224error:
225 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400226 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300227 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400228 pr_warning("File read error: %s\n", strerror(errno));
229
230 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300231}
232
233/*
234 * Show line-range always requires debuginfo to find source file and
235 * line number.
236 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400237int show_line_range(struct line_range *lr)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300238{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400239 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300240 struct line_node *ln;
241 FILE *fp;
242 int fd, ret;
243
244 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400245 ret = init_vmlinux();
246 if (ret < 0)
247 return ret;
248
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300249 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400250 if (fd < 0) {
251 pr_warning("Failed to open debuginfo file.\n");
252 return fd;
253 }
254
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300255 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300256 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400257 if (ret == 0) {
258 pr_warning("Specified source line is not found.\n");
259 return -ENOENT;
260 } else if (ret < 0) {
261 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
262 return ret;
263 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300264
265 setup_pager();
266
267 if (lr->function)
268 fprintf(stdout, "<%s:%d>\n", lr->function,
269 lr->start - lr->offset);
270 else
271 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
272
273 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400274 if (fp == NULL) {
275 pr_warning("Failed to open %s: %s\n", lr->path,
276 strerror(errno));
277 return -errno;
278 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300279 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400280 while (l < lr->start && ret >= 0)
281 ret = show_one_line(fp, l++, true, false);
282 if (ret < 0)
283 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300284
285 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400286 while (ln->line > l && ret >= 0)
287 ret = show_one_line(fp, (l++) - lr->offset,
288 false, false);
289 if (ret >= 0)
290 ret = show_one_line(fp, (l++) - lr->offset,
291 false, true);
292 if (ret < 0)
293 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300294 }
295
296 if (lr->end == INT_MAX)
297 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400298 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400299 ret = show_one_line(fp, (l++) - lr->offset, false, false);
300end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300301 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400302 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300303}
304
305#else /* !DWARF_SUPPORT */
306
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400307static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300308 struct perf_probe_point *pp)
309{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400310 pp->function = strdup(tp->symbol);
311 if (pp->function == NULL)
312 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300313 pp->offset = tp->offset;
314 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400315
316 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300317}
318
319static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
320 struct kprobe_trace_event **tevs __unused)
321{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400322 if (perf_probe_event_need_dwarf(pev)) {
323 pr_warning("Debuginfo-analysis is not supported.\n");
324 return -ENOSYS;
325 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300326 return 0;
327}
328
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400329int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300330{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400331 pr_warning("Debuginfo-analysis is not supported.\n");
332 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300333}
334
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400335#endif
336
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400337int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500338{
339 const char *ptr;
340 char *tmp;
341 /*
342 * <Syntax>
343 * SRC:SLN[+NUM|-ELN]
344 * FUNC[:SLN[+NUM|-ELN]]
345 */
346 ptr = strchr(arg, ':');
347 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400348 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400349 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400350 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400351 lr->end--; /*
352 * Adjust the number of lines here.
353 * If the number of lines == 1, the
354 * the end of line should be equal to
355 * the start of line.
356 */
357 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400358 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500359 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400360 lr->end = INT_MAX;
361 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
362 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500363 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400364 " than end line.\n");
365 return -EINVAL;
366 }
367 if (*tmp != '\0') {
368 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500369 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400370 return -EINVAL;
371 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400372 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400373 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400374 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400375 lr->end = INT_MAX;
376 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400377
378 if (tmp == NULL)
379 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500380
381 if (strchr(tmp, '.'))
382 lr->file = tmp;
383 else
384 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400385
386 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500387}
388
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500389/* Check the name is good for event/group */
390static bool check_event_name(const char *name)
391{
392 if (!isalpha(*name) && *name != '_')
393 return false;
394 while (*++name != '\0') {
395 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
396 return false;
397 }
398 return true;
399}
400
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500401/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400402static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500403{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400404 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500405 char *ptr, *tmp;
406 char c, nc = 0;
407 /*
408 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500409 * perf probe [EVENT=]SRC[:LN|;PTN]
410 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500411 *
412 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500413 */
414
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500415 ptr = strpbrk(arg, ";=@+%");
416 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500417 *ptr = '\0';
418 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400419 if (strchr(arg, ':')) {
420 semantic_error("Group name is not supported yet.\n");
421 return -ENOTSUP;
422 }
423 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500424 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400425 "follow C symbol-naming rule.\n", arg);
426 return -EINVAL;
427 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400428 pev->event = strdup(arg);
429 if (pev->event == NULL)
430 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400431 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500432 arg = tmp;
433 }
434
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500435 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500436 if (ptr) {
437 nc = *ptr;
438 *ptr++ = '\0';
439 }
440
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400441 tmp = strdup(arg);
442 if (tmp == NULL)
443 return -ENOMEM;
444
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500445 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400446 if (strchr(tmp, '.')) /* File */
447 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500448 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400449 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500450
451 /* Parse other options */
452 while (ptr) {
453 arg = ptr;
454 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500455 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400456 pp->lazy_line = strdup(arg);
457 if (pp->lazy_line == NULL)
458 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500459 break;
460 }
461 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500462 if (ptr) {
463 nc = *ptr;
464 *ptr++ = '\0';
465 }
466 switch (c) {
467 case ':': /* Line number */
468 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400469 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500470 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400471 " in line number.\n");
472 return -EINVAL;
473 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500474 break;
475 case '+': /* Byte offset from a symbol */
476 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400477 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500478 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400479 " in offset.\n");
480 return -EINVAL;
481 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500482 break;
483 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400484 if (pp->file) {
485 semantic_error("SRC@SRC is not allowed.\n");
486 return -EINVAL;
487 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400488 pp->file = strdup(arg);
489 if (pp->file == NULL)
490 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500491 break;
492 case '%': /* Probe places */
493 if (strcmp(arg, "return") == 0) {
494 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400495 } else { /* Others not supported yet */
496 semantic_error("%%%s is not supported.\n", arg);
497 return -ENOTSUP;
498 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500499 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400500 default: /* Buggy case */
501 pr_err("This program has a bug at %s:%d.\n",
502 __FILE__, __LINE__);
503 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500504 break;
505 }
506 }
507
508 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400509 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500510 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400511 return -EINVAL;
512 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500513
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400514 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500515 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400516 return -EINVAL;
517 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500518
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500520 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400521 return -EINVAL;
522 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500523
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400524 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500525 semantic_error("File always requires line number or "
526 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400527 return -EINVAL;
528 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500529
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400530 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500531 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400532 return -EINVAL;
533 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500534
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400535 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500536 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400537 return -EINVAL;
538 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500539
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400540 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500541 semantic_error("Offset/Line/Lazy pattern can't be used with "
542 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400543 return -EINVAL;
544 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500545
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400546 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500547 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
548 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400549 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500550}
551
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400552/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400553static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400554{
Masami Hiramatsu48481932010-04-12 13:16:53 -0400555 char *tmp;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400556 struct perf_probe_arg_field **fieldp;
557
558 pr_debug("parsing arg: %s into ", str);
559
Masami Hiramatsu48481932010-04-12 13:16:53 -0400560 tmp = strchr(str, '=');
561 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400562 arg->name = strndup(str, tmp - str);
563 if (arg->name == NULL)
564 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400565 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400566 str = tmp + 1;
567 }
568
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400569 tmp = strchr(str, ':');
570 if (tmp) { /* Type setting */
571 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400572 arg->type = strdup(tmp + 1);
573 if (arg->type == NULL)
574 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400575 pr_debug("type:%s ", arg->type);
576 }
577
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400578 tmp = strpbrk(str, "-.");
579 if (!is_c_varname(str) || !tmp) {
580 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400581 arg->var = strdup(str);
582 if (arg->var == NULL)
583 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400584 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400585 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400586 }
587
588 /* Structure fields */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400589 arg->var = strndup(str, tmp - str);
590 if (arg->var == NULL)
591 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400592 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400593 fieldp = &arg->field;
594
595 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400596 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
597 if (*fieldp == NULL)
598 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400599 if (*tmp == '.') {
600 str = tmp + 1;
601 (*fieldp)->ref = false;
602 } else if (tmp[1] == '>') {
603 str = tmp + 2;
604 (*fieldp)->ref = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400605 } else {
606 semantic_error("Argument parse error: %s\n", str);
607 return -EINVAL;
608 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400609
610 tmp = strpbrk(str, "-.");
611 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400612 (*fieldp)->name = strndup(str, tmp - str);
613 if ((*fieldp)->name == NULL)
614 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400615 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
616 fieldp = &(*fieldp)->next;
617 }
618 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400619 (*fieldp)->name = strdup(str);
620 if ((*fieldp)->name == NULL)
621 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400622 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400623
624 /* If no name is specified, set the last field name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400625 if (!arg->name) {
626 arg->name = strdup((*fieldp)->name);
627 if (arg->name == NULL)
628 return -ENOMEM;
629 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400631}
632
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400633/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400634int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500635{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500636 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400637 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500638
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400639 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400640 if (!argv) {
641 pr_debug("Failed to split arguments.\n");
642 return -ENOMEM;
643 }
644 if (argc - 1 > MAX_PROBE_ARGS) {
645 semantic_error("Too many probe arguments (%d).\n", argc - 1);
646 ret = -ERANGE;
647 goto out;
648 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500649 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400650 ret = parse_perf_probe_point(argv[0], pev);
651 if (ret < 0)
652 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500653
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500654 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400655 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400656 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
657 if (pev->args == NULL) {
658 ret = -ENOMEM;
659 goto out;
660 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 for (i = 0; i < pev->nargs && ret >= 0; i++) {
662 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
663 if (ret >= 0 &&
664 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400665 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400666 " kretprobe.\n");
667 ret = -EINVAL;
668 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500669 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400670out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500671 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400672
673 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500674}
675
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400676/* Return true if this perf_probe_event requires debuginfo */
677bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500678{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400679 int i;
680
681 if (pev->point.file || pev->point.line || pev->point.lazy_line)
682 return true;
683
684 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400685 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400686 return true;
687
688 return false;
689}
690
691/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400692int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400693{
694 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500695 char pr;
696 char *p;
697 int ret, i, argc;
698 char **argv;
699
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400700 pr_debug("Parsing kprobe_events: %s\n", cmd);
701 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 if (!argv) {
703 pr_debug("Failed to split arguments.\n");
704 return -ENOMEM;
705 }
706 if (argc < 2) {
707 semantic_error("Too few probe arguments.\n");
708 ret = -ERANGE;
709 goto out;
710 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500711
712 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800713 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400714 &pr, (float *)(void *)&tev->group,
715 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400716 if (ret != 3) {
717 semantic_error("Failed to parse event name: %s\n", argv[0]);
718 ret = -EINVAL;
719 goto out;
720 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400721 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500722
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400723 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500724
725 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400726 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
727 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500728 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500730
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400731 tev->nargs = argc - 2;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400732 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
733 if (tev->args == NULL) {
734 ret = -ENOMEM;
735 goto out;
736 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400737 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500738 p = strchr(argv[i + 2], '=');
739 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400740 *p++ = '\0';
741 else
742 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400743 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400744 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400745 tev->args[i].value = strdup(p);
746 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
747 ret = -ENOMEM;
748 goto out;
749 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500750 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400751 ret = 0;
752out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500753 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400754 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500755}
756
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400757/* Compose only probe arg */
758int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
759{
760 struct perf_probe_arg_field *field = pa->field;
761 int ret;
762 char *tmp = buf;
763
Masami Hiramatsu48481932010-04-12 13:16:53 -0400764 if (pa->name && pa->var)
765 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
766 else
767 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400768 if (ret <= 0)
769 goto error;
770 tmp += ret;
771 len -= ret;
772
773 while (field) {
774 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
775 field->name);
776 if (ret <= 0)
777 goto error;
778 tmp += ret;
779 len -= ret;
780 field = field->next;
781 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400782
783 if (pa->type) {
784 ret = e_snprintf(tmp, len, ":%s", pa->type);
785 if (ret <= 0)
786 goto error;
787 tmp += ret;
788 len -= ret;
789 }
790
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400791 return tmp - buf;
792error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400793 pr_debug("Failed to synthesize perf probe argument: %s",
794 strerror(-ret));
795 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400796}
797
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400798/* Compose only probe point (not argument) */
799static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500800{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400801 char *buf, *tmp;
802 char offs[32] = "", line[32] = "", file[32] = "";
803 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500804
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400805 buf = zalloc(MAX_CMDLEN);
806 if (buf == NULL) {
807 ret = -ENOMEM;
808 goto error;
809 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500810 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400811 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500812 if (ret <= 0)
813 goto error;
814 }
815 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400816 ret = e_snprintf(line, 32, ":%d", pp->line);
817 if (ret <= 0)
818 goto error;
819 }
820 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400821 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400822 if (len < 0)
823 len = 0;
824 tmp = strchr(pp->file + len, '/');
825 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400826 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400827 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500828 if (ret <= 0)
829 goto error;
830 }
831
832 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400833 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
834 offs, pp->retprobe ? "%return" : "", line,
835 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500836 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400837 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500838 if (ret <= 0)
839 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500840
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400841 return buf;
842error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400843 pr_debug("Failed to synthesize perf probe point: %s",
844 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400845 if (buf)
846 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400847 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400848}
849
850#if 0
851char *synthesize_perf_probe_command(struct perf_probe_event *pev)
852{
853 char *buf;
854 int i, len, ret;
855
856 buf = synthesize_perf_probe_point(&pev->point);
857 if (!buf)
858 return NULL;
859
860 len = strlen(buf);
861 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500862 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400863 pev->args[i].name);
864 if (ret <= 0) {
865 free(buf);
866 return NULL;
867 }
868 len += ret;
869 }
870
871 return buf;
872}
873#endif
874
875static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
876 char **buf, size_t *buflen,
877 int depth)
878{
879 int ret;
880 if (ref->next) {
881 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
882 buflen, depth + 1);
883 if (depth < 0)
884 goto out;
885 }
886
887 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
888 if (ret < 0)
889 depth = ret;
890 else {
891 *buf += ret;
892 *buflen -= ret;
893 }
894out:
895 return depth;
896
897}
898
899static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
900 char *buf, size_t buflen)
901{
902 int ret, depth = 0;
903 char *tmp = buf;
904
905 /* Argument name or separator */
906 if (arg->name)
907 ret = e_snprintf(buf, buflen, " %s=", arg->name);
908 else
909 ret = e_snprintf(buf, buflen, " ");
910 if (ret < 0)
911 return ret;
912 buf += ret;
913 buflen -= ret;
914
915 /* Dereferencing arguments */
916 if (arg->ref) {
917 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
918 &buflen, 1);
919 if (depth < 0)
920 return depth;
921 }
922
923 /* Print argument value */
924 ret = e_snprintf(buf, buflen, "%s", arg->value);
925 if (ret < 0)
926 return ret;
927 buf += ret;
928 buflen -= ret;
929
930 /* Closing */
931 while (depth--) {
932 ret = e_snprintf(buf, buflen, ")");
933 if (ret < 0)
934 return ret;
935 buf += ret;
936 buflen -= ret;
937 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400938 /* Print argument type */
939 if (arg->type) {
940 ret = e_snprintf(buf, buflen, ":%s", arg->type);
941 if (ret <= 0)
942 return ret;
943 buf += ret;
944 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400945
946 return buf - tmp;
947}
948
949char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
950{
951 struct kprobe_trace_point *tp = &tev->point;
952 char *buf;
953 int i, len, ret;
954
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400955 buf = zalloc(MAX_CMDLEN);
956 if (buf == NULL)
957 return NULL;
958
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
960 tp->retprobe ? 'r' : 'p',
961 tev->group, tev->event,
962 tp->symbol, tp->offset);
963 if (len <= 0)
964 goto error;
965
966 for (i = 0; i < tev->nargs; i++) {
967 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
968 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500969 if (ret <= 0)
970 goto error;
971 len += ret;
972 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500973
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500975error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400976 free(buf);
977 return NULL;
978}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500979
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400980int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
981 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400983 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400984 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400985
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300986 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400987 pev->event = strdup(tev->event);
988 pev->group = strdup(tev->group);
989 if (pev->event == NULL || pev->group == NULL)
990 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400991
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300992 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400993 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
994 if (ret < 0)
995 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300996
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400997 /* Convert trace_arg to probe_arg */
998 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400999 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1000 if (pev->args == NULL)
1001 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001002 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001004 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001005 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001006 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1007 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001008 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001009 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001010 if (pev->args[i].name == NULL && ret >= 0)
1011 ret = -ENOMEM;
1012 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001013
1014 if (ret < 0)
1015 clear_perf_probe_event(pev);
1016
1017 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001018}
1019
1020void clear_perf_probe_event(struct perf_probe_event *pev)
1021{
1022 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001023 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001024 int i;
1025
1026 if (pev->event)
1027 free(pev->event);
1028 if (pev->group)
1029 free(pev->group);
1030 if (pp->file)
1031 free(pp->file);
1032 if (pp->function)
1033 free(pp->function);
1034 if (pp->lazy_line)
1035 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001036 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001037 if (pev->args[i].name)
1038 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001039 if (pev->args[i].var)
1040 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001041 if (pev->args[i].type)
1042 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001043 field = pev->args[i].field;
1044 while (field) {
1045 next = field->next;
1046 if (field->name)
1047 free(field->name);
1048 free(field);
1049 field = next;
1050 }
1051 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001052 if (pev->args)
1053 free(pev->args);
1054 memset(pev, 0, sizeof(*pev));
1055}
1056
1057void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1058{
1059 struct kprobe_trace_arg_ref *ref, *next;
1060 int i;
1061
1062 if (tev->event)
1063 free(tev->event);
1064 if (tev->group)
1065 free(tev->group);
1066 if (tev->point.symbol)
1067 free(tev->point.symbol);
1068 for (i = 0; i < tev->nargs; i++) {
1069 if (tev->args[i].name)
1070 free(tev->args[i].name);
1071 if (tev->args[i].value)
1072 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001073 if (tev->args[i].type)
1074 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001075 ref = tev->args[i].ref;
1076 while (ref) {
1077 next = ref->next;
1078 free(ref);
1079 ref = next;
1080 }
1081 }
1082 if (tev->args)
1083 free(tev->args);
1084 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001085}
1086
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001087static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001088{
1089 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001090 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001091 int ret;
1092
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001093 __debugfs = debugfs_find_mountpoint();
1094 if (__debugfs == NULL) {
1095 pr_warning("Debugfs is not mounted.\n");
1096 return -ENOENT;
1097 }
1098
1099 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001101 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001102 if (readwrite && !probe_event_dry_run)
1103 ret = open(buf, O_RDWR, O_APPEND);
1104 else
1105 ret = open(buf, O_RDONLY, 0);
1106 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001107
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001108 if (ret < 0) {
1109 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 pr_warning("kprobe_events file does not exist - please"
1111 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001112 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001113 pr_warning("Failed to open kprobe_events file: %s\n",
1114 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001115 }
1116 return ret;
1117}
1118
1119/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001120static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001121{
1122 int ret, idx;
1123 FILE *fp;
1124 char buf[MAX_CMDLEN];
1125 char *p;
1126 struct strlist *sl;
1127
1128 sl = strlist__new(true, NULL);
1129
1130 fp = fdopen(dup(fd), "r");
1131 while (!feof(fp)) {
1132 p = fgets(buf, MAX_CMDLEN, fp);
1133 if (!p)
1134 break;
1135
1136 idx = strlen(p) - 1;
1137 if (p[idx] == '\n')
1138 p[idx] = '\0';
1139 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001140 if (ret < 0) {
1141 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1142 strlist__delete(sl);
1143 return NULL;
1144 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001145 }
1146 fclose(fp);
1147
1148 return sl;
1149}
1150
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001151/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001152static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001153{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001154 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001155 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001156 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001157
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001158 /* Synthesize only event probe point */
1159 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001160 if (!place)
1161 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001162
1163 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001164 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 return ret;
1166
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001167 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001168
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001170 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001171 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 ret = synthesize_perf_probe_arg(&pev->args[i],
1173 buf, 128);
1174 if (ret < 0)
1175 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001176 printf(" %s", buf);
1177 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001178 }
1179 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001180 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001181 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001182}
1183
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001184/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001185int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001188 struct kprobe_trace_event tev;
1189 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001190 struct strlist *rawlist;
1191 struct str_node *ent;
1192
Masami Hiramatsu72041332010-01-05 17:47:10 -05001193 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194 ret = init_vmlinux();
1195 if (ret < 0)
1196 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001197
1198 memset(&tev, 0, sizeof(tev));
1199 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001200
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001201 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001202 if (fd < 0)
1203 return fd;
1204
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001206 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001207 if (!rawlist)
1208 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001209
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001210 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 ret = parse_kprobe_trace_command(ent->s, &tev);
1212 if (ret >= 0) {
1213 ret = convert_to_perf_probe_event(&tev, &pev);
1214 if (ret >= 0)
1215 ret = show_perf_probe_event(&pev);
1216 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001217 clear_perf_probe_event(&pev);
1218 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 if (ret < 0)
1220 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001221 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001222 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223
1224 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001225}
1226
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001227/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001228static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001229{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001230 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001231 struct strlist *sl, *rawlist;
1232 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001233 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001235
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001236 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001237
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001238 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001239 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001240 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 ret = parse_kprobe_trace_command(ent->s, &tev);
1242 if (ret < 0)
1243 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001244 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1246 tev.event);
1247 if (ret >= 0)
1248 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001249 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001251 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 if (ret < 0)
1253 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001254 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001255 strlist__delete(rawlist);
1256
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257 if (ret < 0) {
1258 strlist__delete(sl);
1259 return NULL;
1260 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001261 return sl;
1262}
1263
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001264static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001265{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001266 int ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001268
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001269 if (!buf) {
1270 pr_debug("Failed to synthesize kprobe trace event.\n");
1271 return -EINVAL;
1272 }
1273
Masami Hiramatsufa282442009-12-08 17:03:23 -05001274 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001275 if (!probe_event_dry_run) {
1276 ret = write(fd, buf, strlen(buf));
1277 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001278 pr_warning("Failed to write event: %s\n",
1279 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001280 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001281 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001282 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001283}
1284
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001285static int get_new_event_name(char *buf, size_t len, const char *base,
1286 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001287{
1288 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001289
1290 /* Try no suffix */
1291 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001292 if (ret < 0) {
1293 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1294 return ret;
1295 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001296 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001297 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001298
Masami Hiramatsud761b082009-12-15 10:32:25 -05001299 if (!allow_suffix) {
1300 pr_warning("Error: event \"%s\" already exists. "
1301 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001302 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001303 }
1304
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001305 /* Try to add suffix */
1306 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001307 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 if (ret < 0) {
1309 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1310 return ret;
1311 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001312 if (!strlist__has_entry(namelist, buf))
1313 break;
1314 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001315 if (i == MAX_EVENT_INDEX) {
1316 pr_warning("Too many events are on the same function.\n");
1317 ret = -ERANGE;
1318 }
1319
1320 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001321}
1322
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001323static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1324 struct kprobe_trace_event *tevs,
1325 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001326{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001328 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001329 char buf[64];
1330 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001331 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001332
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001333 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001334 if (fd < 0)
1335 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001336 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001337 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001338 if (!namelist) {
1339 pr_debug("Failed to get current event list.\n");
1340 return -EIO;
1341 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001342
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001343 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001344 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001345 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001346 tev = &tevs[i];
1347 if (pev->event)
1348 event = pev->event;
1349 else
1350 if (pev->point.function)
1351 event = pev->point.function;
1352 else
1353 event = tev->point.symbol;
1354 if (pev->group)
1355 group = pev->group;
1356 else
1357 group = PERFPROBE_GROUP;
1358
1359 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 ret = get_new_event_name(buf, 64, event,
1361 namelist, allow_suffix);
1362 if (ret < 0)
1363 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001364 event = buf;
1365
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 tev->event = strdup(event);
1367 tev->group = strdup(group);
1368 if (tev->event == NULL || tev->group == NULL) {
1369 ret = -ENOMEM;
1370 break;
1371 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001372 ret = write_kprobe_trace_event(fd, tev);
1373 if (ret < 0)
1374 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001375 /* Add added event name to namelist */
1376 strlist__add(namelist, event);
1377
1378 /* Trick here - save current event/group */
1379 event = pev->event;
1380 group = pev->group;
1381 pev->event = tev->event;
1382 pev->group = tev->group;
1383 show_perf_probe_event(pev);
1384 /* Trick here - restore current event/group */
1385 pev->event = (char *)event;
1386 pev->group = (char *)group;
1387
1388 /*
1389 * Probes after the first probe which comes from same
1390 * user input are always allowed to add suffix, because
1391 * there might be several addresses corresponding to
1392 * one code line.
1393 */
1394 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001395 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001396
1397 if (ret >= 0) {
1398 /* Show how to use the event. */
1399 printf("\nYou can now use it on all perf tools, such as:\n\n");
1400 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1401 tev->event);
1402 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001403
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001404 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001405 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001406 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001407}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001408
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001409static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1410 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001411{
1412 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001413 int ret = 0, i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001414 struct kprobe_trace_event *tev;
1415
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001416 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001417 ret = try_to_find_kprobe_trace_events(pev, tevs);
1418 if (ret != 0)
1419 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001420
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421 /* Allocate trace event buffer */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001422 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1423 if (tev == NULL)
1424 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001425
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001427 tev->point.symbol = strdup(pev->point.function);
1428 if (tev->point.symbol == NULL) {
1429 ret = -ENOMEM;
1430 goto error;
1431 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001432 tev->point.offset = pev->point.offset;
1433 tev->nargs = pev->nargs;
1434 if (tev->nargs) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001435 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1436 * tev->nargs);
1437 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001438 ret = -ENOMEM;
1439 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001440 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001441 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001442 if (pev->args[i].name) {
1443 tev->args[i].name = strdup(pev->args[i].name);
1444 if (tev->args[i].name == NULL) {
1445 ret = -ENOMEM;
1446 goto error;
1447 }
1448 }
1449 tev->args[i].value = strdup(pev->args[i].var);
1450 if (tev->args[i].value == NULL) {
1451 ret = -ENOMEM;
1452 goto error;
1453 }
1454 if (pev->args[i].type) {
1455 tev->args[i].type = strdup(pev->args[i].type);
1456 if (tev->args[i].type == NULL) {
1457 ret = -ENOMEM;
1458 goto error;
1459 }
1460 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001461 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001462 }
1463
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 /* Currently just checking function name from symbol map */
1465 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1466 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001467 if (!sym) {
1468 pr_warning("Kernel symbol \'%s\' not found.\n",
1469 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001470 ret = -ENOENT;
1471 goto error;
1472 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001473
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001474 return 1;
1475error:
1476 clear_kprobe_trace_event(tev);
1477 free(tev);
1478 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001479 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001480}
1481
1482struct __event_package {
1483 struct perf_probe_event *pev;
1484 struct kprobe_trace_event *tevs;
1485 int ntevs;
1486};
1487
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1489 bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001491 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001492 struct __event_package *pkgs;
1493
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001494 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1495 if (pkgs == NULL)
1496 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001497
1498 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 ret = init_vmlinux();
1500 if (ret < 0)
1501 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001502
1503 /* Loop 1: convert all events */
1504 for (i = 0; i < npevs; i++) {
1505 pkgs[i].pev = &pevs[i];
1506 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1508 &pkgs[i].tevs);
1509 if (ret < 0)
1510 goto end;
1511 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001512 }
1513
1514 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001515 for (i = 0; i < npevs && ret >= 0; i++)
1516 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1517 pkgs[i].ntevs, force_add);
1518end:
1519 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001521 for (j = 0; j < pkgs[i].ntevs; j++)
1522 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1523
1524 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001525}
1526
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001528{
1529 char *p;
1530 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001531 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001532
1533 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1535 if (ret < 0)
1536 goto error;
1537
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001538 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001539 if (!p) {
1540 pr_debug("Internal error: %s should have ':' but not.\n",
1541 ent->s);
1542 ret = -ENOTSUP;
1543 goto error;
1544 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001545 *p = '/';
1546
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001547 pr_debug("Writing event: %s\n", buf);
1548 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001549 if (ret < 0)
1550 goto error;
1551
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001552 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001553 return 0;
1554error:
1555 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1556 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001557}
1558
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001559static int del_trace_kprobe_event(int fd, const char *group,
1560 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001561{
1562 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001563 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001564 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001565
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001566 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1567 if (ret < 0) {
1568 pr_err("Failed to copy event.");
1569 return ret;
1570 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001571
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001572 if (strpbrk(buf, "*?")) { /* Glob-exp */
1573 strlist__for_each_safe(ent, n, namelist)
1574 if (strglobmatch(ent->s, buf)) {
1575 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 ret = __del_trace_kprobe_event(fd, ent);
1577 if (ret < 0)
1578 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001579 strlist__remove(namelist, ent);
1580 }
1581 } else {
1582 ent = strlist__find(namelist, buf);
1583 if (ent) {
1584 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001585 ret = __del_trace_kprobe_event(fd, ent);
1586 if (ret >= 0)
1587 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001588 }
1589 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001590 if (found == 0 && ret >= 0)
1591 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1592
1593 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001594}
1595
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001596int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001597{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001598 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001599 const char *group, *event;
1600 char *p, *str;
1601 struct str_node *ent;
1602 struct strlist *namelist;
1603
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001604 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605 if (fd < 0)
1606 return fd;
1607
Masami Hiramatsufa282442009-12-08 17:03:23 -05001608 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001609 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610 if (namelist == NULL)
1611 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001612
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001613 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001614 str = strdup(ent->s);
1615 if (str == NULL) {
1616 ret = -ENOMEM;
1617 break;
1618 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001619 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001620 p = strchr(str, ':');
1621 if (p) {
1622 group = str;
1623 *p = '\0';
1624 event = p + 1;
1625 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001626 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001627 event = str;
1628 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001629 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001630 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001631 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001632 if (ret < 0)
1633 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001634 }
1635 strlist__delete(namelist);
1636 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001637
1638 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001639}
1640