blob: 5bf8ab034466b4977e1fdcdeef819ab00a0c517b [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 Hiramatsu7ca59892010-04-14 18:39:28 -040045#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030046#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040048#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
51#define MAX_PROBE_ARGS 128
52#define PERFPROBE_GROUP "probe"
53
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040054bool probe_event_dry_run; /* Dry run flag */
55
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050058/* If there is no space to write, returns -E2BIG. */
59static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050060 __attribute__((format(printf, 3, 4)));
61
62static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050063{
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
72}
73
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030074static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075static struct map_groups kmap_groups;
76static struct map *kmaps[MAP__NR_TYPES];
77
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030078/* Initialize symbol maps and path of vmlinux */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040088 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040093
94 map_groups__init(&kmap_groups);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040095 ret = map_groups__create_kernel_maps(&kmap_groups, kmaps);
96 if (ret < 0)
97 pr_debug("Failed to create kernel maps.\n");
98
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400103}
104
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300105#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106static int open_vmlinux(void)
107{
108 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
109 pr_debug("Failed to load kernel map.\n");
110 return -EINVAL;
111 }
112 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
113 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
114}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300115
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400116/* Convert trace point to probe point with debuginfo */
117static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
118 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300119{
120 struct symbol *sym;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400121 int fd, ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300122
123 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
124 tp->symbol, NULL);
125 if (sym) {
126 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400127 if (fd >= 0) {
128 ret = find_perf_probe_point(fd,
129 sym->start + tp->offset, pp);
130 close(fd);
131 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300132 }
133 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400134 pr_debug("Failed to find corresponding probes from "
135 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400136 pp->function = strdup(tp->symbol);
137 if (pp->function == NULL)
138 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300139 pp->offset = tp->offset;
140 }
141 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400142
143 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300144}
145
146/* Try to find perf_probe_event with debuginfo */
147static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
148 struct kprobe_trace_event **tevs)
149{
150 bool need_dwarf = perf_probe_event_need_dwarf(pev);
151 int fd, ntevs;
152
153 fd = open_vmlinux();
154 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400155 if (need_dwarf) {
156 pr_warning("Failed to open debuginfo file.\n");
157 return fd;
158 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300159 pr_debug("Could not open vmlinux. Try to use symbols.\n");
160 return 0;
161 }
162
163 /* Searching trace events corresponding to probe event */
164 ntevs = find_kprobe_trace_events(fd, pev, tevs);
165 close(fd);
166
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400167 if (ntevs > 0) { /* Succeeded to find trace events */
168 pr_debug("find %d kprobe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300169 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400170 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300171
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400172 if (ntevs == 0) { /* No error but failed to find probe point. */
173 pr_warning("Probe point '%s' not found.\n",
174 synthesize_perf_probe_point(&pev->point));
175 return -ENOENT;
176 }
177 /* Error path : ntevs < 0 */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300178 if (need_dwarf) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400179 if (ntevs == -EBADF)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300180 pr_warning("No dwarf info found in the vmlinux - "
181 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400182 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300183 }
184 pr_debug("An error occurred in debuginfo analysis."
185 " Try to use symbols.\n");
186 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300187}
188
189#define LINEBUF_SIZE 256
190#define NR_ADDITIONAL_LINES 2
191
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400192static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300193{
194 char buf[LINEBUF_SIZE];
195 const char *color = PERF_COLOR_BLUE;
196
197 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
198 goto error;
199 if (!skip) {
200 if (show_num)
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400201 fprintf(stdout, "%7d %s", l, buf);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300202 else
203 color_fprintf(stdout, color, " %s", buf);
204 }
205
206 while (strlen(buf) == LINEBUF_SIZE - 1 &&
207 buf[LINEBUF_SIZE - 2] != '\n') {
208 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
209 goto error;
210 if (!skip) {
211 if (show_num)
212 fprintf(stdout, "%s", buf);
213 else
214 color_fprintf(stdout, color, "%s", buf);
215 }
216 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400217
218 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300219error:
220 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400221 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300222 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400223 pr_warning("File read error: %s\n", strerror(errno));
224
225 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300226}
227
228/*
229 * Show line-range always requires debuginfo to find source file and
230 * line number.
231 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400232int show_line_range(struct line_range *lr)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300233{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400234 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300235 struct line_node *ln;
236 FILE *fp;
237 int fd, ret;
238
239 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400240 ret = init_vmlinux();
241 if (ret < 0)
242 return ret;
243
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300244 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400245 if (fd < 0) {
246 pr_warning("Failed to open debuginfo file.\n");
247 return fd;
248 }
249
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300250 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300251 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400252 if (ret == 0) {
253 pr_warning("Specified source line is not found.\n");
254 return -ENOENT;
255 } else if (ret < 0) {
256 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
257 return ret;
258 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300259
260 setup_pager();
261
262 if (lr->function)
263 fprintf(stdout, "<%s:%d>\n", lr->function,
264 lr->start - lr->offset);
265 else
266 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
267
268 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400269 if (fp == NULL) {
270 pr_warning("Failed to open %s: %s\n", lr->path,
271 strerror(errno));
272 return -errno;
273 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300274 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400275 while (l < lr->start && ret >= 0)
276 ret = show_one_line(fp, l++, true, false);
277 if (ret < 0)
278 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300279
280 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400281 while (ln->line > l && ret >= 0)
282 ret = show_one_line(fp, (l++) - lr->offset,
283 false, false);
284 if (ret >= 0)
285 ret = show_one_line(fp, (l++) - lr->offset,
286 false, true);
287 if (ret < 0)
288 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300289 }
290
291 if (lr->end == INT_MAX)
292 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400293 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400294 ret = show_one_line(fp, (l++) - lr->offset, false, false);
295end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300296 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400297 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300298}
299
300#else /* !DWARF_SUPPORT */
301
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400302static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300303 struct perf_probe_point *pp)
304{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400305 pp->function = strdup(tp->symbol);
306 if (pp->function == NULL)
307 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300308 pp->offset = tp->offset;
309 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400310
311 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300312}
313
314static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
315 struct kprobe_trace_event **tevs __unused)
316{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400317 if (perf_probe_event_need_dwarf(pev)) {
318 pr_warning("Debuginfo-analysis is not supported.\n");
319 return -ENOSYS;
320 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300321 return 0;
322}
323
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400324int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300325{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400326 pr_warning("Debuginfo-analysis is not supported.\n");
327 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300328}
329
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400330#endif
331
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400332int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500333{
334 const char *ptr;
335 char *tmp;
336 /*
337 * <Syntax>
338 * SRC:SLN[+NUM|-ELN]
339 * FUNC[:SLN[+NUM|-ELN]]
340 */
341 ptr = strchr(arg, ':');
342 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400343 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400344 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400345 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400346 lr->end--; /*
347 * Adjust the number of lines here.
348 * If the number of lines == 1, the
349 * the end of line should be equal to
350 * the start of line.
351 */
352 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400353 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500354 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400355 lr->end = INT_MAX;
356 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
357 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500358 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400359 " than end line.\n");
360 return -EINVAL;
361 }
362 if (*tmp != '\0') {
363 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500364 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400365 return -EINVAL;
366 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400367 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400368 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400369 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400370 lr->end = INT_MAX;
371 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400372
373 if (tmp == NULL)
374 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500375
376 if (strchr(tmp, '.'))
377 lr->file = tmp;
378 else
379 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400380
381 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500382}
383
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500384/* Check the name is good for event/group */
385static bool check_event_name(const char *name)
386{
387 if (!isalpha(*name) && *name != '_')
388 return false;
389 while (*++name != '\0') {
390 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
391 return false;
392 }
393 return true;
394}
395
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500396/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400397static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500398{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400399 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500400 char *ptr, *tmp;
401 char c, nc = 0;
402 /*
403 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500404 * perf probe [EVENT=]SRC[:LN|;PTN]
405 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500406 *
407 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500408 */
409
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500410 ptr = strpbrk(arg, ";=@+%");
411 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500412 *ptr = '\0';
413 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400414 if (strchr(arg, ':')) {
415 semantic_error("Group name is not supported yet.\n");
416 return -ENOTSUP;
417 }
418 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500419 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400420 "follow C symbol-naming rule.\n", arg);
421 return -EINVAL;
422 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400423 pev->event = strdup(arg);
424 if (pev->event == NULL)
425 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400426 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500427 arg = tmp;
428 }
429
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500430 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500431 if (ptr) {
432 nc = *ptr;
433 *ptr++ = '\0';
434 }
435
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400436 tmp = strdup(arg);
437 if (tmp == NULL)
438 return -ENOMEM;
439
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500440 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400441 if (strchr(tmp, '.')) /* File */
442 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500443 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400444 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500445
446 /* Parse other options */
447 while (ptr) {
448 arg = ptr;
449 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500450 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400451 pp->lazy_line = strdup(arg);
452 if (pp->lazy_line == NULL)
453 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500454 break;
455 }
456 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500457 if (ptr) {
458 nc = *ptr;
459 *ptr++ = '\0';
460 }
461 switch (c) {
462 case ':': /* Line number */
463 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400464 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500465 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400466 " in line number.\n");
467 return -EINVAL;
468 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500469 break;
470 case '+': /* Byte offset from a symbol */
471 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400472 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500473 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400474 " in offset.\n");
475 return -EINVAL;
476 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500477 break;
478 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400479 if (pp->file) {
480 semantic_error("SRC@SRC is not allowed.\n");
481 return -EINVAL;
482 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400483 pp->file = strdup(arg);
484 if (pp->file == NULL)
485 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500486 break;
487 case '%': /* Probe places */
488 if (strcmp(arg, "return") == 0) {
489 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400490 } else { /* Others not supported yet */
491 semantic_error("%%%s is not supported.\n", arg);
492 return -ENOTSUP;
493 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500494 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400495 default: /* Buggy case */
496 pr_err("This program has a bug at %s:%d.\n",
497 __FILE__, __LINE__);
498 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500499 break;
500 }
501 }
502
503 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400504 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500505 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400506 return -EINVAL;
507 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500508
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400509 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500510 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400511 return -EINVAL;
512 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500513
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400514 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500515 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400516 return -EINVAL;
517 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500518
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500520 semantic_error("File always requires line number or "
521 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400522 return -EINVAL;
523 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500524
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400525 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500526 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400527 return -EINVAL;
528 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500529
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400530 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500531 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400532 return -EINVAL;
533 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500534
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400535 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500536 semantic_error("Offset/Line/Lazy pattern can't be used with "
537 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400538 return -EINVAL;
539 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500540
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400541 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500542 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
543 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400544 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500545}
546
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400547/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400549{
Masami Hiramatsu48481932010-04-12 13:16:53 -0400550 char *tmp;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400551 struct perf_probe_arg_field **fieldp;
552
553 pr_debug("parsing arg: %s into ", str);
554
Masami Hiramatsu48481932010-04-12 13:16:53 -0400555 tmp = strchr(str, '=');
556 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400557 arg->name = strndup(str, tmp - str);
558 if (arg->name == NULL)
559 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400560 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400561 str = tmp + 1;
562 }
563
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400564 tmp = strchr(str, ':');
565 if (tmp) { /* Type setting */
566 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400567 arg->type = strdup(tmp + 1);
568 if (arg->type == NULL)
569 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400570 pr_debug("type:%s ", arg->type);
571 }
572
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400573 tmp = strpbrk(str, "-.");
574 if (!is_c_varname(str) || !tmp) {
575 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400576 arg->var = strdup(str);
577 if (arg->var == NULL)
578 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400579 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400580 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400581 }
582
583 /* Structure fields */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400584 arg->var = strndup(str, tmp - str);
585 if (arg->var == NULL)
586 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400587 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400588 fieldp = &arg->field;
589
590 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400591 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
592 if (*fieldp == NULL)
593 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400594 if (*tmp == '.') {
595 str = tmp + 1;
596 (*fieldp)->ref = false;
597 } else if (tmp[1] == '>') {
598 str = tmp + 2;
599 (*fieldp)->ref = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400600 } else {
601 semantic_error("Argument parse error: %s\n", str);
602 return -EINVAL;
603 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400604
605 tmp = strpbrk(str, "-.");
606 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400607 (*fieldp)->name = strndup(str, tmp - str);
608 if ((*fieldp)->name == NULL)
609 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400610 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
611 fieldp = &(*fieldp)->next;
612 }
613 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400614 (*fieldp)->name = strdup(str);
615 if ((*fieldp)->name == NULL)
616 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400617 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400618
619 /* If no name is specified, set the last field name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400620 if (!arg->name) {
621 arg->name = strdup((*fieldp)->name);
622 if (arg->name == NULL)
623 return -ENOMEM;
624 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400625 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400626}
627
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400628/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500630{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500631 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400632 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500633
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400634 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400635 if (!argv) {
636 pr_debug("Failed to split arguments.\n");
637 return -ENOMEM;
638 }
639 if (argc - 1 > MAX_PROBE_ARGS) {
640 semantic_error("Too many probe arguments (%d).\n", argc - 1);
641 ret = -ERANGE;
642 goto out;
643 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500644 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400645 ret = parse_perf_probe_point(argv[0], pev);
646 if (ret < 0)
647 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500648
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500649 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400650 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400651 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
652 if (pev->args == NULL) {
653 ret = -ENOMEM;
654 goto out;
655 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400656 for (i = 0; i < pev->nargs && ret >= 0; i++) {
657 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
658 if (ret >= 0 &&
659 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400660 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 " kretprobe.\n");
662 ret = -EINVAL;
663 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500664 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400665out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500666 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400667
668 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500669}
670
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400671/* Return true if this perf_probe_event requires debuginfo */
672bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500673{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400674 int i;
675
676 if (pev->point.file || pev->point.line || pev->point.lazy_line)
677 return true;
678
679 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400680 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400681 return true;
682
683 return false;
684}
685
686/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400687int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400688{
689 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500690 char pr;
691 char *p;
692 int ret, i, argc;
693 char **argv;
694
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400695 pr_debug("Parsing kprobe_events: %s\n", cmd);
696 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400697 if (!argv) {
698 pr_debug("Failed to split arguments.\n");
699 return -ENOMEM;
700 }
701 if (argc < 2) {
702 semantic_error("Too few probe arguments.\n");
703 ret = -ERANGE;
704 goto out;
705 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500706
707 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800708 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400709 &pr, (float *)(void *)&tev->group,
710 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400711 if (ret != 3) {
712 semantic_error("Failed to parse event name: %s\n", argv[0]);
713 ret = -EINVAL;
714 goto out;
715 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400716 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500717
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400718 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500719
720 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400721 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
722 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500723 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400724 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500725
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400726 tev->nargs = argc - 2;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400727 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
728 if (tev->args == NULL) {
729 ret = -ENOMEM;
730 goto out;
731 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400732 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500733 p = strchr(argv[i + 2], '=');
734 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400735 *p++ = '\0';
736 else
737 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400738 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400739 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400740 tev->args[i].value = strdup(p);
741 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
742 ret = -ENOMEM;
743 goto out;
744 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500745 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400746 ret = 0;
747out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500748 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400749 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500750}
751
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400752/* Compose only probe arg */
753int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
754{
755 struct perf_probe_arg_field *field = pa->field;
756 int ret;
757 char *tmp = buf;
758
Masami Hiramatsu48481932010-04-12 13:16:53 -0400759 if (pa->name && pa->var)
760 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
761 else
762 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400763 if (ret <= 0)
764 goto error;
765 tmp += ret;
766 len -= ret;
767
768 while (field) {
769 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
770 field->name);
771 if (ret <= 0)
772 goto error;
773 tmp += ret;
774 len -= ret;
775 field = field->next;
776 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400777
778 if (pa->type) {
779 ret = e_snprintf(tmp, len, ":%s", pa->type);
780 if (ret <= 0)
781 goto error;
782 tmp += ret;
783 len -= ret;
784 }
785
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400786 return tmp - buf;
787error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400788 pr_debug("Failed to synthesize perf probe argument: %s",
789 strerror(-ret));
790 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400791}
792
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400793/* Compose only probe point (not argument) */
794static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500795{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400796 char *buf, *tmp;
797 char offs[32] = "", line[32] = "", file[32] = "";
798 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500799
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400800 buf = zalloc(MAX_CMDLEN);
801 if (buf == NULL) {
802 ret = -ENOMEM;
803 goto error;
804 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500805 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400806 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500807 if (ret <= 0)
808 goto error;
809 }
810 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400811 ret = e_snprintf(line, 32, ":%d", pp->line);
812 if (ret <= 0)
813 goto error;
814 }
815 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400816 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400817 if (len < 0)
818 len = 0;
819 tmp = strchr(pp->file + len, '/');
820 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400821 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400822 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500823 if (ret <= 0)
824 goto error;
825 }
826
827 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400828 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
829 offs, pp->retprobe ? "%return" : "", line,
830 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500831 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400832 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500833 if (ret <= 0)
834 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500835
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400836 return buf;
837error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838 pr_debug("Failed to synthesize perf probe point: %s",
839 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400840 if (buf)
841 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400843}
844
845#if 0
846char *synthesize_perf_probe_command(struct perf_probe_event *pev)
847{
848 char *buf;
849 int i, len, ret;
850
851 buf = synthesize_perf_probe_point(&pev->point);
852 if (!buf)
853 return NULL;
854
855 len = strlen(buf);
856 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500857 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400858 pev->args[i].name);
859 if (ret <= 0) {
860 free(buf);
861 return NULL;
862 }
863 len += ret;
864 }
865
866 return buf;
867}
868#endif
869
870static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
871 char **buf, size_t *buflen,
872 int depth)
873{
874 int ret;
875 if (ref->next) {
876 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
877 buflen, depth + 1);
878 if (depth < 0)
879 goto out;
880 }
881
882 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
883 if (ret < 0)
884 depth = ret;
885 else {
886 *buf += ret;
887 *buflen -= ret;
888 }
889out:
890 return depth;
891
892}
893
894static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
895 char *buf, size_t buflen)
896{
897 int ret, depth = 0;
898 char *tmp = buf;
899
900 /* Argument name or separator */
901 if (arg->name)
902 ret = e_snprintf(buf, buflen, " %s=", arg->name);
903 else
904 ret = e_snprintf(buf, buflen, " ");
905 if (ret < 0)
906 return ret;
907 buf += ret;
908 buflen -= ret;
909
910 /* Dereferencing arguments */
911 if (arg->ref) {
912 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
913 &buflen, 1);
914 if (depth < 0)
915 return depth;
916 }
917
918 /* Print argument value */
919 ret = e_snprintf(buf, buflen, "%s", arg->value);
920 if (ret < 0)
921 return ret;
922 buf += ret;
923 buflen -= ret;
924
925 /* Closing */
926 while (depth--) {
927 ret = e_snprintf(buf, buflen, ")");
928 if (ret < 0)
929 return ret;
930 buf += ret;
931 buflen -= ret;
932 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400933 /* Print argument type */
934 if (arg->type) {
935 ret = e_snprintf(buf, buflen, ":%s", arg->type);
936 if (ret <= 0)
937 return ret;
938 buf += ret;
939 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400940
941 return buf - tmp;
942}
943
944char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
945{
946 struct kprobe_trace_point *tp = &tev->point;
947 char *buf;
948 int i, len, ret;
949
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400950 buf = zalloc(MAX_CMDLEN);
951 if (buf == NULL)
952 return NULL;
953
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400954 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
955 tp->retprobe ? 'r' : 'p',
956 tev->group, tev->event,
957 tp->symbol, tp->offset);
958 if (len <= 0)
959 goto error;
960
961 for (i = 0; i < tev->nargs; i++) {
962 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
963 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500964 if (ret <= 0)
965 goto error;
966 len += ret;
967 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500968
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400969 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500970error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400971 free(buf);
972 return NULL;
973}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500974
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400975int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
976 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400977{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400978 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400979 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400980
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300981 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400982 pev->event = strdup(tev->event);
983 pev->group = strdup(tev->group);
984 if (pev->event == NULL || pev->group == NULL)
985 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400986
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300987 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400988 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
989 if (ret < 0)
990 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300991
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400992 /* Convert trace_arg to probe_arg */
993 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400994 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
995 if (pev->args == NULL)
996 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400997 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400998 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400999 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001001 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1002 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001003 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001004 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001005 if (pev->args[i].name == NULL && ret >= 0)
1006 ret = -ENOMEM;
1007 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001008
1009 if (ret < 0)
1010 clear_perf_probe_event(pev);
1011
1012 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001013}
1014
1015void clear_perf_probe_event(struct perf_probe_event *pev)
1016{
1017 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001018 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001019 int i;
1020
1021 if (pev->event)
1022 free(pev->event);
1023 if (pev->group)
1024 free(pev->group);
1025 if (pp->file)
1026 free(pp->file);
1027 if (pp->function)
1028 free(pp->function);
1029 if (pp->lazy_line)
1030 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001031 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001032 if (pev->args[i].name)
1033 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001034 if (pev->args[i].var)
1035 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001036 if (pev->args[i].type)
1037 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001038 field = pev->args[i].field;
1039 while (field) {
1040 next = field->next;
1041 if (field->name)
1042 free(field->name);
1043 free(field);
1044 field = next;
1045 }
1046 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001047 if (pev->args)
1048 free(pev->args);
1049 memset(pev, 0, sizeof(*pev));
1050}
1051
1052void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1053{
1054 struct kprobe_trace_arg_ref *ref, *next;
1055 int i;
1056
1057 if (tev->event)
1058 free(tev->event);
1059 if (tev->group)
1060 free(tev->group);
1061 if (tev->point.symbol)
1062 free(tev->point.symbol);
1063 for (i = 0; i < tev->nargs; i++) {
1064 if (tev->args[i].name)
1065 free(tev->args[i].name);
1066 if (tev->args[i].value)
1067 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001068 if (tev->args[i].type)
1069 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001070 ref = tev->args[i].ref;
1071 while (ref) {
1072 next = ref->next;
1073 free(ref);
1074 ref = next;
1075 }
1076 }
1077 if (tev->args)
1078 free(tev->args);
1079 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001080}
1081
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001082static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001083{
1084 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001085 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001086 int ret;
1087
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001088 __debugfs = debugfs_find_mountpoint();
1089 if (__debugfs == NULL) {
1090 pr_warning("Debugfs is not mounted.\n");
1091 return -ENOENT;
1092 }
1093
1094 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001095 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001096 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001097 if (readwrite && !probe_event_dry_run)
1098 ret = open(buf, O_RDWR, O_APPEND);
1099 else
1100 ret = open(buf, O_RDONLY, 0);
1101 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001102
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001103 if (ret < 0) {
1104 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 pr_warning("kprobe_events file does not exist - please"
1106 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001107 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 pr_warning("Failed to open kprobe_events file: %s\n",
1109 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001110 }
1111 return ret;
1112}
1113
1114/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001115static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001116{
1117 int ret, idx;
1118 FILE *fp;
1119 char buf[MAX_CMDLEN];
1120 char *p;
1121 struct strlist *sl;
1122
1123 sl = strlist__new(true, NULL);
1124
1125 fp = fdopen(dup(fd), "r");
1126 while (!feof(fp)) {
1127 p = fgets(buf, MAX_CMDLEN, fp);
1128 if (!p)
1129 break;
1130
1131 idx = strlen(p) - 1;
1132 if (p[idx] == '\n')
1133 p[idx] = '\0';
1134 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001135 if (ret < 0) {
1136 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1137 strlist__delete(sl);
1138 return NULL;
1139 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001140 }
1141 fclose(fp);
1142
1143 return sl;
1144}
1145
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001146/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001147static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001148{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001149 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001150 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001151 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001152
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001153 /* Synthesize only event probe point */
1154 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001155 if (!place)
1156 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001157
1158 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001159 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001160 return ret;
1161
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001162 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001163
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001164 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001165 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001166 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001167 ret = synthesize_perf_probe_arg(&pev->args[i],
1168 buf, 128);
1169 if (ret < 0)
1170 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001171 printf(" %s", buf);
1172 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001173 }
1174 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001175 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001176 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001177}
1178
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001179/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001180int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001181{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001182 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 struct kprobe_trace_event tev;
1184 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001185 struct strlist *rawlist;
1186 struct str_node *ent;
1187
Masami Hiramatsu72041332010-01-05 17:47:10 -05001188 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189 ret = init_vmlinux();
1190 if (ret < 0)
1191 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001192
1193 memset(&tev, 0, sizeof(tev));
1194 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001195
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001196 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 if (fd < 0)
1198 return fd;
1199
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001200 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001201 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001202 if (!rawlist)
1203 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001204
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001205 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 ret = parse_kprobe_trace_command(ent->s, &tev);
1207 if (ret >= 0) {
1208 ret = convert_to_perf_probe_event(&tev, &pev);
1209 if (ret >= 0)
1210 ret = show_perf_probe_event(&pev);
1211 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001212 clear_perf_probe_event(&pev);
1213 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214 if (ret < 0)
1215 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001216 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001217 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218
1219 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001220}
1221
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001222/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001223static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001224{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001225 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001226 struct strlist *sl, *rawlist;
1227 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001228 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001230
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001231 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001232
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001233 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001234 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001235 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001236 ret = parse_kprobe_trace_command(ent->s, &tev);
1237 if (ret < 0)
1238 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001239 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001240 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1241 tev.event);
1242 if (ret >= 0)
1243 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001244 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001246 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 if (ret < 0)
1248 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001249 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001250 strlist__delete(rawlist);
1251
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 if (ret < 0) {
1253 strlist__delete(sl);
1254 return NULL;
1255 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001256 return sl;
1257}
1258
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001259static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001260{
1261 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001262 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001263
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001264 if (!buf) {
1265 pr_debug("Failed to synthesize kprobe trace event.\n");
1266 return -EINVAL;
1267 }
1268
Masami Hiramatsufa282442009-12-08 17:03:23 -05001269 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001270 if (!probe_event_dry_run) {
1271 ret = write(fd, buf, strlen(buf));
1272 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273 pr_warning("Failed to write event: %s\n",
1274 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001275 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001276 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001278}
1279
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280static int get_new_event_name(char *buf, size_t len, const char *base,
1281 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001282{
1283 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001284
1285 /* Try no suffix */
1286 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 if (ret < 0) {
1288 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1289 return ret;
1290 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001291 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001292 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001293
Masami Hiramatsud761b082009-12-15 10:32:25 -05001294 if (!allow_suffix) {
1295 pr_warning("Error: event \"%s\" already exists. "
1296 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001297 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001298 }
1299
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001300 /* Try to add suffix */
1301 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001302 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001303 if (ret < 0) {
1304 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1305 return ret;
1306 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001307 if (!strlist__has_entry(namelist, buf))
1308 break;
1309 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001310 if (i == MAX_EVENT_INDEX) {
1311 pr_warning("Too many events are on the same function.\n");
1312 ret = -ERANGE;
1313 }
1314
1315 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001316}
1317
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001318static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1319 struct kprobe_trace_event *tevs,
1320 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001321{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001323 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001324 char buf[64];
1325 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001326 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001327
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001328 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001329 if (fd < 0)
1330 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001331 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001332 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001333 if (!namelist) {
1334 pr_debug("Failed to get current event list.\n");
1335 return -EIO;
1336 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001337
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001338 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001339 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001340 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001341 tev = &tevs[i];
1342 if (pev->event)
1343 event = pev->event;
1344 else
1345 if (pev->point.function)
1346 event = pev->point.function;
1347 else
1348 event = tev->point.symbol;
1349 if (pev->group)
1350 group = pev->group;
1351 else
1352 group = PERFPROBE_GROUP;
1353
1354 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 ret = get_new_event_name(buf, 64, event,
1356 namelist, allow_suffix);
1357 if (ret < 0)
1358 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359 event = buf;
1360
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001361 tev->event = strdup(event);
1362 tev->group = strdup(group);
1363 if (tev->event == NULL || tev->group == NULL) {
1364 ret = -ENOMEM;
1365 break;
1366 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001367 ret = write_kprobe_trace_event(fd, tev);
1368 if (ret < 0)
1369 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001370 /* Add added event name to namelist */
1371 strlist__add(namelist, event);
1372
1373 /* Trick here - save current event/group */
1374 event = pev->event;
1375 group = pev->group;
1376 pev->event = tev->event;
1377 pev->group = tev->group;
1378 show_perf_probe_event(pev);
1379 /* Trick here - restore current event/group */
1380 pev->event = (char *)event;
1381 pev->group = (char *)group;
1382
1383 /*
1384 * Probes after the first probe which comes from same
1385 * user input are always allowed to add suffix, because
1386 * there might be several addresses corresponding to
1387 * one code line.
1388 */
1389 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001390 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391
1392 if (ret >= 0) {
1393 /* Show how to use the event. */
1394 printf("\nYou can now use it on all perf tools, such as:\n\n");
1395 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1396 tev->event);
1397 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001398
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001399 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001400 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001401 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001402}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001403
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1405 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001406{
1407 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001408 int ret = 0, i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001409 struct kprobe_trace_event *tev;
1410
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001411 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001412 ret = try_to_find_kprobe_trace_events(pev, tevs);
1413 if (ret != 0)
1414 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001415
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001416 /* Allocate trace event buffer */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001417 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1418 if (tev == NULL)
1419 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001420
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001422 tev->point.symbol = strdup(pev->point.function);
1423 if (tev->point.symbol == NULL) {
1424 ret = -ENOMEM;
1425 goto error;
1426 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001427 tev->point.offset = pev->point.offset;
1428 tev->nargs = pev->nargs;
1429 if (tev->nargs) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001430 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1431 * tev->nargs);
1432 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001433 ret = -ENOMEM;
1434 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001435 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001436 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001437 if (pev->args[i].name) {
1438 tev->args[i].name = strdup(pev->args[i].name);
1439 if (tev->args[i].name == NULL) {
1440 ret = -ENOMEM;
1441 goto error;
1442 }
1443 }
1444 tev->args[i].value = strdup(pev->args[i].var);
1445 if (tev->args[i].value == NULL) {
1446 ret = -ENOMEM;
1447 goto error;
1448 }
1449 if (pev->args[i].type) {
1450 tev->args[i].type = strdup(pev->args[i].type);
1451 if (tev->args[i].type == NULL) {
1452 ret = -ENOMEM;
1453 goto error;
1454 }
1455 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001456 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001457 }
1458
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001459 /* Currently just checking function name from symbol map */
1460 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1461 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001462 if (!sym) {
1463 pr_warning("Kernel symbol \'%s\' not found.\n",
1464 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001465 ret = -ENOENT;
1466 goto error;
1467 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001468
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001469 return 1;
1470error:
1471 clear_kprobe_trace_event(tev);
1472 free(tev);
1473 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001474 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475}
1476
1477struct __event_package {
1478 struct perf_probe_event *pev;
1479 struct kprobe_trace_event *tevs;
1480 int ntevs;
1481};
1482
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001483int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1484 bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001485{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001486 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001487 struct __event_package *pkgs;
1488
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001489 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1490 if (pkgs == NULL)
1491 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001492
1493 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494 ret = init_vmlinux();
1495 if (ret < 0)
1496 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001497
1498 /* Loop 1: convert all events */
1499 for (i = 0; i < npevs; i++) {
1500 pkgs[i].pev = &pevs[i];
1501 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001502 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1503 &pkgs[i].tevs);
1504 if (ret < 0)
1505 goto end;
1506 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001507 }
1508
1509 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 for (i = 0; i < npevs && ret >= 0; i++)
1511 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1512 pkgs[i].ntevs, force_add);
1513end:
1514 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516 for (j = 0; j < pkgs[i].ntevs; j++)
1517 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1518
1519 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001520}
1521
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001523{
1524 char *p;
1525 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001527
1528 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1530 if (ret < 0)
1531 goto error;
1532
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001533 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 if (!p) {
1535 pr_debug("Internal error: %s should have ':' but not.\n",
1536 ent->s);
1537 ret = -ENOTSUP;
1538 goto error;
1539 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001540 *p = '/';
1541
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001542 pr_debug("Writing event: %s\n", buf);
1543 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001544 if (ret < 0)
1545 goto error;
1546
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001547 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548 return 0;
1549error:
1550 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1551 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001552}
1553
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554static int del_trace_kprobe_event(int fd, const char *group,
1555 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001556{
1557 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001558 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001559 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001560
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001561 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1562 if (ret < 0) {
1563 pr_err("Failed to copy event.");
1564 return ret;
1565 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001566
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001567 if (strpbrk(buf, "*?")) { /* Glob-exp */
1568 strlist__for_each_safe(ent, n, namelist)
1569 if (strglobmatch(ent->s, buf)) {
1570 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001571 ret = __del_trace_kprobe_event(fd, ent);
1572 if (ret < 0)
1573 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001574 strlist__remove(namelist, ent);
1575 }
1576 } else {
1577 ent = strlist__find(namelist, buf);
1578 if (ent) {
1579 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001580 ret = __del_trace_kprobe_event(fd, ent);
1581 if (ret >= 0)
1582 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001583 }
1584 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001585 if (found == 0 && ret >= 0)
1586 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1587
1588 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001589}
1590
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001591int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001592{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001593 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001594 const char *group, *event;
1595 char *p, *str;
1596 struct str_node *ent;
1597 struct strlist *namelist;
1598
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001599 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001600 if (fd < 0)
1601 return fd;
1602
Masami Hiramatsufa282442009-12-08 17:03:23 -05001603 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001604 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605 if (namelist == NULL)
1606 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001607
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001608 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001609 str = strdup(ent->s);
1610 if (str == NULL) {
1611 ret = -ENOMEM;
1612 break;
1613 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001614 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001615 p = strchr(str, ':');
1616 if (p) {
1617 group = str;
1618 *p = '\0';
1619 event = p + 1;
1620 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001621 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001622 event = str;
1623 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001624 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001625 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001626 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001627 if (ret < 0)
1628 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001629 }
1630 strlist__delete(namelist);
1631 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001632
1633 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001634}
1635