blob: 3b6a5297bf16cd5a318273bc0a9bf198734a0cfe [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
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 Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
83 if (symbol_conf.vmlinux_name == NULL)
84 symbol_conf.try_vmlinux_path = true;
85 else
86 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040087 ret = symbol__init();
88 if (ret < 0) {
89 pr_debug("Failed to init symbol map.\n");
90 goto out;
91 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040092
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090093 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094 if (ret < 0)
95 goto out;
96
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 if (machine__create_kernel_maps(&machine) < 0) {
98 pr_debug("machine__create_kernel_maps ");
99 goto out;
100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900107static struct symbol *__find_kernel_function_by_name(const char *name,
108 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400109{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110 return machine__find_kernel_function_by_name(&machine, name, mapp,
111 NULL);
112}
113
114const char *kernel_get_module_path(const char *module)
115{
116 struct dso *dso;
117
118 if (module) {
119 list_for_each_entry(dso, &machine.kernel_dsos, node) {
120 if (strncmp(dso->short_name + 1, module,
121 dso->short_name_len - 2) == 0)
122 goto found;
123 }
124 pr_debug("Failed to find module %s.\n", module);
125 return NULL;
126 } else {
127 dso = machine.vmlinux_maps[MAP__FUNCTION]->dso;
128 if (dso__load_vmlinux_path(dso,
129 machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
130 pr_debug("Failed to load kernel map.\n");
131 return NULL;
132 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400133 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900134found:
135 return dso->long_name;
136}
137
138#ifdef DWARF_SUPPORT
139static int open_vmlinux(const char *module)
140{
141 const char *path = kernel_get_module_path(module);
142 if (!path) {
143 pr_err("Failed to find path of %s module", module ?: "kernel");
144 return -ENOENT;
145 }
146 pr_debug("Try to open %s\n", path);
147 return open(path, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400148}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300149
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530150/*
151 * Convert trace point to probe point with debuginfo
152 * Currently only handles kprobes.
153 */
154static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900155 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300156{
157 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900158 struct map *map;
159 u64 addr;
160 int ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300161
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900162 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300163 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900164 addr = map->unmap_ip(map, sym->start + tp->offset);
165 pr_debug("try to find %s+%ld@%llx\n", tp->symbol,
166 tp->offset, addr);
167 ret = find_perf_probe_point((unsigned long)addr, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300168 }
169 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400170 pr_debug("Failed to find corresponding probes from "
171 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400172 pp->function = strdup(tp->symbol);
173 if (pp->function == NULL)
174 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300175 pp->offset = tp->offset;
176 }
177 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400178
179 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300180}
181
182/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530183static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
184 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900185 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300186{
187 bool need_dwarf = perf_probe_event_need_dwarf(pev);
188 int fd, ntevs;
189
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900190 fd = open_vmlinux(module);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300191 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400192 if (need_dwarf) {
193 pr_warning("Failed to open debuginfo file.\n");
194 return fd;
195 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300196 pr_debug("Could not open vmlinux. Try to use symbols.\n");
197 return 0;
198 }
199
200 /* Searching trace events corresponding to probe event */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530201 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300202 close(fd);
203
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400204 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530205 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300206 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400207 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300208
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400209 if (ntevs == 0) { /* No error but failed to find probe point. */
210 pr_warning("Probe point '%s' not found.\n",
211 synthesize_perf_probe_point(&pev->point));
212 return -ENOENT;
213 }
214 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400215 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
216 if (ntevs == -EBADF) {
217 pr_warning("Warning: No dwarf info found in the vmlinux - "
218 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
219 if (!need_dwarf) {
220 pr_debug("Trying to use symbols.\nn");
221 return 0;
222 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300223 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400224 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300225}
226
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900227/*
228 * Find a src file from a DWARF tag path. Prepend optional source path prefix
229 * and chop off leading directories that do not exist. Result is passed back as
230 * a newly allocated path on success.
231 * Return 0 if file was found and readable, -errno otherwise.
232 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900233static int get_real_path(const char *raw_path, const char *comp_dir,
234 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900235{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900236 const char *prefix = symbol_conf.source_prefix;
237
238 if (!prefix) {
239 if (raw_path[0] != '/' && comp_dir)
240 /* If not an absolute path, try to use comp_dir */
241 prefix = comp_dir;
242 else {
243 if (access(raw_path, R_OK) == 0) {
244 *new_path = strdup(raw_path);
245 return 0;
246 } else
247 return -errno;
248 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900249 }
250
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900251 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900252 if (!*new_path)
253 return -ENOMEM;
254
255 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900256 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900257
258 if (access(*new_path, R_OK) == 0)
259 return 0;
260
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900261 if (!symbol_conf.source_prefix)
262 /* In case of searching comp_dir, don't retry */
263 return -errno;
264
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900265 switch (errno) {
266 case ENAMETOOLONG:
267 case ENOENT:
268 case EROFS:
269 case EFAULT:
270 raw_path = strchr(++raw_path, '/');
271 if (!raw_path) {
272 free(*new_path);
273 *new_path = NULL;
274 return -ENOENT;
275 }
276 continue;
277
278 default:
279 free(*new_path);
280 *new_path = NULL;
281 return -errno;
282 }
283 }
284}
285
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300286#define LINEBUF_SIZE 256
287#define NR_ADDITIONAL_LINES 2
288
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400289static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300290{
291 char buf[LINEBUF_SIZE];
292 const char *color = PERF_COLOR_BLUE;
293
294 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
295 goto error;
296 if (!skip) {
297 if (show_num)
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400298 fprintf(stdout, "%7d %s", l, buf);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300299 else
300 color_fprintf(stdout, color, " %s", buf);
301 }
302
303 while (strlen(buf) == LINEBUF_SIZE - 1 &&
304 buf[LINEBUF_SIZE - 2] != '\n') {
305 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
306 goto error;
307 if (!skip) {
308 if (show_num)
309 fprintf(stdout, "%s", buf);
310 else
311 color_fprintf(stdout, color, "%s", buf);
312 }
313 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400314
315 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300316error:
317 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400318 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300319 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400320 pr_warning("File read error: %s\n", strerror(errno));
321
322 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300323}
324
325/*
326 * Show line-range always requires debuginfo to find source file and
327 * line number.
328 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900329int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300330{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400331 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300332 struct line_node *ln;
333 FILE *fp;
334 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900335 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300336
337 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400338 ret = init_vmlinux();
339 if (ret < 0)
340 return ret;
341
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900342 fd = open_vmlinux(module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400343 if (fd < 0) {
344 pr_warning("Failed to open debuginfo file.\n");
345 return fd;
346 }
347
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300348 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300349 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400350 if (ret == 0) {
351 pr_warning("Specified source line is not found.\n");
352 return -ENOENT;
353 } else if (ret < 0) {
354 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
355 return ret;
356 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300357
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900358 /* Convert source file path */
359 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900360 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900361 free(tmp); /* Free old path */
362 if (ret < 0) {
363 pr_warning("Failed to find source file. (%d)\n", ret);
364 return ret;
365 }
366
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300367 setup_pager();
368
369 if (lr->function)
370 fprintf(stdout, "<%s:%d>\n", lr->function,
371 lr->start - lr->offset);
372 else
373 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
374
375 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400376 if (fp == NULL) {
377 pr_warning("Failed to open %s: %s\n", lr->path,
378 strerror(errno));
379 return -errno;
380 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300381 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400382 while (l < lr->start && ret >= 0)
383 ret = show_one_line(fp, l++, true, false);
384 if (ret < 0)
385 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300386
387 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400388 while (ln->line > l && ret >= 0)
389 ret = show_one_line(fp, (l++) - lr->offset,
390 false, false);
391 if (ret >= 0)
392 ret = show_one_line(fp, (l++) - lr->offset,
393 false, true);
394 if (ret < 0)
395 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300396 }
397
398 if (lr->end == INT_MAX)
399 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400400 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400401 ret = show_one_line(fp, (l++) - lr->offset, false, false);
402end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300403 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400404 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300405}
406
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900407static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900408 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900409{
410 char *buf;
411 int ret, i;
412 struct str_node *node;
413 struct variable_list *vls = NULL, *vl;
414
415 buf = synthesize_perf_probe_point(&pev->point);
416 if (!buf)
417 return -EINVAL;
418 pr_debug("Searching variables at %s\n", buf);
419
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900420 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900421 if (ret > 0) {
422 /* Some variables were found */
423 fprintf(stdout, "Available variables at %s\n", buf);
424 for (i = 0; i < ret; i++) {
425 vl = &vls[i];
426 /*
427 * A probe point might be converted to
428 * several trace points.
429 */
430 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
431 vl->point.offset);
432 free(vl->point.symbol);
433 if (vl->vars) {
434 strlist__for_each(node, vl->vars)
435 fprintf(stdout, "\t\t%s\n", node->s);
436 strlist__delete(vl->vars);
437 } else
438 fprintf(stdout, "(No variables)\n");
439 }
440 free(vls);
441 } else
442 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
443
444 free(buf);
445 return ret;
446}
447
448/* Show available variables on given probe point */
449int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900450 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900451{
452 int i, fd, ret = 0;
453
454 ret = init_vmlinux();
455 if (ret < 0)
456 return ret;
457
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900458 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900459 if (fd < 0) {
460 pr_warning("Failed to open debuginfo file.\n");
461 return fd;
462 }
463
464 setup_pager();
465
466 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900467 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900468
469 close(fd);
470 return ret;
471}
472
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300473#else /* !DWARF_SUPPORT */
474
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530475static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900476 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300477{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900478 struct symbol *sym;
479
480 sym = __find_kernel_function_by_name(tp->symbol, NULL);
481 if (!sym) {
482 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
483 return -ENOENT;
484 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400485 pp->function = strdup(tp->symbol);
486 if (pp->function == NULL)
487 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300488 pp->offset = tp->offset;
489 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400490
491 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300492}
493
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530494static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
495 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900496 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300497{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400498 if (perf_probe_event_need_dwarf(pev)) {
499 pr_warning("Debuginfo-analysis is not supported.\n");
500 return -ENOSYS;
501 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300502 return 0;
503}
504
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900505int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300506{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400507 pr_warning("Debuginfo-analysis is not supported.\n");
508 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300509}
510
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900511int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900512 int npevs __unused, int max_vls __unused,
513 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900514{
515 pr_warning("Debuginfo-analysis is not supported.\n");
516 return -ENOSYS;
517}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400518#endif
519
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400520int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500521{
522 const char *ptr;
523 char *tmp;
524 /*
525 * <Syntax>
526 * SRC:SLN[+NUM|-ELN]
527 * FUNC[:SLN[+NUM|-ELN]]
528 */
529 ptr = strchr(arg, ':');
530 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400531 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400532 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400533 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400534 lr->end--; /*
535 * Adjust the number of lines here.
536 * If the number of lines == 1, the
537 * the end of line should be equal to
538 * the start of line.
539 */
540 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400541 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500542 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400543 lr->end = INT_MAX;
544 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
545 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500546 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400547 " than end line.\n");
548 return -EINVAL;
549 }
550 if (*tmp != '\0') {
551 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500552 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400553 return -EINVAL;
554 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400555 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400556 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400557 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400558 lr->end = INT_MAX;
559 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400560
561 if (tmp == NULL)
562 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500563
564 if (strchr(tmp, '.'))
565 lr->file = tmp;
566 else
567 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400568
569 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500570}
571
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500572/* Check the name is good for event/group */
573static bool check_event_name(const char *name)
574{
575 if (!isalpha(*name) && *name != '_')
576 return false;
577 while (*++name != '\0') {
578 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
579 return false;
580 }
581 return true;
582}
583
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500584/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400585static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500586{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400587 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500588 char *ptr, *tmp;
589 char c, nc = 0;
590 /*
591 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500592 * perf probe [EVENT=]SRC[:LN|;PTN]
593 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500594 *
595 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500596 */
597
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500598 ptr = strpbrk(arg, ";=@+%");
599 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500600 *ptr = '\0';
601 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400602 if (strchr(arg, ':')) {
603 semantic_error("Group name is not supported yet.\n");
604 return -ENOTSUP;
605 }
606 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500607 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400608 "follow C symbol-naming rule.\n", arg);
609 return -EINVAL;
610 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400611 pev->event = strdup(arg);
612 if (pev->event == NULL)
613 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400614 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500615 arg = tmp;
616 }
617
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500618 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500619 if (ptr) {
620 nc = *ptr;
621 *ptr++ = '\0';
622 }
623
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400624 tmp = strdup(arg);
625 if (tmp == NULL)
626 return -ENOMEM;
627
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500628 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400629 if (strchr(tmp, '.')) /* File */
630 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500631 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400632 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500633
634 /* Parse other options */
635 while (ptr) {
636 arg = ptr;
637 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500638 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400639 pp->lazy_line = strdup(arg);
640 if (pp->lazy_line == NULL)
641 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500642 break;
643 }
644 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500645 if (ptr) {
646 nc = *ptr;
647 *ptr++ = '\0';
648 }
649 switch (c) {
650 case ':': /* Line number */
651 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400652 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500653 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400654 " in line number.\n");
655 return -EINVAL;
656 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500657 break;
658 case '+': /* Byte offset from a symbol */
659 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400660 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500661 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400662 " in offset.\n");
663 return -EINVAL;
664 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500665 break;
666 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400667 if (pp->file) {
668 semantic_error("SRC@SRC is not allowed.\n");
669 return -EINVAL;
670 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400671 pp->file = strdup(arg);
672 if (pp->file == NULL)
673 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500674 break;
675 case '%': /* Probe places */
676 if (strcmp(arg, "return") == 0) {
677 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400678 } else { /* Others not supported yet */
679 semantic_error("%%%s is not supported.\n", arg);
680 return -ENOTSUP;
681 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500682 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400683 default: /* Buggy case */
684 pr_err("This program has a bug at %s:%d.\n",
685 __FILE__, __LINE__);
686 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500687 break;
688 }
689 }
690
691 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400692 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500693 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400694 return -EINVAL;
695 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500696
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400697 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500698 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400699 return -EINVAL;
700 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500701
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500703 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 return -EINVAL;
705 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500706
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500708 semantic_error("File always requires line number or "
709 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400710 return -EINVAL;
711 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500712
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400713 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500714 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400715 return -EINVAL;
716 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500717
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400718 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500719 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400720 return -EINVAL;
721 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500722
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400723 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500724 semantic_error("Offset/Line/Lazy pattern can't be used with "
725 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400726 return -EINVAL;
727 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500728
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500730 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
731 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400732 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500733}
734
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400735/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400736static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400737{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400738 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400739 struct perf_probe_arg_field **fieldp;
740
741 pr_debug("parsing arg: %s into ", str);
742
Masami Hiramatsu48481932010-04-12 13:16:53 -0400743 tmp = strchr(str, '=');
744 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400745 arg->name = strndup(str, tmp - str);
746 if (arg->name == NULL)
747 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400748 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400749 str = tmp + 1;
750 }
751
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400752 tmp = strchr(str, ':');
753 if (tmp) { /* Type setting */
754 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400755 arg->type = strdup(tmp + 1);
756 if (arg->type == NULL)
757 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400758 pr_debug("type:%s ", arg->type);
759 }
760
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400761 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400762 if (!is_c_varname(str) || !tmp) {
763 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400764 arg->var = strdup(str);
765 if (arg->var == NULL)
766 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400767 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400768 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400769 }
770
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400771 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400772 arg->var = strndup(str, tmp - str);
773 if (arg->var == NULL)
774 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400775 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400776 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400777 fieldp = &arg->field;
778
779 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400780 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
781 if (*fieldp == NULL)
782 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400783 if (*tmp == '[') { /* Array */
784 str = tmp;
785 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400786 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400787 if (*tmp != ']' || tmp == str + 1) {
788 semantic_error("Array index must be a"
789 " number.\n");
790 return -EINVAL;
791 }
792 tmp++;
793 if (*tmp == '\0')
794 tmp = NULL;
795 } else { /* Structure */
796 if (*tmp == '.') {
797 str = tmp + 1;
798 (*fieldp)->ref = false;
799 } else if (tmp[1] == '>') {
800 str = tmp + 2;
801 (*fieldp)->ref = true;
802 } else {
803 semantic_error("Argument parse error: %s\n",
804 str);
805 return -EINVAL;
806 }
807 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400808 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400809 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400810 (*fieldp)->name = strndup(str, tmp - str);
811 if ((*fieldp)->name == NULL)
812 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400813 if (*str != '[')
814 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400815 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
816 fieldp = &(*fieldp)->next;
817 }
818 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400819 (*fieldp)->name = strdup(str);
820 if ((*fieldp)->name == NULL)
821 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400822 if (*str != '[')
823 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400824 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400825
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400826 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400827 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400828 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400829 if (arg->name == NULL)
830 return -ENOMEM;
831 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400832 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400833}
834
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400835/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400836int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500837{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500838 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400839 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500840
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400841 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842 if (!argv) {
843 pr_debug("Failed to split arguments.\n");
844 return -ENOMEM;
845 }
846 if (argc - 1 > MAX_PROBE_ARGS) {
847 semantic_error("Too many probe arguments (%d).\n", argc - 1);
848 ret = -ERANGE;
849 goto out;
850 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500851 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400852 ret = parse_perf_probe_point(argv[0], pev);
853 if (ret < 0)
854 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500855
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500856 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400857 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400858 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
859 if (pev->args == NULL) {
860 ret = -ENOMEM;
861 goto out;
862 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400863 for (i = 0; i < pev->nargs && ret >= 0; i++) {
864 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
865 if (ret >= 0 &&
866 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400867 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400868 " kretprobe.\n");
869 ret = -EINVAL;
870 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500871 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400872out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500873 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400874
875 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500876}
877
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400878/* Return true if this perf_probe_event requires debuginfo */
879bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500880{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400881 int i;
882
883 if (pev->point.file || pev->point.line || pev->point.lazy_line)
884 return true;
885
886 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400887 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400888 return true;
889
890 return false;
891}
892
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530893/* Parse probe_events event into struct probe_point */
894static int parse_probe_trace_command(const char *cmd,
895 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400896{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530897 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500898 char pr;
899 char *p;
900 int ret, i, argc;
901 char **argv;
902
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530903 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400904 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400905 if (!argv) {
906 pr_debug("Failed to split arguments.\n");
907 return -ENOMEM;
908 }
909 if (argc < 2) {
910 semantic_error("Too few probe arguments.\n");
911 ret = -ERANGE;
912 goto out;
913 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500914
915 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800916 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400917 &pr, (float *)(void *)&tev->group,
918 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400919 if (ret != 3) {
920 semantic_error("Failed to parse event name: %s\n", argv[0]);
921 ret = -EINVAL;
922 goto out;
923 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400924 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500925
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400926 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500927
928 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400929 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
930 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500931 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400932 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500933
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400934 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530935 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400936 if (tev->args == NULL) {
937 ret = -ENOMEM;
938 goto out;
939 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400940 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500941 p = strchr(argv[i + 2], '=');
942 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400943 *p++ = '\0';
944 else
945 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400946 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400947 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400948 tev->args[i].value = strdup(p);
949 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
950 ret = -ENOMEM;
951 goto out;
952 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500953 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400954 ret = 0;
955out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500956 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400957 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500958}
959
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400960/* Compose only probe arg */
961int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
962{
963 struct perf_probe_arg_field *field = pa->field;
964 int ret;
965 char *tmp = buf;
966
Masami Hiramatsu48481932010-04-12 13:16:53 -0400967 if (pa->name && pa->var)
968 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
969 else
970 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400971 if (ret <= 0)
972 goto error;
973 tmp += ret;
974 len -= ret;
975
976 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400977 if (field->name[0] == '[')
978 ret = e_snprintf(tmp, len, "%s", field->name);
979 else
980 ret = e_snprintf(tmp, len, "%s%s",
981 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400982 if (ret <= 0)
983 goto error;
984 tmp += ret;
985 len -= ret;
986 field = field->next;
987 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400988
989 if (pa->type) {
990 ret = e_snprintf(tmp, len, ":%s", pa->type);
991 if (ret <= 0)
992 goto error;
993 tmp += ret;
994 len -= ret;
995 }
996
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400997 return tmp - buf;
998error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400999 pr_debug("Failed to synthesize perf probe argument: %s",
1000 strerror(-ret));
1001 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001002}
1003
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001004/* Compose only probe point (not argument) */
1005static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001006{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001007 char *buf, *tmp;
1008 char offs[32] = "", line[32] = "", file[32] = "";
1009 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001010
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001011 buf = zalloc(MAX_CMDLEN);
1012 if (buf == NULL) {
1013 ret = -ENOMEM;
1014 goto error;
1015 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001016 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001017 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001018 if (ret <= 0)
1019 goto error;
1020 }
1021 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001022 ret = e_snprintf(line, 32, ":%d", pp->line);
1023 if (ret <= 0)
1024 goto error;
1025 }
1026 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001027 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001028 if (len < 0)
1029 len = 0;
1030 tmp = strchr(pp->file + len, '/');
1031 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001032 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001033 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001034 if (ret <= 0)
1035 goto error;
1036 }
1037
1038 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001039 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1040 offs, pp->retprobe ? "%return" : "", line,
1041 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001042 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001043 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001044 if (ret <= 0)
1045 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001046
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001047 return buf;
1048error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 pr_debug("Failed to synthesize perf probe point: %s",
1050 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001051 if (buf)
1052 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001054}
1055
1056#if 0
1057char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1058{
1059 char *buf;
1060 int i, len, ret;
1061
1062 buf = synthesize_perf_probe_point(&pev->point);
1063 if (!buf)
1064 return NULL;
1065
1066 len = strlen(buf);
1067 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001068 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001069 pev->args[i].name);
1070 if (ret <= 0) {
1071 free(buf);
1072 return NULL;
1073 }
1074 len += ret;
1075 }
1076
1077 return buf;
1078}
1079#endif
1080
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301081static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082 char **buf, size_t *buflen,
1083 int depth)
1084{
1085 int ret;
1086 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301087 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001088 buflen, depth + 1);
1089 if (depth < 0)
1090 goto out;
1091 }
1092
1093 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1094 if (ret < 0)
1095 depth = ret;
1096 else {
1097 *buf += ret;
1098 *buflen -= ret;
1099 }
1100out:
1101 return depth;
1102
1103}
1104
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301105static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001106 char *buf, size_t buflen)
1107{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301108 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001109 int ret, depth = 0;
1110 char *tmp = buf;
1111
1112 /* Argument name or separator */
1113 if (arg->name)
1114 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1115 else
1116 ret = e_snprintf(buf, buflen, " ");
1117 if (ret < 0)
1118 return ret;
1119 buf += ret;
1120 buflen -= ret;
1121
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001122 /* Special case: @XXX */
1123 if (arg->value[0] == '@' && arg->ref)
1124 ref = ref->next;
1125
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001126 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001127 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301128 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001129 &buflen, 1);
1130 if (depth < 0)
1131 return depth;
1132 }
1133
1134 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001135 if (arg->value[0] == '@' && arg->ref)
1136 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1137 arg->ref->offset);
1138 else
1139 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001140 if (ret < 0)
1141 return ret;
1142 buf += ret;
1143 buflen -= ret;
1144
1145 /* Closing */
1146 while (depth--) {
1147 ret = e_snprintf(buf, buflen, ")");
1148 if (ret < 0)
1149 return ret;
1150 buf += ret;
1151 buflen -= ret;
1152 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001153 /* Print argument type */
1154 if (arg->type) {
1155 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1156 if (ret <= 0)
1157 return ret;
1158 buf += ret;
1159 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001160
1161 return buf - tmp;
1162}
1163
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301164char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001165{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301166 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001167 char *buf;
1168 int i, len, ret;
1169
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001170 buf = zalloc(MAX_CMDLEN);
1171 if (buf == NULL)
1172 return NULL;
1173
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001174 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1175 tp->retprobe ? 'r' : 'p',
1176 tev->group, tev->event,
1177 tp->symbol, tp->offset);
1178 if (len <= 0)
1179 goto error;
1180
1181 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301182 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001184 if (ret <= 0)
1185 goto error;
1186 len += ret;
1187 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001188
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001189 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001190error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001191 free(buf);
1192 return NULL;
1193}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001194
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301195static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001196 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001197{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001198 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001200
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001201 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001202 pev->event = strdup(tev->event);
1203 pev->group = strdup(tev->group);
1204 if (pev->event == NULL || pev->group == NULL)
1205 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001206
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001207 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301208 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 if (ret < 0)
1210 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001211
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001212 /* Convert trace_arg to probe_arg */
1213 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001214 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1215 if (pev->args == NULL)
1216 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001217 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001218 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001219 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001220 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301221 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001223 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001224 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001225 if (pev->args[i].name == NULL && ret >= 0)
1226 ret = -ENOMEM;
1227 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228
1229 if (ret < 0)
1230 clear_perf_probe_event(pev);
1231
1232 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001233}
1234
1235void clear_perf_probe_event(struct perf_probe_event *pev)
1236{
1237 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001238 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001239 int i;
1240
1241 if (pev->event)
1242 free(pev->event);
1243 if (pev->group)
1244 free(pev->group);
1245 if (pp->file)
1246 free(pp->file);
1247 if (pp->function)
1248 free(pp->function);
1249 if (pp->lazy_line)
1250 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001251 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252 if (pev->args[i].name)
1253 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001254 if (pev->args[i].var)
1255 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001256 if (pev->args[i].type)
1257 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001258 field = pev->args[i].field;
1259 while (field) {
1260 next = field->next;
1261 if (field->name)
1262 free(field->name);
1263 free(field);
1264 field = next;
1265 }
1266 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 if (pev->args)
1268 free(pev->args);
1269 memset(pev, 0, sizeof(*pev));
1270}
1271
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301272static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001273{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301274 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001275 int i;
1276
1277 if (tev->event)
1278 free(tev->event);
1279 if (tev->group)
1280 free(tev->group);
1281 if (tev->point.symbol)
1282 free(tev->point.symbol);
1283 for (i = 0; i < tev->nargs; i++) {
1284 if (tev->args[i].name)
1285 free(tev->args[i].name);
1286 if (tev->args[i].value)
1287 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001288 if (tev->args[i].type)
1289 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001290 ref = tev->args[i].ref;
1291 while (ref) {
1292 next = ref->next;
1293 free(ref);
1294 ref = next;
1295 }
1296 }
1297 if (tev->args)
1298 free(tev->args);
1299 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001300}
1301
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001302static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001303{
1304 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001305 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001306 int ret;
1307
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001308 __debugfs = debugfs_find_mountpoint();
1309 if (__debugfs == NULL) {
1310 pr_warning("Debugfs is not mounted.\n");
1311 return -ENOENT;
1312 }
1313
1314 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001315 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001316 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 if (readwrite && !probe_event_dry_run)
1318 ret = open(buf, O_RDWR, O_APPEND);
1319 else
1320 ret = open(buf, O_RDONLY, 0);
1321 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001322
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001323 if (ret < 0) {
1324 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001325 pr_warning("kprobe_events file does not exist - please"
1326 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001327 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001328 pr_warning("Failed to open kprobe_events file: %s\n",
1329 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001330 }
1331 return ret;
1332}
1333
1334/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301335static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001336{
1337 int ret, idx;
1338 FILE *fp;
1339 char buf[MAX_CMDLEN];
1340 char *p;
1341 struct strlist *sl;
1342
1343 sl = strlist__new(true, NULL);
1344
1345 fp = fdopen(dup(fd), "r");
1346 while (!feof(fp)) {
1347 p = fgets(buf, MAX_CMDLEN, fp);
1348 if (!p)
1349 break;
1350
1351 idx = strlen(p) - 1;
1352 if (p[idx] == '\n')
1353 p[idx] = '\0';
1354 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 if (ret < 0) {
1356 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1357 strlist__delete(sl);
1358 return NULL;
1359 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001360 }
1361 fclose(fp);
1362
1363 return sl;
1364}
1365
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001366/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001367static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001368{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001369 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001370 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001371 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001372
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373 /* Synthesize only event probe point */
1374 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 if (!place)
1376 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001377
1378 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001379 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001380 return ret;
1381
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001382 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001383
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001384 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001385 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001386 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 ret = synthesize_perf_probe_arg(&pev->args[i],
1388 buf, 128);
1389 if (ret < 0)
1390 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001391 printf(" %s", buf);
1392 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001393 }
1394 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001395 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001396 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001397}
1398
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001399/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001402 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301403 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001405 struct strlist *rawlist;
1406 struct str_node *ent;
1407
Masami Hiramatsu72041332010-01-05 17:47:10 -05001408 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001409 ret = init_vmlinux();
1410 if (ret < 0)
1411 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001412
1413 memset(&tev, 0, sizeof(tev));
1414 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001415
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001416 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001417 if (fd < 0)
1418 return fd;
1419
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301420 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001421 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001422 if (!rawlist)
1423 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001424
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001425 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301426 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001427 if (ret >= 0) {
1428 ret = convert_to_perf_probe_event(&tev, &pev);
1429 if (ret >= 0)
1430 ret = show_perf_probe_event(&pev);
1431 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001432 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301433 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001434 if (ret < 0)
1435 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001436 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001437 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001438
1439 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001440}
1441
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001442/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301443static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001444{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001445 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001446 struct strlist *sl, *rawlist;
1447 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301448 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001449 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001450
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001451 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301452 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001453 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001454 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301455 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (ret < 0)
1457 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001458 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1460 tev.event);
1461 if (ret >= 0)
1462 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001463 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301465 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 if (ret < 0)
1467 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001468 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001469 strlist__delete(rawlist);
1470
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001471 if (ret < 0) {
1472 strlist__delete(sl);
1473 return NULL;
1474 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001475 return sl;
1476}
1477
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301478static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001479{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001480 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301481 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001482
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001483 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301484 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001485 return -EINVAL;
1486 }
1487
Masami Hiramatsufa282442009-12-08 17:03:23 -05001488 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001489 if (!probe_event_dry_run) {
1490 ret = write(fd, buf, strlen(buf));
1491 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001492 pr_warning("Failed to write event: %s\n",
1493 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001494 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001495 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001496 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001497}
1498
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499static int get_new_event_name(char *buf, size_t len, const char *base,
1500 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001501{
1502 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001503
1504 /* Try no suffix */
1505 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001506 if (ret < 0) {
1507 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1508 return ret;
1509 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001510 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001511 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001512
Masami Hiramatsud761b082009-12-15 10:32:25 -05001513 if (!allow_suffix) {
1514 pr_warning("Error: event \"%s\" already exists. "
1515 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001517 }
1518
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001519 /* Try to add suffix */
1520 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001521 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522 if (ret < 0) {
1523 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1524 return ret;
1525 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001526 if (!strlist__has_entry(namelist, buf))
1527 break;
1528 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 if (i == MAX_EVENT_INDEX) {
1530 pr_warning("Too many events are on the same function.\n");
1531 ret = -ERANGE;
1532 }
1533
1534 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001535}
1536
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301537static int __add_probe_trace_events(struct perf_probe_event *pev,
1538 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001539 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001540{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301542 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001543 char buf[64];
1544 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001545 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001546
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001547 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548 if (fd < 0)
1549 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001550 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301551 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001552 if (!namelist) {
1553 pr_debug("Failed to get current event list.\n");
1554 return -EIO;
1555 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001556
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001557 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001558 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001559 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001560 tev = &tevs[i];
1561 if (pev->event)
1562 event = pev->event;
1563 else
1564 if (pev->point.function)
1565 event = pev->point.function;
1566 else
1567 event = tev->point.symbol;
1568 if (pev->group)
1569 group = pev->group;
1570 else
1571 group = PERFPROBE_GROUP;
1572
1573 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 ret = get_new_event_name(buf, 64, event,
1575 namelist, allow_suffix);
1576 if (ret < 0)
1577 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001578 event = buf;
1579
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001580 tev->event = strdup(event);
1581 tev->group = strdup(group);
1582 if (tev->event == NULL || tev->group == NULL) {
1583 ret = -ENOMEM;
1584 break;
1585 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301586 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587 if (ret < 0)
1588 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001589 /* Add added event name to namelist */
1590 strlist__add(namelist, event);
1591
1592 /* Trick here - save current event/group */
1593 event = pev->event;
1594 group = pev->group;
1595 pev->event = tev->event;
1596 pev->group = tev->group;
1597 show_perf_probe_event(pev);
1598 /* Trick here - restore current event/group */
1599 pev->event = (char *)event;
1600 pev->group = (char *)group;
1601
1602 /*
1603 * Probes after the first probe which comes from same
1604 * user input are always allowed to add suffix, because
1605 * there might be several addresses corresponding to
1606 * one code line.
1607 */
1608 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001609 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610
1611 if (ret >= 0) {
1612 /* Show how to use the event. */
1613 printf("\nYou can now use it on all perf tools, such as:\n\n");
1614 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1615 tev->event);
1616 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001617
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001618 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001619 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001620 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001621}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001622
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301623static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1624 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001625 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001626{
1627 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001628 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301629 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001630
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001631 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001632 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001633 if (ret != 0)
1634 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001635
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301637 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001638 if (tev == NULL)
1639 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001640
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001641 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001642 tev->point.symbol = strdup(pev->point.function);
1643 if (tev->point.symbol == NULL) {
1644 ret = -ENOMEM;
1645 goto error;
1646 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001647 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001648 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 tev->nargs = pev->nargs;
1650 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001652 * tev->nargs);
1653 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001654 ret = -ENOMEM;
1655 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001656 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001657 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001658 if (pev->args[i].name) {
1659 tev->args[i].name = strdup(pev->args[i].name);
1660 if (tev->args[i].name == NULL) {
1661 ret = -ENOMEM;
1662 goto error;
1663 }
1664 }
1665 tev->args[i].value = strdup(pev->args[i].var);
1666 if (tev->args[i].value == NULL) {
1667 ret = -ENOMEM;
1668 goto error;
1669 }
1670 if (pev->args[i].type) {
1671 tev->args[i].type = strdup(pev->args[i].type);
1672 if (tev->args[i].type == NULL) {
1673 ret = -ENOMEM;
1674 goto error;
1675 }
1676 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001677 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001678 }
1679
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001680 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001681 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001682 if (!sym) {
1683 pr_warning("Kernel symbol \'%s\' not found.\n",
1684 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001685 ret = -ENOENT;
1686 goto error;
1687 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001688
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001689 return 1;
1690error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301691 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001692 free(tev);
1693 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001694 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695}
1696
1697struct __event_package {
1698 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301699 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700 int ntevs;
1701};
1702
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001703int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001704 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001705{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001706 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707 struct __event_package *pkgs;
1708
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001709 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1710 if (pkgs == NULL)
1711 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001712
1713 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001714 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001715 if (ret < 0) {
1716 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001717 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001718 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719
1720 /* Loop 1: convert all events */
1721 for (i = 0; i < npevs; i++) {
1722 pkgs[i].pev = &pevs[i];
1723 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301724 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001725 &pkgs[i].tevs,
1726 max_tevs,
1727 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001728 if (ret < 0)
1729 goto end;
1730 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001731 }
1732
1733 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001734 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301735 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736 pkgs[i].ntevs, force_add);
1737end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001738 /* Loop 3: cleanup and free trace events */
1739 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001740 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301741 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001742 free(pkgs[i].tevs);
1743 }
1744 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001745
1746 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001747}
1748
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301749static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001750{
1751 char *p;
1752 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001753 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001754
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301755 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001756 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1757 if (ret < 0)
1758 goto error;
1759
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001760 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001761 if (!p) {
1762 pr_debug("Internal error: %s should have ':' but not.\n",
1763 ent->s);
1764 ret = -ENOTSUP;
1765 goto error;
1766 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001767 *p = '/';
1768
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769 pr_debug("Writing event: %s\n", buf);
1770 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001771 if (ret < 0)
1772 goto error;
1773
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001774 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001775 return 0;
1776error:
1777 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1778 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001779}
1780
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301781static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001782 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001783{
1784 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001785 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001786 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001787
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001788 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1789 if (ret < 0) {
1790 pr_err("Failed to copy event.");
1791 return ret;
1792 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001793
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001794 if (strpbrk(buf, "*?")) { /* Glob-exp */
1795 strlist__for_each_safe(ent, n, namelist)
1796 if (strglobmatch(ent->s, buf)) {
1797 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301798 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001799 if (ret < 0)
1800 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001801 strlist__remove(namelist, ent);
1802 }
1803 } else {
1804 ent = strlist__find(namelist, buf);
1805 if (ent) {
1806 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301807 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001808 if (ret >= 0)
1809 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001810 }
1811 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001812 if (found == 0 && ret >= 0)
1813 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1814
1815 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001816}
1817
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001818int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001819{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001820 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001821 const char *group, *event;
1822 char *p, *str;
1823 struct str_node *ent;
1824 struct strlist *namelist;
1825
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001826 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001827 if (fd < 0)
1828 return fd;
1829
Masami Hiramatsufa282442009-12-08 17:03:23 -05001830 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301831 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001832 if (namelist == NULL)
1833 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001834
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001835 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 str = strdup(ent->s);
1837 if (str == NULL) {
1838 ret = -ENOMEM;
1839 break;
1840 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001841 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001842 p = strchr(str, ':');
1843 if (p) {
1844 group = str;
1845 *p = '\0';
1846 event = p + 1;
1847 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001848 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001849 event = str;
1850 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001851 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301852 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001853 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001854 if (ret < 0)
1855 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001856 }
1857 strlist__delete(namelist);
1858 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001859
1860 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001861}
1862