blob: 351baa9a36952321313fbd94cf50736a24690ffe [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);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030075static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030077/* Initialize symbol maps and path of vmlinux */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080080 struct dso *kernel;
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
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094 ret = machine__init(&machine, "/", 0);
95 if (ret < 0)
96 goto out;
97
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080098 kernel = dso__new_kernel(symbol_conf.vmlinux_name);
99 if (kernel == NULL)
100 die("Failed to create kernel dso.");
101
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300102 ret = __machine__create_kernel_maps(&machine, kernel);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400103 if (ret < 0)
104 pr_debug("Failed to create kernel maps.\n");
105
106out:
107 if (ret < 0)
108 pr_warning("Failed to init vmlinux path.\n");
109 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400110}
111
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300112#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400113static int open_vmlinux(void)
114{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300115 if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400116 pr_debug("Failed to load kernel map.\n");
117 return -EINVAL;
118 }
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300119 pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name);
120 return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400121}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300122
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400123/* Convert trace point to probe point with debuginfo */
124static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
125 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300126{
127 struct symbol *sym;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400128 int fd, ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300129
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300130 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300131 tp->symbol, NULL);
132 if (sym) {
133 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400134 if (fd >= 0) {
135 ret = find_perf_probe_point(fd,
136 sym->start + tp->offset, pp);
137 close(fd);
138 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300139 }
140 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400141 pr_debug("Failed to find corresponding probes from "
142 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400143 pp->function = strdup(tp->symbol);
144 if (pp->function == NULL)
145 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300146 pp->offset = tp->offset;
147 }
148 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400149
150 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300151}
152
153/* Try to find perf_probe_event with debuginfo */
154static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400155 struct kprobe_trace_event **tevs,
156 int max_tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300157{
158 bool need_dwarf = perf_probe_event_need_dwarf(pev);
159 int fd, ntevs;
160
161 fd = open_vmlinux();
162 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400163 if (need_dwarf) {
164 pr_warning("Failed to open debuginfo file.\n");
165 return fd;
166 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300167 pr_debug("Could not open vmlinux. Try to use symbols.\n");
168 return 0;
169 }
170
171 /* Searching trace events corresponding to probe event */
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400172 ntevs = find_kprobe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300173 close(fd);
174
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400175 if (ntevs > 0) { /* Succeeded to find trace events */
176 pr_debug("find %d kprobe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300177 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400178 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300179
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400180 if (ntevs == 0) { /* No error but failed to find probe point. */
181 pr_warning("Probe point '%s' not found.\n",
182 synthesize_perf_probe_point(&pev->point));
183 return -ENOENT;
184 }
185 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400186 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
187 if (ntevs == -EBADF) {
188 pr_warning("Warning: No dwarf info found in the vmlinux - "
189 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
190 if (!need_dwarf) {
191 pr_debug("Trying to use symbols.\nn");
192 return 0;
193 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300194 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400195 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300196}
197
198#define LINEBUF_SIZE 256
199#define NR_ADDITIONAL_LINES 2
200
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400201static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300202{
203 char buf[LINEBUF_SIZE];
204 const char *color = PERF_COLOR_BLUE;
205
206 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
207 goto error;
208 if (!skip) {
209 if (show_num)
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400210 fprintf(stdout, "%7d %s", l, buf);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300211 else
212 color_fprintf(stdout, color, " %s", buf);
213 }
214
215 while (strlen(buf) == LINEBUF_SIZE - 1 &&
216 buf[LINEBUF_SIZE - 2] != '\n') {
217 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
218 goto error;
219 if (!skip) {
220 if (show_num)
221 fprintf(stdout, "%s", buf);
222 else
223 color_fprintf(stdout, color, "%s", buf);
224 }
225 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400226
227 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300228error:
229 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400230 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300231 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400232 pr_warning("File read error: %s\n", strerror(errno));
233
234 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300235}
236
237/*
238 * Show line-range always requires debuginfo to find source file and
239 * line number.
240 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400241int show_line_range(struct line_range *lr)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300242{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400243 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300244 struct line_node *ln;
245 FILE *fp;
246 int fd, ret;
247
248 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400249 ret = init_vmlinux();
250 if (ret < 0)
251 return ret;
252
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300253 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400254 if (fd < 0) {
255 pr_warning("Failed to open debuginfo file.\n");
256 return fd;
257 }
258
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300259 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300260 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400261 if (ret == 0) {
262 pr_warning("Specified source line is not found.\n");
263 return -ENOENT;
264 } else if (ret < 0) {
265 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
266 return ret;
267 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300268
269 setup_pager();
270
271 if (lr->function)
272 fprintf(stdout, "<%s:%d>\n", lr->function,
273 lr->start - lr->offset);
274 else
275 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
276
277 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400278 if (fp == NULL) {
279 pr_warning("Failed to open %s: %s\n", lr->path,
280 strerror(errno));
281 return -errno;
282 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300283 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400284 while (l < lr->start && ret >= 0)
285 ret = show_one_line(fp, l++, true, false);
286 if (ret < 0)
287 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300288
289 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400290 while (ln->line > l && ret >= 0)
291 ret = show_one_line(fp, (l++) - lr->offset,
292 false, false);
293 if (ret >= 0)
294 ret = show_one_line(fp, (l++) - lr->offset,
295 false, true);
296 if (ret < 0)
297 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300298 }
299
300 if (lr->end == INT_MAX)
301 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400302 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400303 ret = show_one_line(fp, (l++) - lr->offset, false, false);
304end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300305 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400306 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300307}
308
309#else /* !DWARF_SUPPORT */
310
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400311static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300312 struct perf_probe_point *pp)
313{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400314 pp->function = strdup(tp->symbol);
315 if (pp->function == NULL)
316 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300317 pp->offset = tp->offset;
318 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400319
320 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300321}
322
323static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400324 struct kprobe_trace_event **tevs __unused,
325 int max_tevs __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300326{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400327 if (perf_probe_event_need_dwarf(pev)) {
328 pr_warning("Debuginfo-analysis is not supported.\n");
329 return -ENOSYS;
330 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300331 return 0;
332}
333
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400334int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300335{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400336 pr_warning("Debuginfo-analysis is not supported.\n");
337 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300338}
339
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400340#endif
341
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400342int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500343{
344 const char *ptr;
345 char *tmp;
346 /*
347 * <Syntax>
348 * SRC:SLN[+NUM|-ELN]
349 * FUNC[:SLN[+NUM|-ELN]]
350 */
351 ptr = strchr(arg, ':');
352 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400353 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400354 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400355 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400356 lr->end--; /*
357 * Adjust the number of lines here.
358 * If the number of lines == 1, the
359 * the end of line should be equal to
360 * the start of line.
361 */
362 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400363 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500364 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400365 lr->end = INT_MAX;
366 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
367 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500368 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400369 " than end line.\n");
370 return -EINVAL;
371 }
372 if (*tmp != '\0') {
373 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500374 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400375 return -EINVAL;
376 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400377 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400378 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400379 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400380 lr->end = INT_MAX;
381 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400382
383 if (tmp == NULL)
384 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500385
386 if (strchr(tmp, '.'))
387 lr->file = tmp;
388 else
389 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400390
391 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500392}
393
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500394/* Check the name is good for event/group */
395static bool check_event_name(const char *name)
396{
397 if (!isalpha(*name) && *name != '_')
398 return false;
399 while (*++name != '\0') {
400 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
401 return false;
402 }
403 return true;
404}
405
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500406/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400407static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500408{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400409 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500410 char *ptr, *tmp;
411 char c, nc = 0;
412 /*
413 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500414 * perf probe [EVENT=]SRC[:LN|;PTN]
415 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500416 *
417 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500418 */
419
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500420 ptr = strpbrk(arg, ";=@+%");
421 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500422 *ptr = '\0';
423 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400424 if (strchr(arg, ':')) {
425 semantic_error("Group name is not supported yet.\n");
426 return -ENOTSUP;
427 }
428 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500429 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400430 "follow C symbol-naming rule.\n", arg);
431 return -EINVAL;
432 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400433 pev->event = strdup(arg);
434 if (pev->event == NULL)
435 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400436 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500437 arg = tmp;
438 }
439
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500440 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500441 if (ptr) {
442 nc = *ptr;
443 *ptr++ = '\0';
444 }
445
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400446 tmp = strdup(arg);
447 if (tmp == NULL)
448 return -ENOMEM;
449
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500450 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400451 if (strchr(tmp, '.')) /* File */
452 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500453 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400454 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500455
456 /* Parse other options */
457 while (ptr) {
458 arg = ptr;
459 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500460 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400461 pp->lazy_line = strdup(arg);
462 if (pp->lazy_line == NULL)
463 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500464 break;
465 }
466 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500467 if (ptr) {
468 nc = *ptr;
469 *ptr++ = '\0';
470 }
471 switch (c) {
472 case ':': /* Line number */
473 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400474 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500475 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400476 " in line number.\n");
477 return -EINVAL;
478 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500479 break;
480 case '+': /* Byte offset from a symbol */
481 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400482 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500483 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400484 " in offset.\n");
485 return -EINVAL;
486 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500487 break;
488 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400489 if (pp->file) {
490 semantic_error("SRC@SRC is not allowed.\n");
491 return -EINVAL;
492 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400493 pp->file = strdup(arg);
494 if (pp->file == NULL)
495 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500496 break;
497 case '%': /* Probe places */
498 if (strcmp(arg, "return") == 0) {
499 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400500 } else { /* Others not supported yet */
501 semantic_error("%%%s is not supported.\n", arg);
502 return -ENOTSUP;
503 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500504 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400505 default: /* Buggy case */
506 pr_err("This program has a bug at %s:%d.\n",
507 __FILE__, __LINE__);
508 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500509 break;
510 }
511 }
512
513 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400514 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500515 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400516 return -EINVAL;
517 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500518
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500520 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400521 return -EINVAL;
522 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500523
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400524 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500525 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400526 return -EINVAL;
527 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500528
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400529 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500530 semantic_error("File always requires line number or "
531 "lazy pattern.");
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->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500536 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400537 return -EINVAL;
538 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500539
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400540 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500541 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400542 return -EINVAL;
543 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500544
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400545 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500546 semantic_error("Offset/Line/Lazy pattern can't be used with "
547 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548 return -EINVAL;
549 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500550
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400551 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500552 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
553 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400554 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500555}
556
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400557/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400558static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400559{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400560 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400561 struct perf_probe_arg_field **fieldp;
562
563 pr_debug("parsing arg: %s into ", str);
564
Masami Hiramatsu48481932010-04-12 13:16:53 -0400565 tmp = strchr(str, '=');
566 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400567 arg->name = strndup(str, tmp - str);
568 if (arg->name == NULL)
569 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400570 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400571 str = tmp + 1;
572 }
573
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400574 tmp = strchr(str, ':');
575 if (tmp) { /* Type setting */
576 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400577 arg->type = strdup(tmp + 1);
578 if (arg->type == NULL)
579 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400580 pr_debug("type:%s ", arg->type);
581 }
582
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400583 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400584 if (!is_c_varname(str) || !tmp) {
585 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400586 arg->var = strdup(str);
587 if (arg->var == NULL)
588 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400589 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400590 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400591 }
592
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400593 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400594 arg->var = strndup(str, tmp - str);
595 if (arg->var == NULL)
596 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400597 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400598 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400599 fieldp = &arg->field;
600
601 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400602 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
603 if (*fieldp == NULL)
604 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400605 if (*tmp == '[') { /* Array */
606 str = tmp;
607 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400608 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400609 if (*tmp != ']' || tmp == str + 1) {
610 semantic_error("Array index must be a"
611 " number.\n");
612 return -EINVAL;
613 }
614 tmp++;
615 if (*tmp == '\0')
616 tmp = NULL;
617 } else { /* Structure */
618 if (*tmp == '.') {
619 str = tmp + 1;
620 (*fieldp)->ref = false;
621 } else if (tmp[1] == '>') {
622 str = tmp + 2;
623 (*fieldp)->ref = true;
624 } else {
625 semantic_error("Argument parse error: %s\n",
626 str);
627 return -EINVAL;
628 }
629 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400631 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400632 (*fieldp)->name = strndup(str, tmp - str);
633 if ((*fieldp)->name == NULL)
634 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400635 if (*str != '[')
636 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400637 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
638 fieldp = &(*fieldp)->next;
639 }
640 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400641 (*fieldp)->name = strdup(str);
642 if ((*fieldp)->name == NULL)
643 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400644 if (*str != '[')
645 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400646 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400647
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400648 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400649 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400650 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400651 if (arg->name == NULL)
652 return -ENOMEM;
653 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400654 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400655}
656
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400657/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400658int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500659{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500660 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500662
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400663 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400664 if (!argv) {
665 pr_debug("Failed to split arguments.\n");
666 return -ENOMEM;
667 }
668 if (argc - 1 > MAX_PROBE_ARGS) {
669 semantic_error("Too many probe arguments (%d).\n", argc - 1);
670 ret = -ERANGE;
671 goto out;
672 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500673 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400674 ret = parse_perf_probe_point(argv[0], pev);
675 if (ret < 0)
676 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500677
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500678 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400679 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400680 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
681 if (pev->args == NULL) {
682 ret = -ENOMEM;
683 goto out;
684 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400685 for (i = 0; i < pev->nargs && ret >= 0; i++) {
686 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
687 if (ret >= 0 &&
688 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400689 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400690 " kretprobe.\n");
691 ret = -EINVAL;
692 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500693 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400694out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500695 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400696
697 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500698}
699
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400700/* Return true if this perf_probe_event requires debuginfo */
701bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500702{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400703 int i;
704
705 if (pev->point.file || pev->point.line || pev->point.lazy_line)
706 return true;
707
708 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400709 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400710 return true;
711
712 return false;
713}
714
715/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400716int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400717{
718 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500719 char pr;
720 char *p;
721 int ret, i, argc;
722 char **argv;
723
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400724 pr_debug("Parsing kprobe_events: %s\n", cmd);
725 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400726 if (!argv) {
727 pr_debug("Failed to split arguments.\n");
728 return -ENOMEM;
729 }
730 if (argc < 2) {
731 semantic_error("Too few probe arguments.\n");
732 ret = -ERANGE;
733 goto out;
734 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500735
736 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800737 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400738 &pr, (float *)(void *)&tev->group,
739 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400740 if (ret != 3) {
741 semantic_error("Failed to parse event name: %s\n", argv[0]);
742 ret = -EINVAL;
743 goto out;
744 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400745 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500746
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400747 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500748
749 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400750 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
751 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500752 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400753 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500754
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400755 tev->nargs = argc - 2;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400756 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
757 if (tev->args == NULL) {
758 ret = -ENOMEM;
759 goto out;
760 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400761 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500762 p = strchr(argv[i + 2], '=');
763 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400764 *p++ = '\0';
765 else
766 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400767 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400768 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400769 tev->args[i].value = strdup(p);
770 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
771 ret = -ENOMEM;
772 goto out;
773 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500774 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400775 ret = 0;
776out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500777 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400778 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500779}
780
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400781/* Compose only probe arg */
782int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
783{
784 struct perf_probe_arg_field *field = pa->field;
785 int ret;
786 char *tmp = buf;
787
Masami Hiramatsu48481932010-04-12 13:16:53 -0400788 if (pa->name && pa->var)
789 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
790 else
791 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400792 if (ret <= 0)
793 goto error;
794 tmp += ret;
795 len -= ret;
796
797 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400798 if (field->name[0] == '[')
799 ret = e_snprintf(tmp, len, "%s", field->name);
800 else
801 ret = e_snprintf(tmp, len, "%s%s",
802 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400803 if (ret <= 0)
804 goto error;
805 tmp += ret;
806 len -= ret;
807 field = field->next;
808 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400809
810 if (pa->type) {
811 ret = e_snprintf(tmp, len, ":%s", pa->type);
812 if (ret <= 0)
813 goto error;
814 tmp += ret;
815 len -= ret;
816 }
817
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400818 return tmp - buf;
819error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400820 pr_debug("Failed to synthesize perf probe argument: %s",
821 strerror(-ret));
822 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400823}
824
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400825/* Compose only probe point (not argument) */
826static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500827{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400828 char *buf, *tmp;
829 char offs[32] = "", line[32] = "", file[32] = "";
830 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500831
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400832 buf = zalloc(MAX_CMDLEN);
833 if (buf == NULL) {
834 ret = -ENOMEM;
835 goto error;
836 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500837 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400838 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500839 if (ret <= 0)
840 goto error;
841 }
842 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400843 ret = e_snprintf(line, 32, ":%d", pp->line);
844 if (ret <= 0)
845 goto error;
846 }
847 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400848 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400849 if (len < 0)
850 len = 0;
851 tmp = strchr(pp->file + len, '/');
852 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400853 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400854 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500855 if (ret <= 0)
856 goto error;
857 }
858
859 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400860 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
861 offs, pp->retprobe ? "%return" : "", line,
862 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500863 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400864 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500865 if (ret <= 0)
866 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500867
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400868 return buf;
869error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400870 pr_debug("Failed to synthesize perf probe point: %s",
871 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400872 if (buf)
873 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400874 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400875}
876
877#if 0
878char *synthesize_perf_probe_command(struct perf_probe_event *pev)
879{
880 char *buf;
881 int i, len, ret;
882
883 buf = synthesize_perf_probe_point(&pev->point);
884 if (!buf)
885 return NULL;
886
887 len = strlen(buf);
888 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500889 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400890 pev->args[i].name);
891 if (ret <= 0) {
892 free(buf);
893 return NULL;
894 }
895 len += ret;
896 }
897
898 return buf;
899}
900#endif
901
902static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
903 char **buf, size_t *buflen,
904 int depth)
905{
906 int ret;
907 if (ref->next) {
908 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
909 buflen, depth + 1);
910 if (depth < 0)
911 goto out;
912 }
913
914 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
915 if (ret < 0)
916 depth = ret;
917 else {
918 *buf += ret;
919 *buflen -= ret;
920 }
921out:
922 return depth;
923
924}
925
926static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
927 char *buf, size_t buflen)
928{
929 int ret, depth = 0;
930 char *tmp = buf;
931
932 /* Argument name or separator */
933 if (arg->name)
934 ret = e_snprintf(buf, buflen, " %s=", arg->name);
935 else
936 ret = e_snprintf(buf, buflen, " ");
937 if (ret < 0)
938 return ret;
939 buf += ret;
940 buflen -= ret;
941
942 /* Dereferencing arguments */
943 if (arg->ref) {
944 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
945 &buflen, 1);
946 if (depth < 0)
947 return depth;
948 }
949
950 /* Print argument value */
951 ret = e_snprintf(buf, buflen, "%s", arg->value);
952 if (ret < 0)
953 return ret;
954 buf += ret;
955 buflen -= ret;
956
957 /* Closing */
958 while (depth--) {
959 ret = e_snprintf(buf, buflen, ")");
960 if (ret < 0)
961 return ret;
962 buf += ret;
963 buflen -= ret;
964 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400965 /* Print argument type */
966 if (arg->type) {
967 ret = e_snprintf(buf, buflen, ":%s", arg->type);
968 if (ret <= 0)
969 return ret;
970 buf += ret;
971 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400972
973 return buf - tmp;
974}
975
976char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
977{
978 struct kprobe_trace_point *tp = &tev->point;
979 char *buf;
980 int i, len, ret;
981
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400982 buf = zalloc(MAX_CMDLEN);
983 if (buf == NULL)
984 return NULL;
985
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400986 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
987 tp->retprobe ? 'r' : 'p',
988 tev->group, tev->event,
989 tp->symbol, tp->offset);
990 if (len <= 0)
991 goto error;
992
993 for (i = 0; i < tev->nargs; i++) {
994 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
995 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500996 if (ret <= 0)
997 goto error;
998 len += ret;
999 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001000
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001001 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001002error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003 free(buf);
1004 return NULL;
1005}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001006
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001007int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
1008 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001009{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001010 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001011 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001012
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001013 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001014 pev->event = strdup(tev->event);
1015 pev->group = strdup(tev->group);
1016 if (pev->event == NULL || pev->group == NULL)
1017 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001018
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001019 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001020 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1021 if (ret < 0)
1022 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001023
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001024 /* Convert trace_arg to probe_arg */
1025 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001026 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1027 if (pev->args == NULL)
1028 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001029 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001030 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001031 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001032 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1034 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001035 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001036 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001037 if (pev->args[i].name == NULL && ret >= 0)
1038 ret = -ENOMEM;
1039 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001040
1041 if (ret < 0)
1042 clear_perf_probe_event(pev);
1043
1044 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001045}
1046
1047void clear_perf_probe_event(struct perf_probe_event *pev)
1048{
1049 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001050 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001051 int i;
1052
1053 if (pev->event)
1054 free(pev->event);
1055 if (pev->group)
1056 free(pev->group);
1057 if (pp->file)
1058 free(pp->file);
1059 if (pp->function)
1060 free(pp->function);
1061 if (pp->lazy_line)
1062 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001063 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001064 if (pev->args[i].name)
1065 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001066 if (pev->args[i].var)
1067 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001068 if (pev->args[i].type)
1069 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001070 field = pev->args[i].field;
1071 while (field) {
1072 next = field->next;
1073 if (field->name)
1074 free(field->name);
1075 free(field);
1076 field = next;
1077 }
1078 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001079 if (pev->args)
1080 free(pev->args);
1081 memset(pev, 0, sizeof(*pev));
1082}
1083
1084void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1085{
1086 struct kprobe_trace_arg_ref *ref, *next;
1087 int i;
1088
1089 if (tev->event)
1090 free(tev->event);
1091 if (tev->group)
1092 free(tev->group);
1093 if (tev->point.symbol)
1094 free(tev->point.symbol);
1095 for (i = 0; i < tev->nargs; i++) {
1096 if (tev->args[i].name)
1097 free(tev->args[i].name);
1098 if (tev->args[i].value)
1099 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001100 if (tev->args[i].type)
1101 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001102 ref = tev->args[i].ref;
1103 while (ref) {
1104 next = ref->next;
1105 free(ref);
1106 ref = next;
1107 }
1108 }
1109 if (tev->args)
1110 free(tev->args);
1111 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001112}
1113
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001114static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001115{
1116 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001117 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001118 int ret;
1119
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001120 __debugfs = debugfs_find_mountpoint();
1121 if (__debugfs == NULL) {
1122 pr_warning("Debugfs is not mounted.\n");
1123 return -ENOENT;
1124 }
1125
1126 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001127 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001128 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001129 if (readwrite && !probe_event_dry_run)
1130 ret = open(buf, O_RDWR, O_APPEND);
1131 else
1132 ret = open(buf, O_RDONLY, 0);
1133 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001134
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001135 if (ret < 0) {
1136 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001137 pr_warning("kprobe_events file does not exist - please"
1138 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001139 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001140 pr_warning("Failed to open kprobe_events file: %s\n",
1141 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001142 }
1143 return ret;
1144}
1145
1146/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001147static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001148{
1149 int ret, idx;
1150 FILE *fp;
1151 char buf[MAX_CMDLEN];
1152 char *p;
1153 struct strlist *sl;
1154
1155 sl = strlist__new(true, NULL);
1156
1157 fp = fdopen(dup(fd), "r");
1158 while (!feof(fp)) {
1159 p = fgets(buf, MAX_CMDLEN, fp);
1160 if (!p)
1161 break;
1162
1163 idx = strlen(p) - 1;
1164 if (p[idx] == '\n')
1165 p[idx] = '\0';
1166 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001167 if (ret < 0) {
1168 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1169 strlist__delete(sl);
1170 return NULL;
1171 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001172 }
1173 fclose(fp);
1174
1175 return sl;
1176}
1177
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001178/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001179static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001180{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001181 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001182 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001184
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001185 /* Synthesize only event probe point */
1186 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 if (!place)
1188 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001189
1190 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001191 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 return ret;
1193
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001194 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001195
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001196 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001197 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001198 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199 ret = synthesize_perf_probe_arg(&pev->args[i],
1200 buf, 128);
1201 if (ret < 0)
1202 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001203 printf(" %s", buf);
1204 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001205 }
1206 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001208 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001209}
1210
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001211/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001213{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001215 struct kprobe_trace_event tev;
1216 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001217 struct strlist *rawlist;
1218 struct str_node *ent;
1219
Masami Hiramatsu72041332010-01-05 17:47:10 -05001220 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001221 ret = init_vmlinux();
1222 if (ret < 0)
1223 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001224
1225 memset(&tev, 0, sizeof(tev));
1226 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001227
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001228 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 if (fd < 0)
1230 return fd;
1231
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001232 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001233 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 if (!rawlist)
1235 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001236
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001237 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001238 ret = parse_kprobe_trace_command(ent->s, &tev);
1239 if (ret >= 0) {
1240 ret = convert_to_perf_probe_event(&tev, &pev);
1241 if (ret >= 0)
1242 ret = show_perf_probe_event(&pev);
1243 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 clear_perf_probe_event(&pev);
1245 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001246 if (ret < 0)
1247 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001248 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001249 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250
1251 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001252}
1253
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001254/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001255static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001256{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001257 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001258 struct strlist *sl, *rawlist;
1259 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001260 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001262
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001263 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001264
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001266 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001267 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001268 ret = parse_kprobe_trace_command(ent->s, &tev);
1269 if (ret < 0)
1270 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001271 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001272 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1273 tev.event);
1274 if (ret >= 0)
1275 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001276 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001279 if (ret < 0)
1280 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001281 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001282 strlist__delete(rawlist);
1283
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001284 if (ret < 0) {
1285 strlist__delete(sl);
1286 return NULL;
1287 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001288 return sl;
1289}
1290
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001292{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001293 int ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001294 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001295
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 if (!buf) {
1297 pr_debug("Failed to synthesize kprobe trace event.\n");
1298 return -EINVAL;
1299 }
1300
Masami Hiramatsufa282442009-12-08 17:03:23 -05001301 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001302 if (!probe_event_dry_run) {
1303 ret = write(fd, buf, strlen(buf));
1304 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001305 pr_warning("Failed to write event: %s\n",
1306 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001307 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001308 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001309 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001310}
1311
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312static int get_new_event_name(char *buf, size_t len, const char *base,
1313 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001314{
1315 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001316
1317 /* Try no suffix */
1318 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001319 if (ret < 0) {
1320 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1321 return ret;
1322 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001323 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001324 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001325
Masami Hiramatsud761b082009-12-15 10:32:25 -05001326 if (!allow_suffix) {
1327 pr_warning("Error: event \"%s\" already exists. "
1328 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001329 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001330 }
1331
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001332 /* Try to add suffix */
1333 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001334 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001335 if (ret < 0) {
1336 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1337 return ret;
1338 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001339 if (!strlist__has_entry(namelist, buf))
1340 break;
1341 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001342 if (i == MAX_EVENT_INDEX) {
1343 pr_warning("Too many events are on the same function.\n");
1344 ret = -ERANGE;
1345 }
1346
1347 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001348}
1349
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001350static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1351 struct kprobe_trace_event *tevs,
1352 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001353{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001354 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001355 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001356 char buf[64];
1357 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001358 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001359
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001360 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001361 if (fd < 0)
1362 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001363 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001364 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365 if (!namelist) {
1366 pr_debug("Failed to get current event list.\n");
1367 return -EIO;
1368 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001369
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001371 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001372 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373 tev = &tevs[i];
1374 if (pev->event)
1375 event = pev->event;
1376 else
1377 if (pev->point.function)
1378 event = pev->point.function;
1379 else
1380 event = tev->point.symbol;
1381 if (pev->group)
1382 group = pev->group;
1383 else
1384 group = PERFPROBE_GROUP;
1385
1386 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 ret = get_new_event_name(buf, 64, event,
1388 namelist, allow_suffix);
1389 if (ret < 0)
1390 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391 event = buf;
1392
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001393 tev->event = strdup(event);
1394 tev->group = strdup(group);
1395 if (tev->event == NULL || tev->group == NULL) {
1396 ret = -ENOMEM;
1397 break;
1398 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399 ret = write_kprobe_trace_event(fd, tev);
1400 if (ret < 0)
1401 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001402 /* Add added event name to namelist */
1403 strlist__add(namelist, event);
1404
1405 /* Trick here - save current event/group */
1406 event = pev->event;
1407 group = pev->group;
1408 pev->event = tev->event;
1409 pev->group = tev->group;
1410 show_perf_probe_event(pev);
1411 /* Trick here - restore current event/group */
1412 pev->event = (char *)event;
1413 pev->group = (char *)group;
1414
1415 /*
1416 * Probes after the first probe which comes from same
1417 * user input are always allowed to add suffix, because
1418 * there might be several addresses corresponding to
1419 * one code line.
1420 */
1421 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001422 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001423
1424 if (ret >= 0) {
1425 /* Show how to use the event. */
1426 printf("\nYou can now use it on all perf tools, such as:\n\n");
1427 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1428 tev->event);
1429 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001430
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001431 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001432 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001434}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001435
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001436static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001437 struct kprobe_trace_event **tevs,
1438 int max_tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001439{
1440 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001441 int ret = 0, i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001442 struct kprobe_trace_event *tev;
1443
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001444 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001445 ret = try_to_find_kprobe_trace_events(pev, tevs, max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001446 if (ret != 0)
1447 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001448
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001449 /* Allocate trace event buffer */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001450 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1451 if (tev == NULL)
1452 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001453
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001454 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001455 tev->point.symbol = strdup(pev->point.function);
1456 if (tev->point.symbol == NULL) {
1457 ret = -ENOMEM;
1458 goto error;
1459 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001460 tev->point.offset = pev->point.offset;
1461 tev->nargs = pev->nargs;
1462 if (tev->nargs) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001463 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1464 * tev->nargs);
1465 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001466 ret = -ENOMEM;
1467 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001468 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001469 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001470 if (pev->args[i].name) {
1471 tev->args[i].name = strdup(pev->args[i].name);
1472 if (tev->args[i].name == NULL) {
1473 ret = -ENOMEM;
1474 goto error;
1475 }
1476 }
1477 tev->args[i].value = strdup(pev->args[i].var);
1478 if (tev->args[i].value == NULL) {
1479 ret = -ENOMEM;
1480 goto error;
1481 }
1482 if (pev->args[i].type) {
1483 tev->args[i].type = strdup(pev->args[i].type);
1484 if (tev->args[i].type == NULL) {
1485 ret = -ENOMEM;
1486 goto error;
1487 }
1488 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001489 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001490 }
1491
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001492 /* Currently just checking function name from symbol map */
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001493 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001494 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001495 if (!sym) {
1496 pr_warning("Kernel symbol \'%s\' not found.\n",
1497 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001498 ret = -ENOENT;
1499 goto error;
1500 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001501
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001502 return 1;
1503error:
1504 clear_kprobe_trace_event(tev);
1505 free(tev);
1506 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001507 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001508}
1509
1510struct __event_package {
1511 struct perf_probe_event *pev;
1512 struct kprobe_trace_event *tevs;
1513 int ntevs;
1514};
1515
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001517 bool force_add, int max_tevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001518{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001519 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 struct __event_package *pkgs;
1521
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001522 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1523 if (pkgs == NULL)
1524 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001525
1526 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 ret = init_vmlinux();
1528 if (ret < 0)
1529 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001530
1531 /* Loop 1: convert all events */
1532 for (i = 0; i < npevs; i++) {
1533 pkgs[i].pev = &pevs[i];
1534 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001536 &pkgs[i].tevs, max_tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001537 if (ret < 0)
1538 goto end;
1539 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001540 }
1541
1542 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001543 for (i = 0; i < npevs && ret >= 0; i++)
1544 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1545 pkgs[i].ntevs, force_add);
1546end:
1547 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001548 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001549 for (j = 0; j < pkgs[i].ntevs; j++)
1550 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1551
1552 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001553}
1554
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001555static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001556{
1557 char *p;
1558 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001559 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001560
1561 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001562 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1563 if (ret < 0)
1564 goto error;
1565
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001566 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001567 if (!p) {
1568 pr_debug("Internal error: %s should have ':' but not.\n",
1569 ent->s);
1570 ret = -ENOTSUP;
1571 goto error;
1572 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001573 *p = '/';
1574
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001575 pr_debug("Writing event: %s\n", buf);
1576 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001577 if (ret < 0)
1578 goto error;
1579
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001580 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001581 return 0;
1582error:
1583 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1584 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001585}
1586
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587static int del_trace_kprobe_event(int fd, const char *group,
1588 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001589{
1590 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001591 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001592 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001593
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1595 if (ret < 0) {
1596 pr_err("Failed to copy event.");
1597 return ret;
1598 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001599
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001600 if (strpbrk(buf, "*?")) { /* Glob-exp */
1601 strlist__for_each_safe(ent, n, namelist)
1602 if (strglobmatch(ent->s, buf)) {
1603 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001604 ret = __del_trace_kprobe_event(fd, ent);
1605 if (ret < 0)
1606 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001607 strlist__remove(namelist, ent);
1608 }
1609 } else {
1610 ent = strlist__find(namelist, buf);
1611 if (ent) {
1612 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001613 ret = __del_trace_kprobe_event(fd, ent);
1614 if (ret >= 0)
1615 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001616 }
1617 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001618 if (found == 0 && ret >= 0)
1619 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1620
1621 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001622}
1623
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001624int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001625{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001627 const char *group, *event;
1628 char *p, *str;
1629 struct str_node *ent;
1630 struct strlist *namelist;
1631
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001632 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001633 if (fd < 0)
1634 return fd;
1635
Masami Hiramatsufa282442009-12-08 17:03:23 -05001636 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001637 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001638 if (namelist == NULL)
1639 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001640
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001641 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001642 str = strdup(ent->s);
1643 if (str == NULL) {
1644 ret = -ENOMEM;
1645 break;
1646 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001647 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001648 p = strchr(str, ':');
1649 if (p) {
1650 group = str;
1651 *p = '\0';
1652 event = p + 1;
1653 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001654 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001655 event = str;
1656 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001657 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001658 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001659 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001660 if (ret < 0)
1661 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001662 }
1663 strlist__delete(namelist);
1664 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001665
1666 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001667}
1668