blob: bd68f7b33b21ea1e214532d26af5235dd1842c50 [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 Hiramatsu4b4da7f2010-03-22 13:10:26 -030045#include "trace-event.h" /* For __unused */
46#include "parse-events.h" /* For debugfs_path */
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{
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
94 map_groups__init(&kmap_groups);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040095 ret = map_groups__create_kernel_maps(&kmap_groups, kmaps);
96 if (ret < 0)
97 pr_debug("Failed to create kernel maps.\n");
98
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400103}
104
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300105#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106static int open_vmlinux(void)
107{
108 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
109 pr_debug("Failed to load kernel map.\n");
110 return -EINVAL;
111 }
112 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
113 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
114}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300115
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400116/* Convert trace point to probe point with debuginfo */
117static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
118 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300119{
120 struct symbol *sym;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400121 int fd, ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300122
123 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
124 tp->symbol, NULL);
125 if (sym) {
126 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400127 if (fd >= 0) {
128 ret = find_perf_probe_point(fd,
129 sym->start + tp->offset, pp);
130 close(fd);
131 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300132 }
133 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400134 pr_debug("Failed to find corresponding probes from "
135 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300136 pp->function = xstrdup(tp->symbol);
137 pp->offset = tp->offset;
138 }
139 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400140
141 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300142}
143
144/* Try to find perf_probe_event with debuginfo */
145static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
146 struct kprobe_trace_event **tevs)
147{
148 bool need_dwarf = perf_probe_event_need_dwarf(pev);
149 int fd, ntevs;
150
151 fd = open_vmlinux();
152 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400153 if (need_dwarf) {
154 pr_warning("Failed to open debuginfo file.\n");
155 return fd;
156 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300157 pr_debug("Could not open vmlinux. Try to use symbols.\n");
158 return 0;
159 }
160
161 /* Searching trace events corresponding to probe event */
162 ntevs = find_kprobe_trace_events(fd, pev, tevs);
163 close(fd);
164
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400165 if (ntevs > 0) { /* Succeeded to find trace events */
166 pr_debug("find %d kprobe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300167 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400168 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300169
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400170 if (ntevs == 0) { /* No error but failed to find probe point. */
171 pr_warning("Probe point '%s' not found.\n",
172 synthesize_perf_probe_point(&pev->point));
173 return -ENOENT;
174 }
175 /* Error path : ntevs < 0 */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300176 if (need_dwarf) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400177 if (ntevs == -EBADF)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300178 pr_warning("No dwarf info found in the vmlinux - "
179 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400180 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300181 }
182 pr_debug("An error occurred in debuginfo analysis."
183 " Try to use symbols.\n");
184 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300185}
186
187#define LINEBUF_SIZE 256
188#define NR_ADDITIONAL_LINES 2
189
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400190static int show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300191{
192 char buf[LINEBUF_SIZE];
193 const char *color = PERF_COLOR_BLUE;
194
195 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
196 goto error;
197 if (!skip) {
198 if (show_num)
199 fprintf(stdout, "%7u %s", l, buf);
200 else
201 color_fprintf(stdout, color, " %s", buf);
202 }
203
204 while (strlen(buf) == LINEBUF_SIZE - 1 &&
205 buf[LINEBUF_SIZE - 2] != '\n') {
206 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
207 goto error;
208 if (!skip) {
209 if (show_num)
210 fprintf(stdout, "%s", buf);
211 else
212 color_fprintf(stdout, color, "%s", buf);
213 }
214 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400215
216 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300217error:
218 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400219 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300220 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400221 pr_warning("File read error: %s\n", strerror(errno));
222
223 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300224}
225
226/*
227 * Show line-range always requires debuginfo to find source file and
228 * line number.
229 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400230int show_line_range(struct line_range *lr)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300231{
232 unsigned int l = 1;
233 struct line_node *ln;
234 FILE *fp;
235 int fd, ret;
236
237 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400238 ret = init_vmlinux();
239 if (ret < 0)
240 return ret;
241
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300242 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400243 if (fd < 0) {
244 pr_warning("Failed to open debuginfo file.\n");
245 return fd;
246 }
247
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300248 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300249 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400250 if (ret == 0) {
251 pr_warning("Specified source line is not found.\n");
252 return -ENOENT;
253 } else if (ret < 0) {
254 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
255 return ret;
256 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300257
258 setup_pager();
259
260 if (lr->function)
261 fprintf(stdout, "<%s:%d>\n", lr->function,
262 lr->start - lr->offset);
263 else
264 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
265
266 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400267 if (fp == NULL) {
268 pr_warning("Failed to open %s: %s\n", lr->path,
269 strerror(errno));
270 return -errno;
271 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300272 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400273 while (l < lr->start && ret >= 0)
274 ret = show_one_line(fp, l++, true, false);
275 if (ret < 0)
276 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300277
278 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400279 while (ln->line > l && ret >= 0)
280 ret = show_one_line(fp, (l++) - lr->offset,
281 false, false);
282 if (ret >= 0)
283 ret = show_one_line(fp, (l++) - lr->offset,
284 false, true);
285 if (ret < 0)
286 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300287 }
288
289 if (lr->end == INT_MAX)
290 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400291 while (l < lr->end && !feof(fp) && ret >= 0)
292 ret = show_one_line(fp, (l++) - lr->offset, false, false);
293end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300294 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400295 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300296}
297
298#else /* !DWARF_SUPPORT */
299
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400300static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300301 struct perf_probe_point *pp)
302{
303 pp->function = xstrdup(tp->symbol);
304 pp->offset = tp->offset;
305 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400306
307 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300308}
309
310static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
311 struct kprobe_trace_event **tevs __unused)
312{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400313 if (perf_probe_event_need_dwarf(pev)) {
314 pr_warning("Debuginfo-analysis is not supported.\n");
315 return -ENOSYS;
316 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300317 return 0;
318}
319
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400320int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300321{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400322 pr_warning("Debuginfo-analysis is not supported.\n");
323 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300324}
325
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400326#endif
327
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400328int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500329{
330 const char *ptr;
331 char *tmp;
332 /*
333 * <Syntax>
334 * SRC:SLN[+NUM|-ELN]
335 * FUNC[:SLN[+NUM|-ELN]]
336 */
337 ptr = strchr(arg, ':');
338 if (ptr) {
339 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
340 if (*tmp == '+')
341 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
342 &tmp, 0);
343 else if (*tmp == '-')
344 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
345 else
346 lr->end = 0;
347 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400348 if (lr->end && lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500349 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400350 " than end line.\n");
351 return -EINVAL;
352 }
353 if (*tmp != '\0') {
354 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500355 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400356 return -EINVAL;
357 }
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400358 tmp = xstrndup(arg, (ptr - arg));
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500359 } else
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400360 tmp = xstrdup(arg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500361
362 if (strchr(tmp, '.'))
363 lr->file = tmp;
364 else
365 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400366
367 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500368}
369
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500370/* Check the name is good for event/group */
371static bool check_event_name(const char *name)
372{
373 if (!isalpha(*name) && *name != '_')
374 return false;
375 while (*++name != '\0') {
376 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
377 return false;
378 }
379 return true;
380}
381
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500382/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400383static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500384{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400385 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500386 char *ptr, *tmp;
387 char c, nc = 0;
388 /*
389 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500390 * perf probe [EVENT=]SRC[:LN|;PTN]
391 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500392 *
393 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500394 */
395
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500396 ptr = strpbrk(arg, ";=@+%");
397 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500398 *ptr = '\0';
399 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400400 if (strchr(arg, ':')) {
401 semantic_error("Group name is not supported yet.\n");
402 return -ENOTSUP;
403 }
404 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500405 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400406 "follow C symbol-naming rule.\n", arg);
407 return -EINVAL;
408 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400409 pev->event = xstrdup(arg);
410 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500411 arg = tmp;
412 }
413
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500414 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500415 if (ptr) {
416 nc = *ptr;
417 *ptr++ = '\0';
418 }
419
420 /* Check arg is function or file and copy it */
421 if (strchr(arg, '.')) /* File */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400422 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500423 else /* Function */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400424 pp->function = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500425
426 /* Parse other options */
427 while (ptr) {
428 arg = ptr;
429 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500430 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400431 pp->lazy_line = xstrdup(arg);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500432 break;
433 }
434 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500435 if (ptr) {
436 nc = *ptr;
437 *ptr++ = '\0';
438 }
439 switch (c) {
440 case ':': /* Line number */
441 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400442 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500443 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400444 " in line number.\n");
445 return -EINVAL;
446 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500447 break;
448 case '+': /* Byte offset from a symbol */
449 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400450 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500451 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400452 " in offset.\n");
453 return -EINVAL;
454 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500455 break;
456 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400457 if (pp->file) {
458 semantic_error("SRC@SRC is not allowed.\n");
459 return -EINVAL;
460 }
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400461 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500462 break;
463 case '%': /* Probe places */
464 if (strcmp(arg, "return") == 0) {
465 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400466 } else { /* Others not supported yet */
467 semantic_error("%%%s is not supported.\n", arg);
468 return -ENOTSUP;
469 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500470 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400471 default: /* Buggy case */
472 pr_err("This program has a bug at %s:%d.\n",
473 __FILE__, __LINE__);
474 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500475 break;
476 }
477 }
478
479 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400480 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500481 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400482 return -EINVAL;
483 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500484
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400485 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500486 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400487 return -EINVAL;
488 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500489
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400490 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500491 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400492 return -EINVAL;
493 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500494
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400495 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500496 semantic_error("File always requires line number or "
497 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400498 return -EINVAL;
499 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500500
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400501 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500502 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400503 return -EINVAL;
504 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500505
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400506 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500507 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400508 return -EINVAL;
509 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500510
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400511 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500512 semantic_error("Offset/Line/Lazy pattern can't be used with "
513 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400514 return -EINVAL;
515 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500516
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400517 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500518 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
519 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400520 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500521}
522
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400523/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400524static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400525{
Masami Hiramatsu48481932010-04-12 13:16:53 -0400526 char *tmp;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400527 struct perf_probe_arg_field **fieldp;
528
529 pr_debug("parsing arg: %s into ", str);
530
Masami Hiramatsu48481932010-04-12 13:16:53 -0400531 tmp = strchr(str, '=');
532 if (tmp) {
533 arg->name = xstrndup(str, tmp - str);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400534 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400535 str = tmp + 1;
536 }
537
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400538 tmp = strchr(str, ':');
539 if (tmp) { /* Type setting */
540 *tmp = '\0';
541 arg->type = xstrdup(tmp + 1);
542 pr_debug("type:%s ", arg->type);
543 }
544
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400545 tmp = strpbrk(str, "-.");
546 if (!is_c_varname(str) || !tmp) {
547 /* A variable, register, symbol or special value */
Masami Hiramatsu48481932010-04-12 13:16:53 -0400548 arg->var = xstrdup(str);
549 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400550 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400551 }
552
553 /* Structure fields */
Masami Hiramatsu48481932010-04-12 13:16:53 -0400554 arg->var = xstrndup(str, tmp - str);
555 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400556 fieldp = &arg->field;
557
558 do {
559 *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
560 if (*tmp == '.') {
561 str = tmp + 1;
562 (*fieldp)->ref = false;
563 } else if (tmp[1] == '>') {
564 str = tmp + 2;
565 (*fieldp)->ref = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400566 } else {
567 semantic_error("Argument parse error: %s\n", str);
568 return -EINVAL;
569 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400570
571 tmp = strpbrk(str, "-.");
572 if (tmp) {
573 (*fieldp)->name = xstrndup(str, tmp - str);
574 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
575 fieldp = &(*fieldp)->next;
576 }
577 } while (tmp);
578 (*fieldp)->name = xstrdup(str);
579 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400580
581 /* If no name is specified, set the last field name */
582 if (!arg->name)
583 arg->name = xstrdup((*fieldp)->name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400584
585 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400586}
587
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400588/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400589int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500590{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500591 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400592 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500593
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400594 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400595 if (!argv) {
596 pr_debug("Failed to split arguments.\n");
597 return -ENOMEM;
598 }
599 if (argc - 1 > MAX_PROBE_ARGS) {
600 semantic_error("Too many probe arguments (%d).\n", argc - 1);
601 ret = -ERANGE;
602 goto out;
603 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500604 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400605 ret = parse_perf_probe_point(argv[0], pev);
606 if (ret < 0)
607 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500608
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500609 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400610 pev->nargs = argc - 1;
611 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400612 for (i = 0; i < pev->nargs && ret >= 0; i++) {
613 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
614 if (ret >= 0 &&
615 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400616 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400617 " kretprobe.\n");
618 ret = -EINVAL;
619 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500620 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400621out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500622 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400623
624 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500625}
626
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400627/* Return true if this perf_probe_event requires debuginfo */
628bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500629{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400630 int i;
631
632 if (pev->point.file || pev->point.line || pev->point.lazy_line)
633 return true;
634
635 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400636 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400637 return true;
638
639 return false;
640}
641
642/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400643int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400644{
645 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500646 char pr;
647 char *p;
648 int ret, i, argc;
649 char **argv;
650
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400651 pr_debug("Parsing kprobe_events: %s\n", cmd);
652 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400653 if (!argv) {
654 pr_debug("Failed to split arguments.\n");
655 return -ENOMEM;
656 }
657 if (argc < 2) {
658 semantic_error("Too few probe arguments.\n");
659 ret = -ERANGE;
660 goto out;
661 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500662
663 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800664 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400665 &pr, (float *)(void *)&tev->group,
666 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400667 if (ret != 3) {
668 semantic_error("Failed to parse event name: %s\n", argv[0]);
669 ret = -EINVAL;
670 goto out;
671 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400672 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500673
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400674 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500675
676 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400677 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
678 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500679 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400680 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500681
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400682 tev->nargs = argc - 2;
683 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
684 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500685 p = strchr(argv[i + 2], '=');
686 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400687 *p++ = '\0';
688 else
689 p = argv[i + 2];
690 tev->args[i].name = xstrdup(argv[i + 2]);
691 /* TODO: parse regs and offset */
692 tev->args[i].value = xstrdup(p);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500693 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400694 ret = 0;
695out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500696 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400697 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500698}
699
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400700/* Compose only probe arg */
701int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
702{
703 struct perf_probe_arg_field *field = pa->field;
704 int ret;
705 char *tmp = buf;
706
Masami Hiramatsu48481932010-04-12 13:16:53 -0400707 if (pa->name && pa->var)
708 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
709 else
710 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400711 if (ret <= 0)
712 goto error;
713 tmp += ret;
714 len -= ret;
715
716 while (field) {
717 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
718 field->name);
719 if (ret <= 0)
720 goto error;
721 tmp += ret;
722 len -= ret;
723 field = field->next;
724 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400725
726 if (pa->type) {
727 ret = e_snprintf(tmp, len, ":%s", pa->type);
728 if (ret <= 0)
729 goto error;
730 tmp += ret;
731 len -= ret;
732 }
733
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400734 return tmp - buf;
735error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400736 pr_debug("Failed to synthesize perf probe argument: %s",
737 strerror(-ret));
738 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400739}
740
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400741/* Compose only probe point (not argument) */
742static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500743{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400744 char *buf, *tmp;
745 char offs[32] = "", line[32] = "", file[32] = "";
746 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500747
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400748 buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500749 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400750 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500751 if (ret <= 0)
752 goto error;
753 }
754 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400755 ret = e_snprintf(line, 32, ":%d", pp->line);
756 if (ret <= 0)
757 goto error;
758 }
759 if (pp->file) {
760 len = strlen(pp->file) - 32;
761 if (len < 0)
762 len = 0;
763 tmp = strchr(pp->file + len, '/');
764 if (!tmp)
765 tmp = pp->file + len - 1;
766 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500767 if (ret <= 0)
768 goto error;
769 }
770
771 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400772 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
773 offs, pp->retprobe ? "%return" : "", line,
774 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500775 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400776 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500777 if (ret <= 0)
778 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500779
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400780 return buf;
781error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400782 pr_debug("Failed to synthesize perf probe point: %s",
783 strerror(-ret));
784 free(buf);
785 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400786}
787
788#if 0
789char *synthesize_perf_probe_command(struct perf_probe_event *pev)
790{
791 char *buf;
792 int i, len, ret;
793
794 buf = synthesize_perf_probe_point(&pev->point);
795 if (!buf)
796 return NULL;
797
798 len = strlen(buf);
799 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500800 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400801 pev->args[i].name);
802 if (ret <= 0) {
803 free(buf);
804 return NULL;
805 }
806 len += ret;
807 }
808
809 return buf;
810}
811#endif
812
813static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
814 char **buf, size_t *buflen,
815 int depth)
816{
817 int ret;
818 if (ref->next) {
819 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
820 buflen, depth + 1);
821 if (depth < 0)
822 goto out;
823 }
824
825 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
826 if (ret < 0)
827 depth = ret;
828 else {
829 *buf += ret;
830 *buflen -= ret;
831 }
832out:
833 return depth;
834
835}
836
837static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
838 char *buf, size_t buflen)
839{
840 int ret, depth = 0;
841 char *tmp = buf;
842
843 /* Argument name or separator */
844 if (arg->name)
845 ret = e_snprintf(buf, buflen, " %s=", arg->name);
846 else
847 ret = e_snprintf(buf, buflen, " ");
848 if (ret < 0)
849 return ret;
850 buf += ret;
851 buflen -= ret;
852
853 /* Dereferencing arguments */
854 if (arg->ref) {
855 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
856 &buflen, 1);
857 if (depth < 0)
858 return depth;
859 }
860
861 /* Print argument value */
862 ret = e_snprintf(buf, buflen, "%s", arg->value);
863 if (ret < 0)
864 return ret;
865 buf += ret;
866 buflen -= ret;
867
868 /* Closing */
869 while (depth--) {
870 ret = e_snprintf(buf, buflen, ")");
871 if (ret < 0)
872 return ret;
873 buf += ret;
874 buflen -= ret;
875 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400876 /* Print argument type */
877 if (arg->type) {
878 ret = e_snprintf(buf, buflen, ":%s", arg->type);
879 if (ret <= 0)
880 return ret;
881 buf += ret;
882 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400883
884 return buf - tmp;
885}
886
887char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
888{
889 struct kprobe_trace_point *tp = &tev->point;
890 char *buf;
891 int i, len, ret;
892
893 buf = xzalloc(MAX_CMDLEN);
894 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
895 tp->retprobe ? 'r' : 'p',
896 tev->group, tev->event,
897 tp->symbol, tp->offset);
898 if (len <= 0)
899 goto error;
900
901 for (i = 0; i < tev->nargs; i++) {
902 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
903 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500904 if (ret <= 0)
905 goto error;
906 len += ret;
907 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500908
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400909 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500910error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400911 free(buf);
912 return NULL;
913}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500914
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400915int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
916 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400917{
918 char buf[64];
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400919 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400920
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300921 /* Convert event/group name */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400922 pev->event = xstrdup(tev->event);
923 pev->group = xstrdup(tev->group);
924
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300925 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400926 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
927 if (ret < 0)
928 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300929
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400930 /* Convert trace_arg to probe_arg */
931 pev->nargs = tev->nargs;
932 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400933 for (i = 0; i < tev->nargs && ret >= 0; i++)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400934 if (tev->args[i].name)
935 pev->args[i].name = xstrdup(tev->args[i].name);
936 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400937 ret = synthesize_kprobe_trace_arg(&tev->args[i],
938 buf, 64);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400939 pev->args[i].name = xstrdup(buf);
940 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400941
942 if (ret < 0)
943 clear_perf_probe_event(pev);
944
945 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400946}
947
948void clear_perf_probe_event(struct perf_probe_event *pev)
949{
950 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400951 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400952 int i;
953
954 if (pev->event)
955 free(pev->event);
956 if (pev->group)
957 free(pev->group);
958 if (pp->file)
959 free(pp->file);
960 if (pp->function)
961 free(pp->function);
962 if (pp->lazy_line)
963 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400964 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400965 if (pev->args[i].name)
966 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400967 if (pev->args[i].var)
968 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400969 if (pev->args[i].type)
970 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400971 field = pev->args[i].field;
972 while (field) {
973 next = field->next;
974 if (field->name)
975 free(field->name);
976 free(field);
977 field = next;
978 }
979 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400980 if (pev->args)
981 free(pev->args);
982 memset(pev, 0, sizeof(*pev));
983}
984
985void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
986{
987 struct kprobe_trace_arg_ref *ref, *next;
988 int i;
989
990 if (tev->event)
991 free(tev->event);
992 if (tev->group)
993 free(tev->group);
994 if (tev->point.symbol)
995 free(tev->point.symbol);
996 for (i = 0; i < tev->nargs; i++) {
997 if (tev->args[i].name)
998 free(tev->args[i].name);
999 if (tev->args[i].value)
1000 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001001 if (tev->args[i].type)
1002 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003 ref = tev->args[i].ref;
1004 while (ref) {
1005 next = ref->next;
1006 free(ref);
1007 ref = next;
1008 }
1009 }
1010 if (tev->args)
1011 free(tev->args);
1012 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001013}
1014
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001015static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001016{
1017 char buf[PATH_MAX];
1018 int ret;
1019
1020 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001021 if (ret >= 0) {
1022 if (readwrite && !probe_event_dry_run)
1023 ret = open(buf, O_RDWR, O_APPEND);
1024 else
1025 ret = open(buf, O_RDONLY, 0);
1026 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001027
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001028 if (ret < 0) {
1029 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001030 pr_warning("kprobe_events file does not exist - please"
1031 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001032 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 pr_warning("Failed to open kprobe_events file: %s\n",
1034 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001035 }
1036 return ret;
1037}
1038
1039/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001040static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001041{
1042 int ret, idx;
1043 FILE *fp;
1044 char buf[MAX_CMDLEN];
1045 char *p;
1046 struct strlist *sl;
1047
1048 sl = strlist__new(true, NULL);
1049
1050 fp = fdopen(dup(fd), "r");
1051 while (!feof(fp)) {
1052 p = fgets(buf, MAX_CMDLEN, fp);
1053 if (!p)
1054 break;
1055
1056 idx = strlen(p) - 1;
1057 if (p[idx] == '\n')
1058 p[idx] = '\0';
1059 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001060 if (ret < 0) {
1061 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1062 strlist__delete(sl);
1063 return NULL;
1064 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001065 }
1066 fclose(fp);
1067
1068 return sl;
1069}
1070
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001071/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001072static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001073{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001074 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001075 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001076 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001077
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001078 /* Synthesize only event probe point */
1079 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001080 if (!place)
1081 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082
1083 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001084 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001085 return ret;
1086
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001087 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001088
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001089 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001090 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001091 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092 ret = synthesize_perf_probe_arg(&pev->args[i],
1093 buf, 128);
1094 if (ret < 0)
1095 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001096 printf(" %s", buf);
1097 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001098 }
1099 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001100 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001101 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001102}
1103
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001104/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001106{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001107 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001108 struct kprobe_trace_event tev;
1109 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001110 struct strlist *rawlist;
1111 struct str_node *ent;
1112
Masami Hiramatsu72041332010-01-05 17:47:10 -05001113 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001114 ret = init_vmlinux();
1115 if (ret < 0)
1116 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001117
1118 memset(&tev, 0, sizeof(tev));
1119 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001120
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001121 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001122 if (fd < 0)
1123 return fd;
1124
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001125 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001126 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001127 if (!rawlist)
1128 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001129
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001130 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001131 ret = parse_kprobe_trace_command(ent->s, &tev);
1132 if (ret >= 0) {
1133 ret = convert_to_perf_probe_event(&tev, &pev);
1134 if (ret >= 0)
1135 ret = show_perf_probe_event(&pev);
1136 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001137 clear_perf_probe_event(&pev);
1138 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001139 if (ret < 0)
1140 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001141 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001142 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001143
1144 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001145}
1146
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001147/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001148static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001149{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001150 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001151 struct strlist *sl, *rawlist;
1152 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001153 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001154 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001155
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001156 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001157
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001158 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001159 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001160 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001161 ret = parse_kprobe_trace_command(ent->s, &tev);
1162 if (ret < 0)
1163 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001164 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1166 tev.event);
1167 if (ret >= 0)
1168 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001169 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001171 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 if (ret < 0)
1173 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001174 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001175 strlist__delete(rawlist);
1176
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001177 if (ret < 0) {
1178 strlist__delete(sl);
1179 return NULL;
1180 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001181 return sl;
1182}
1183
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001184static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001185{
1186 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001187 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001188
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189 if (!buf) {
1190 pr_debug("Failed to synthesize kprobe trace event.\n");
1191 return -EINVAL;
1192 }
1193
Masami Hiramatsufa282442009-12-08 17:03:23 -05001194 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001195 if (!probe_event_dry_run) {
1196 ret = write(fd, buf, strlen(buf));
1197 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 pr_warning("Failed to write event: %s\n",
1199 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001200 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001201 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001202 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203}
1204
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001205static int get_new_event_name(char *buf, size_t len, const char *base,
1206 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001207{
1208 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001209
1210 /* Try no suffix */
1211 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212 if (ret < 0) {
1213 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1214 return ret;
1215 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001216 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001218
Masami Hiramatsud761b082009-12-15 10:32:25 -05001219 if (!allow_suffix) {
1220 pr_warning("Error: event \"%s\" already exists. "
1221 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001223 }
1224
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001225 /* Try to add suffix */
1226 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001227 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228 if (ret < 0) {
1229 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1230 return ret;
1231 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001232 if (!strlist__has_entry(namelist, buf))
1233 break;
1234 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 if (i == MAX_EVENT_INDEX) {
1236 pr_warning("Too many events are on the same function.\n");
1237 ret = -ERANGE;
1238 }
1239
1240 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001241}
1242
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001243static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1244 struct kprobe_trace_event *tevs,
1245 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001246{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001248 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001249 char buf[64];
1250 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001251 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001252
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001253 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254 if (fd < 0)
1255 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001256 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001257 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 if (!namelist) {
1259 pr_debug("Failed to get current event list.\n");
1260 return -EIO;
1261 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001262
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001264 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001265 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001266 tev = &tevs[i];
1267 if (pev->event)
1268 event = pev->event;
1269 else
1270 if (pev->point.function)
1271 event = pev->point.function;
1272 else
1273 event = tev->point.symbol;
1274 if (pev->group)
1275 group = pev->group;
1276 else
1277 group = PERFPROBE_GROUP;
1278
1279 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280 ret = get_new_event_name(buf, 64, event,
1281 namelist, allow_suffix);
1282 if (ret < 0)
1283 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284 event = buf;
1285
1286 tev->event = xstrdup(event);
1287 tev->group = xstrdup(group);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001288 ret = write_kprobe_trace_event(fd, tev);
1289 if (ret < 0)
1290 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001291 /* Add added event name to namelist */
1292 strlist__add(namelist, event);
1293
1294 /* Trick here - save current event/group */
1295 event = pev->event;
1296 group = pev->group;
1297 pev->event = tev->event;
1298 pev->group = tev->group;
1299 show_perf_probe_event(pev);
1300 /* Trick here - restore current event/group */
1301 pev->event = (char *)event;
1302 pev->group = (char *)group;
1303
1304 /*
1305 * Probes after the first probe which comes from same
1306 * user input are always allowed to add suffix, because
1307 * there might be several addresses corresponding to
1308 * one code line.
1309 */
1310 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001311 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312
1313 if (ret >= 0) {
1314 /* Show how to use the event. */
1315 printf("\nYou can now use it on all perf tools, such as:\n\n");
1316 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1317 tev->event);
1318 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001319
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001320 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001321 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001323}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001324
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001325static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1326 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001327{
1328 struct symbol *sym;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001329 int ntevs = 0, i;
1330 struct kprobe_trace_event *tev;
1331
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001332 /* Convert perf_probe_event with debuginfo */
1333 ntevs = try_to_find_kprobe_trace_events(pev, tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001334 if (ntevs != 0)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001335 return ntevs;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001336
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001337 /* Allocate trace event buffer */
1338 ntevs = 1;
1339 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001340
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001341 /* Copy parameters */
1342 tev->point.symbol = xstrdup(pev->point.function);
1343 tev->point.offset = pev->point.offset;
1344 tev->nargs = pev->nargs;
1345 if (tev->nargs) {
1346 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
1347 * tev->nargs);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001348 for (i = 0; i < tev->nargs; i++) {
1349 if (pev->args[i].name)
1350 tev->args[i].name = xstrdup(pev->args[i].name);
1351 tev->args[i].value = xstrdup(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001352 if (pev->args[i].type)
1353 tev->args[i].type = xstrdup(pev->args[i].type);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001354 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001355 }
1356
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001357 /* Currently just checking function name from symbol map */
1358 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1359 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 if (!sym) {
1361 pr_warning("Kernel symbol \'%s\' not found.\n",
1362 tev->point.symbol);
1363 return -ENOENT;
1364 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001365 return ntevs;
1366}
1367
1368struct __event_package {
1369 struct perf_probe_event *pev;
1370 struct kprobe_trace_event *tevs;
1371 int ntevs;
1372};
1373
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001374int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1375 bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001376{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001378 struct __event_package *pkgs;
1379
1380 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1381
1382 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001383 ret = init_vmlinux();
1384 if (ret < 0)
1385 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001386
1387 /* Loop 1: convert all events */
1388 for (i = 0; i < npevs; i++) {
1389 pkgs[i].pev = &pevs[i];
1390 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1392 &pkgs[i].tevs);
1393 if (ret < 0)
1394 goto end;
1395 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001396 }
1397
1398 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399 for (i = 0; i < npevs && ret >= 0; i++)
1400 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1401 pkgs[i].ntevs, force_add);
1402end:
1403 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405 for (j = 0; j < pkgs[i].ntevs; j++)
1406 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1407
1408 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001409}
1410
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001412{
1413 char *p;
1414 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001415 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001416
1417 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1419 if (ret < 0)
1420 goto error;
1421
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001422 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001423 if (!p) {
1424 pr_debug("Internal error: %s should have ':' but not.\n",
1425 ent->s);
1426 ret = -ENOTSUP;
1427 goto error;
1428 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001429 *p = '/';
1430
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001431 pr_debug("Writing event: %s\n", buf);
1432 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 if (ret < 0)
1434 goto error;
1435
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001436 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 return 0;
1438error:
1439 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1440 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001441}
1442
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443static int del_trace_kprobe_event(int fd, const char *group,
1444 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001445{
1446 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001447 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001449
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1451 if (ret < 0) {
1452 pr_err("Failed to copy event.");
1453 return ret;
1454 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001455
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001456 if (strpbrk(buf, "*?")) { /* Glob-exp */
1457 strlist__for_each_safe(ent, n, namelist)
1458 if (strglobmatch(ent->s, buf)) {
1459 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001460 ret = __del_trace_kprobe_event(fd, ent);
1461 if (ret < 0)
1462 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001463 strlist__remove(namelist, ent);
1464 }
1465 } else {
1466 ent = strlist__find(namelist, buf);
1467 if (ent) {
1468 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469 ret = __del_trace_kprobe_event(fd, ent);
1470 if (ret >= 0)
1471 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001472 }
1473 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474 if (found == 0 && ret >= 0)
1475 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1476
1477 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001478}
1479
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001481{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001483 const char *group, *event;
1484 char *p, *str;
1485 struct str_node *ent;
1486 struct strlist *namelist;
1487
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001488 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001489 if (fd < 0)
1490 return fd;
1491
Masami Hiramatsufa282442009-12-08 17:03:23 -05001492 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001493 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494 if (namelist == NULL)
1495 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001496
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001497 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001498 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001499 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001500 p = strchr(str, ':');
1501 if (p) {
1502 group = str;
1503 *p = '\0';
1504 event = p + 1;
1505 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001506 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001507 event = str;
1508 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001509 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001511 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001512 if (ret < 0)
1513 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001514 }
1515 strlist__delete(namelist);
1516 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001517
1518 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001519}
1520