blob: 88a3b6d75c7c8aece211cb2e9651c5ba682f56de [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 Hiramatsu50656ee2009-11-30 19:19:58 -050043#include "parse-events.h" /* For debugfs_path */
44#include "probe-event.h"
45
46#define MAX_CMDLEN 256
47#define MAX_PROBE_ARGS 128
48#define PERFPROBE_GROUP "probe"
49
50#define semantic_error(msg ...) die("Semantic error :" msg)
51
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050052/* If there is no space to write, returns -E2BIG. */
53static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050054 __attribute__((format(printf, 3, 4)));
55
56static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057{
58 int ret;
59 va_list ap;
60 va_start(ap, format);
61 ret = vsnprintf(str, size, format, ap);
62 va_end(ap);
63 if (ret >= (int)size)
64 ret = -E2BIG;
65 return ret;
66}
67
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050068void parse_line_range_desc(const char *arg, struct line_range *lr)
69{
70 const char *ptr;
71 char *tmp;
72 /*
73 * <Syntax>
74 * SRC:SLN[+NUM|-ELN]
75 * FUNC[:SLN[+NUM|-ELN]]
76 */
77 ptr = strchr(arg, ':');
78 if (ptr) {
79 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
80 if (*tmp == '+')
81 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
82 &tmp, 0);
83 else if (*tmp == '-')
84 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
85 else
86 lr->end = 0;
87 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
88 if (lr->end && lr->start > lr->end)
89 semantic_error("Start line must be smaller"
90 " than end line.");
91 if (*tmp != '\0')
92 semantic_error("Tailing with invalid character '%d'.",
93 *tmp);
Masami Hiramatsu31facc52010-03-16 18:05:30 -040094 tmp = xstrndup(arg, (ptr - arg));
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050095 } else
Masami Hiramatsu31facc52010-03-16 18:05:30 -040096 tmp = xstrdup(arg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050097
98 if (strchr(tmp, '.'))
99 lr->file = tmp;
100 else
101 lr->function = tmp;
102}
103
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500104/* Check the name is good for event/group */
105static bool check_event_name(const char *name)
106{
107 if (!isalpha(*name) && *name != '_')
108 return false;
109 while (*++name != '\0') {
110 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
111 return false;
112 }
113 return true;
114}
115
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500116/* Parse probepoint definition. */
117static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
118{
119 char *ptr, *tmp;
120 char c, nc = 0;
121 /*
122 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500123 * perf probe [EVENT=]SRC[:LN|;PTN]
124 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500125 *
126 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500127 */
128
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500129 ptr = strpbrk(arg, ";=@+%");
130 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500131 *ptr = '\0';
132 tmp = ptr + 1;
133 ptr = strchr(arg, ':');
134 if (ptr) /* Group name is not supported yet. */
135 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500136 if (!check_event_name(arg))
137 semantic_error("%s is bad for event name -it must "
138 "follow C symbol-naming rule.", arg);
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400139 pp->event = xstrdup(arg);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500140 arg = tmp;
141 }
142
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500143 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500144 if (ptr) {
145 nc = *ptr;
146 *ptr++ = '\0';
147 }
148
149 /* Check arg is function or file and copy it */
150 if (strchr(arg, '.')) /* File */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400151 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500152 else /* Function */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400153 pp->function = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500154
155 /* Parse other options */
156 while (ptr) {
157 arg = ptr;
158 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500159 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400160 pp->lazy_line = xstrdup(arg);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500161 break;
162 }
163 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500164 if (ptr) {
165 nc = *ptr;
166 *ptr++ = '\0';
167 }
168 switch (c) {
169 case ':': /* Line number */
170 pp->line = strtoul(arg, &tmp, 0);
171 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500172 semantic_error("There is non-digit char"
173 " in line number.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500174 break;
175 case '+': /* Byte offset from a symbol */
176 pp->offset = strtoul(arg, &tmp, 0);
177 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500178 semantic_error("There is non-digit character"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500179 " in offset.");
180 break;
181 case '@': /* File name */
182 if (pp->file)
183 semantic_error("SRC@SRC is not allowed.");
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400184 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500185 break;
186 case '%': /* Probe places */
187 if (strcmp(arg, "return") == 0) {
188 pp->retprobe = 1;
189 } else /* Others not supported yet */
190 semantic_error("%%%s is not supported.", arg);
191 break;
192 default:
193 DIE_IF("Program has a bug.");
194 break;
195 }
196 }
197
198 /* Exclusion check */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500199 if (pp->lazy_line && pp->line)
200 semantic_error("Lazy pattern can't be used with line number.");
201
202 if (pp->lazy_line && pp->offset)
203 semantic_error("Lazy pattern can't be used with offset.");
204
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500205 if (pp->line && pp->offset)
206 semantic_error("Offset can't be used with line number.");
207
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500208 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
209 semantic_error("File always requires line number or "
210 "lazy pattern.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500211
212 if (pp->offset && !pp->function)
213 semantic_error("Offset requires an entry function.");
214
215 if (pp->retprobe && !pp->function)
216 semantic_error("Return probe requires an entry function.");
217
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500218 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
219 semantic_error("Offset/Line/Lazy pattern can't be used with "
220 "return probe.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500221
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500222 pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n",
223 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
224 pp->lazy_line);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500225}
226
227/* Parse perf-probe event definition */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500228void parse_perf_probe_event(const char *str, struct probe_point *pp,
229 bool *need_dwarf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500230{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500231 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500232 int argc, i;
233
234 *need_dwarf = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500235
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500236 argv = argv_split(str, &argc);
237 if (!argv)
238 die("argv_split failed.");
239 if (argc > MAX_PROBE_ARGS + 1)
240 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500241
242 /* Parse probe point */
243 parse_perf_probe_probepoint(argv[0], pp);
Masami Hiramatsufc6ceea2010-03-12 18:22:24 -0500244 if (pp->file || pp->line || pp->lazy_line)
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500245 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500246
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500247 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500248 pp->nr_args = argc - 1;
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400249 pp->args = xzalloc(sizeof(char *) * pp->nr_args);
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500250 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400251 pp->args[i] = xstrdup(argv[i + 1]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500252 if (is_c_varname(pp->args[i])) {
253 if (pp->retprobe)
254 semantic_error("You can't specify local"
255 " variable for kretprobe");
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500256 *need_dwarf = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500257 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500258 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500259
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500260 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500261}
262
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500263/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500264void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500265{
266 char pr;
267 char *p;
268 int ret, i, argc;
269 char **argv;
270
271 pr_debug("Parsing kprobe_events: %s\n", str);
272 argv = argv_split(str, &argc);
273 if (!argv)
274 die("argv_split failed.");
275 if (argc < 2)
276 semantic_error("Too less arguments.");
277
278 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800279 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500280 &pr, (float *)(void *)&pp->group,
281 (float *)(void *)&pp->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500282 if (ret != 3)
283 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500284 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500285
286 pp->retprobe = (pr == 'r');
287
288 /* Scan function name and offset */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500289 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
290 &pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500291 if (ret == 1)
292 pp->offset = 0;
293
294 /* kprobe_events doesn't have this information */
295 pp->line = 0;
296 pp->file = NULL;
297
298 pp->nr_args = argc - 2;
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400299 pp->args = xzalloc(sizeof(char *) * pp->nr_args);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500300 for (i = 0; i < pp->nr_args; i++) {
301 p = strchr(argv[i + 2], '=');
302 if (p) /* We don't need which register is assigned. */
303 *p = '\0';
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400304 pp->args[i] = xstrdup(argv[i + 2]);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500305 }
306
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500307 argv_free(argv);
308}
309
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500310/* Synthesize only probe point (not argument) */
311int synthesize_perf_probe_point(struct probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500312{
313 char *buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500314 char offs[64] = "", line[64] = "";
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500315 int ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500316
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400317 pp->probes[0] = buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu388c3aa2010-02-18 13:16:52 -0500318 pp->found = 1;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500319 if (pp->offset) {
320 ret = e_snprintf(offs, 64, "+%d", pp->offset);
321 if (ret <= 0)
322 goto error;
323 }
324 if (pp->line) {
325 ret = e_snprintf(line, 64, ":%d", pp->line);
326 if (ret <= 0)
327 goto error;
328 }
329
330 if (pp->function)
331 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
332 offs, pp->retprobe ? "%return" : "", line);
333 else
Masami Hiramatsu84988452009-12-07 12:00:53 -0500334 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500335 if (ret <= 0) {
336error:
337 free(pp->probes[0]);
338 pp->probes[0] = NULL;
Masami Hiramatsu388c3aa2010-02-18 13:16:52 -0500339 pp->found = 0;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500340 }
341 return ret;
342}
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500343
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500344int synthesize_perf_probe_event(struct probe_point *pp)
345{
346 char *buf;
347 int i, len, ret;
348
349 len = synthesize_perf_probe_point(pp);
350 if (len < 0)
351 return 0;
352
353 buf = pp->probes[0];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500354 for (i = 0; i < pp->nr_args; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500355 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
356 pp->args[i]);
357 if (ret <= 0)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500358 goto error;
359 len += ret;
360 }
361 pp->found = 1;
362
363 return pp->found;
364error:
365 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500366 pp->probes[0] = NULL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500367
368 return ret;
369}
370
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500371int synthesize_trace_kprobe_event(struct probe_point *pp)
372{
373 char *buf;
374 int i, len, ret;
375
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400376 pp->probes[0] = buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500377 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
378 if (ret <= 0)
379 goto error;
380 len = ret;
381
382 for (i = 0; i < pp->nr_args; i++) {
383 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
384 pp->args[i]);
385 if (ret <= 0)
386 goto error;
387 len += ret;
388 }
389 pp->found = 1;
390
391 return pp->found;
392error:
393 free(pp->probes[0]);
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500394 pp->probes[0] = NULL;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500395
396 return ret;
397}
398
399static int open_kprobe_events(int flags, int mode)
400{
401 char buf[PATH_MAX];
402 int ret;
403
404 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
405 if (ret < 0)
406 die("Failed to make kprobe_events path.");
407
408 ret = open(buf, flags, mode);
409 if (ret < 0) {
410 if (errno == ENOENT)
411 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800412 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500413 else
414 die("Could not open kprobe_events file: %s",
415 strerror(errno));
416 }
417 return ret;
418}
419
420/* Get raw string list of current kprobe_events */
421static struct strlist *get_trace_kprobe_event_rawlist(int fd)
422{
423 int ret, idx;
424 FILE *fp;
425 char buf[MAX_CMDLEN];
426 char *p;
427 struct strlist *sl;
428
429 sl = strlist__new(true, NULL);
430
431 fp = fdopen(dup(fd), "r");
432 while (!feof(fp)) {
433 p = fgets(buf, MAX_CMDLEN, fp);
434 if (!p)
435 break;
436
437 idx = strlen(p) - 1;
438 if (p[idx] == '\n')
439 p[idx] = '\0';
440 ret = strlist__add(sl, buf);
441 if (ret < 0)
442 die("strlist__add failed: %s", strerror(-ret));
443 }
444 fclose(fp);
445
446 return sl;
447}
448
449/* Free and zero clear probe_point */
450static void clear_probe_point(struct probe_point *pp)
451{
452 int i;
453
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500454 if (pp->event)
455 free(pp->event);
456 if (pp->group)
457 free(pp->group);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500458 if (pp->function)
459 free(pp->function);
460 if (pp->file)
461 free(pp->file);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500462 if (pp->lazy_line)
463 free(pp->lazy_line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500464 for (i = 0; i < pp->nr_args; i++)
465 free(pp->args[i]);
466 if (pp->args)
467 free(pp->args);
468 for (i = 0; i < pp->found; i++)
469 free(pp->probes[i]);
Julia Lawall5660ce32009-12-09 20:26:18 +0100470 memset(pp, 0, sizeof(*pp));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500471}
472
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500473/* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500474static void show_perf_probe_event(const char *event, const char *place,
475 struct probe_point *pp)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500476{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500477 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500478 char buf[128];
479
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500480 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500481 if (ret < 0)
482 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500483 printf(" %-40s (on %s", buf, place);
484
485 if (pp->nr_args > 0) {
486 printf(" with");
487 for (i = 0; i < pp->nr_args; i++)
488 printf(" %s", pp->args[i]);
489 }
490 printf(")\n");
491}
492
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500493/* List up current perf-probe events */
494void show_perf_probe_events(void)
495{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500496 int fd;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500497 struct probe_point pp;
498 struct strlist *rawlist;
499 struct str_node *ent;
500
Masami Hiramatsu72041332010-01-05 17:47:10 -0500501 setup_pager();
Masami Hiramatsu388c3aa2010-02-18 13:16:52 -0500502 memset(&pp, 0, sizeof(pp));
Masami Hiramatsu72041332010-01-05 17:47:10 -0500503
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500504 fd = open_kprobe_events(O_RDONLY, 0);
505 rawlist = get_trace_kprobe_event_rawlist(fd);
506 close(fd);
507
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500508 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500509 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500510 /* Synthesize only event probe point */
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500511 synthesize_perf_probe_point(&pp);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500512 /* Show an event */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500513 show_perf_probe_event(pp.event, pp.probes[0], &pp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500514 clear_probe_point(&pp);
515 }
516
517 strlist__delete(rawlist);
518}
519
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500520/* Get current perf-probe event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500521static struct strlist *get_perf_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500522{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500523 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500524 struct strlist *sl, *rawlist;
525 struct str_node *ent;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500526 struct probe_point pp;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500527
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500528 memset(&pp, 0, sizeof(pp));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500529 rawlist = get_trace_kprobe_event_rawlist(fd);
530
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500531 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500532 strlist__for_each(ent, rawlist) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500533 parse_trace_kprobe_event(ent->s, &pp);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500534 if (include_group) {
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500535 if (e_snprintf(buf, 128, "%s:%s", pp.group,
536 pp.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500537 die("Failed to copy group:event name.");
538 strlist__add(sl, buf);
539 } else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500540 strlist__add(sl, pp.event);
541 clear_probe_point(&pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500542 }
543
544 strlist__delete(rawlist);
545
546 return sl;
547}
548
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500549static void write_trace_kprobe_event(int fd, const char *buf)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500550{
551 int ret;
552
Masami Hiramatsufa282442009-12-08 17:03:23 -0500553 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500554 ret = write(fd, buf, strlen(buf));
555 if (ret <= 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500556 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500557}
558
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500559static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -0500560 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500561{
562 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500563
564 /* Try no suffix */
565 ret = e_snprintf(buf, len, "%s", base);
566 if (ret < 0)
567 die("snprintf() failed: %s", strerror(-ret));
568 if (!strlist__has_entry(namelist, buf))
569 return;
570
Masami Hiramatsud761b082009-12-15 10:32:25 -0500571 if (!allow_suffix) {
572 pr_warning("Error: event \"%s\" already exists. "
573 "(Use -f to force duplicates.)\n", base);
574 die("Can't add new event.");
575 }
576
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -0500577 /* Try to add suffix */
578 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500579 ret = e_snprintf(buf, len, "%s_%d", base, i);
580 if (ret < 0)
581 die("snprintf() failed: %s", strerror(-ret));
582 if (!strlist__has_entry(namelist, buf))
583 break;
584 }
585 if (i == MAX_EVENT_INDEX)
586 die("Too many events are on the same function.");
587}
588
Masami Hiramatsud761b082009-12-15 10:32:25 -0500589void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
590 bool force_add)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500591{
592 int i, j, fd;
593 struct probe_point *pp;
594 char buf[MAX_CMDLEN];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500595 char event[64];
596 struct strlist *namelist;
Masami Hiramatsud761b082009-12-15 10:32:25 -0500597 bool allow_suffix;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500598
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500599 fd = open_kprobe_events(O_RDWR, O_APPEND);
600 /* Get current event names */
Masami Hiramatsufa282442009-12-08 17:03:23 -0500601 namelist = get_perf_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500602
603 for (j = 0; j < nr_probes; j++) {
604 pp = probes + j;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500605 if (!pp->event)
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400606 pp->event = xstrdup(pp->function);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500607 if (!pp->group)
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400608 pp->group = xstrdup(PERFPROBE_GROUP);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500609 /* If force_add is true, suffix search is allowed */
610 allow_suffix = force_add;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500611 for (i = 0; i < pp->found; i++) {
612 /* Get an unused new event name */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500613 get_new_event_name(event, 64, pp->event, namelist,
614 allow_suffix);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500615 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
616 pp->retprobe ? 'r' : 'p',
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500617 pp->group, event,
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500618 pp->probes[i]);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500619 write_trace_kprobe_event(fd, buf);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500620 printf("Added new event:\n");
621 /* Get the first parameter (probe-point) */
622 sscanf(pp->probes[i], "%s", buf);
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500623 show_perf_probe_event(event, buf, pp);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500624 /* Add added event name to namelist */
625 strlist__add(namelist, event);
Masami Hiramatsud761b082009-12-15 10:32:25 -0500626 /*
627 * Probes after the first probe which comes from same
628 * user input are always allowed to add suffix, because
629 * there might be several addresses corresponding to
630 * one code line.
631 */
632 allow_suffix = true;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500633 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500634 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -0500635 /* Show how to use the event. */
636 printf("\nYou can now use it on all perf tools, such as:\n\n");
637 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
638
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500639 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500640 close(fd);
641}
Masami Hiramatsufa282442009-12-08 17:03:23 -0500642
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500643static void __del_trace_kprobe_event(int fd, struct str_node *ent)
644{
645 char *p;
646 char buf[128];
647
648 /* Convert from perf-probe event to trace-kprobe event */
649 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
650 die("Failed to copy event.");
651 p = strchr(buf + 2, ':');
652 if (!p)
653 die("Internal error: %s should have ':' but not.", ent->s);
654 *p = '/';
655
656 write_trace_kprobe_event(fd, buf);
657 printf("Remove event: %s\n", ent->s);
658}
659
Masami Hiramatsufa282442009-12-08 17:03:23 -0500660static void del_trace_kprobe_event(int fd, const char *group,
661 const char *event, struct strlist *namelist)
662{
663 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500664 struct str_node *ent, *n;
665 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500666
667 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
668 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -0500669
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500670 if (strpbrk(buf, "*?")) { /* Glob-exp */
671 strlist__for_each_safe(ent, n, namelist)
672 if (strglobmatch(ent->s, buf)) {
673 found++;
674 __del_trace_kprobe_event(fd, ent);
675 strlist__remove(namelist, ent);
676 }
677 } else {
678 ent = strlist__find(namelist, buf);
679 if (ent) {
680 found++;
681 __del_trace_kprobe_event(fd, ent);
682 strlist__remove(namelist, ent);
683 }
684 }
685 if (found == 0)
686 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500687}
688
689void del_trace_kprobe_events(struct strlist *dellist)
690{
691 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -0500692 const char *group, *event;
693 char *p, *str;
694 struct str_node *ent;
695 struct strlist *namelist;
696
697 fd = open_kprobe_events(O_RDWR, O_APPEND);
698 /* Get current event names */
699 namelist = get_perf_event_names(fd, true);
700
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500701 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400702 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500703 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500704 p = strchr(str, ':');
705 if (p) {
706 group = str;
707 *p = '\0';
708 event = p + 1;
709 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500710 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -0500711 event = str;
712 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500713 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500714 del_trace_kprobe_event(fd, group, event, namelist);
715 free(str);
716 }
717 strlist__delete(namelist);
718 close(fd);
719}
720
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500721#define LINEBUF_SIZE 256
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -0500722#define NR_ADDITIONAL_LINES 2
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500723
724static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
725{
726 char buf[LINEBUF_SIZE];
727 const char *color = PERF_COLOR_BLUE;
728
729 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
730 goto error;
731 if (!skip) {
732 if (show_num)
733 fprintf(stdout, "%7u %s", l, buf);
734 else
735 color_fprintf(stdout, color, " %s", buf);
736 }
737
738 while (strlen(buf) == LINEBUF_SIZE - 1 &&
739 buf[LINEBUF_SIZE - 2] != '\n') {
740 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
741 goto error;
742 if (!skip) {
743 if (show_num)
744 fprintf(stdout, "%s", buf);
745 else
746 color_fprintf(stdout, color, "%s", buf);
747 }
748 }
749 return;
750error:
751 if (feof(fp))
752 die("Source file is shorter than expected.");
753 else
754 die("File read error: %s", strerror(errno));
755}
756
757void show_line_range(struct line_range *lr)
758{
759 unsigned int l = 1;
760 struct line_node *ln;
761 FILE *fp;
762
763 setup_pager();
764
765 if (lr->function)
766 fprintf(stdout, "<%s:%d>\n", lr->function,
767 lr->start - lr->offset);
768 else
769 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
770
771 fp = fopen(lr->path, "r");
772 if (fp == NULL)
773 die("Failed to open %s: %s", lr->path, strerror(errno));
774 /* Skip to starting line number */
775 while (l < lr->start)
776 show_one_line(fp, l++, true, false);
777
778 list_for_each_entry(ln, &lr->line_list, list) {
779 while (ln->line > l)
780 show_one_line(fp, (l++) - lr->offset, false, false);
781 show_one_line(fp, (l++) - lr->offset, false, true);
782 }
Masami Hiramatsu5c8d1cb2010-02-25 08:36:04 -0500783
784 if (lr->end == INT_MAX)
785 lr->end = l + NR_ADDITIONAL_LINES;
786 while (l < lr->end && !feof(fp))
787 show_one_line(fp, (l++) - lr->offset, false, false);
788
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500789 fclose(fp);
790}