blob: 954ca210e4b70a501bcf2342d5da7ba014ef453f [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 Hiramatsu146a1432010-04-12 13:17:42 -0400293 while (l < lr->end && !feof(fp) && ret >= 0)
294 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 Hiramatsu631c9de2010-01-06 09:45:34 -0500344 if (*tmp == '+')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400345 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500346 else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400347 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500348 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400349 lr->end = INT_MAX;
350 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
351 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500352 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400353 " than end line.\n");
354 return -EINVAL;
355 }
356 if (*tmp != '\0') {
357 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500358 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400359 return -EINVAL;
360 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400361 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400362 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400363 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400364 lr->end = INT_MAX;
365 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400366
367 if (tmp == NULL)
368 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500369
370 if (strchr(tmp, '.'))
371 lr->file = tmp;
372 else
373 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400374
375 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500376}
377
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500378/* Check the name is good for event/group */
379static bool check_event_name(const char *name)
380{
381 if (!isalpha(*name) && *name != '_')
382 return false;
383 while (*++name != '\0') {
384 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
385 return false;
386 }
387 return true;
388}
389
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500390/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400391static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500392{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400393 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500394 char *ptr, *tmp;
395 char c, nc = 0;
396 /*
397 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500398 * perf probe [EVENT=]SRC[:LN|;PTN]
399 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500400 *
401 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500402 */
403
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500404 ptr = strpbrk(arg, ";=@+%");
405 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500406 *ptr = '\0';
407 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400408 if (strchr(arg, ':')) {
409 semantic_error("Group name is not supported yet.\n");
410 return -ENOTSUP;
411 }
412 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500413 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400414 "follow C symbol-naming rule.\n", arg);
415 return -EINVAL;
416 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400417 pev->event = strdup(arg);
418 if (pev->event == NULL)
419 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400420 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500421 arg = tmp;
422 }
423
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500424 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500425 if (ptr) {
426 nc = *ptr;
427 *ptr++ = '\0';
428 }
429
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400430 tmp = strdup(arg);
431 if (tmp == NULL)
432 return -ENOMEM;
433
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500434 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400435 if (strchr(tmp, '.')) /* File */
436 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500437 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400438 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500439
440 /* Parse other options */
441 while (ptr) {
442 arg = ptr;
443 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500444 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400445 pp->lazy_line = strdup(arg);
446 if (pp->lazy_line == NULL)
447 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500448 break;
449 }
450 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500451 if (ptr) {
452 nc = *ptr;
453 *ptr++ = '\0';
454 }
455 switch (c) {
456 case ':': /* Line number */
457 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400458 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500459 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400460 " in line number.\n");
461 return -EINVAL;
462 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500463 break;
464 case '+': /* Byte offset from a symbol */
465 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400466 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500467 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400468 " in offset.\n");
469 return -EINVAL;
470 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500471 break;
472 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400473 if (pp->file) {
474 semantic_error("SRC@SRC is not allowed.\n");
475 return -EINVAL;
476 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400477 pp->file = strdup(arg);
478 if (pp->file == NULL)
479 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500480 break;
481 case '%': /* Probe places */
482 if (strcmp(arg, "return") == 0) {
483 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400484 } else { /* Others not supported yet */
485 semantic_error("%%%s is not supported.\n", arg);
486 return -ENOTSUP;
487 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500488 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400489 default: /* Buggy case */
490 pr_err("This program has a bug at %s:%d.\n",
491 __FILE__, __LINE__);
492 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500493 break;
494 }
495 }
496
497 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400498 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500499 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400500 return -EINVAL;
501 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500502
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400503 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500504 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400505 return -EINVAL;
506 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500507
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400508 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500509 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400510 return -EINVAL;
511 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500512
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400513 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500514 semantic_error("File always requires line number or "
515 "lazy pattern.");
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->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500520 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400521 return -EINVAL;
522 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500523
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400524 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500525 semantic_error("Return probe requires an entry function.");
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->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500530 semantic_error("Offset/Line/Lazy pattern can't be used with "
531 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400532 return -EINVAL;
533 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500534
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400535 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500536 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
537 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400538 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500539}
540
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400541/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400542static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400543{
Masami Hiramatsu48481932010-04-12 13:16:53 -0400544 char *tmp;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400545 struct perf_probe_arg_field **fieldp;
546
547 pr_debug("parsing arg: %s into ", str);
548
Masami Hiramatsu48481932010-04-12 13:16:53 -0400549 tmp = strchr(str, '=');
550 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400551 arg->name = strndup(str, tmp - str);
552 if (arg->name == NULL)
553 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400554 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400555 str = tmp + 1;
556 }
557
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400558 tmp = strchr(str, ':');
559 if (tmp) { /* Type setting */
560 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400561 arg->type = strdup(tmp + 1);
562 if (arg->type == NULL)
563 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400564 pr_debug("type:%s ", arg->type);
565 }
566
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400567 tmp = strpbrk(str, "-.");
568 if (!is_c_varname(str) || !tmp) {
569 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400570 arg->var = strdup(str);
571 if (arg->var == NULL)
572 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400573 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400574 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400575 }
576
577 /* Structure fields */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400578 arg->var = strndup(str, tmp - str);
579 if (arg->var == NULL)
580 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400581 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400582 fieldp = &arg->field;
583
584 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400585 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
586 if (*fieldp == NULL)
587 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400588 if (*tmp == '.') {
589 str = tmp + 1;
590 (*fieldp)->ref = false;
591 } else if (tmp[1] == '>') {
592 str = tmp + 2;
593 (*fieldp)->ref = true;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400594 } else {
595 semantic_error("Argument parse error: %s\n", str);
596 return -EINVAL;
597 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400598
599 tmp = strpbrk(str, "-.");
600 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400601 (*fieldp)->name = strndup(str, tmp - str);
602 if ((*fieldp)->name == NULL)
603 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400604 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
605 fieldp = &(*fieldp)->next;
606 }
607 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400608 (*fieldp)->name = strdup(str);
609 if ((*fieldp)->name == NULL)
610 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400611 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400612
613 /* If no name is specified, set the last field name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400614 if (!arg->name) {
615 arg->name = strdup((*fieldp)->name);
616 if (arg->name == NULL)
617 return -ENOMEM;
618 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400619 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400620}
621
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400622/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400623int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500624{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500625 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400626 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500627
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400628 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400629 if (!argv) {
630 pr_debug("Failed to split arguments.\n");
631 return -ENOMEM;
632 }
633 if (argc - 1 > MAX_PROBE_ARGS) {
634 semantic_error("Too many probe arguments (%d).\n", argc - 1);
635 ret = -ERANGE;
636 goto out;
637 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500638 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400639 ret = parse_perf_probe_point(argv[0], pev);
640 if (ret < 0)
641 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500642
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500643 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400644 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400645 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
646 if (pev->args == NULL) {
647 ret = -ENOMEM;
648 goto out;
649 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400650 for (i = 0; i < pev->nargs && ret >= 0; i++) {
651 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
652 if (ret >= 0 &&
653 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400654 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400655 " kretprobe.\n");
656 ret = -EINVAL;
657 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500658 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400659out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500660 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661
662 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500663}
664
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400665/* Return true if this perf_probe_event requires debuginfo */
666bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500667{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400668 int i;
669
670 if (pev->point.file || pev->point.line || pev->point.lazy_line)
671 return true;
672
673 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400674 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400675 return true;
676
677 return false;
678}
679
680/* Parse kprobe_events event into struct probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400681int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400682{
683 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500684 char pr;
685 char *p;
686 int ret, i, argc;
687 char **argv;
688
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400689 pr_debug("Parsing kprobe_events: %s\n", cmd);
690 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400691 if (!argv) {
692 pr_debug("Failed to split arguments.\n");
693 return -ENOMEM;
694 }
695 if (argc < 2) {
696 semantic_error("Too few probe arguments.\n");
697 ret = -ERANGE;
698 goto out;
699 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500700
701 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800702 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400703 &pr, (float *)(void *)&tev->group,
704 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400705 if (ret != 3) {
706 semantic_error("Failed to parse event name: %s\n", argv[0]);
707 ret = -EINVAL;
708 goto out;
709 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400710 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500711
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400712 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500713
714 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400715 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
716 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500717 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400718 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500719
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400720 tev->nargs = argc - 2;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400721 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
722 if (tev->args == NULL) {
723 ret = -ENOMEM;
724 goto out;
725 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400726 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500727 p = strchr(argv[i + 2], '=');
728 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729 *p++ = '\0';
730 else
731 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400732 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400733 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400734 tev->args[i].value = strdup(p);
735 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
736 ret = -ENOMEM;
737 goto out;
738 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500739 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400740 ret = 0;
741out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500742 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400743 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500744}
745
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400746/* Compose only probe arg */
747int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
748{
749 struct perf_probe_arg_field *field = pa->field;
750 int ret;
751 char *tmp = buf;
752
Masami Hiramatsu48481932010-04-12 13:16:53 -0400753 if (pa->name && pa->var)
754 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
755 else
756 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400757 if (ret <= 0)
758 goto error;
759 tmp += ret;
760 len -= ret;
761
762 while (field) {
763 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
764 field->name);
765 if (ret <= 0)
766 goto error;
767 tmp += ret;
768 len -= ret;
769 field = field->next;
770 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400771
772 if (pa->type) {
773 ret = e_snprintf(tmp, len, ":%s", pa->type);
774 if (ret <= 0)
775 goto error;
776 tmp += ret;
777 len -= ret;
778 }
779
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400780 return tmp - buf;
781error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400782 pr_debug("Failed to synthesize perf probe argument: %s",
783 strerror(-ret));
784 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400785}
786
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400787/* Compose only probe point (not argument) */
788static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500789{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400790 char *buf, *tmp;
791 char offs[32] = "", line[32] = "", file[32] = "";
792 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500793
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400794 buf = zalloc(MAX_CMDLEN);
795 if (buf == NULL) {
796 ret = -ENOMEM;
797 goto error;
798 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500799 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400800 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500801 if (ret <= 0)
802 goto error;
803 }
804 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400805 ret = e_snprintf(line, 32, ":%d", pp->line);
806 if (ret <= 0)
807 goto error;
808 }
809 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400810 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400811 if (len < 0)
812 len = 0;
813 tmp = strchr(pp->file + len, '/');
814 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400815 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400816 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500817 if (ret <= 0)
818 goto error;
819 }
820
821 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400822 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
823 offs, pp->retprobe ? "%return" : "", line,
824 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500825 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400826 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500827 if (ret <= 0)
828 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500829
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400830 return buf;
831error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400832 pr_debug("Failed to synthesize perf probe point: %s",
833 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400834 if (buf)
835 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400836 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837}
838
839#if 0
840char *synthesize_perf_probe_command(struct perf_probe_event *pev)
841{
842 char *buf;
843 int i, len, ret;
844
845 buf = synthesize_perf_probe_point(&pev->point);
846 if (!buf)
847 return NULL;
848
849 len = strlen(buf);
850 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500851 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400852 pev->args[i].name);
853 if (ret <= 0) {
854 free(buf);
855 return NULL;
856 }
857 len += ret;
858 }
859
860 return buf;
861}
862#endif
863
864static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
865 char **buf, size_t *buflen,
866 int depth)
867{
868 int ret;
869 if (ref->next) {
870 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
871 buflen, depth + 1);
872 if (depth < 0)
873 goto out;
874 }
875
876 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
877 if (ret < 0)
878 depth = ret;
879 else {
880 *buf += ret;
881 *buflen -= ret;
882 }
883out:
884 return depth;
885
886}
887
888static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
889 char *buf, size_t buflen)
890{
891 int ret, depth = 0;
892 char *tmp = buf;
893
894 /* Argument name or separator */
895 if (arg->name)
896 ret = e_snprintf(buf, buflen, " %s=", arg->name);
897 else
898 ret = e_snprintf(buf, buflen, " ");
899 if (ret < 0)
900 return ret;
901 buf += ret;
902 buflen -= ret;
903
904 /* Dereferencing arguments */
905 if (arg->ref) {
906 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
907 &buflen, 1);
908 if (depth < 0)
909 return depth;
910 }
911
912 /* Print argument value */
913 ret = e_snprintf(buf, buflen, "%s", arg->value);
914 if (ret < 0)
915 return ret;
916 buf += ret;
917 buflen -= ret;
918
919 /* Closing */
920 while (depth--) {
921 ret = e_snprintf(buf, buflen, ")");
922 if (ret < 0)
923 return ret;
924 buf += ret;
925 buflen -= ret;
926 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400927 /* Print argument type */
928 if (arg->type) {
929 ret = e_snprintf(buf, buflen, ":%s", arg->type);
930 if (ret <= 0)
931 return ret;
932 buf += ret;
933 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400934
935 return buf - tmp;
936}
937
938char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
939{
940 struct kprobe_trace_point *tp = &tev->point;
941 char *buf;
942 int i, len, ret;
943
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400944 buf = zalloc(MAX_CMDLEN);
945 if (buf == NULL)
946 return NULL;
947
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400948 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
949 tp->retprobe ? 'r' : 'p',
950 tev->group, tev->event,
951 tp->symbol, tp->offset);
952 if (len <= 0)
953 goto error;
954
955 for (i = 0; i < tev->nargs; i++) {
956 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
957 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500958 if (ret <= 0)
959 goto error;
960 len += ret;
961 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500962
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400963 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500964error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400965 free(buf);
966 return NULL;
967}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500968
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400969int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
970 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400971{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400972 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300975 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400976 pev->event = strdup(tev->event);
977 pev->group = strdup(tev->group);
978 if (pev->event == NULL || pev->group == NULL)
979 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400980
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300981 /* Convert trace_point to probe_point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400982 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
983 if (ret < 0)
984 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400986 /* Convert trace_arg to probe_arg */
987 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400988 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
989 if (pev->args == NULL)
990 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400991 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400992 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400993 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400994 else {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400995 ret = synthesize_kprobe_trace_arg(&tev->args[i],
996 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400997 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400998 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400999 if (pev->args[i].name == NULL && ret >= 0)
1000 ret = -ENOMEM;
1001 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001002
1003 if (ret < 0)
1004 clear_perf_probe_event(pev);
1005
1006 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001007}
1008
1009void clear_perf_probe_event(struct perf_probe_event *pev)
1010{
1011 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001012 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001013 int i;
1014
1015 if (pev->event)
1016 free(pev->event);
1017 if (pev->group)
1018 free(pev->group);
1019 if (pp->file)
1020 free(pp->file);
1021 if (pp->function)
1022 free(pp->function);
1023 if (pp->lazy_line)
1024 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001025 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001026 if (pev->args[i].name)
1027 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001028 if (pev->args[i].var)
1029 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001030 if (pev->args[i].type)
1031 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001032 field = pev->args[i].field;
1033 while (field) {
1034 next = field->next;
1035 if (field->name)
1036 free(field->name);
1037 free(field);
1038 field = next;
1039 }
1040 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001041 if (pev->args)
1042 free(pev->args);
1043 memset(pev, 0, sizeof(*pev));
1044}
1045
1046void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1047{
1048 struct kprobe_trace_arg_ref *ref, *next;
1049 int i;
1050
1051 if (tev->event)
1052 free(tev->event);
1053 if (tev->group)
1054 free(tev->group);
1055 if (tev->point.symbol)
1056 free(tev->point.symbol);
1057 for (i = 0; i < tev->nargs; i++) {
1058 if (tev->args[i].name)
1059 free(tev->args[i].name);
1060 if (tev->args[i].value)
1061 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001062 if (tev->args[i].type)
1063 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001064 ref = tev->args[i].ref;
1065 while (ref) {
1066 next = ref->next;
1067 free(ref);
1068 ref = next;
1069 }
1070 }
1071 if (tev->args)
1072 free(tev->args);
1073 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001074}
1075
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001076static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001077{
1078 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001079 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001080 int ret;
1081
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001082 __debugfs = debugfs_find_mountpoint();
1083 if (__debugfs == NULL) {
1084 pr_warning("Debugfs is not mounted.\n");
1085 return -ENOENT;
1086 }
1087
1088 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001090 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001091 if (readwrite && !probe_event_dry_run)
1092 ret = open(buf, O_RDWR, O_APPEND);
1093 else
1094 ret = open(buf, O_RDONLY, 0);
1095 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001096
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001097 if (ret < 0) {
1098 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001099 pr_warning("kprobe_events file does not exist - please"
1100 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001101 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001102 pr_warning("Failed to open kprobe_events file: %s\n",
1103 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001104 }
1105 return ret;
1106}
1107
1108/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001109static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001110{
1111 int ret, idx;
1112 FILE *fp;
1113 char buf[MAX_CMDLEN];
1114 char *p;
1115 struct strlist *sl;
1116
1117 sl = strlist__new(true, NULL);
1118
1119 fp = fdopen(dup(fd), "r");
1120 while (!feof(fp)) {
1121 p = fgets(buf, MAX_CMDLEN, fp);
1122 if (!p)
1123 break;
1124
1125 idx = strlen(p) - 1;
1126 if (p[idx] == '\n')
1127 p[idx] = '\0';
1128 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001129 if (ret < 0) {
1130 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1131 strlist__delete(sl);
1132 return NULL;
1133 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001134 }
1135 fclose(fp);
1136
1137 return sl;
1138}
1139
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001140/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001141static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001142{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001143 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001144 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001145 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001146
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001147 /* Synthesize only event probe point */
1148 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001149 if (!place)
1150 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001151
1152 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001153 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001154 return ret;
1155
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001156 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001157
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001158 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001159 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001160 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001161 ret = synthesize_perf_probe_arg(&pev->args[i],
1162 buf, 128);
1163 if (ret < 0)
1164 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001165 printf(" %s", buf);
1166 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001167 }
1168 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001171}
1172
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001173/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001175{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001176 int fd, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001177 struct kprobe_trace_event tev;
1178 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001179 struct strlist *rawlist;
1180 struct str_node *ent;
1181
Masami Hiramatsu72041332010-01-05 17:47:10 -05001182 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001183 ret = init_vmlinux();
1184 if (ret < 0)
1185 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001186
1187 memset(&tev, 0, sizeof(tev));
1188 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001189
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001190 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191 if (fd < 0)
1192 return fd;
1193
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001194 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001195 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196 if (!rawlist)
1197 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001198
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001199 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001200 ret = parse_kprobe_trace_command(ent->s, &tev);
1201 if (ret >= 0) {
1202 ret = convert_to_perf_probe_event(&tev, &pev);
1203 if (ret >= 0)
1204 ret = show_perf_probe_event(&pev);
1205 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001206 clear_perf_probe_event(&pev);
1207 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001208 if (ret < 0)
1209 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001210 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001211 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212
1213 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001214}
1215
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001216/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001217static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001218{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001219 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001220 struct strlist *sl, *rawlist;
1221 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222 struct kprobe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001224
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001225 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001226
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001227 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001228 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001229 strlist__for_each(ent, rawlist) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 ret = parse_kprobe_trace_command(ent->s, &tev);
1231 if (ret < 0)
1232 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001233 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1235 tev.event);
1236 if (ret >= 0)
1237 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001238 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 ret = strlist__add(sl, tev.event);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001240 clear_kprobe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 if (ret < 0)
1242 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001243 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001244 strlist__delete(rawlist);
1245
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001246 if (ret < 0) {
1247 strlist__delete(sl);
1248 return NULL;
1249 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001250 return sl;
1251}
1252
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001254{
1255 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001256 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 if (!buf) {
1259 pr_debug("Failed to synthesize kprobe trace event.\n");
1260 return -EINVAL;
1261 }
1262
Masami Hiramatsufa282442009-12-08 17:03:23 -05001263 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001264 if (!probe_event_dry_run) {
1265 ret = write(fd, buf, strlen(buf));
1266 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001267 pr_warning("Failed to write event: %s\n",
1268 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001269 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001270 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001271 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001272}
1273
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001274static int get_new_event_name(char *buf, size_t len, const char *base,
1275 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001276{
1277 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001278
1279 /* Try no suffix */
1280 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001281 if (ret < 0) {
1282 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1283 return ret;
1284 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001285 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001286 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001287
Masami Hiramatsud761b082009-12-15 10:32:25 -05001288 if (!allow_suffix) {
1289 pr_warning("Error: event \"%s\" already exists. "
1290 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001292 }
1293
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001294 /* Try to add suffix */
1295 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001296 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001297 if (ret < 0) {
1298 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1299 return ret;
1300 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001301 if (!strlist__has_entry(namelist, buf))
1302 break;
1303 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001304 if (i == MAX_EVENT_INDEX) {
1305 pr_warning("Too many events are on the same function.\n");
1306 ret = -ERANGE;
1307 }
1308
1309 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001310}
1311
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1313 struct kprobe_trace_event *tevs,
1314 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001315{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001316 int i, fd, ret;
Ingo Molnar55632772010-03-18 16:51:16 +01001317 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001318 char buf[64];
1319 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001320 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001321
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001322 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001323 if (fd < 0)
1324 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001325 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 if (!namelist) {
1328 pr_debug("Failed to get current event list.\n");
1329 return -EIO;
1330 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001331
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001333 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001334 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001335 tev = &tevs[i];
1336 if (pev->event)
1337 event = pev->event;
1338 else
1339 if (pev->point.function)
1340 event = pev->point.function;
1341 else
1342 event = tev->point.symbol;
1343 if (pev->group)
1344 group = pev->group;
1345 else
1346 group = PERFPROBE_GROUP;
1347
1348 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001349 ret = get_new_event_name(buf, 64, event,
1350 namelist, allow_suffix);
1351 if (ret < 0)
1352 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 event = buf;
1354
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001355 tev->event = strdup(event);
1356 tev->group = strdup(group);
1357 if (tev->event == NULL || tev->group == NULL) {
1358 ret = -ENOMEM;
1359 break;
1360 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001361 ret = write_kprobe_trace_event(fd, tev);
1362 if (ret < 0)
1363 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001364 /* Add added event name to namelist */
1365 strlist__add(namelist, event);
1366
1367 /* Trick here - save current event/group */
1368 event = pev->event;
1369 group = pev->group;
1370 pev->event = tev->event;
1371 pev->group = tev->group;
1372 show_perf_probe_event(pev);
1373 /* Trick here - restore current event/group */
1374 pev->event = (char *)event;
1375 pev->group = (char *)group;
1376
1377 /*
1378 * Probes after the first probe which comes from same
1379 * user input are always allowed to add suffix, because
1380 * there might be several addresses corresponding to
1381 * one code line.
1382 */
1383 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001384 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001385
1386 if (ret >= 0) {
1387 /* Show how to use the event. */
1388 printf("\nYou can now use it on all perf tools, such as:\n\n");
1389 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1390 tev->event);
1391 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001392
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001393 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001396}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001397
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1399 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001400{
1401 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001402 int ret = 0, i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001403 struct kprobe_trace_event *tev;
1404
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001405 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001406 ret = try_to_find_kprobe_trace_events(pev, tevs);
1407 if (ret != 0)
1408 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001409
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001410 /* Allocate trace event buffer */
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001411 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1412 if (tev == NULL)
1413 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001414
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001415 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001416 tev->point.symbol = strdup(pev->point.function);
1417 if (tev->point.symbol == NULL) {
1418 ret = -ENOMEM;
1419 goto error;
1420 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421 tev->point.offset = pev->point.offset;
1422 tev->nargs = pev->nargs;
1423 if (tev->nargs) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001424 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1425 * tev->nargs);
1426 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001427 ret = -ENOMEM;
1428 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001429 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001430 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001431 if (pev->args[i].name) {
1432 tev->args[i].name = strdup(pev->args[i].name);
1433 if (tev->args[i].name == NULL) {
1434 ret = -ENOMEM;
1435 goto error;
1436 }
1437 }
1438 tev->args[i].value = strdup(pev->args[i].var);
1439 if (tev->args[i].value == NULL) {
1440 ret = -ENOMEM;
1441 goto error;
1442 }
1443 if (pev->args[i].type) {
1444 tev->args[i].type = strdup(pev->args[i].type);
1445 if (tev->args[i].type == NULL) {
1446 ret = -ENOMEM;
1447 goto error;
1448 }
1449 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001450 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001451 }
1452
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 /* Currently just checking function name from symbol map */
1454 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1455 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (!sym) {
1457 pr_warning("Kernel symbol \'%s\' not found.\n",
1458 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001459 ret = -ENOENT;
1460 goto error;
1461 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001462
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001463 return 1;
1464error:
1465 clear_kprobe_trace_event(tev);
1466 free(tev);
1467 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001468 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001469}
1470
1471struct __event_package {
1472 struct perf_probe_event *pev;
1473 struct kprobe_trace_event *tevs;
1474 int ntevs;
1475};
1476
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001477int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1478 bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001479{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 struct __event_package *pkgs;
1482
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001483 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1484 if (pkgs == NULL)
1485 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001486
1487 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488 ret = init_vmlinux();
1489 if (ret < 0)
1490 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001491
1492 /* Loop 1: convert all events */
1493 for (i = 0; i < npevs; i++) {
1494 pkgs[i].pev = &pevs[i];
1495 /* Convert with or without debuginfo */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001496 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1497 &pkgs[i].tevs);
1498 if (ret < 0)
1499 goto end;
1500 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001501 }
1502
1503 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001504 for (i = 0; i < npevs && ret >= 0; i++)
1505 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1506 pkgs[i].ntevs, force_add);
1507end:
1508 /* Loop 3: cleanup trace events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509 for (i = 0; i < npevs; i++)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 for (j = 0; j < pkgs[i].ntevs; j++)
1511 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1512
1513 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001514}
1515
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516static int __del_trace_kprobe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001517{
1518 char *p;
1519 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001520 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001521
1522 /* Convert from perf-probe event to trace-kprobe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1524 if (ret < 0)
1525 goto error;
1526
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001527 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 if (!p) {
1529 pr_debug("Internal error: %s should have ':' but not.\n",
1530 ent->s);
1531 ret = -ENOTSUP;
1532 goto error;
1533 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001534 *p = '/';
1535
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001536 pr_debug("Writing event: %s\n", buf);
1537 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001538 if (ret < 0)
1539 goto error;
1540
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001541 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001542 return 0;
1543error:
1544 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1545 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001546}
1547
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548static int del_trace_kprobe_event(int fd, const char *group,
1549 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001550{
1551 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001552 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001553 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001554
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001555 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1556 if (ret < 0) {
1557 pr_err("Failed to copy event.");
1558 return ret;
1559 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001560
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001561 if (strpbrk(buf, "*?")) { /* Glob-exp */
1562 strlist__for_each_safe(ent, n, namelist)
1563 if (strglobmatch(ent->s, buf)) {
1564 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001565 ret = __del_trace_kprobe_event(fd, ent);
1566 if (ret < 0)
1567 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001568 strlist__remove(namelist, ent);
1569 }
1570 } else {
1571 ent = strlist__find(namelist, buf);
1572 if (ent) {
1573 found++;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 ret = __del_trace_kprobe_event(fd, ent);
1575 if (ret >= 0)
1576 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001577 }
1578 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001579 if (found == 0 && ret >= 0)
1580 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1581
1582 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001583}
1584
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001585int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001586{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001588 const char *group, *event;
1589 char *p, *str;
1590 struct str_node *ent;
1591 struct strlist *namelist;
1592
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001593 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594 if (fd < 0)
1595 return fd;
1596
Masami Hiramatsufa282442009-12-08 17:03:23 -05001597 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001599 if (namelist == NULL)
1600 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001601
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001602 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001603 str = strdup(ent->s);
1604 if (str == NULL) {
1605 ret = -ENOMEM;
1606 break;
1607 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001608 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001609 p = strchr(str, ':');
1610 if (p) {
1611 group = str;
1612 *p = '\0';
1613 event = p + 1;
1614 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001615 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001616 event = str;
1617 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001618 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001619 ret = del_trace_kprobe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001620 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001621 if (ret < 0)
1622 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001623 }
1624 strlist__delete(namelist);
1625 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626
1627 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001628}
1629