blob: aacbf730b4754dae4dfcf69db1fc1f18a5f3cce4 [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 {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400559 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
560 if (*fieldp == NULL)
561 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400562 if (*tmp == '.') {
563 str = tmp + 1;
564 (*fieldp)->ref = false;
565 } else if (tmp[1] == '>') {
566 str = tmp + 2;
567 (*fieldp)->ref = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400568 } else {
569 semantic_error("Argument parse error: %s\n", str);
570 return -EINVAL;
571 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400572
573 tmp = strpbrk(str, "-.");
574 if (tmp) {
575 (*fieldp)->name = xstrndup(str, tmp - str);
576 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
577 fieldp = &(*fieldp)->next;
578 }
579 } while (tmp);
580 (*fieldp)->name = xstrdup(str);
581 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400582
583 /* If no name is specified, set the last field name */
584 if (!arg->name)
585 arg->name = xstrdup((*fieldp)->name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400586
587 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400588}
589
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400590/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400591int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500592{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500593 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400594 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500595
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400596 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400597 if (!argv) {
598 pr_debug("Failed to split arguments.\n");
599 return -ENOMEM;
600 }
601 if (argc - 1 > MAX_PROBE_ARGS) {
602 semantic_error("Too many probe arguments (%d).\n", argc - 1);
603 ret = -ERANGE;
604 goto out;
605 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500606 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400607 ret = parse_perf_probe_point(argv[0], pev);
608 if (ret < 0)
609 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500610
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500611 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400612 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400613 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
614 if (pev->args == NULL) {
615 ret = -ENOMEM;
616 goto out;
617 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400618 for (i = 0; i < pev->nargs && ret >= 0; i++) {
619 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
620 if (ret >= 0 &&
621 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400622 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400623 " kretprobe.\n");
624 ret = -EINVAL;
625 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500626 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400627out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500628 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629
630 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500631}
632
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400633/* Return true if this perf_probe_event requires debuginfo */
634bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500635{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400636 int i;
637
638 if (pev->point.file || pev->point.line || pev->point.lazy_line)
639 return true;
640
641 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400642 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400643 return true;
644
645 return false;
646}
647
648/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400649int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400650{
651 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500652 char pr;
653 char *p;
654 int ret, i, argc;
655 char **argv;
656
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400657 pr_debug("Parsing kprobe_events: %s\n", cmd);
658 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400659 if (!argv) {
660 pr_debug("Failed to split arguments.\n");
661 return -ENOMEM;
662 }
663 if (argc < 2) {
664 semantic_error("Too few probe arguments.\n");
665 ret = -ERANGE;
666 goto out;
667 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500668
669 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800670 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400671 &pr, (float *)(void *)&tev->group,
672 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400673 if (ret != 3) {
674 semantic_error("Failed to parse event name: %s\n", argv[0]);
675 ret = -EINVAL;
676 goto out;
677 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400678 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500679
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400680 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500681
682 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400683 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
684 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500685 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400686 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500687
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400688 tev->nargs = argc - 2;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400689 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
690 if (tev->args == NULL) {
691 ret = -ENOMEM;
692 goto out;
693 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400694 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500695 p = strchr(argv[i + 2], '=');
696 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400697 *p++ = '\0';
698 else
699 p = argv[i + 2];
700 tev->args[i].name = xstrdup(argv[i + 2]);
701 /* TODO: parse regs and offset */
702 tev->args[i].value = xstrdup(p);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500703 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 ret = 0;
705out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500706 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500708}
709
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400710/* Compose only probe arg */
711int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
712{
713 struct perf_probe_arg_field *field = pa->field;
714 int ret;
715 char *tmp = buf;
716
Masami Hiramatsu48481932010-04-12 13:16:53 -0400717 if (pa->name && pa->var)
718 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
719 else
720 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400721 if (ret <= 0)
722 goto error;
723 tmp += ret;
724 len -= ret;
725
726 while (field) {
727 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
728 field->name);
729 if (ret <= 0)
730 goto error;
731 tmp += ret;
732 len -= ret;
733 field = field->next;
734 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400735
736 if (pa->type) {
737 ret = e_snprintf(tmp, len, ":%s", pa->type);
738 if (ret <= 0)
739 goto error;
740 tmp += ret;
741 len -= ret;
742 }
743
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400744 return tmp - buf;
745error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400746 pr_debug("Failed to synthesize perf probe argument: %s",
747 strerror(-ret));
748 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400749}
750
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400751/* Compose only probe point (not argument) */
752static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500753{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400754 char *buf, *tmp;
755 char offs[32] = "", line[32] = "", file[32] = "";
756 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500757
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400758 buf = zalloc(MAX_CMDLEN);
759 if (buf == NULL) {
760 ret = -ENOMEM;
761 goto error;
762 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500763 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400764 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500765 if (ret <= 0)
766 goto error;
767 }
768 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400769 ret = e_snprintf(line, 32, ":%d", pp->line);
770 if (ret <= 0)
771 goto error;
772 }
773 if (pp->file) {
774 len = strlen(pp->file) - 32;
775 if (len < 0)
776 len = 0;
777 tmp = strchr(pp->file + len, '/');
778 if (!tmp)
779 tmp = pp->file + len - 1;
780 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500781 if (ret <= 0)
782 goto error;
783 }
784
785 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400786 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
787 offs, pp->retprobe ? "%return" : "", line,
788 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500789 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400790 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500791 if (ret <= 0)
792 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500793
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400794 return buf;
795error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400796 pr_debug("Failed to synthesize perf probe point: %s",
797 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400798 if (buf)
799 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400800 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400801}
802
803#if 0
804char *synthesize_perf_probe_command(struct perf_probe_event *pev)
805{
806 char *buf;
807 int i, len, ret;
808
809 buf = synthesize_perf_probe_point(&pev->point);
810 if (!buf)
811 return NULL;
812
813 len = strlen(buf);
814 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500815 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400816 pev->args[i].name);
817 if (ret <= 0) {
818 free(buf);
819 return NULL;
820 }
821 len += ret;
822 }
823
824 return buf;
825}
826#endif
827
828static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
829 char **buf, size_t *buflen,
830 int depth)
831{
832 int ret;
833 if (ref->next) {
834 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
835 buflen, depth + 1);
836 if (depth < 0)
837 goto out;
838 }
839
840 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
841 if (ret < 0)
842 depth = ret;
843 else {
844 *buf += ret;
845 *buflen -= ret;
846 }
847out:
848 return depth;
849
850}
851
852static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
853 char *buf, size_t buflen)
854{
855 int ret, depth = 0;
856 char *tmp = buf;
857
858 /* Argument name or separator */
859 if (arg->name)
860 ret = e_snprintf(buf, buflen, " %s=", arg->name);
861 else
862 ret = e_snprintf(buf, buflen, " ");
863 if (ret < 0)
864 return ret;
865 buf += ret;
866 buflen -= ret;
867
868 /* Dereferencing arguments */
869 if (arg->ref) {
870 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
871 &buflen, 1);
872 if (depth < 0)
873 return depth;
874 }
875
876 /* Print argument value */
877 ret = e_snprintf(buf, buflen, "%s", arg->value);
878 if (ret < 0)
879 return ret;
880 buf += ret;
881 buflen -= ret;
882
883 /* Closing */
884 while (depth--) {
885 ret = e_snprintf(buf, buflen, ")");
886 if (ret < 0)
887 return ret;
888 buf += ret;
889 buflen -= ret;
890 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400891 /* Print argument type */
892 if (arg->type) {
893 ret = e_snprintf(buf, buflen, ":%s", arg->type);
894 if (ret <= 0)
895 return ret;
896 buf += ret;
897 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400898
899 return buf - tmp;
900}
901
902char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
903{
904 struct kprobe_trace_point *tp = &tev->point;
905 char *buf;
906 int i, len, ret;
907
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400908 buf = zalloc(MAX_CMDLEN);
909 if (buf == NULL)
910 return NULL;
911
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400912 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
913 tp->retprobe ? 'r' : 'p',
914 tev->group, tev->event,
915 tp->symbol, tp->offset);
916 if (len <= 0)
917 goto error;
918
919 for (i = 0; i < tev->nargs; i++) {
920 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
921 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500922 if (ret <= 0)
923 goto error;
924 len += ret;
925 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500926
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400927 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500928error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400929 free(buf);
930 return NULL;
931}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500932
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400933int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
934 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400935{
936 char buf[64];
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400937 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400938
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300939 /* Convert event/group name */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400940 pev->event = xstrdup(tev->event);
941 pev->group = xstrdup(tev->group);
942
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300943 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400944 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
945 if (ret < 0)
946 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300947
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400948 /* Convert trace_arg to probe_arg */
949 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400950 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
951 if (pev->args == NULL)
952 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400953 for (i = 0; i < tev->nargs && ret >= 0; i++)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400954 if (tev->args[i].name)
955 pev->args[i].name = xstrdup(tev->args[i].name);
956 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400957 ret = synthesize_kprobe_trace_arg(&tev->args[i],
958 buf, 64);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 pev->args[i].name = xstrdup(buf);
960 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400961
962 if (ret < 0)
963 clear_perf_probe_event(pev);
964
965 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400966}
967
968void clear_perf_probe_event(struct perf_probe_event *pev)
969{
970 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400971 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400972 int i;
973
974 if (pev->event)
975 free(pev->event);
976 if (pev->group)
977 free(pev->group);
978 if (pp->file)
979 free(pp->file);
980 if (pp->function)
981 free(pp->function);
982 if (pp->lazy_line)
983 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400984 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400985 if (pev->args[i].name)
986 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400987 if (pev->args[i].var)
988 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400989 if (pev->args[i].type)
990 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400991 field = pev->args[i].field;
992 while (field) {
993 next = field->next;
994 if (field->name)
995 free(field->name);
996 free(field);
997 field = next;
998 }
999 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 if (pev->args)
1001 free(pev->args);
1002 memset(pev, 0, sizeof(*pev));
1003}
1004
1005void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1006{
1007 struct kprobe_trace_arg_ref *ref, *next;
1008 int i;
1009
1010 if (tev->event)
1011 free(tev->event);
1012 if (tev->group)
1013 free(tev->group);
1014 if (tev->point.symbol)
1015 free(tev->point.symbol);
1016 for (i = 0; i < tev->nargs; i++) {
1017 if (tev->args[i].name)
1018 free(tev->args[i].name);
1019 if (tev->args[i].value)
1020 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001021 if (tev->args[i].type)
1022 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001023 ref = tev->args[i].ref;
1024 while (ref) {
1025 next = ref->next;
1026 free(ref);
1027 ref = next;
1028 }
1029 }
1030 if (tev->args)
1031 free(tev->args);
1032 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001033}
1034
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001035static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001036{
1037 char buf[PATH_MAX];
1038 int ret;
1039
1040 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001041 if (ret >= 0) {
1042 if (readwrite && !probe_event_dry_run)
1043 ret = open(buf, O_RDWR, O_APPEND);
1044 else
1045 ret = open(buf, O_RDONLY, 0);
1046 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001047
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001048 if (ret < 0) {
1049 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001050 pr_warning("kprobe_events file does not exist - please"
1051 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001052 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 pr_warning("Failed to open kprobe_events file: %s\n",
1054 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001055 }
1056 return ret;
1057}
1058
1059/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001060static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001061{
1062 int ret, idx;
1063 FILE *fp;
1064 char buf[MAX_CMDLEN];
1065 char *p;
1066 struct strlist *sl;
1067
1068 sl = strlist__new(true, NULL);
1069
1070 fp = fdopen(dup(fd), "r");
1071 while (!feof(fp)) {
1072 p = fgets(buf, MAX_CMDLEN, fp);
1073 if (!p)
1074 break;
1075
1076 idx = strlen(p) - 1;
1077 if (p[idx] == '\n')
1078 p[idx] = '\0';
1079 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001080 if (ret < 0) {
1081 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1082 strlist__delete(sl);
1083 return NULL;
1084 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001085 }
1086 fclose(fp);
1087
1088 return sl;
1089}
1090
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001091/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001092static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001093{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001094 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001095 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001096 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001097
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001098 /* Synthesize only event probe point */
1099 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001100 if (!place)
1101 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001102
1103 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001104 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 return ret;
1106
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001107 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001108
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001109 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001110 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001111 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001112 ret = synthesize_perf_probe_arg(&pev->args[i],
1113 buf, 128);
1114 if (ret < 0)
1115 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001116 printf(" %s", buf);
1117 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001118 }
1119 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001120 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001121 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001122}
1123
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001124/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001125int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001126{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001127 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001128 struct kprobe_trace_event tev;
1129 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001130 struct strlist *rawlist;
1131 struct str_node *ent;
1132
Masami Hiramatsu72041332010-01-05 17:47:10 -05001133 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001134 ret = init_vmlinux();
1135 if (ret < 0)
1136 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001137
1138 memset(&tev, 0, sizeof(tev));
1139 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001140
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001141 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001142 if (fd < 0)
1143 return fd;
1144
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001145 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001146 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001147 if (!rawlist)
1148 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001149
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001150 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001151 ret = parse_kprobe_trace_command(ent->s, &tev);
1152 if (ret >= 0) {
1153 ret = convert_to_perf_probe_event(&tev, &pev);
1154 if (ret >= 0)
1155 ret = show_perf_probe_event(&pev);
1156 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001157 clear_perf_probe_event(&pev);
1158 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 if (ret < 0)
1160 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001161 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001162 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001163
1164 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001165}
1166
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001167/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001168static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001169{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001170 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001171 struct strlist *sl, *rawlist;
1172 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001173 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001175
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001176 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001177
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001178 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001179 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001180 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001181 ret = parse_kprobe_trace_command(ent->s, &tev);
1182 if (ret < 0)
1183 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001184 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001185 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1186 tev.event);
1187 if (ret >= 0)
1188 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001189 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001191 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 if (ret < 0)
1193 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001194 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001195 strlist__delete(rawlist);
1196
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 if (ret < 0) {
1198 strlist__delete(sl);
1199 return NULL;
1200 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001201 return sl;
1202}
1203
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205{
1206 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001208
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 if (!buf) {
1210 pr_debug("Failed to synthesize kprobe trace event.\n");
1211 return -EINVAL;
1212 }
1213
Masami Hiramatsufa282442009-12-08 17:03:23 -05001214 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001215 if (!probe_event_dry_run) {
1216 ret = write(fd, buf, strlen(buf));
1217 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218 pr_warning("Failed to write event: %s\n",
1219 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001220 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001221 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001223}
1224
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225static int get_new_event_name(char *buf, size_t len, const char *base,
1226 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001227{
1228 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001229
1230 /* Try no suffix */
1231 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001232 if (ret < 0) {
1233 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1234 return ret;
1235 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001236 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001238
Masami Hiramatsud761b082009-12-15 10:32:25 -05001239 if (!allow_suffix) {
1240 pr_warning("Error: event \"%s\" already exists. "
1241 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001243 }
1244
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001245 /* Try to add suffix */
1246 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001247 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248 if (ret < 0) {
1249 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1250 return ret;
1251 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001252 if (!strlist__has_entry(namelist, buf))
1253 break;
1254 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 if (i == MAX_EVENT_INDEX) {
1256 pr_warning("Too many events are on the same function.\n");
1257 ret = -ERANGE;
1258 }
1259
1260 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001261}
1262
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1264 struct kprobe_trace_event *tevs,
1265 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001267 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001268 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 char buf[64];
1270 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001271 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001272
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001273 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001274 if (fd < 0)
1275 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001276 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001278 if (!namelist) {
1279 pr_debug("Failed to get current event list.\n");
1280 return -EIO;
1281 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001282
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001283 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001284 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001285 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001286 tev = &tevs[i];
1287 if (pev->event)
1288 event = pev->event;
1289 else
1290 if (pev->point.function)
1291 event = pev->point.function;
1292 else
1293 event = tev->point.symbol;
1294 if (pev->group)
1295 group = pev->group;
1296 else
1297 group = PERFPROBE_GROUP;
1298
1299 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001300 ret = get_new_event_name(buf, 64, event,
1301 namelist, allow_suffix);
1302 if (ret < 0)
1303 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001304 event = buf;
1305
1306 tev->event = xstrdup(event);
1307 tev->group = xstrdup(group);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 ret = write_kprobe_trace_event(fd, tev);
1309 if (ret < 0)
1310 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001311 /* Add added event name to namelist */
1312 strlist__add(namelist, event);
1313
1314 /* Trick here - save current event/group */
1315 event = pev->event;
1316 group = pev->group;
1317 pev->event = tev->event;
1318 pev->group = tev->group;
1319 show_perf_probe_event(pev);
1320 /* Trick here - restore current event/group */
1321 pev->event = (char *)event;
1322 pev->group = (char *)group;
1323
1324 /*
1325 * Probes after the first probe which comes from same
1326 * user input are always allowed to add suffix, because
1327 * there might be several addresses corresponding to
1328 * one code line.
1329 */
1330 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001331 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332
1333 if (ret >= 0) {
1334 /* Show how to use the event. */
1335 printf("\nYou can now use it on all perf tools, such as:\n\n");
1336 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1337 tev->event);
1338 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001339
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001340 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001341 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001342 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001343}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001344
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001345static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1346 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001347{
1348 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001349 int ret = 0, i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001350 struct kprobe_trace_event *tev;
1351
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001352 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001353 ret = try_to_find_kprobe_trace_events(pev, tevs);
1354 if (ret != 0)
1355 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001356
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001357 /* Allocate trace event buffer */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001358 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1359 if (tev == NULL)
1360 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001361
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001362 /* Copy parameters */
1363 tev->point.symbol = xstrdup(pev->point.function);
1364 tev->point.offset = pev->point.offset;
1365 tev->nargs = pev->nargs;
1366 if (tev->nargs) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001367 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1368 * tev->nargs);
1369 if (tev->args == NULL) {
1370 free(tev);
1371 *tevs = NULL;
1372 return -ENOMEM;
1373 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001374 for (i = 0; i < tev->nargs; i++) {
1375 if (pev->args[i].name)
1376 tev->args[i].name = xstrdup(pev->args[i].name);
1377 tev->args[i].value = xstrdup(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001378 if (pev->args[i].type)
1379 tev->args[i].type = xstrdup(pev->args[i].type);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001380 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001381 }
1382
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001383 /* Currently just checking function name from symbol map */
1384 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1385 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001386 if (!sym) {
1387 pr_warning("Kernel symbol \'%s\' not found.\n",
1388 tev->point.symbol);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001389 clear_kprobe_trace_event(tev);
1390 free(tev);
1391 *tevs = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001392 return -ENOENT;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001393 } else
1394 ret = 1;
1395
1396 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001397}
1398
1399struct __event_package {
1400 struct perf_probe_event *pev;
1401 struct kprobe_trace_event *tevs;
1402 int ntevs;
1403};
1404
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1406 bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001407{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001409 struct __event_package *pkgs;
1410
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001411 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1412 if (pkgs == NULL)
1413 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001414
1415 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416 ret = init_vmlinux();
1417 if (ret < 0)
1418 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001419
1420 /* Loop 1: convert all events */
1421 for (i = 0; i < npevs; i++) {
1422 pkgs[i].pev = &pevs[i];
1423 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001424 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1425 &pkgs[i].tevs);
1426 if (ret < 0)
1427 goto end;
1428 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 }
1430
1431 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001432 for (i = 0; i < npevs && ret >= 0; i++)
1433 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1434 pkgs[i].ntevs, force_add);
1435end:
1436 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001438 for (j = 0; j < pkgs[i].ntevs; j++)
1439 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1440
1441 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001442}
1443
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001445{
1446 char *p;
1447 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001448 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001449
1450 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1452 if (ret < 0)
1453 goto error;
1454
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001455 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (!p) {
1457 pr_debug("Internal error: %s should have ':' but not.\n",
1458 ent->s);
1459 ret = -ENOTSUP;
1460 goto error;
1461 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001462 *p = '/';
1463
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 pr_debug("Writing event: %s\n", buf);
1465 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 if (ret < 0)
1467 goto error;
1468
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001469 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 return 0;
1471error:
1472 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1473 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001474}
1475
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001476static int del_trace_kprobe_event(int fd, const char *group,
1477 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001478{
1479 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001480 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001481 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001482
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001483 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1484 if (ret < 0) {
1485 pr_err("Failed to copy event.");
1486 return ret;
1487 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001488
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001489 if (strpbrk(buf, "*?")) { /* Glob-exp */
1490 strlist__for_each_safe(ent, n, namelist)
1491 if (strglobmatch(ent->s, buf)) {
1492 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001493 ret = __del_trace_kprobe_event(fd, ent);
1494 if (ret < 0)
1495 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001496 strlist__remove(namelist, ent);
1497 }
1498 } else {
1499 ent = strlist__find(namelist, buf);
1500 if (ent) {
1501 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001502 ret = __del_trace_kprobe_event(fd, ent);
1503 if (ret >= 0)
1504 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001505 }
1506 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 if (found == 0 && ret >= 0)
1508 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1509
1510 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001511}
1512
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001513int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001514{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001515 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001516 const char *group, *event;
1517 char *p, *str;
1518 struct str_node *ent;
1519 struct strlist *namelist;
1520
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001521 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522 if (fd < 0)
1523 return fd;
1524
Masami Hiramatsufa282442009-12-08 17:03:23 -05001525 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 if (namelist == NULL)
1528 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001529
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001530 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001531 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001532 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001533 p = strchr(str, ':');
1534 if (p) {
1535 group = str;
1536 *p = '\0';
1537 event = p + 1;
1538 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001539 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001540 event = str;
1541 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001542 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001543 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001544 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001545 if (ret < 0)
1546 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001547 }
1548 strlist__delete(namelist);
1549 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550
1551 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001552}
1553