blob: 4e3c1aea789282e405405dce14c0e2947dc2151a [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 Hiramatsu4235b042010-03-16 18:06:12 -0400265/* Parse perf-probe event command */
266void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500267{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500268 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500269 int argc, i;
270
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400271 argv = argv_split(cmd, &argc);
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500272 if (!argv)
273 die("argv_split failed.");
274 if (argc > MAX_PROBE_ARGS + 1)
275 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500276
277 /* Parse probe point */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400278 parse_perf_probe_point(argv[0], pev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500279
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500280 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400281 pev->nargs = argc - 1;
282 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
283 for (i = 0; i < pev->nargs; i++) {
284 pev->args[i].name = xstrdup(argv[i + 1]);
285 if (is_c_varname(pev->args[i].name) && pev->point.retprobe)
286 semantic_error("You can't specify local variable for"
287 " kretprobe");
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500288 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500289
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500290 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500291}
292
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400293/* Return true if this perf_probe_event requires debuginfo */
294bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500295{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400296 int i;
297
298 if (pev->point.file || pev->point.line || pev->point.lazy_line)
299 return true;
300
301 for (i = 0; i < pev->nargs; i++)
302 if (is_c_varname(pev->args[i].name))
303 return true;
304
305 return false;
306}
307
308/* Parse kprobe_events event into struct probe_point */
309void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
310{
311 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500312 char pr;
313 char *p;
314 int ret, i, argc;
315 char **argv;
316
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400317 pr_debug("Parsing kprobe_events: %s\n", cmd);
318 argv = argv_split(cmd, &argc);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500319 if (!argv)
320 die("argv_split failed.");
321 if (argc < 2)
322 semantic_error("Too less arguments.");
323
324 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800325 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400326 &pr, (float *)(void *)&tev->group,
327 (float *)(void *)&tev->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500328 if (ret != 3)
329 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400330 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500331
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400332 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500333
334 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400335 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
336 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500337 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400338 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500339
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400340 tev->nargs = argc - 2;
341 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
342 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500343 p = strchr(argv[i + 2], '=');
344 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400345 *p++ = '\0';
346 else
347 p = argv[i + 2];
348 tev->args[i].name = xstrdup(argv[i + 2]);
349 /* TODO: parse regs and offset */
350 tev->args[i].value = xstrdup(p);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500351 }
352
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500353 argv_free(argv);
354}
355
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400356/* Compose only probe point (not argument) */
357static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500358{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400359 char *buf, *tmp;
360 char offs[32] = "", line[32] = "", file[32] = "";
361 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500362
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400363 buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500364 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400365 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500366 if (ret <= 0)
367 goto error;
368 }
369 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400370 ret = e_snprintf(line, 32, ":%d", pp->line);
371 if (ret <= 0)
372 goto error;
373 }
374 if (pp->file) {
375 len = strlen(pp->file) - 32;
376 if (len < 0)
377 len = 0;
378 tmp = strchr(pp->file + len, '/');
379 if (!tmp)
380 tmp = pp->file + len - 1;
381 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500382 if (ret <= 0)
383 goto error;
384 }
385
386 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400387 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
388 offs, pp->retprobe ? "%return" : "", line,
389 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500390 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400391 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500392 if (ret <= 0)
393 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500394
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400395 return buf;
396error:
397 die("Failed to synthesize perf probe point: %s", strerror(-ret));
398}
399
400#if 0
401char *synthesize_perf_probe_command(struct perf_probe_event *pev)
402{
403 char *buf;
404 int i, len, ret;
405
406 buf = synthesize_perf_probe_point(&pev->point);
407 if (!buf)
408 return NULL;
409
410 len = strlen(buf);
411 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500412 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400413 pev->args[i].name);
414 if (ret <= 0) {
415 free(buf);
416 return NULL;
417 }
418 len += ret;
419 }
420
421 return buf;
422}
423#endif
424
425static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
426 char **buf, size_t *buflen,
427 int depth)
428{
429 int ret;
430 if (ref->next) {
431 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
432 buflen, depth + 1);
433 if (depth < 0)
434 goto out;
435 }
436
437 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
438 if (ret < 0)
439 depth = ret;
440 else {
441 *buf += ret;
442 *buflen -= ret;
443 }
444out:
445 return depth;
446
447}
448
449static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
450 char *buf, size_t buflen)
451{
452 int ret, depth = 0;
453 char *tmp = buf;
454
455 /* Argument name or separator */
456 if (arg->name)
457 ret = e_snprintf(buf, buflen, " %s=", arg->name);
458 else
459 ret = e_snprintf(buf, buflen, " ");
460 if (ret < 0)
461 return ret;
462 buf += ret;
463 buflen -= ret;
464
465 /* Dereferencing arguments */
466 if (arg->ref) {
467 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
468 &buflen, 1);
469 if (depth < 0)
470 return depth;
471 }
472
473 /* Print argument value */
474 ret = e_snprintf(buf, buflen, "%s", arg->value);
475 if (ret < 0)
476 return ret;
477 buf += ret;
478 buflen -= ret;
479
480 /* Closing */
481 while (depth--) {
482 ret = e_snprintf(buf, buflen, ")");
483 if (ret < 0)
484 return ret;
485 buf += ret;
486 buflen -= ret;
487 }
488
489 return buf - tmp;
490}
491
492char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
493{
494 struct kprobe_trace_point *tp = &tev->point;
495 char *buf;
496 int i, len, ret;
497
498 buf = xzalloc(MAX_CMDLEN);
499 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
500 tp->retprobe ? 'r' : 'p',
501 tev->group, tev->event,
502 tp->symbol, tp->offset);
503 if (len <= 0)
504 goto error;
505
506 for (i = 0; i < tev->nargs; i++) {
507 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
508 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500509 if (ret <= 0)
510 goto error;
511 len += ret;
512 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500513
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400514 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500515error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400516 free(buf);
517 return NULL;
518}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500519
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400520void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
521 struct perf_probe_event *pev)
522{
523 char buf[64];
524 int i;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400525#ifndef NO_DWARF_SUPPORT
526 struct symbol *sym;
527 int fd, ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400528
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400529 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
530 tev->point.symbol, NULL);
531 if (sym) {
532 fd = open_vmlinux();
533 ret = find_perf_probe_point(fd, sym->start + tev->point.offset,
534 &pev->point);
535 close(fd);
536 }
537 if (ret <= 0) {
538 pev->point.function = xstrdup(tev->point.symbol);
539 pev->point.offset = tev->point.offset;
540 }
541#else
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400542 /* Convert trace_point to probe_point */
543 pev->point.function = xstrdup(tev->point.symbol);
544 pev->point.offset = tev->point.offset;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400545#endif
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400546 pev->point.retprobe = tev->point.retprobe;
547
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400548 pev->event = xstrdup(tev->event);
549 pev->group = xstrdup(tev->group);
550
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400551 /* Convert trace_arg to probe_arg */
552 pev->nargs = tev->nargs;
553 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
554 for (i = 0; i < tev->nargs; i++)
555 if (tev->args[i].name)
556 pev->args[i].name = xstrdup(tev->args[i].name);
557 else {
558 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
559 pev->args[i].name = xstrdup(buf);
560 }
561}
562
563void clear_perf_probe_event(struct perf_probe_event *pev)
564{
565 struct perf_probe_point *pp = &pev->point;
566 int i;
567
568 if (pev->event)
569 free(pev->event);
570 if (pev->group)
571 free(pev->group);
572 if (pp->file)
573 free(pp->file);
574 if (pp->function)
575 free(pp->function);
576 if (pp->lazy_line)
577 free(pp->lazy_line);
578 for (i = 0; i < pev->nargs; i++)
579 if (pev->args[i].name)
580 free(pev->args[i].name);
581 if (pev->args)
582 free(pev->args);
583 memset(pev, 0, sizeof(*pev));
584}
585
586void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
587{
588 struct kprobe_trace_arg_ref *ref, *next;
589 int i;
590
591 if (tev->event)
592 free(tev->event);
593 if (tev->group)
594 free(tev->group);
595 if (tev->point.symbol)
596 free(tev->point.symbol);
597 for (i = 0; i < tev->nargs; i++) {
598 if (tev->args[i].name)
599 free(tev->args[i].name);
600 if (tev->args[i].value)
601 free(tev->args[i].value);
602 ref = tev->args[i].ref;
603 while (ref) {
604 next = ref->next;
605 free(ref);
606 ref = next;
607 }
608 }
609 if (tev->args)
610 free(tev->args);
611 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500612}
613
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400614static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500615{
616 char buf[PATH_MAX];
617 int ret;
618
619 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
620 if (ret < 0)
621 die("Failed to make kprobe_events path.");
622
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400623 if (readwrite && !probe_event_dry_run)
624 ret = open(buf, O_RDWR, O_APPEND);
625 else
626 ret = open(buf, O_RDONLY, 0);
627
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500628 if (ret < 0) {
629 if (errno == ENOENT)
630 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800631 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500632 else
633 die("Could not open kprobe_events file: %s",
634 strerror(errno));
635 }
636 return ret;
637}
638
639/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400640static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500641{
642 int ret, idx;
643 FILE *fp;
644 char buf[MAX_CMDLEN];
645 char *p;
646 struct strlist *sl;
647
648 sl = strlist__new(true, NULL);
649
650 fp = fdopen(dup(fd), "r");
651 while (!feof(fp)) {
652 p = fgets(buf, MAX_CMDLEN, fp);
653 if (!p)
654 break;
655
656 idx = strlen(p) - 1;
657 if (p[idx] == '\n')
658 p[idx] = '\0';
659 ret = strlist__add(sl, buf);
660 if (ret < 0)
661 die("strlist__add failed: %s", strerror(-ret));
662 }
663 fclose(fp);
664
665 return sl;
666}
667
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500668/* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400669static void show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500670{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500671 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500672 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400673 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500674
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400675 /* Synthesize only event probe point */
676 place = synthesize_perf_probe_point(&pev->point);
677
678 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500679 if (ret < 0)
680 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400681 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500682
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400683 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500684 printf(" with");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400685 for (i = 0; i < pev->nargs; i++)
686 printf(" %s", pev->args[i].name);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500687 }
688 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400689 free(place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500690}
691
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500692/* List up current perf-probe events */
693void show_perf_probe_events(void)
694{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500695 int fd;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400696 struct kprobe_trace_event tev;
697 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500698 struct strlist *rawlist;
699 struct str_node *ent;
700
Masami Hiramatsu72041332010-01-05 17:47:10 -0500701 setup_pager();
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400702 init_vmlinux();
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400703
704 memset(&tev, 0, sizeof(tev));
705 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -0500706
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400707 fd = open_kprobe_events(false);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400708 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500709 close(fd);
710
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500711 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400712 parse_kprobe_trace_command(ent->s, &tev);
713 convert_to_perf_probe_event(&tev, &pev);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500714 /* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400715 show_perf_probe_event(&pev);
716 clear_perf_probe_event(&pev);
717 clear_kprobe_trace_event(&tev);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500718 }
719
720 strlist__delete(rawlist);
721}
722
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500723/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400724static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500725{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500726 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500727 struct strlist *sl, *rawlist;
728 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729 struct kprobe_trace_event tev;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500730
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400731 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500732
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400733 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500734 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500735 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400736 parse_kprobe_trace_command(ent->s, &tev);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500737 if (include_group) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400738 if (e_snprintf(buf, 128, "%s:%s", tev.group,
739 tev.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500740 die("Failed to copy group:event name.");
741 strlist__add(sl, buf);
742 } else
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400743 strlist__add(sl, tev.event);
744 clear_kprobe_trace_event(&tev);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500745 }
746
747 strlist__delete(rawlist);
748
749 return sl;
750}
751
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400752static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500753{
754 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400755 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500756
Masami Hiramatsufa282442009-12-08 17:03:23 -0500757 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400758 if (!probe_event_dry_run) {
759 ret = write(fd, buf, strlen(buf));
760 if (ret <= 0)
761 die("Failed to write event: %s", strerror(errno));
762 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400763 free(buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500764}
765
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500766static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500767 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500768{
769 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500770
771 /* Try no suffix */
772 ret = e_snprintf(buf, len, "%s", base);
773 if (ret < 0)
774 die("snprintf() failed: %s", strerror(-ret));
775 if (!strlist__has_entry(namelist, buf))
776 return;
777
Masami Hiramatsud761b082009-12-15 10:32:25 -0500778 if (!allow_suffix) {
779 pr_warning("Error: event \"%s\" already exists. "
780 "(Use -f to force duplicates.)\n", base);
781 die("Can't add new event.");
782 }
783
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500784 /* Try to add suffix */
785 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500786 ret = e_snprintf(buf, len, "%s_%d", base, i);
787 if (ret < 0)
788 die("snprintf() failed: %s", strerror(-ret));
789 if (!strlist__has_entry(namelist, buf))
790 break;
791 }
792 if (i == MAX_EVENT_INDEX)
793 die("Too many events are on the same function.");
794}
795
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400796static void __add_kprobe_trace_events(struct perf_probe_event *pev,
797 struct kprobe_trace_event *tevs,
798 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500799{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400800 int i, fd;
801 struct kprobe_trace_event *tev;
802 char buf[64];
803 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500804 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500805
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400806 fd = open_kprobe_events(true);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500807 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400808 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500809
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400810 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
811 for (i = 0; i < ntevs; i++) {
812 tev = &tevs[i];
813 if (pev->event)
814 event = pev->event;
815 else
816 if (pev->point.function)
817 event = pev->point.function;
818 else
819 event = tev->point.symbol;
820 if (pev->group)
821 group = pev->group;
822 else
823 group = PERFPROBE_GROUP;
824
825 /* Get an unused new event name */
826 get_new_event_name(buf, 64, event, namelist, allow_suffix);
827 event = buf;
828
829 tev->event = xstrdup(event);
830 tev->group = xstrdup(group);
831 write_kprobe_trace_event(fd, tev);
832 /* Add added event name to namelist */
833 strlist__add(namelist, event);
834
835 /* Trick here - save current event/group */
836 event = pev->event;
837 group = pev->group;
838 pev->event = tev->event;
839 pev->group = tev->group;
840 show_perf_probe_event(pev);
841 /* Trick here - restore current event/group */
842 pev->event = (char *)event;
843 pev->group = (char *)group;
844
845 /*
846 * Probes after the first probe which comes from same
847 * user input are always allowed to add suffix, because
848 * there might be several addresses corresponding to
849 * one code line.
850 */
851 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500852 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500853 /* Show how to use the event. */
854 printf("\nYou can now use it on all perf tools, such as:\n\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400855 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500856
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500857 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500858 close(fd);
859}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500860
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400861static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
862 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400863{
864 struct symbol *sym;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400865 bool need_dwarf;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400866#ifndef NO_DWARF_SUPPORT
867 int fd;
868#endif
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400869 int ntevs = 0, i;
870 struct kprobe_trace_event *tev;
871
872 need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400873
874 if (need_dwarf)
875#ifdef NO_DWARF_SUPPORT
876 die("Debuginfo-analysis is not supported");
877#else /* !NO_DWARF_SUPPORT */
878 pr_debug("Some probes require debuginfo.\n");
879
880 fd = open_vmlinux();
881 if (fd < 0) {
882 if (need_dwarf)
883 die("Could not open debuginfo file.");
884
885 pr_debug("Could not open vmlinux/module file."
886 " Try to use symbols.\n");
887 goto end_dwarf;
888 }
889
890 /* Searching probe points */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400891 ntevs = find_kprobe_trace_events(fd, pev, tevs);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400892
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400893 if (ntevs > 0) /* Found */
894 goto found;
895
896 if (ntevs == 0) /* No error but failed to find probe point. */
897 die("Probe point '%s' not found. - probe not added.",
898 synthesize_perf_probe_point(&pev->point));
899
900 /* Error path */
901 if (need_dwarf) {
902 if (ntevs == -ENOENT)
903 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
904 die("Could not analyze debuginfo.");
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400905 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400906 pr_debug("An error occurred in debuginfo analysis."
907 " Try to use symbols.\n");
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400908
909end_dwarf:
910#endif /* !NO_DWARF_SUPPORT */
911
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400912 /* Allocate trace event buffer */
913 ntevs = 1;
914 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400915
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400916 /* Copy parameters */
917 tev->point.symbol = xstrdup(pev->point.function);
918 tev->point.offset = pev->point.offset;
919 tev->nargs = pev->nargs;
920 if (tev->nargs) {
921 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
922 * tev->nargs);
923 for (i = 0; i < tev->nargs; i++)
924 tev->args[i].value = xstrdup(pev->args[i].name);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400925 }
926
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400927 /* Currently just checking function name from symbol map */
928 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
929 tev->point.symbol, NULL);
930 if (!sym)
931 die("Kernel symbol \'%s\' not found - probe not added.",
932 tev->point.symbol);
933found:
934 close(fd);
935 return ntevs;
936}
937
938struct __event_package {
939 struct perf_probe_event *pev;
940 struct kprobe_trace_event *tevs;
941 int ntevs;
942};
943
944void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
945 bool force_add)
946{
947 int i;
948 struct __event_package *pkgs;
949
950 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
951
952 /* Init vmlinux path */
953 init_vmlinux();
954
955 /* Loop 1: convert all events */
956 for (i = 0; i < npevs; i++) {
957 pkgs[i].pev = &pevs[i];
958 /* Convert with or without debuginfo */
959 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
960 &pkgs[i].tevs);
961 }
962
963 /* Loop 2: add all events */
964 for (i = 0; i < npevs; i++)
965 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
966 pkgs[i].ntevs, force_add);
967 /* TODO: cleanup all trace events? */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400968}
969
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500970static void __del_trace_kprobe_event(int fd, struct str_node *ent)
971{
972 char *p;
973 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500975
976 /* Convert from perf-probe event to trace-kprobe event */
977 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
978 die("Failed to copy event.");
979 p = strchr(buf + 2, ':');
980 if (!p)
981 die("Internal error: %s should have ':' but not.", ent->s);
982 *p = '/';
983
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400984 pr_debug("Writing event: %s\n", buf);
985 ret = write(fd, buf, strlen(buf));
986 if (ret <= 0)
987 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500988 printf("Remove event: %s\n", ent->s);
989}
990
Masami Hiramatsufa282442009-12-08 17:03:23 -0500991static void del_trace_kprobe_event(int fd, const char *group,
992 const char *event, struct strlist *namelist)
993{
994 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500995 struct str_node *ent, *n;
996 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500997
998 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
999 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -05001000
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001001 if (strpbrk(buf, "*?")) { /* Glob-exp */
1002 strlist__for_each_safe(ent, n, namelist)
1003 if (strglobmatch(ent->s, buf)) {
1004 found++;
1005 __del_trace_kprobe_event(fd, ent);
1006 strlist__remove(namelist, ent);
1007 }
1008 } else {
1009 ent = strlist__find(namelist, buf);
1010 if (ent) {
1011 found++;
1012 __del_trace_kprobe_event(fd, ent);
1013 strlist__remove(namelist, ent);
1014 }
1015 }
1016 if (found == 0)
1017 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001018}
1019
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001020void del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001021{
1022 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001023 const char *group, *event;
1024 char *p, *str;
1025 struct str_node *ent;
1026 struct strlist *namelist;
1027
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001028 fd = open_kprobe_events(true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001029 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001030 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001031
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001032 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001033 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001034 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001035 p = strchr(str, ':');
1036 if (p) {
1037 group = str;
1038 *p = '\0';
1039 event = p + 1;
1040 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001041 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001042 event = str;
1043 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001044 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001045 del_trace_kprobe_event(fd, group, event, namelist);
1046 free(str);
1047 }
1048 strlist__delete(namelist);
1049 close(fd);
1050}
1051
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001052#define LINEBUF_SIZE 256
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -05001053#define NR_ADDITIONAL_LINES 2
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001054
1055static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
1056{
1057 char buf[LINEBUF_SIZE];
1058 const char *color = PERF_COLOR_BLUE;
1059
1060 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1061 goto error;
1062 if (!skip) {
1063 if (show_num)
1064 fprintf(stdout, "%7u %s", l, buf);
1065 else
1066 color_fprintf(stdout, color, " %s", buf);
1067 }
1068
1069 while (strlen(buf) == LINEBUF_SIZE - 1 &&
1070 buf[LINEBUF_SIZE - 2] != '\n') {
1071 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1072 goto error;
1073 if (!skip) {
1074 if (show_num)
1075 fprintf(stdout, "%s", buf);
1076 else
1077 color_fprintf(stdout, color, "%s", buf);
1078 }
1079 }
1080 return;
1081error:
1082 if (feof(fp))
1083 die("Source file is shorter than expected.");
1084 else
1085 die("File read error: %s", strerror(errno));
1086}
1087
1088void show_line_range(struct line_range *lr)
1089{
1090 unsigned int l = 1;
1091 struct line_node *ln;
1092 FILE *fp;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001093 int fd, ret;
1094
1095 /* Search a line range */
1096 init_vmlinux();
1097 fd = open_vmlinux();
1098 if (fd < 0)
1099 die("Could not open debuginfo file.");
1100 ret = find_line_range(fd, lr);
1101 if (ret <= 0)
1102 die("Source line is not found.\n");
1103 close(fd);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001104
1105 setup_pager();
1106
1107 if (lr->function)
1108 fprintf(stdout, "<%s:%d>\n", lr->function,
1109 lr->start - lr->offset);
1110 else
1111 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
1112
1113 fp = fopen(lr->path, "r");
1114 if (fp == NULL)
1115 die("Failed to open %s: %s", lr->path, strerror(errno));
1116 /* Skip to starting line number */
1117 while (l < lr->start)
1118 show_one_line(fp, l++, true, false);
1119
1120 list_for_each_entry(ln, &lr->line_list, list) {
1121 while (ln->line > l)
1122 show_one_line(fp, (l++) - lr->offset, false, false);
1123 show_one_line(fp, (l++) - lr->offset, false, true);
1124 }
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -05001125
1126 if (lr->end == INT_MAX)
1127 lr->end = l + NR_ADDITIONAL_LINES;
1128 while (l < lr->end && !feof(fp))
1129 show_one_line(fp, (l++) - lr->offset, false, false);
1130
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001131 fclose(fp);
1132}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001133
1134