blob: 64dea6c3d58ac53923f3ad5e09250ae5876eb807 [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 Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "parse-events.h" /* For debugfs_path */
46#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048
49#define MAX_CMDLEN 256
50#define MAX_PROBE_ARGS 128
51#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050055#define semantic_error(msg ...) die("Semantic error :" msg)
56
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040073static struct map_groups kmap_groups;
74static struct map *kmaps[MAP__NR_TYPES];
75
76/* Initialize symbol maps for vmlinux */
77static void init_vmlinux(void)
78{
79 symbol_conf.sort_by_name = true;
80 if (symbol_conf.vmlinux_name == NULL)
81 symbol_conf.try_vmlinux_path = true;
82 else
83 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
84 if (symbol__init() < 0)
85 die("Failed to init symbol map.");
86
87 map_groups__init(&kmap_groups);
88 if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
89 die("Failed to create kernel maps.");
90}
91
92#ifndef NO_DWARF_SUPPORT
93static int open_vmlinux(void)
94{
95 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
96 pr_debug("Failed to load kernel map.\n");
97 return -EINVAL;
98 }
99 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
100 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
101}
102#endif
103
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500104void parse_line_range_desc(const char *arg, struct line_range *lr)
105{
106 const char *ptr;
107 char *tmp;
108 /*
109 * <Syntax>
110 * SRC:SLN[+NUM|-ELN]
111 * FUNC[:SLN[+NUM|-ELN]]
112 */
113 ptr = strchr(arg, ':');
114 if (ptr) {
115 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
116 if (*tmp == '+')
117 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
118 &tmp, 0);
119 else if (*tmp == '-')
120 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
121 else
122 lr->end = 0;
123 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
124 if (lr->end && lr->start > lr->end)
125 semantic_error("Start line must be smaller"
126 " than end line.");
127 if (*tmp != '\0')
128 semantic_error("Tailing with invalid character '%d'.",
129 *tmp);
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400130 tmp = xstrndup(arg, (ptr - arg));
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500131 } else
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400132 tmp = xstrdup(arg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500133
134 if (strchr(tmp, '.'))
135 lr->file = tmp;
136 else
137 lr->function = tmp;
138}
139
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500140/* Check the name is good for event/group */
141static bool check_event_name(const char *name)
142{
143 if (!isalpha(*name) && *name != '_')
144 return false;
145 while (*++name != '\0') {
146 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
147 return false;
148 }
149 return true;
150}
151
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500152/* Parse probepoint definition. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400153static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500154{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400155 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500156 char *ptr, *tmp;
157 char c, nc = 0;
158 /*
159 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500160 * perf probe [EVENT=]SRC[:LN|;PTN]
161 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500162 *
163 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500164 */
165
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500166 ptr = strpbrk(arg, ";=@+%");
167 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500168 *ptr = '\0';
169 tmp = ptr + 1;
170 ptr = strchr(arg, ':');
171 if (ptr) /* Group name is not supported yet. */
172 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500173 if (!check_event_name(arg))
174 semantic_error("%s is bad for event name -it must "
175 "follow C symbol-naming rule.", arg);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400176 pev->event = xstrdup(arg);
177 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500178 arg = tmp;
179 }
180
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500181 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500182 if (ptr) {
183 nc = *ptr;
184 *ptr++ = '\0';
185 }
186
187 /* Check arg is function or file and copy it */
188 if (strchr(arg, '.')) /* File */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400189 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500190 else /* Function */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400191 pp->function = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500192
193 /* Parse other options */
194 while (ptr) {
195 arg = ptr;
196 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500197 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400198 pp->lazy_line = xstrdup(arg);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500199 break;
200 }
201 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500202 if (ptr) {
203 nc = *ptr;
204 *ptr++ = '\0';
205 }
206 switch (c) {
207 case ':': /* Line number */
208 pp->line = strtoul(arg, &tmp, 0);
209 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500210 semantic_error("There is non-digit char"
211 " in line number.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500212 break;
213 case '+': /* Byte offset from a symbol */
214 pp->offset = strtoul(arg, &tmp, 0);
215 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500216 semantic_error("There is non-digit character"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500217 " in offset.");
218 break;
219 case '@': /* File name */
220 if (pp->file)
221 semantic_error("SRC@SRC is not allowed.");
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400222 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500223 break;
224 case '%': /* Probe places */
225 if (strcmp(arg, "return") == 0) {
226 pp->retprobe = 1;
227 } else /* Others not supported yet */
228 semantic_error("%%%s is not supported.", arg);
229 break;
230 default:
231 DIE_IF("Program has a bug.");
232 break;
233 }
234 }
235
236 /* Exclusion check */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500237 if (pp->lazy_line && pp->line)
238 semantic_error("Lazy pattern can't be used with line number.");
239
240 if (pp->lazy_line && pp->offset)
241 semantic_error("Lazy pattern can't be used with offset.");
242
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500243 if (pp->line && pp->offset)
244 semantic_error("Offset can't be used with line number.");
245
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500246 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
247 semantic_error("File always requires line number or "
248 "lazy pattern.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500249
250 if (pp->offset && !pp->function)
251 semantic_error("Offset requires an entry function.");
252
253 if (pp->retprobe && !pp->function)
254 semantic_error("Return probe requires an entry function.");
255
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500256 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
257 semantic_error("Offset/Line/Lazy pattern can't be used with "
258 "return probe.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500259
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400260 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500261 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
262 pp->lazy_line);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500263}
264
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400265/* Parse perf-probe event argument */
266static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg)
267{
268 const char *tmp;
269 struct perf_probe_arg_field **fieldp;
270
271 pr_debug("parsing arg: %s into ", str);
272
273 tmp = strpbrk(str, "-.");
274 if (!is_c_varname(str) || !tmp) {
275 /* A variable, register, symbol or special value */
276 arg->name = xstrdup(str);
277 pr_debug("%s\n", arg->name);
278 return;
279 }
280
281 /* Structure fields */
282 arg->name = xstrndup(str, tmp - str);
283 pr_debug("%s, ", arg->name);
284 fieldp = &arg->field;
285
286 do {
287 *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
288 if (*tmp == '.') {
289 str = tmp + 1;
290 (*fieldp)->ref = false;
291 } else if (tmp[1] == '>') {
292 str = tmp + 2;
293 (*fieldp)->ref = true;
294 } else
295 semantic_error("Argument parse error: %s", str);
296
297 tmp = strpbrk(str, "-.");
298 if (tmp) {
299 (*fieldp)->name = xstrndup(str, tmp - str);
300 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
301 fieldp = &(*fieldp)->next;
302 }
303 } while (tmp);
304 (*fieldp)->name = xstrdup(str);
305 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
306}
307
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400308/* Parse perf-probe event command */
309void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500310{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500311 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500312 int argc, i;
313
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400314 argv = argv_split(cmd, &argc);
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500315 if (!argv)
316 die("argv_split failed.");
317 if (argc > MAX_PROBE_ARGS + 1)
318 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500319
320 /* Parse probe point */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400321 parse_perf_probe_point(argv[0], pev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500322
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500323 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400324 pev->nargs = argc - 1;
325 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
326 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400327 parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400328 if (is_c_varname(pev->args[i].name) && pev->point.retprobe)
329 semantic_error("You can't specify local variable for"
330 " kretprobe");
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500331 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500332
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500333 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500334}
335
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400336/* Return true if this perf_probe_event requires debuginfo */
337bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500338{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400339 int i;
340
341 if (pev->point.file || pev->point.line || pev->point.lazy_line)
342 return true;
343
344 for (i = 0; i < pev->nargs; i++)
345 if (is_c_varname(pev->args[i].name))
346 return true;
347
348 return false;
349}
350
351/* Parse kprobe_events event into struct probe_point */
352void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
353{
354 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500355 char pr;
356 char *p;
357 int ret, i, argc;
358 char **argv;
359
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400360 pr_debug("Parsing kprobe_events: %s\n", cmd);
361 argv = argv_split(cmd, &argc);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500362 if (!argv)
363 die("argv_split failed.");
364 if (argc < 2)
365 semantic_error("Too less arguments.");
366
367 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800368 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400369 &pr, (float *)(void *)&tev->group,
370 (float *)(void *)&tev->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500371 if (ret != 3)
372 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400373 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500374
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400375 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500376
377 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400378 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
379 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500380 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400381 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500382
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400383 tev->nargs = argc - 2;
384 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
385 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500386 p = strchr(argv[i + 2], '=');
387 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400388 *p++ = '\0';
389 else
390 p = argv[i + 2];
391 tev->args[i].name = xstrdup(argv[i + 2]);
392 /* TODO: parse regs and offset */
393 tev->args[i].value = xstrdup(p);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500394 }
395
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500396 argv_free(argv);
397}
398
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400399/* Compose only probe arg */
400int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
401{
402 struct perf_probe_arg_field *field = pa->field;
403 int ret;
404 char *tmp = buf;
405
406 ret = e_snprintf(tmp, len, "%s", pa->name);
407 if (ret <= 0)
408 goto error;
409 tmp += ret;
410 len -= ret;
411
412 while (field) {
413 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
414 field->name);
415 if (ret <= 0)
416 goto error;
417 tmp += ret;
418 len -= ret;
419 field = field->next;
420 }
421 return tmp - buf;
422error:
423 die("Failed to synthesize perf probe argument: %s", strerror(-ret));
424}
425
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400426/* Compose only probe point (not argument) */
427static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500428{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400429 char *buf, *tmp;
430 char offs[32] = "", line[32] = "", file[32] = "";
431 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500432
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400433 buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500434 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400435 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500436 if (ret <= 0)
437 goto error;
438 }
439 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400440 ret = e_snprintf(line, 32, ":%d", pp->line);
441 if (ret <= 0)
442 goto error;
443 }
444 if (pp->file) {
445 len = strlen(pp->file) - 32;
446 if (len < 0)
447 len = 0;
448 tmp = strchr(pp->file + len, '/');
449 if (!tmp)
450 tmp = pp->file + len - 1;
451 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500452 if (ret <= 0)
453 goto error;
454 }
455
456 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400457 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
458 offs, pp->retprobe ? "%return" : "", line,
459 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500460 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400461 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500462 if (ret <= 0)
463 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500464
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400465 return buf;
466error:
467 die("Failed to synthesize perf probe point: %s", strerror(-ret));
468}
469
470#if 0
471char *synthesize_perf_probe_command(struct perf_probe_event *pev)
472{
473 char *buf;
474 int i, len, ret;
475
476 buf = synthesize_perf_probe_point(&pev->point);
477 if (!buf)
478 return NULL;
479
480 len = strlen(buf);
481 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500482 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400483 pev->args[i].name);
484 if (ret <= 0) {
485 free(buf);
486 return NULL;
487 }
488 len += ret;
489 }
490
491 return buf;
492}
493#endif
494
495static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
496 char **buf, size_t *buflen,
497 int depth)
498{
499 int ret;
500 if (ref->next) {
501 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
502 buflen, depth + 1);
503 if (depth < 0)
504 goto out;
505 }
506
507 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
508 if (ret < 0)
509 depth = ret;
510 else {
511 *buf += ret;
512 *buflen -= ret;
513 }
514out:
515 return depth;
516
517}
518
519static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
520 char *buf, size_t buflen)
521{
522 int ret, depth = 0;
523 char *tmp = buf;
524
525 /* Argument name or separator */
526 if (arg->name)
527 ret = e_snprintf(buf, buflen, " %s=", arg->name);
528 else
529 ret = e_snprintf(buf, buflen, " ");
530 if (ret < 0)
531 return ret;
532 buf += ret;
533 buflen -= ret;
534
535 /* Dereferencing arguments */
536 if (arg->ref) {
537 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
538 &buflen, 1);
539 if (depth < 0)
540 return depth;
541 }
542
543 /* Print argument value */
544 ret = e_snprintf(buf, buflen, "%s", arg->value);
545 if (ret < 0)
546 return ret;
547 buf += ret;
548 buflen -= ret;
549
550 /* Closing */
551 while (depth--) {
552 ret = e_snprintf(buf, buflen, ")");
553 if (ret < 0)
554 return ret;
555 buf += ret;
556 buflen -= ret;
557 }
558
559 return buf - tmp;
560}
561
562char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
563{
564 struct kprobe_trace_point *tp = &tev->point;
565 char *buf;
566 int i, len, ret;
567
568 buf = xzalloc(MAX_CMDLEN);
569 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
570 tp->retprobe ? 'r' : 'p',
571 tev->group, tev->event,
572 tp->symbol, tp->offset);
573 if (len <= 0)
574 goto error;
575
576 for (i = 0; i < tev->nargs; i++) {
577 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
578 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500579 if (ret <= 0)
580 goto error;
581 len += ret;
582 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500583
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400584 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500585error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400586 free(buf);
587 return NULL;
588}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500589
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400590void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
591 struct perf_probe_event *pev)
592{
593 char buf[64];
594 int i;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400595#ifndef NO_DWARF_SUPPORT
596 struct symbol *sym;
597 int fd, ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400598
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400599 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
600 tev->point.symbol, NULL);
601 if (sym) {
602 fd = open_vmlinux();
603 ret = find_perf_probe_point(fd, sym->start + tev->point.offset,
604 &pev->point);
605 close(fd);
606 }
607 if (ret <= 0) {
608 pev->point.function = xstrdup(tev->point.symbol);
609 pev->point.offset = tev->point.offset;
610 }
611#else
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400612 /* Convert trace_point to probe_point */
613 pev->point.function = xstrdup(tev->point.symbol);
614 pev->point.offset = tev->point.offset;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400615#endif
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400616 pev->point.retprobe = tev->point.retprobe;
617
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400618 pev->event = xstrdup(tev->event);
619 pev->group = xstrdup(tev->group);
620
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400621 /* Convert trace_arg to probe_arg */
622 pev->nargs = tev->nargs;
623 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
624 for (i = 0; i < tev->nargs; i++)
625 if (tev->args[i].name)
626 pev->args[i].name = xstrdup(tev->args[i].name);
627 else {
628 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
629 pev->args[i].name = xstrdup(buf);
630 }
631}
632
633void clear_perf_probe_event(struct perf_probe_event *pev)
634{
635 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400636 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400637 int i;
638
639 if (pev->event)
640 free(pev->event);
641 if (pev->group)
642 free(pev->group);
643 if (pp->file)
644 free(pp->file);
645 if (pp->function)
646 free(pp->function);
647 if (pp->lazy_line)
648 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400649 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400650 if (pev->args[i].name)
651 free(pev->args[i].name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400652 field = pev->args[i].field;
653 while (field) {
654 next = field->next;
655 if (field->name)
656 free(field->name);
657 free(field);
658 field = next;
659 }
660 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400661 if (pev->args)
662 free(pev->args);
663 memset(pev, 0, sizeof(*pev));
664}
665
666void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
667{
668 struct kprobe_trace_arg_ref *ref, *next;
669 int i;
670
671 if (tev->event)
672 free(tev->event);
673 if (tev->group)
674 free(tev->group);
675 if (tev->point.symbol)
676 free(tev->point.symbol);
677 for (i = 0; i < tev->nargs; i++) {
678 if (tev->args[i].name)
679 free(tev->args[i].name);
680 if (tev->args[i].value)
681 free(tev->args[i].value);
682 ref = tev->args[i].ref;
683 while (ref) {
684 next = ref->next;
685 free(ref);
686 ref = next;
687 }
688 }
689 if (tev->args)
690 free(tev->args);
691 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500692}
693
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400694static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500695{
696 char buf[PATH_MAX];
697 int ret;
698
699 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
700 if (ret < 0)
701 die("Failed to make kprobe_events path.");
702
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400703 if (readwrite && !probe_event_dry_run)
704 ret = open(buf, O_RDWR, O_APPEND);
705 else
706 ret = open(buf, O_RDONLY, 0);
707
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500708 if (ret < 0) {
709 if (errno == ENOENT)
710 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800711 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500712 else
713 die("Could not open kprobe_events file: %s",
714 strerror(errno));
715 }
716 return ret;
717}
718
719/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400720static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500721{
722 int ret, idx;
723 FILE *fp;
724 char buf[MAX_CMDLEN];
725 char *p;
726 struct strlist *sl;
727
728 sl = strlist__new(true, NULL);
729
730 fp = fdopen(dup(fd), "r");
731 while (!feof(fp)) {
732 p = fgets(buf, MAX_CMDLEN, fp);
733 if (!p)
734 break;
735
736 idx = strlen(p) - 1;
737 if (p[idx] == '\n')
738 p[idx] = '\0';
739 ret = strlist__add(sl, buf);
740 if (ret < 0)
741 die("strlist__add failed: %s", strerror(-ret));
742 }
743 fclose(fp);
744
745 return sl;
746}
747
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500748/* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400749static void show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500750{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500751 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500752 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400753 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500754
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400755 /* Synthesize only event probe point */
756 place = synthesize_perf_probe_point(&pev->point);
757
758 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500759 if (ret < 0)
760 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400761 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500762
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400763 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500764 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400765 for (i = 0; i < pev->nargs; i++) {
766 synthesize_perf_probe_arg(&pev->args[i], buf, 128);
767 printf(" %s", buf);
768 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500769 }
770 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400771 free(place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500772}
773
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500774/* List up current perf-probe events */
775void show_perf_probe_events(void)
776{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500777 int fd;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400778 struct kprobe_trace_event tev;
779 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500780 struct strlist *rawlist;
781 struct str_node *ent;
782
Masami Hiramatsu72041332010-01-05 17:47:10 -0500783 setup_pager();
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400784 init_vmlinux();
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400785
786 memset(&tev, 0, sizeof(tev));
787 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -0500788
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400789 fd = open_kprobe_events(false);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400790 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500791 close(fd);
792
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500793 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400794 parse_kprobe_trace_command(ent->s, &tev);
795 convert_to_perf_probe_event(&tev, &pev);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500796 /* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400797 show_perf_probe_event(&pev);
798 clear_perf_probe_event(&pev);
799 clear_kprobe_trace_event(&tev);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500800 }
801
802 strlist__delete(rawlist);
803}
804
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500805/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400806static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500807{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500808 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500809 struct strlist *sl, *rawlist;
810 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400811 struct kprobe_trace_event tev;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500812
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400813 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500814
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400815 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500816 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500817 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400818 parse_kprobe_trace_command(ent->s, &tev);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500819 if (include_group) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400820 if (e_snprintf(buf, 128, "%s:%s", tev.group,
821 tev.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500822 die("Failed to copy group:event name.");
823 strlist__add(sl, buf);
824 } else
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400825 strlist__add(sl, tev.event);
826 clear_kprobe_trace_event(&tev);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500827 }
828
829 strlist__delete(rawlist);
830
831 return sl;
832}
833
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400834static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500835{
836 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500838
Masami Hiramatsufa282442009-12-08 17:03:23 -0500839 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400840 if (!probe_event_dry_run) {
841 ret = write(fd, buf, strlen(buf));
842 if (ret <= 0)
843 die("Failed to write event: %s", strerror(errno));
844 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400845 free(buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500846}
847
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500848static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500849 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500850{
851 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500852
853 /* Try no suffix */
854 ret = e_snprintf(buf, len, "%s", base);
855 if (ret < 0)
856 die("snprintf() failed: %s", strerror(-ret));
857 if (!strlist__has_entry(namelist, buf))
858 return;
859
Masami Hiramatsud761b082009-12-15 10:32:25 -0500860 if (!allow_suffix) {
861 pr_warning("Error: event \"%s\" already exists. "
862 "(Use -f to force duplicates.)\n", base);
863 die("Can't add new event.");
864 }
865
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500866 /* Try to add suffix */
867 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500868 ret = e_snprintf(buf, len, "%s_%d", base, i);
869 if (ret < 0)
870 die("snprintf() failed: %s", strerror(-ret));
871 if (!strlist__has_entry(namelist, buf))
872 break;
873 }
874 if (i == MAX_EVENT_INDEX)
875 die("Too many events are on the same function.");
876}
877
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400878static void __add_kprobe_trace_events(struct perf_probe_event *pev,
879 struct kprobe_trace_event *tevs,
880 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500881{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400882 int i, fd;
883 struct kprobe_trace_event *tev;
884 char buf[64];
885 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500886 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500887
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400888 fd = open_kprobe_events(true);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500889 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400890 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500891
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400892 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
893 for (i = 0; i < ntevs; i++) {
894 tev = &tevs[i];
895 if (pev->event)
896 event = pev->event;
897 else
898 if (pev->point.function)
899 event = pev->point.function;
900 else
901 event = tev->point.symbol;
902 if (pev->group)
903 group = pev->group;
904 else
905 group = PERFPROBE_GROUP;
906
907 /* Get an unused new event name */
908 get_new_event_name(buf, 64, event, namelist, allow_suffix);
909 event = buf;
910
911 tev->event = xstrdup(event);
912 tev->group = xstrdup(group);
913 write_kprobe_trace_event(fd, tev);
914 /* Add added event name to namelist */
915 strlist__add(namelist, event);
916
917 /* Trick here - save current event/group */
918 event = pev->event;
919 group = pev->group;
920 pev->event = tev->event;
921 pev->group = tev->group;
922 show_perf_probe_event(pev);
923 /* Trick here - restore current event/group */
924 pev->event = (char *)event;
925 pev->group = (char *)group;
926
927 /*
928 * Probes after the first probe which comes from same
929 * user input are always allowed to add suffix, because
930 * there might be several addresses corresponding to
931 * one code line.
932 */
933 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500934 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500935 /* Show how to use the event. */
936 printf("\nYou can now use it on all perf tools, such as:\n\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400937 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500938
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500939 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500940 close(fd);
941}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500942
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400943static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
944 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400945{
946 struct symbol *sym;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400947 bool need_dwarf;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400948#ifndef NO_DWARF_SUPPORT
949 int fd;
950#endif
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400951 int ntevs = 0, i;
952 struct kprobe_trace_event *tev;
953
954 need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400955
956 if (need_dwarf)
957#ifdef NO_DWARF_SUPPORT
958 die("Debuginfo-analysis is not supported");
959#else /* !NO_DWARF_SUPPORT */
960 pr_debug("Some probes require debuginfo.\n");
961
962 fd = open_vmlinux();
963 if (fd < 0) {
964 if (need_dwarf)
965 die("Could not open debuginfo file.");
966
967 pr_debug("Could not open vmlinux/module file."
968 " Try to use symbols.\n");
969 goto end_dwarf;
970 }
971
972 /* Searching probe points */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400973 ntevs = find_kprobe_trace_events(fd, pev, tevs);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400974
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400975 if (ntevs > 0) /* Found */
976 goto found;
977
978 if (ntevs == 0) /* No error but failed to find probe point. */
979 die("Probe point '%s' not found. - probe not added.",
980 synthesize_perf_probe_point(&pev->point));
981
982 /* Error path */
983 if (need_dwarf) {
984 if (ntevs == -ENOENT)
985 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
986 die("Could not analyze debuginfo.");
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400987 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400988 pr_debug("An error occurred in debuginfo analysis."
989 " Try to use symbols.\n");
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400990
991end_dwarf:
992#endif /* !NO_DWARF_SUPPORT */
993
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400994 /* Allocate trace event buffer */
995 ntevs = 1;
996 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400997
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400998 /* Copy parameters */
999 tev->point.symbol = xstrdup(pev->point.function);
1000 tev->point.offset = pev->point.offset;
1001 tev->nargs = pev->nargs;
1002 if (tev->nargs) {
1003 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
1004 * tev->nargs);
1005 for (i = 0; i < tev->nargs; i++)
1006 tev->args[i].value = xstrdup(pev->args[i].name);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001007 }
1008
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001009 /* Currently just checking function name from symbol map */
1010 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1011 tev->point.symbol, NULL);
1012 if (!sym)
1013 die("Kernel symbol \'%s\' not found - probe not added.",
1014 tev->point.symbol);
1015found:
1016 close(fd);
1017 return ntevs;
1018}
1019
1020struct __event_package {
1021 struct perf_probe_event *pev;
1022 struct kprobe_trace_event *tevs;
1023 int ntevs;
1024};
1025
1026void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1027 bool force_add)
1028{
1029 int i;
1030 struct __event_package *pkgs;
1031
1032 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1033
1034 /* Init vmlinux path */
1035 init_vmlinux();
1036
1037 /* Loop 1: convert all events */
1038 for (i = 0; i < npevs; i++) {
1039 pkgs[i].pev = &pevs[i];
1040 /* Convert with or without debuginfo */
1041 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
1042 &pkgs[i].tevs);
1043 }
1044
1045 /* Loop 2: add all events */
1046 for (i = 0; i < npevs; i++)
1047 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1048 pkgs[i].ntevs, force_add);
1049 /* TODO: cleanup all trace events? */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001050}
1051
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001052static void __del_trace_kprobe_event(int fd, struct str_node *ent)
1053{
1054 char *p;
1055 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001056 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001057
1058 /* Convert from perf-probe event to trace-kprobe event */
1059 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
1060 die("Failed to copy event.");
1061 p = strchr(buf + 2, ':');
1062 if (!p)
1063 die("Internal error: %s should have ':' but not.", ent->s);
1064 *p = '/';
1065
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001066 pr_debug("Writing event: %s\n", buf);
1067 ret = write(fd, buf, strlen(buf));
1068 if (ret <= 0)
1069 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001070 printf("Remove event: %s\n", ent->s);
1071}
1072
Masami Hiramatsufa282442009-12-08 17:03:23 -05001073static void del_trace_kprobe_event(int fd, const char *group,
1074 const char *event, struct strlist *namelist)
1075{
1076 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001077 struct str_node *ent, *n;
1078 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001079
1080 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
1081 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -05001082
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001083 if (strpbrk(buf, "*?")) { /* Glob-exp */
1084 strlist__for_each_safe(ent, n, namelist)
1085 if (strglobmatch(ent->s, buf)) {
1086 found++;
1087 __del_trace_kprobe_event(fd, ent);
1088 strlist__remove(namelist, ent);
1089 }
1090 } else {
1091 ent = strlist__find(namelist, buf);
1092 if (ent) {
1093 found++;
1094 __del_trace_kprobe_event(fd, ent);
1095 strlist__remove(namelist, ent);
1096 }
1097 }
1098 if (found == 0)
1099 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001100}
1101
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001102void del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001103{
1104 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001105 const char *group, *event;
1106 char *p, *str;
1107 struct str_node *ent;
1108 struct strlist *namelist;
1109
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001110 fd = open_kprobe_events(true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001111 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001112 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001113
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001114 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001115 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001116 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001117 p = strchr(str, ':');
1118 if (p) {
1119 group = str;
1120 *p = '\0';
1121 event = p + 1;
1122 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001123 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001124 event = str;
1125 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001126 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001127 del_trace_kprobe_event(fd, group, event, namelist);
1128 free(str);
1129 }
1130 strlist__delete(namelist);
1131 close(fd);
1132}
1133
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001134#define LINEBUF_SIZE 256
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -05001135#define NR_ADDITIONAL_LINES 2
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001136
1137static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
1138{
1139 char buf[LINEBUF_SIZE];
1140 const char *color = PERF_COLOR_BLUE;
1141
1142 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1143 goto error;
1144 if (!skip) {
1145 if (show_num)
1146 fprintf(stdout, "%7u %s", l, buf);
1147 else
1148 color_fprintf(stdout, color, " %s", buf);
1149 }
1150
1151 while (strlen(buf) == LINEBUF_SIZE - 1 &&
1152 buf[LINEBUF_SIZE - 2] != '\n') {
1153 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1154 goto error;
1155 if (!skip) {
1156 if (show_num)
1157 fprintf(stdout, "%s", buf);
1158 else
1159 color_fprintf(stdout, color, "%s", buf);
1160 }
1161 }
1162 return;
1163error:
1164 if (feof(fp))
1165 die("Source file is shorter than expected.");
1166 else
1167 die("File read error: %s", strerror(errno));
1168}
1169
1170void show_line_range(struct line_range *lr)
1171{
1172 unsigned int l = 1;
1173 struct line_node *ln;
1174 FILE *fp;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001175 int fd, ret;
1176
1177 /* Search a line range */
1178 init_vmlinux();
1179 fd = open_vmlinux();
1180 if (fd < 0)
1181 die("Could not open debuginfo file.");
1182 ret = find_line_range(fd, lr);
1183 if (ret <= 0)
1184 die("Source line is not found.\n");
1185 close(fd);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001186
1187 setup_pager();
1188
1189 if (lr->function)
1190 fprintf(stdout, "<%s:%d>\n", lr->function,
1191 lr->start - lr->offset);
1192 else
1193 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
1194
1195 fp = fopen(lr->path, "r");
1196 if (fp == NULL)
1197 die("Failed to open %s: %s", lr->path, strerror(errno));
1198 /* Skip to starting line number */
1199 while (l < lr->start)
1200 show_one_line(fp, l++, true, false);
1201
1202 list_for_each_entry(ln, &lr->line_list, list) {
1203 while (ln->line > l)
1204 show_one_line(fp, (l++) - lr->offset, false, false);
1205 show_one_line(fp, (l++) - lr->offset, false, true);
1206 }
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -05001207
1208 if (lr->end == INT_MAX)
1209 lr->end = l + NR_ADDITIONAL_LINES;
1210 while (l < lr->end && !feof(fp))
1211 show_one_line(fp, (l++) - lr->offset, false, false);
1212
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001213 fclose(fp);
1214}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001215
1216