Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / tools / perf / util / probe-event.c
CommitLineData
50656eec 1/*
0e60836b 2 * probe-event.c : perf-probe definition to probe_events format converter
50656eec
MH
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
50656eec
MH
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
4de189fe
MH
31#include <stdarg.h>
32#include <limits.h>
e80711ca 33#include <elf.h>
50656eec 34
31facc5f 35#include "util.h"
50656eec 36#include "event.h"
4de189fe 37#include "strlist.h"
50656eec 38#include "debug.h"
72041334 39#include "cache.h"
631c9def 40#include "color.h"
e0faa8d3
MH
41#include "symbol.h"
42#include "thread.h"
85c66be1 43#include <lk/debugfs.h>
1d037ca1 44#include "trace-event.h" /* For __maybe_unused */
50656eec 45#include "probe-event.h"
4235b045 46#include "probe-finder.h"
225466f1 47#include "session.h"
50656eec
MH
48
49#define MAX_CMDLEN 256
50656eec
MH
50#define PERFPROBE_GROUP "probe"
51
f4d7da49
MH
52bool probe_event_dry_run; /* Dry run flag */
53
146a1439 54#define semantic_error(msg ...) pr_err("Semantic error :" msg)
50656eec 55
4de189fe 56/* If there is no space to write, returns -E2BIG. */
84988450
MH
57static int e_snprintf(char *str, size_t size, const char *format, ...)
58 __attribute__((format(printf, 3, 4)));
59
4de189fe
MH
60static int e_snprintf(char *str, size_t size, const char *format, ...)
61{
62 int ret;
63 va_list ap;
64 va_start(ap, format);
65 ret = vsnprintf(str, size, format, ap);
66 va_end(ap);
67 if (ret >= (int)size)
68 ret = -E2BIG;
69 return ret;
70}
71
4b4da7f7 72static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
225466f1
SD
73static int convert_name_to_addr(struct perf_probe_event *pev,
74 const char *exec);
d28c6223 75static struct machine machine;
e0faa8d3 76
469b9b88 77/* Initialize symbol maps and path of vmlinux/modules */
146a1439 78static int init_vmlinux(void)
e0faa8d3 79{
146a1439
MH
80 int ret;
81
e0faa8d3
MH
82 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);
146a1439
MH
87 ret = symbol__init();
88 if (ret < 0) {
89 pr_debug("Failed to init symbol map.\n");
90 goto out;
91 }
e0faa8d3 92
469b9b88 93 ret = machine__init(&machine, "", HOST_KERNEL_ID);
d28c6223
ACM
94 if (ret < 0)
95 goto out;
96
469b9b88 97 if (machine__create_kernel_maps(&machine) < 0) {
0e43e5d2 98 pr_debug("machine__create_kernel_maps() failed.\n");
469b9b88
MH
99 goto out;
100 }
146a1439
MH
101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
e0faa8d3
MH
105}
106
469b9b88
MH
107static struct symbol *__find_kernel_function_by_name(const char *name,
108 struct map **mapp)
109{
110 return machine__find_kernel_function_by_name(&machine, name, mapp,
111 NULL);
112}
113
e80711ca
MH
114static struct map *kernel_get_module_map(const char *module)
115{
116 struct rb_node *nd;
117 struct map_groups *grp = &machine.kmaps;
118
14a8fd7c
MH
119 /* A file path -- this is an offline module */
120 if (module && strchr(module, '/'))
121 return machine__new_module(&machine, 0, module);
122
e80711ca
MH
123 if (!module)
124 module = "kernel";
125
126 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
127 struct map *pos = rb_entry(nd, struct map, rb_node);
128 if (strncmp(pos->dso->short_name + 1, module,
129 pos->dso->short_name_len - 2) == 0) {
130 return pos;
131 }
132 }
133 return NULL;
134}
135
136static struct dso *kernel_get_module_dso(const char *module)
469b9b88
MH
137{
138 struct dso *dso;
fd930ff9
FBH
139 struct map *map;
140 const char *vmlinux_name;
469b9b88
MH
141
142 if (module) {
143 list_for_each_entry(dso, &machine.kernel_dsos, node) {
144 if (strncmp(dso->short_name + 1, module,
145 dso->short_name_len - 2) == 0)
146 goto found;
147 }
148 pr_debug("Failed to find module %s.\n", module);
149 return NULL;
fd930ff9
FBH
150 }
151
152 map = machine.vmlinux_maps[MAP__FUNCTION];
153 dso = map->dso;
154
155 vmlinux_name = symbol_conf.vmlinux_name;
156 if (vmlinux_name) {
5230fb7d 157 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
fd930ff9 158 return NULL;
469b9b88 159 } else {
c3a34e06 160 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
469b9b88
MH
161 pr_debug("Failed to load kernel map.\n");
162 return NULL;
163 }
164 }
165found:
e80711ca
MH
166 return dso;
167}
168
169const char *kernel_get_module_path(const char *module)
170{
171 struct dso *dso = kernel_get_module_dso(module);
172 return (dso) ? dso->long_name : NULL;
469b9b88
MH
173}
174
225466f1
SD
175static int init_user_exec(void)
176{
177 int ret = 0;
178
179 symbol_conf.try_vmlinux_path = false;
180 symbol_conf.sort_by_name = true;
181 ret = symbol__init();
182
183 if (ret < 0)
184 pr_debug("Failed to init symbol map.\n");
185
186 return ret;
187}
188
189static int convert_to_perf_probe_point(struct probe_trace_point *tp,
190 struct perf_probe_point *pp)
191{
192 pp->function = strdup(tp->symbol);
193
194 if (pp->function == NULL)
195 return -ENOMEM;
196
197 pp->offset = tp->offset;
198 pp->retprobe = tp->retprobe;
199
200 return 0;
201}
202
89fe808a 203#ifdef HAVE_DWARF_SUPPORT
ff741783
MH
204/* Open new debuginfo of given module */
205static struct debuginfo *open_debuginfo(const char *module)
e0faa8d3 206{
14a8fd7c 207 const char *path;
ff741783 208
14a8fd7c
MH
209 /* A file path -- this is an offline module */
210 if (module && strchr(module, '/'))
211 path = module;
212 else {
213 path = kernel_get_module_path(module);
214
215 if (!path) {
216 pr_err("Failed to find path of %s module.\n",
217 module ?: "kernel");
218 return NULL;
219 }
e0faa8d3 220 }
ff741783 221 return debuginfo__new(path);
e0faa8d3 222}
4b4da7f7 223
0e60836b
SD
224/*
225 * Convert trace point to probe point with debuginfo
226 * Currently only handles kprobes.
227 */
228static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
469b9b88 229 struct perf_probe_point *pp)
4b4da7f7
MH
230{
231 struct symbol *sym;
469b9b88
MH
232 struct map *map;
233 u64 addr;
234 int ret = -ENOENT;
ff741783 235 struct debuginfo *dinfo;
4b4da7f7 236
469b9b88 237 sym = __find_kernel_function_by_name(tp->symbol, &map);
4b4da7f7 238 if (sym) {
469b9b88 239 addr = map->unmap_ip(map, sym->start + tp->offset);
9486aa38 240 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
469b9b88 241 tp->offset, addr);
ff741783
MH
242
243 dinfo = debuginfo__new_online_kernel(addr);
244 if (dinfo) {
245 ret = debuginfo__find_probe_point(dinfo,
246 (unsigned long)addr, pp);
247 debuginfo__delete(dinfo);
248 } else {
249 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
250 addr);
251 ret = -ENOENT;
252 }
4b4da7f7
MH
253 }
254 if (ret <= 0) {
146a1439
MH
255 pr_debug("Failed to find corresponding probes from "
256 "debuginfo. Use kprobe event information.\n");
225466f1 257 return convert_to_perf_probe_point(tp, pp);
4b4da7f7
MH
258 }
259 pp->retprobe = tp->retprobe;
146a1439
MH
260
261 return 0;
4b4da7f7
MH
262}
263
190b57fc
MH
264static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
265 int ntevs, const char *module)
266{
14a8fd7c
MH
267 int i, ret = 0;
268 char *tmp;
269
270 if (!module)
271 return 0;
272
273 tmp = strrchr(module, '/');
274 if (tmp) {
275 /* This is a module path -- get the module name */
276 module = strdup(tmp + 1);
277 if (!module)
278 return -ENOMEM;
279 tmp = strchr(module, '.');
280 if (tmp)
281 *tmp = '\0';
282 tmp = (char *)module; /* For free() */
283 }
284
190b57fc
MH
285 for (i = 0; i < ntevs; i++) {
286 tevs[i].point.module = strdup(module);
14a8fd7c
MH
287 if (!tevs[i].point.module) {
288 ret = -ENOMEM;
289 break;
290 }
190b57fc 291 }
14a8fd7c
MH
292
293 if (tmp)
294 free(tmp);
295
296 return ret;
190b57fc
MH
297}
298
4b4da7f7 299/* Try to find perf_probe_event with debuginfo */
0e60836b 300static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
190b57fc 301 struct probe_trace_event **tevs,
4eced234 302 int max_tevs, const char *target)
4b4da7f7
MH
303{
304 bool need_dwarf = perf_probe_event_need_dwarf(pev);
225466f1 305 struct debuginfo *dinfo;
190b57fc 306 int ntevs, ret = 0;
4b4da7f7 307
225466f1
SD
308 if (pev->uprobes) {
309 if (need_dwarf) {
310 pr_warning("Debuginfo-analysis is not yet supported"
311 " with -x/--exec option.\n");
312 return -ENOSYS;
313 }
314 return convert_name_to_addr(pev, target);
315 }
316
317 dinfo = open_debuginfo(target);
318
ff741783 319 if (!dinfo) {
146a1439
MH
320 if (need_dwarf) {
321 pr_warning("Failed to open debuginfo file.\n");
ff741783 322 return -ENOENT;
146a1439 323 }
ff741783 324 pr_debug("Could not open debuginfo. Try to use symbols.\n");
4b4da7f7
MH
325 return 0;
326 }
327
ff741783
MH
328 /* Searching trace events corresponding to a probe event */
329 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
330
331 debuginfo__delete(dinfo);
4b4da7f7 332
146a1439 333 if (ntevs > 0) { /* Succeeded to find trace events */
0e60836b 334 pr_debug("find %d probe_trace_events.\n", ntevs);
4eced234 335 if (target)
190b57fc 336 ret = add_module_to_probe_trace_events(*tevs, ntevs,
4eced234 337 target);
190b57fc 338 return ret < 0 ? ret : ntevs;
146a1439 339 }
4b4da7f7 340
146a1439
MH
341 if (ntevs == 0) { /* No error but failed to find probe point. */
342 pr_warning("Probe point '%s' not found.\n",
343 synthesize_perf_probe_point(&pev->point));
344 return -ENOENT;
345 }
346 /* Error path : ntevs < 0 */
15eca306
MH
347 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
348 if (ntevs == -EBADF) {
349 pr_warning("Warning: No dwarf info found in the vmlinux - "
350 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
351 if (!need_dwarf) {
0e43e5d2 352 pr_debug("Trying to use symbols.\n");
15eca306
MH
353 return 0;
354 }
4b4da7f7 355 }
15eca306 356 return ntevs;
4b4da7f7
MH
357}
358
7cf0b79e
MH
359/*
360 * Find a src file from a DWARF tag path. Prepend optional source path prefix
361 * and chop off leading directories that do not exist. Result is passed back as
362 * a newly allocated path on success.
363 * Return 0 if file was found and readable, -errno otherwise.
364 */
6a330a3c
MH
365static int get_real_path(const char *raw_path, const char *comp_dir,
366 char **new_path)
7cf0b79e 367{
6a330a3c
MH
368 const char *prefix = symbol_conf.source_prefix;
369
370 if (!prefix) {
371 if (raw_path[0] != '/' && comp_dir)
372 /* If not an absolute path, try to use comp_dir */
373 prefix = comp_dir;
374 else {
375 if (access(raw_path, R_OK) == 0) {
376 *new_path = strdup(raw_path);
377 return 0;
378 } else
379 return -errno;
380 }
7cf0b79e
MH
381 }
382
6a330a3c 383 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
7cf0b79e
MH
384 if (!*new_path)
385 return -ENOMEM;
386
387 for (;;) {
6a330a3c 388 sprintf(*new_path, "%s/%s", prefix, raw_path);
7cf0b79e
MH
389
390 if (access(*new_path, R_OK) == 0)
391 return 0;
392
6a330a3c
MH
393 if (!symbol_conf.source_prefix)
394 /* In case of searching comp_dir, don't retry */
395 return -errno;
396
7cf0b79e
MH
397 switch (errno) {
398 case ENAMETOOLONG:
399 case ENOENT:
400 case EROFS:
401 case EFAULT:
402 raw_path = strchr(++raw_path, '/');
403 if (!raw_path) {
404 free(*new_path);
405 *new_path = NULL;
406 return -ENOENT;
407 }
408 continue;
409
410 default:
411 free(*new_path);
412 *new_path = NULL;
413 return -errno;
414 }
415 }
416}
417
4b4da7f7
MH
418#define LINEBUF_SIZE 256
419#define NR_ADDITIONAL_LINES 2
420
fde52dbd 421static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
4b4da7f7
MH
422{
423 char buf[LINEBUF_SIZE];
befe3414
FBH
424 const char *color = show_num ? "" : PERF_COLOR_BLUE;
425 const char *prefix = NULL;
4b4da7f7 426
befe3414 427 do {
4b4da7f7
MH
428 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
429 goto error;
befe3414
FBH
430 if (skip)
431 continue;
432 if (!prefix) {
433 prefix = show_num ? "%7d " : " ";
434 color_fprintf(stdout, color, prefix, l);
4b4da7f7 435 }
befe3414
FBH
436 color_fprintf(stdout, color, "%s", buf);
437
438 } while (strchr(buf, '\n') == NULL);
146a1439 439
fde52dbd 440 return 1;
4b4da7f7 441error:
fde52dbd 442 if (ferror(fp)) {
32b2b6ec 443 pr_warning("File read error: %s\n", strerror(errno));
fde52dbd
FBH
444 return -1;
445 }
446 return 0;
447}
146a1439 448
fde52dbd
FBH
449static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
450{
451 int rv = __show_one_line(fp, l, skip, show_num);
452 if (rv == 0) {
453 pr_warning("Source file is shorter than expected.\n");
454 rv = -1;
455 }
456 return rv;
4b4da7f7
MH
457}
458
fde52dbd
FBH
459#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
460#define show_one_line(f,l) _show_one_line(f,l,false,false)
461#define skip_one_line(f,l) _show_one_line(f,l,true,false)
462#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
463
4b4da7f7
MH
464/*
465 * Show line-range always requires debuginfo to find source file and
466 * line number.
467 */
469b9b88 468int show_line_range(struct line_range *lr, const char *module)
4b4da7f7 469{
d3b63d7a 470 int l = 1;
4b4da7f7 471 struct line_node *ln;
ff741783 472 struct debuginfo *dinfo;
4b4da7f7 473 FILE *fp;
ff741783 474 int ret;
7cf0b79e 475 char *tmp;
4b4da7f7
MH
476
477 /* Search a line range */
146a1439
MH
478 ret = init_vmlinux();
479 if (ret < 0)
480 return ret;
481
ff741783
MH
482 dinfo = open_debuginfo(module);
483 if (!dinfo) {
146a1439 484 pr_warning("Failed to open debuginfo file.\n");
ff741783 485 return -ENOENT;
146a1439
MH
486 }
487
ff741783
MH
488 ret = debuginfo__find_line_range(dinfo, lr);
489 debuginfo__delete(dinfo);
146a1439
MH
490 if (ret == 0) {
491 pr_warning("Specified source line is not found.\n");
492 return -ENOENT;
493 } else if (ret < 0) {
494 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
495 return ret;
496 }
4b4da7f7 497
7cf0b79e
MH
498 /* Convert source file path */
499 tmp = lr->path;
6a330a3c 500 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
7cf0b79e
MH
501 free(tmp); /* Free old path */
502 if (ret < 0) {
503 pr_warning("Failed to find source file. (%d)\n", ret);
504 return ret;
505 }
506
4b4da7f7
MH
507 setup_pager();
508
509 if (lr->function)
8737ebde 510 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
4b4da7f7
MH
511 lr->start - lr->offset);
512 else
62c15fc4 513 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
4b4da7f7
MH
514
515 fp = fopen(lr->path, "r");
146a1439
MH
516 if (fp == NULL) {
517 pr_warning("Failed to open %s: %s\n", lr->path,
518 strerror(errno));
519 return -errno;
520 }
4b4da7f7 521 /* Skip to starting line number */
44b81e92 522 while (l < lr->start) {
fde52dbd 523 ret = skip_one_line(fp, l++);
44b81e92
FBH
524 if (ret < 0)
525 goto end;
526 }
4b4da7f7
MH
527
528 list_for_each_entry(ln, &lr->line_list, list) {
44b81e92 529 for (; ln->line > l; l++) {
fde52dbd 530 ret = show_one_line(fp, l - lr->offset);
44b81e92
FBH
531 if (ret < 0)
532 goto end;
533 }
fde52dbd 534 ret = show_one_line_with_num(fp, l++ - lr->offset);
146a1439
MH
535 if (ret < 0)
536 goto end;
4b4da7f7
MH
537 }
538
539 if (lr->end == INT_MAX)
540 lr->end = l + NR_ADDITIONAL_LINES;
fde52dbd
FBH
541 while (l <= lr->end) {
542 ret = show_one_line_or_eof(fp, l++ - lr->offset);
543 if (ret <= 0)
44b81e92
FBH
544 break;
545 }
146a1439 546end:
4b4da7f7 547 fclose(fp);
146a1439 548 return ret;
4b4da7f7
MH
549}
550
ff741783
MH
551static int show_available_vars_at(struct debuginfo *dinfo,
552 struct perf_probe_event *pev,
bd09d7b5
MH
553 int max_vls, struct strfilter *_filter,
554 bool externs)
cf6eb489
MH
555{
556 char *buf;
bd09d7b5 557 int ret, i, nvars;
cf6eb489
MH
558 struct str_node *node;
559 struct variable_list *vls = NULL, *vl;
bd09d7b5 560 const char *var;
cf6eb489
MH
561
562 buf = synthesize_perf_probe_point(&pev->point);
563 if (!buf)
564 return -EINVAL;
565 pr_debug("Searching variables at %s\n", buf);
566
ff741783
MH
567 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
568 max_vls, externs);
bd09d7b5
MH
569 if (ret <= 0) {
570 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
571 goto end;
572 }
573 /* Some variables are found */
574 fprintf(stdout, "Available variables at %s\n", buf);
575 for (i = 0; i < ret; i++) {
576 vl = &vls[i];
577 /*
578 * A probe point might be converted to
579 * several trace points.
580 */
581 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
582 vl->point.offset);
583 free(vl->point.symbol);
584 nvars = 0;
585 if (vl->vars) {
586 strlist__for_each(node, vl->vars) {
587 var = strchr(node->s, '\t') + 1;
588 if (strfilter__compare(_filter, var)) {
cf6eb489 589 fprintf(stdout, "\t\t%s\n", node->s);
bd09d7b5
MH
590 nvars++;
591 }
592 }
593 strlist__delete(vl->vars);
cf6eb489 594 }
bd09d7b5
MH
595 if (nvars == 0)
596 fprintf(stdout, "\t\t(No matched variables)\n");
597 }
598 free(vls);
599end:
cf6eb489
MH
600 free(buf);
601 return ret;
602}
603
604/* Show available variables on given probe point */
605int show_available_vars(struct perf_probe_event *pevs, int npevs,
bd09d7b5
MH
606 int max_vls, const char *module,
607 struct strfilter *_filter, bool externs)
cf6eb489 608{
ff741783
MH
609 int i, ret = 0;
610 struct debuginfo *dinfo;
cf6eb489
MH
611
612 ret = init_vmlinux();
613 if (ret < 0)
614 return ret;
615
ff741783
MH
616 dinfo = open_debuginfo(module);
617 if (!dinfo) {
618 pr_warning("Failed to open debuginfo file.\n");
619 return -ENOENT;
620 }
621
cf6eb489
MH
622 setup_pager();
623
ff741783
MH
624 for (i = 0; i < npevs && ret >= 0; i++)
625 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
bd09d7b5 626 externs);
ff741783
MH
627
628 debuginfo__delete(dinfo);
cf6eb489
MH
629 return ret;
630}
631
89fe808a 632#else /* !HAVE_DWARF_SUPPORT */
4b4da7f7 633
0e60836b 634static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
469b9b88 635 struct perf_probe_point *pp)
4b4da7f7 636{
469b9b88
MH
637 struct symbol *sym;
638
639 sym = __find_kernel_function_by_name(tp->symbol, NULL);
640 if (!sym) {
641 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
642 return -ENOENT;
643 }
146a1439 644
225466f1 645 return convert_to_perf_probe_point(tp, pp);
4b4da7f7
MH
646}
647
0e60836b 648static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1d037ca1
IT
649 struct probe_trace_event **tevs __maybe_unused,
650 int max_tevs __maybe_unused, const char *target)
4b4da7f7 651{
146a1439
MH
652 if (perf_probe_event_need_dwarf(pev)) {
653 pr_warning("Debuginfo-analysis is not supported.\n");
654 return -ENOSYS;
655 }
225466f1
SD
656
657 if (pev->uprobes)
658 return convert_name_to_addr(pev, target);
659
4b4da7f7
MH
660 return 0;
661}
662
1d037ca1
IT
663int show_line_range(struct line_range *lr __maybe_unused,
664 const char *module __maybe_unused)
4b4da7f7 665{
146a1439
MH
666 pr_warning("Debuginfo-analysis is not supported.\n");
667 return -ENOSYS;
4b4da7f7
MH
668}
669
1d037ca1
IT
670int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
671 int npevs __maybe_unused, int max_vls __maybe_unused,
672 const char *module __maybe_unused,
673 struct strfilter *filter __maybe_unused,
674 bool externs __maybe_unused)
cf6eb489
MH
675{
676 pr_warning("Debuginfo-analysis is not supported.\n");
677 return -ENOSYS;
678}
e0faa8d3
MH
679#endif
680
21dd9ae5
FBH
681static int parse_line_num(char **ptr, int *val, const char *what)
682{
683 const char *start = *ptr;
684
685 errno = 0;
686 *val = strtol(*ptr, ptr, 0);
687 if (errno || *ptr == start) {
688 semantic_error("'%s' is not a valid number.\n", what);
689 return -EINVAL;
690 }
691 return 0;
692}
693
9d95b580
FBH
694/*
695 * Stuff 'lr' according to the line range described by 'arg'.
696 * The line range syntax is described by:
697 *
698 * SRC[:SLN[+NUM|-ELN]]
e116dfa1 699 * FNC[@SRC][:SLN[+NUM|-ELN]]
9d95b580 700 */
146a1439 701int parse_line_range_desc(const char *arg, struct line_range *lr)
631c9def 702{
e116dfa1 703 char *range, *file, *name = strdup(arg);
21dd9ae5
FBH
704 int err;
705
706 if (!name)
707 return -ENOMEM;
708
709 lr->start = 0;
710 lr->end = INT_MAX;
711
712 range = strchr(name, ':');
713 if (range) {
714 *range++ = '\0';
715
716 err = parse_line_num(&range, &lr->start, "start line");
717 if (err)
718 goto err;
719
720 if (*range == '+' || *range == '-') {
721 const char c = *range++;
722
723 err = parse_line_num(&range, &lr->end, "end line");
724 if (err)
725 goto err;
726
727 if (c == '+') {
728 lr->end += lr->start;
729 /*
730 * Adjust the number of lines here.
731 * If the number of lines == 1, the
732 * the end of line should be equal to
733 * the start of line.
734 */
735 lr->end--;
736 }
737 }
9d95b580 738
d3b63d7a 739 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
21dd9ae5
FBH
740
741 err = -EINVAL;
d3b63d7a 742 if (lr->start > lr->end) {
631c9def 743 semantic_error("Start line must be smaller"
146a1439 744 " than end line.\n");
21dd9ae5 745 goto err;
146a1439 746 }
21dd9ae5
FBH
747 if (*range != '\0') {
748 semantic_error("Tailing with invalid str '%s'.\n", range);
749 goto err;
146a1439 750 }
d3b63d7a 751 }
02b95dad 752
e116dfa1
MH
753 file = strchr(name, '@');
754 if (file) {
755 *file = '\0';
756 lr->file = strdup(++file);
757 if (lr->file == NULL) {
758 err = -ENOMEM;
759 goto err;
760 }
761 lr->function = name;
762 } else if (strchr(name, '.'))
21dd9ae5 763 lr->file = name;
631c9def 764 else
21dd9ae5 765 lr->function = name;
146a1439
MH
766
767 return 0;
21dd9ae5
FBH
768err:
769 free(name);
770 return err;
631c9def
MH
771}
772
b7702a21
MH
773/* Check the name is good for event/group */
774static bool check_event_name(const char *name)
775{
776 if (!isalpha(*name) && *name != '_')
777 return false;
778 while (*++name != '\0') {
779 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
780 return false;
781 }
782 return true;
783}
784
50656eec 785/* Parse probepoint definition. */
146a1439 786static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 787{
4235b045 788 struct perf_probe_point *pp = &pev->point;
50656eec
MH
789 char *ptr, *tmp;
790 char c, nc = 0;
791 /*
792 * <Syntax>
2a9c8c36
MH
793 * perf probe [EVENT=]SRC[:LN|;PTN]
794 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
795 *
796 * TODO:Group name support
50656eec
MH
797 */
798
2a9c8c36
MH
799 ptr = strpbrk(arg, ";=@+%");
800 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
801 *ptr = '\0';
802 tmp = ptr + 1;
146a1439
MH
803 if (strchr(arg, ':')) {
804 semantic_error("Group name is not supported yet.\n");
805 return -ENOTSUP;
806 }
807 if (!check_event_name(arg)) {
b7702a21 808 semantic_error("%s is bad for event name -it must "
146a1439
MH
809 "follow C symbol-naming rule.\n", arg);
810 return -EINVAL;
811 }
02b95dad
MH
812 pev->event = strdup(arg);
813 if (pev->event == NULL)
814 return -ENOMEM;
4235b045 815 pev->group = NULL;
af663d75
MH
816 arg = tmp;
817 }
818
2a9c8c36 819 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
820 if (ptr) {
821 nc = *ptr;
822 *ptr++ = '\0';
823 }
824
02b95dad
MH
825 tmp = strdup(arg);
826 if (tmp == NULL)
827 return -ENOMEM;
828
50656eec 829 /* Check arg is function or file and copy it */
02b95dad
MH
830 if (strchr(tmp, '.')) /* File */
831 pp->file = tmp;
50656eec 832 else /* Function */
02b95dad 833 pp->function = tmp;
50656eec
MH
834
835 /* Parse other options */
836 while (ptr) {
837 arg = ptr;
838 c = nc;
2a9c8c36 839 if (c == ';') { /* Lazy pattern must be the last part */
02b95dad
MH
840 pp->lazy_line = strdup(arg);
841 if (pp->lazy_line == NULL)
842 return -ENOMEM;
2a9c8c36
MH
843 break;
844 }
845 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
846 if (ptr) {
847 nc = *ptr;
848 *ptr++ = '\0';
849 }
850 switch (c) {
851 case ':': /* Line number */
852 pp->line = strtoul(arg, &tmp, 0);
146a1439 853 if (*tmp != '\0') {
2a9c8c36 854 semantic_error("There is non-digit char"
146a1439
MH
855 " in line number.\n");
856 return -EINVAL;
857 }
50656eec
MH
858 break;
859 case '+': /* Byte offset from a symbol */
860 pp->offset = strtoul(arg, &tmp, 0);
146a1439 861 if (*tmp != '\0') {
2a9c8c36 862 semantic_error("There is non-digit character"
146a1439
MH
863 " in offset.\n");
864 return -EINVAL;
865 }
50656eec
MH
866 break;
867 case '@': /* File name */
146a1439
MH
868 if (pp->file) {
869 semantic_error("SRC@SRC is not allowed.\n");
870 return -EINVAL;
871 }
02b95dad
MH
872 pp->file = strdup(arg);
873 if (pp->file == NULL)
874 return -ENOMEM;
50656eec
MH
875 break;
876 case '%': /* Probe places */
877 if (strcmp(arg, "return") == 0) {
878 pp->retprobe = 1;
146a1439
MH
879 } else { /* Others not supported yet */
880 semantic_error("%%%s is not supported.\n", arg);
881 return -ENOTSUP;
882 }
50656eec 883 break;
146a1439
MH
884 default: /* Buggy case */
885 pr_err("This program has a bug at %s:%d.\n",
886 __FILE__, __LINE__);
887 return -ENOTSUP;
50656eec
MH
888 break;
889 }
890 }
891
892 /* Exclusion check */
146a1439 893 if (pp->lazy_line && pp->line) {
0e43e5d2
MH
894 semantic_error("Lazy pattern can't be used with"
895 " line number.\n");
146a1439
MH
896 return -EINVAL;
897 }
2a9c8c36 898
146a1439 899 if (pp->lazy_line && pp->offset) {
0e43e5d2 900 semantic_error("Lazy pattern can't be used with offset.\n");
146a1439
MH
901 return -EINVAL;
902 }
2a9c8c36 903
146a1439 904 if (pp->line && pp->offset) {
0e43e5d2 905 semantic_error("Offset can't be used with line number.\n");
146a1439
MH
906 return -EINVAL;
907 }
50656eec 908
146a1439 909 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
2a9c8c36 910 semantic_error("File always requires line number or "
0e43e5d2 911 "lazy pattern.\n");
146a1439
MH
912 return -EINVAL;
913 }
50656eec 914
146a1439 915 if (pp->offset && !pp->function) {
0e43e5d2 916 semantic_error("Offset requires an entry function.\n");
146a1439
MH
917 return -EINVAL;
918 }
50656eec 919
146a1439 920 if (pp->retprobe && !pp->function) {
0e43e5d2 921 semantic_error("Return probe requires an entry function.\n");
146a1439
MH
922 return -EINVAL;
923 }
50656eec 924
146a1439 925 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
2a9c8c36 926 semantic_error("Offset/Line/Lazy pattern can't be used with "
0e43e5d2 927 "return probe.\n");
146a1439
MH
928 return -EINVAL;
929 }
50656eec 930
4235b045 931 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
932 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
933 pp->lazy_line);
146a1439 934 return 0;
50656eec
MH
935}
936
7df2f329 937/* Parse perf-probe event argument */
146a1439 938static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
7df2f329 939{
b2a3c12b 940 char *tmp, *goodname;
7df2f329
MH
941 struct perf_probe_arg_field **fieldp;
942
943 pr_debug("parsing arg: %s into ", str);
944
48481938
MH
945 tmp = strchr(str, '=');
946 if (tmp) {
02b95dad
MH
947 arg->name = strndup(str, tmp - str);
948 if (arg->name == NULL)
949 return -ENOMEM;
11a1ca35 950 pr_debug("name:%s ", arg->name);
48481938
MH
951 str = tmp + 1;
952 }
953
11a1ca35
MH
954 tmp = strchr(str, ':');
955 if (tmp) { /* Type setting */
956 *tmp = '\0';
02b95dad
MH
957 arg->type = strdup(tmp + 1);
958 if (arg->type == NULL)
959 return -ENOMEM;
11a1ca35
MH
960 pr_debug("type:%s ", arg->type);
961 }
962
b2a3c12b 963 tmp = strpbrk(str, "-.[");
7df2f329
MH
964 if (!is_c_varname(str) || !tmp) {
965 /* A variable, register, symbol or special value */
02b95dad
MH
966 arg->var = strdup(str);
967 if (arg->var == NULL)
968 return -ENOMEM;
48481938 969 pr_debug("%s\n", arg->var);
146a1439 970 return 0;
7df2f329
MH
971 }
972
b2a3c12b 973 /* Structure fields or array element */
02b95dad
MH
974 arg->var = strndup(str, tmp - str);
975 if (arg->var == NULL)
976 return -ENOMEM;
b2a3c12b 977 goodname = arg->var;
48481938 978 pr_debug("%s, ", arg->var);
7df2f329
MH
979 fieldp = &arg->field;
980
981 do {
e334016f
MH
982 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
983 if (*fieldp == NULL)
984 return -ENOMEM;
b2a3c12b
MH
985 if (*tmp == '[') { /* Array */
986 str = tmp;
987 (*fieldp)->index = strtol(str + 1, &tmp, 0);
7df2f329 988 (*fieldp)->ref = true;
b2a3c12b
MH
989 if (*tmp != ']' || tmp == str + 1) {
990 semantic_error("Array index must be a"
991 " number.\n");
992 return -EINVAL;
993 }
994 tmp++;
995 if (*tmp == '\0')
996 tmp = NULL;
997 } else { /* Structure */
998 if (*tmp == '.') {
999 str = tmp + 1;
1000 (*fieldp)->ref = false;
1001 } else if (tmp[1] == '>') {
1002 str = tmp + 2;
1003 (*fieldp)->ref = true;
1004 } else {
1005 semantic_error("Argument parse error: %s\n",
1006 str);
1007 return -EINVAL;
1008 }
1009 tmp = strpbrk(str, "-.[");
146a1439 1010 }
7df2f329 1011 if (tmp) {
02b95dad
MH
1012 (*fieldp)->name = strndup(str, tmp - str);
1013 if ((*fieldp)->name == NULL)
1014 return -ENOMEM;
b2a3c12b
MH
1015 if (*str != '[')
1016 goodname = (*fieldp)->name;
7df2f329
MH
1017 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1018 fieldp = &(*fieldp)->next;
1019 }
1020 } while (tmp);
02b95dad
MH
1021 (*fieldp)->name = strdup(str);
1022 if ((*fieldp)->name == NULL)
1023 return -ENOMEM;
b2a3c12b
MH
1024 if (*str != '[')
1025 goodname = (*fieldp)->name;
7df2f329 1026 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
df0faf4b 1027
b2a3c12b 1028 /* If no name is specified, set the last field name (not array index)*/
02b95dad 1029 if (!arg->name) {
b2a3c12b 1030 arg->name = strdup(goodname);
02b95dad
MH
1031 if (arg->name == NULL)
1032 return -ENOMEM;
1033 }
146a1439 1034 return 0;
7df2f329
MH
1035}
1036
4235b045 1037/* Parse perf-probe event command */
146a1439 1038int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 1039{
e1c01d61 1040 char **argv;
146a1439 1041 int argc, i, ret = 0;
fac13fd5 1042
4235b045 1043 argv = argv_split(cmd, &argc);
146a1439
MH
1044 if (!argv) {
1045 pr_debug("Failed to split arguments.\n");
1046 return -ENOMEM;
1047 }
1048 if (argc - 1 > MAX_PROBE_ARGS) {
1049 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1050 ret = -ERANGE;
1051 goto out;
1052 }
50656eec 1053 /* Parse probe point */
146a1439
MH
1054 ret = parse_perf_probe_point(argv[0], pev);
1055 if (ret < 0)
1056 goto out;
50656eec 1057
e1c01d61 1058 /* Copy arguments and ensure return probe has no C argument */
4235b045 1059 pev->nargs = argc - 1;
e334016f
MH
1060 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1061 if (pev->args == NULL) {
1062 ret = -ENOMEM;
1063 goto out;
1064 }
146a1439
MH
1065 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1066 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1067 if (ret >= 0 &&
1068 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
4235b045 1069 semantic_error("You can't specify local variable for"
146a1439
MH
1070 " kretprobe.\n");
1071 ret = -EINVAL;
1072 }
e1c01d61 1073 }
146a1439 1074out:
e1c01d61 1075 argv_free(argv);
146a1439
MH
1076
1077 return ret;
50656eec
MH
1078}
1079
4235b045
MH
1080/* Return true if this perf_probe_event requires debuginfo */
1081bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1082{
1083 int i;
1084
1085 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1086 return true;
1087
1088 for (i = 0; i < pev->nargs; i++)
48481938 1089 if (is_c_varname(pev->args[i].var))
4235b045
MH
1090 return true;
1091
1092 return false;
1093}
1094
0e60836b
SD
1095/* Parse probe_events event into struct probe_point */
1096static int parse_probe_trace_command(const char *cmd,
190b57fc 1097 struct probe_trace_event *tev)
4de189fe 1098{
0e60836b 1099 struct probe_trace_point *tp = &tev->point;
4de189fe
MH
1100 char pr;
1101 char *p;
bcbd0040 1102 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
4de189fe
MH
1103 int ret, i, argc;
1104 char **argv;
1105
0e60836b 1106 pr_debug("Parsing probe_events: %s\n", cmd);
4235b045 1107 argv = argv_split(cmd, &argc);
146a1439
MH
1108 if (!argv) {
1109 pr_debug("Failed to split arguments.\n");
1110 return -ENOMEM;
1111 }
1112 if (argc < 2) {
1113 semantic_error("Too few probe arguments.\n");
1114 ret = -ERANGE;
1115 goto out;
1116 }
4de189fe
MH
1117
1118 /* Scan event and group name. */
bcbd0040
IT
1119 argv0_str = strdup(argv[0]);
1120 if (argv0_str == NULL) {
1121 ret = -ENOMEM;
1122 goto out;
1123 }
1124 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1125 fmt2_str = strtok_r(NULL, "/", &fmt);
1126 fmt3_str = strtok_r(NULL, " \t", &fmt);
1127 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1128 || fmt3_str == NULL) {
146a1439
MH
1129 semantic_error("Failed to parse event name: %s\n", argv[0]);
1130 ret = -EINVAL;
1131 goto out;
1132 }
bcbd0040
IT
1133 pr = fmt1_str[0];
1134 tev->group = strdup(fmt2_str);
1135 tev->event = strdup(fmt3_str);
1136 if (tev->group == NULL || tev->event == NULL) {
1137 ret = -ENOMEM;
1138 goto out;
1139 }
4235b045 1140 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 1141
4235b045 1142 tp->retprobe = (pr == 'r');
4de189fe 1143
190b57fc
MH
1144 /* Scan module name(if there), function name and offset */
1145 p = strchr(argv[1], ':');
1146 if (p) {
1147 tp->module = strndup(argv[1], p - argv[1]);
1148 p++;
1149 } else
1150 p = argv[1];
bcbd0040
IT
1151 fmt1_str = strtok_r(p, "+", &fmt);
1152 tp->symbol = strdup(fmt1_str);
1153 if (tp->symbol == NULL) {
1154 ret = -ENOMEM;
1155 goto out;
1156 }
1157 fmt2_str = strtok_r(NULL, "", &fmt);
1158 if (fmt2_str == NULL)
4235b045 1159 tp->offset = 0;
bcbd0040
IT
1160 else
1161 tp->offset = strtoul(fmt2_str, NULL, 10);
4de189fe 1162
4235b045 1163 tev->nargs = argc - 2;
0e60836b 1164 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
e334016f
MH
1165 if (tev->args == NULL) {
1166 ret = -ENOMEM;
1167 goto out;
1168 }
4235b045 1169 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
1170 p = strchr(argv[i + 2], '=');
1171 if (p) /* We don't need which register is assigned. */
4235b045
MH
1172 *p++ = '\0';
1173 else
1174 p = argv[i + 2];
02b95dad 1175 tev->args[i].name = strdup(argv[i + 2]);
4235b045 1176 /* TODO: parse regs and offset */
02b95dad
MH
1177 tev->args[i].value = strdup(p);
1178 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1179 ret = -ENOMEM;
1180 goto out;
1181 }
4de189fe 1182 }
146a1439
MH
1183 ret = 0;
1184out:
bcbd0040 1185 free(argv0_str);
4de189fe 1186 argv_free(argv);
146a1439 1187 return ret;
4de189fe
MH
1188}
1189
7df2f329
MH
1190/* Compose only probe arg */
1191int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1192{
1193 struct perf_probe_arg_field *field = pa->field;
1194 int ret;
1195 char *tmp = buf;
1196
48481938
MH
1197 if (pa->name && pa->var)
1198 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1199 else
1200 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
1201 if (ret <= 0)
1202 goto error;
1203 tmp += ret;
1204 len -= ret;
1205
1206 while (field) {
b2a3c12b
MH
1207 if (field->name[0] == '[')
1208 ret = e_snprintf(tmp, len, "%s", field->name);
1209 else
1210 ret = e_snprintf(tmp, len, "%s%s",
1211 field->ref ? "->" : ".", field->name);
7df2f329
MH
1212 if (ret <= 0)
1213 goto error;
1214 tmp += ret;
1215 len -= ret;
1216 field = field->next;
1217 }
11a1ca35
MH
1218
1219 if (pa->type) {
1220 ret = e_snprintf(tmp, len, ":%s", pa->type);
1221 if (ret <= 0)
1222 goto error;
1223 tmp += ret;
1224 len -= ret;
1225 }
1226
7df2f329
MH
1227 return tmp - buf;
1228error:
0e43e5d2 1229 pr_debug("Failed to synthesize perf probe argument: %s\n",
146a1439
MH
1230 strerror(-ret));
1231 return ret;
7df2f329
MH
1232}
1233
4235b045
MH
1234/* Compose only probe point (not argument) */
1235static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 1236{
fb1587d8
MH
1237 char *buf, *tmp;
1238 char offs[32] = "", line[32] = "", file[32] = "";
1239 int ret, len;
4de189fe 1240
e334016f
MH
1241 buf = zalloc(MAX_CMDLEN);
1242 if (buf == NULL) {
1243 ret = -ENOMEM;
1244 goto error;
1245 }
4de189fe 1246 if (pp->offset) {
fb1587d8 1247 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
1248 if (ret <= 0)
1249 goto error;
1250 }
1251 if (pp->line) {
fb1587d8
MH
1252 ret = e_snprintf(line, 32, ":%d", pp->line);
1253 if (ret <= 0)
1254 goto error;
1255 }
1256 if (pp->file) {
32ae2ade
FBH
1257 tmp = pp->file;
1258 len = strlen(tmp);
1259 if (len > 30) {
1260 tmp = strchr(pp->file + len - 30, '/');
1261 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1262 }
1263 ret = e_snprintf(file, 32, "@%s", tmp);
4de189fe
MH
1264 if (ret <= 0)
1265 goto error;
1266 }
1267
1268 if (pp->function)
fb1587d8
MH
1269 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1270 offs, pp->retprobe ? "%return" : "", line,
1271 file);
4de189fe 1272 else
fb1587d8 1273 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
1274 if (ret <= 0)
1275 goto error;
1276
1277 return buf;
7ef17aaf 1278error:
0e43e5d2 1279 pr_debug("Failed to synthesize perf probe point: %s\n",
146a1439 1280 strerror(-ret));
e334016f
MH
1281 if (buf)
1282 free(buf);
146a1439 1283 return NULL;
7ef17aaf
MH
1284}
1285
4235b045
MH
1286#if 0
1287char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
1288{
1289 char *buf;
1290 int i, len, ret;
1291
4235b045
MH
1292 buf = synthesize_perf_probe_point(&pev->point);
1293 if (!buf)
1294 return NULL;
4de189fe 1295
4235b045
MH
1296 len = strlen(buf);
1297 for (i = 0; i < pev->nargs; i++) {
4de189fe 1298 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
1299 pev->args[i].name);
1300 if (ret <= 0) {
1301 free(buf);
1302 return NULL;
1303 }
4de189fe
MH
1304 len += ret;
1305 }
4de189fe 1306
4235b045
MH
1307 return buf;
1308}
1309#endif
1310
0e60836b 1311static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
4235b045
MH
1312 char **buf, size_t *buflen,
1313 int depth)
1314{
1315 int ret;
1316 if (ref->next) {
0e60836b 1317 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
4235b045
MH
1318 buflen, depth + 1);
1319 if (depth < 0)
1320 goto out;
1321 }
1322
1323 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1324 if (ret < 0)
1325 depth = ret;
1326 else {
1327 *buf += ret;
1328 *buflen -= ret;
1329 }
1330out:
1331 return depth;
4de189fe 1332
4de189fe
MH
1333}
1334
0e60836b 1335static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
4235b045 1336 char *buf, size_t buflen)
50656eec 1337{
0e60836b 1338 struct probe_trace_arg_ref *ref = arg->ref;
4235b045
MH
1339 int ret, depth = 0;
1340 char *tmp = buf;
1341
1342 /* Argument name or separator */
1343 if (arg->name)
1344 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1345 else
1346 ret = e_snprintf(buf, buflen, " ");
1347 if (ret < 0)
1348 return ret;
1349 buf += ret;
1350 buflen -= ret;
1351
b7dcb857
MH
1352 /* Special case: @XXX */
1353 if (arg->value[0] == '@' && arg->ref)
1354 ref = ref->next;
1355
4235b045 1356 /* Dereferencing arguments */
b7dcb857 1357 if (ref) {
0e60836b 1358 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
4235b045
MH
1359 &buflen, 1);
1360 if (depth < 0)
1361 return depth;
1362 }
1363
1364 /* Print argument value */
b7dcb857
MH
1365 if (arg->value[0] == '@' && arg->ref)
1366 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1367 arg->ref->offset);
1368 else
1369 ret = e_snprintf(buf, buflen, "%s", arg->value);
4235b045
MH
1370 if (ret < 0)
1371 return ret;
1372 buf += ret;
1373 buflen -= ret;
1374
1375 /* Closing */
1376 while (depth--) {
1377 ret = e_snprintf(buf, buflen, ")");
1378 if (ret < 0)
1379 return ret;
1380 buf += ret;
1381 buflen -= ret;
1382 }
4984912e
MH
1383 /* Print argument type */
1384 if (arg->type) {
1385 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1386 if (ret <= 0)
1387 return ret;
1388 buf += ret;
1389 }
4235b045
MH
1390
1391 return buf - tmp;
1392}
1393
0e60836b 1394char *synthesize_probe_trace_command(struct probe_trace_event *tev)
4235b045 1395{
0e60836b 1396 struct probe_trace_point *tp = &tev->point;
50656eec
MH
1397 char *buf;
1398 int i, len, ret;
1399
e334016f
MH
1400 buf = zalloc(MAX_CMDLEN);
1401 if (buf == NULL)
1402 return NULL;
1403
225466f1
SD
1404 if (tev->uprobes)
1405 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1406 tp->retprobe ? 'r' : 'p',
1407 tev->group, tev->event,
1408 tp->module, tp->symbol);
1409 else
1410 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1411 tp->retprobe ? 'r' : 'p',
1412 tev->group, tev->event,
1413 tp->module ?: "", tp->module ? ":" : "",
1414 tp->symbol, tp->offset);
1415
4235b045 1416 if (len <= 0)
50656eec 1417 goto error;
50656eec 1418
4235b045 1419 for (i = 0; i < tev->nargs; i++) {
0e60836b 1420 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
4235b045 1421 MAX_CMDLEN - len);
4de189fe 1422 if (ret <= 0)
50656eec
MH
1423 goto error;
1424 len += ret;
1425 }
50656eec 1426
4235b045 1427 return buf;
50656eec 1428error:
4235b045
MH
1429 free(buf);
1430 return NULL;
1431}
50656eec 1432
0e60836b 1433static int convert_to_perf_probe_event(struct probe_trace_event *tev,
225466f1 1434 struct perf_probe_event *pev, bool is_kprobe)
4235b045 1435{
02b95dad 1436 char buf[64] = "";
146a1439 1437 int i, ret;
4235b045 1438
4b4da7f7 1439 /* Convert event/group name */
02b95dad
MH
1440 pev->event = strdup(tev->event);
1441 pev->group = strdup(tev->group);
1442 if (pev->event == NULL || pev->group == NULL)
1443 return -ENOMEM;
fb1587d8 1444
4b4da7f7 1445 /* Convert trace_point to probe_point */
225466f1
SD
1446 if (is_kprobe)
1447 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1448 else
1449 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1450
146a1439
MH
1451 if (ret < 0)
1452 return ret;
4b4da7f7 1453
4235b045
MH
1454 /* Convert trace_arg to probe_arg */
1455 pev->nargs = tev->nargs;
e334016f
MH
1456 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1457 if (pev->args == NULL)
1458 return -ENOMEM;
02b95dad 1459 for (i = 0; i < tev->nargs && ret >= 0; i++) {
4235b045 1460 if (tev->args[i].name)
02b95dad 1461 pev->args[i].name = strdup(tev->args[i].name);
4235b045 1462 else {
0e60836b 1463 ret = synthesize_probe_trace_arg(&tev->args[i],
146a1439 1464 buf, 64);
02b95dad 1465 pev->args[i].name = strdup(buf);
4235b045 1466 }
02b95dad
MH
1467 if (pev->args[i].name == NULL && ret >= 0)
1468 ret = -ENOMEM;
1469 }
146a1439
MH
1470
1471 if (ret < 0)
1472 clear_perf_probe_event(pev);
1473
1474 return ret;
4235b045
MH
1475}
1476
1477void clear_perf_probe_event(struct perf_probe_event *pev)
1478{
1479 struct perf_probe_point *pp = &pev->point;
7df2f329 1480 struct perf_probe_arg_field *field, *next;
4235b045
MH
1481 int i;
1482
1483 if (pev->event)
1484 free(pev->event);
1485 if (pev->group)
1486 free(pev->group);
1487 if (pp->file)
1488 free(pp->file);
1489 if (pp->function)
1490 free(pp->function);
1491 if (pp->lazy_line)
1492 free(pp->lazy_line);
7df2f329 1493 for (i = 0; i < pev->nargs; i++) {
4235b045
MH
1494 if (pev->args[i].name)
1495 free(pev->args[i].name);
48481938
MH
1496 if (pev->args[i].var)
1497 free(pev->args[i].var);
11a1ca35
MH
1498 if (pev->args[i].type)
1499 free(pev->args[i].type);
7df2f329
MH
1500 field = pev->args[i].field;
1501 while (field) {
1502 next = field->next;
1503 if (field->name)
1504 free(field->name);
1505 free(field);
1506 field = next;
1507 }
1508 }
4235b045
MH
1509 if (pev->args)
1510 free(pev->args);
1511 memset(pev, 0, sizeof(*pev));
1512}
1513
0e60836b 1514static void clear_probe_trace_event(struct probe_trace_event *tev)
4235b045 1515{
0e60836b 1516 struct probe_trace_arg_ref *ref, *next;
4235b045
MH
1517 int i;
1518
1519 if (tev->event)
1520 free(tev->event);
1521 if (tev->group)
1522 free(tev->group);
1523 if (tev->point.symbol)
1524 free(tev->point.symbol);
190b57fc
MH
1525 if (tev->point.module)
1526 free(tev->point.module);
4235b045
MH
1527 for (i = 0; i < tev->nargs; i++) {
1528 if (tev->args[i].name)
1529 free(tev->args[i].name);
1530 if (tev->args[i].value)
1531 free(tev->args[i].value);
4984912e
MH
1532 if (tev->args[i].type)
1533 free(tev->args[i].type);
4235b045
MH
1534 ref = tev->args[i].ref;
1535 while (ref) {
1536 next = ref->next;
1537 free(ref);
1538 ref = next;
1539 }
1540 }
1541 if (tev->args)
1542 free(tev->args);
1543 memset(tev, 0, sizeof(*tev));
50656eec
MH
1544}
1545
225466f1
SD
1546static void print_warn_msg(const char *file, bool is_kprobe)
1547{
1548
1549 if (errno == ENOENT) {
1550 const char *config;
1551
1552 if (!is_kprobe)
1553 config = "CONFIG_UPROBE_EVENTS";
1554 else
1555 config = "CONFIG_KPROBE_EVENTS";
1556
1557 pr_warning("%s file does not exist - please rebuild kernel"
1558 " with %s.\n", file, config);
1559 } else
1560 pr_warning("Failed to open %s file: %s\n", file,
1561 strerror(errno));
1562}
1563
1564static int open_probe_events(const char *trace_file, bool readwrite,
1565 bool is_kprobe)
4de189fe
MH
1566{
1567 char buf[PATH_MAX];
7ca5989d 1568 const char *__debugfs;
4de189fe
MH
1569 int ret;
1570
7ca5989d
MH
1571 __debugfs = debugfs_find_mountpoint();
1572 if (__debugfs == NULL) {
1573 pr_warning("Debugfs is not mounted.\n");
1574 return -ENOENT;
1575 }
1576
225466f1 1577 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
146a1439 1578 if (ret >= 0) {
7ca5989d 1579 pr_debug("Opening %s write=%d\n", buf, readwrite);
146a1439
MH
1580 if (readwrite && !probe_event_dry_run)
1581 ret = open(buf, O_RDWR, O_APPEND);
1582 else
1583 ret = open(buf, O_RDONLY, 0);
f4d7da49 1584
225466f1
SD
1585 if (ret < 0)
1586 print_warn_msg(buf, is_kprobe);
4de189fe
MH
1587 }
1588 return ret;
1589}
1590
225466f1
SD
1591static int open_kprobe_events(bool readwrite)
1592{
1593 return open_probe_events("tracing/kprobe_events", readwrite, true);
1594}
1595
1596static int open_uprobe_events(bool readwrite)
1597{
1598 return open_probe_events("tracing/uprobe_events", readwrite, false);
1599}
1600
1601/* Get raw string list of current kprobe_events or uprobe_events */
0e60836b 1602static struct strlist *get_probe_trace_command_rawlist(int fd)
4de189fe
MH
1603{
1604 int ret, idx;
1605 FILE *fp;
1606 char buf[MAX_CMDLEN];
1607 char *p;
1608 struct strlist *sl;
1609
1610 sl = strlist__new(true, NULL);
1611
1612 fp = fdopen(dup(fd), "r");
1613 while (!feof(fp)) {
1614 p = fgets(buf, MAX_CMDLEN, fp);
1615 if (!p)
1616 break;
1617
1618 idx = strlen(p) - 1;
1619 if (p[idx] == '\n')
1620 p[idx] = '\0';
1621 ret = strlist__add(sl, buf);
146a1439
MH
1622 if (ret < 0) {
1623 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1624 strlist__delete(sl);
1625 return NULL;
1626 }
4de189fe
MH
1627 }
1628 fclose(fp);
1629
1630 return sl;
1631}
1632
278498d4 1633/* Show an event */
146a1439 1634static int show_perf_probe_event(struct perf_probe_event *pev)
278498d4 1635{
7e990a51 1636 int i, ret;
278498d4 1637 char buf[128];
4235b045 1638 char *place;
278498d4 1639
4235b045
MH
1640 /* Synthesize only event probe point */
1641 place = synthesize_perf_probe_point(&pev->point);
146a1439
MH
1642 if (!place)
1643 return -EINVAL;
4235b045
MH
1644
1645 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
7e990a51 1646 if (ret < 0)
146a1439
MH
1647 return ret;
1648
fb1587d8 1649 printf(" %-20s (on %s", buf, place);
278498d4 1650
4235b045 1651 if (pev->nargs > 0) {
278498d4 1652 printf(" with");
7df2f329 1653 for (i = 0; i < pev->nargs; i++) {
146a1439
MH
1654 ret = synthesize_perf_probe_arg(&pev->args[i],
1655 buf, 128);
1656 if (ret < 0)
1657 break;
7df2f329
MH
1658 printf(" %s", buf);
1659 }
278498d4
MH
1660 }
1661 printf(")\n");
4235b045 1662 free(place);
146a1439 1663 return ret;
278498d4
MH
1664}
1665
225466f1 1666static int __show_perf_probe_events(int fd, bool is_kprobe)
4de189fe 1667{
225466f1 1668 int ret = 0;
0e60836b 1669 struct probe_trace_event tev;
4235b045 1670 struct perf_probe_event pev;
4de189fe
MH
1671 struct strlist *rawlist;
1672 struct str_node *ent;
1673
4235b045
MH
1674 memset(&tev, 0, sizeof(tev));
1675 memset(&pev, 0, sizeof(pev));
72041334 1676
0e60836b 1677 rawlist = get_probe_trace_command_rawlist(fd);
146a1439
MH
1678 if (!rawlist)
1679 return -ENOENT;
4de189fe 1680
adf365f4 1681 strlist__for_each(ent, rawlist) {
0e60836b 1682 ret = parse_probe_trace_command(ent->s, &tev);
146a1439 1683 if (ret >= 0) {
225466f1
SD
1684 ret = convert_to_perf_probe_event(&tev, &pev,
1685 is_kprobe);
146a1439
MH
1686 if (ret >= 0)
1687 ret = show_perf_probe_event(&pev);
1688 }
4235b045 1689 clear_perf_probe_event(&pev);
0e60836b 1690 clear_probe_trace_event(&tev);
146a1439
MH
1691 if (ret < 0)
1692 break;
4de189fe 1693 }
4de189fe 1694 strlist__delete(rawlist);
146a1439
MH
1695
1696 return ret;
4de189fe
MH
1697}
1698
225466f1
SD
1699/* List up current perf-probe events */
1700int show_perf_probe_events(void)
1701{
1702 int fd, ret;
1703
1704 setup_pager();
1705 fd = open_kprobe_events(false);
1706
1707 if (fd < 0)
1708 return fd;
1709
1710 ret = init_vmlinux();
1711 if (ret < 0)
1712 return ret;
1713
1714 ret = __show_perf_probe_events(fd, true);
1715 close(fd);
1716
1717 fd = open_uprobe_events(false);
1718 if (fd >= 0) {
1719 ret = __show_perf_probe_events(fd, false);
1720 close(fd);
1721 }
1722
1723 return ret;
1724}
1725
b498ce1f 1726/* Get current perf-probe event names */
0e60836b 1727static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
b498ce1f 1728{
fa28244d 1729 char buf[128];
b498ce1f
MH
1730 struct strlist *sl, *rawlist;
1731 struct str_node *ent;
0e60836b 1732 struct probe_trace_event tev;
146a1439 1733 int ret = 0;
b498ce1f 1734
4235b045 1735 memset(&tev, 0, sizeof(tev));
0e60836b 1736 rawlist = get_probe_trace_command_rawlist(fd);
e1d2017b 1737 sl = strlist__new(true, NULL);
adf365f4 1738 strlist__for_each(ent, rawlist) {
0e60836b 1739 ret = parse_probe_trace_command(ent->s, &tev);
146a1439
MH
1740 if (ret < 0)
1741 break;
fa28244d 1742 if (include_group) {
146a1439
MH
1743 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1744 tev.event);
1745 if (ret >= 0)
1746 ret = strlist__add(sl, buf);
fa28244d 1747 } else
146a1439 1748 ret = strlist__add(sl, tev.event);
0e60836b 1749 clear_probe_trace_event(&tev);
146a1439
MH
1750 if (ret < 0)
1751 break;
b498ce1f 1752 }
b498ce1f
MH
1753 strlist__delete(rawlist);
1754
146a1439
MH
1755 if (ret < 0) {
1756 strlist__delete(sl);
1757 return NULL;
1758 }
b498ce1f
MH
1759 return sl;
1760}
1761
0e60836b 1762static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
50656eec 1763{
6eca8cc3 1764 int ret = 0;
0e60836b 1765 char *buf = synthesize_probe_trace_command(tev);
50656eec 1766
146a1439 1767 if (!buf) {
0e60836b 1768 pr_debug("Failed to synthesize probe trace event.\n");
146a1439
MH
1769 return -EINVAL;
1770 }
1771
fa28244d 1772 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
1773 if (!probe_event_dry_run) {
1774 ret = write(fd, buf, strlen(buf));
1775 if (ret <= 0)
146a1439
MH
1776 pr_warning("Failed to write event: %s\n",
1777 strerror(errno));
f4d7da49 1778 }
4235b045 1779 free(buf);
146a1439 1780 return ret;
50656eec
MH
1781}
1782
146a1439
MH
1783static int get_new_event_name(char *buf, size_t len, const char *base,
1784 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
1785{
1786 int i, ret;
17f88fcd
MH
1787
1788 /* Try no suffix */
1789 ret = e_snprintf(buf, len, "%s", base);
146a1439
MH
1790 if (ret < 0) {
1791 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1792 return ret;
1793 }
17f88fcd 1794 if (!strlist__has_entry(namelist, buf))
146a1439 1795 return 0;
17f88fcd 1796
d761b08b
MH
1797 if (!allow_suffix) {
1798 pr_warning("Error: event \"%s\" already exists. "
1799 "(Use -f to force duplicates.)\n", base);
146a1439 1800 return -EEXIST;
d761b08b
MH
1801 }
1802
17f88fcd
MH
1803 /* Try to add suffix */
1804 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f 1805 ret = e_snprintf(buf, len, "%s_%d", base, i);
146a1439
MH
1806 if (ret < 0) {
1807 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1808 return ret;
1809 }
b498ce1f
MH
1810 if (!strlist__has_entry(namelist, buf))
1811 break;
1812 }
146a1439
MH
1813 if (i == MAX_EVENT_INDEX) {
1814 pr_warning("Too many events are on the same function.\n");
1815 ret = -ERANGE;
1816 }
1817
1818 return ret;
b498ce1f
MH
1819}
1820
0e60836b
SD
1821static int __add_probe_trace_events(struct perf_probe_event *pev,
1822 struct probe_trace_event *tevs,
146a1439 1823 int ntevs, bool allow_suffix)
50656eec 1824{
146a1439 1825 int i, fd, ret;
0e60836b 1826 struct probe_trace_event *tev = NULL;
4235b045
MH
1827 char buf[64];
1828 const char *event, *group;
b498ce1f 1829 struct strlist *namelist;
50656eec 1830
225466f1
SD
1831 if (pev->uprobes)
1832 fd = open_uprobe_events(true);
1833 else
1834 fd = open_kprobe_events(true);
1835
146a1439
MH
1836 if (fd < 0)
1837 return fd;
b498ce1f 1838 /* Get current event names */
0e60836b 1839 namelist = get_probe_trace_event_names(fd, false);
146a1439
MH
1840 if (!namelist) {
1841 pr_debug("Failed to get current event list.\n");
1842 return -EIO;
1843 }
4235b045 1844
146a1439 1845 ret = 0;
a844d1ef 1846 printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
02b95dad 1847 for (i = 0; i < ntevs; i++) {
4235b045
MH
1848 tev = &tevs[i];
1849 if (pev->event)
1850 event = pev->event;
1851 else
1852 if (pev->point.function)
1853 event = pev->point.function;
1854 else
1855 event = tev->point.symbol;
1856 if (pev->group)
1857 group = pev->group;
1858 else
1859 group = PERFPROBE_GROUP;
1860
1861 /* Get an unused new event name */
146a1439
MH
1862 ret = get_new_event_name(buf, 64, event,
1863 namelist, allow_suffix);
1864 if (ret < 0)
1865 break;
4235b045
MH
1866 event = buf;
1867
02b95dad
MH
1868 tev->event = strdup(event);
1869 tev->group = strdup(group);
1870 if (tev->event == NULL || tev->group == NULL) {
1871 ret = -ENOMEM;
1872 break;
1873 }
0e60836b 1874 ret = write_probe_trace_event(fd, tev);
146a1439
MH
1875 if (ret < 0)
1876 break;
4235b045
MH
1877 /* Add added event name to namelist */
1878 strlist__add(namelist, event);
1879
1880 /* Trick here - save current event/group */
1881 event = pev->event;
1882 group = pev->group;
1883 pev->event = tev->event;
1884 pev->group = tev->group;
1885 show_perf_probe_event(pev);
1886 /* Trick here - restore current event/group */
1887 pev->event = (char *)event;
1888 pev->group = (char *)group;
1889
1890 /*
1891 * Probes after the first probe which comes from same
1892 * user input are always allowed to add suffix, because
1893 * there might be several addresses corresponding to
1894 * one code line.
1895 */
1896 allow_suffix = true;
50656eec 1897 }
146a1439
MH
1898
1899 if (ret >= 0) {
1900 /* Show how to use the event. */
a844d1ef 1901 printf("\nYou can now use it in all perf tools, such as:\n\n");
146a1439
MH
1902 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1903 tev->event);
1904 }
a9b495b0 1905
e1d2017b 1906 strlist__delete(namelist);
50656eec 1907 close(fd);
146a1439 1908 return ret;
50656eec 1909}
fa28244d 1910
0e60836b
SD
1911static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1912 struct probe_trace_event **tevs,
4eced234 1913 int max_tevs, const char *target)
e0faa8d3
MH
1914{
1915 struct symbol *sym;
e334016f 1916 int ret = 0, i;
0e60836b 1917 struct probe_trace_event *tev;
4235b045 1918
4b4da7f7 1919 /* Convert perf_probe_event with debuginfo */
4eced234 1920 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
e334016f 1921 if (ret != 0)
190b57fc 1922 return ret; /* Found in debuginfo or got an error */
e0faa8d3 1923
4235b045 1924 /* Allocate trace event buffer */
0e60836b 1925 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
e334016f
MH
1926 if (tev == NULL)
1927 return -ENOMEM;
4235b045
MH
1928
1929 /* Copy parameters */
02b95dad
MH
1930 tev->point.symbol = strdup(pev->point.function);
1931 if (tev->point.symbol == NULL) {
1932 ret = -ENOMEM;
1933 goto error;
1934 }
ce27a443 1935
4eced234
SD
1936 if (target) {
1937 tev->point.module = strdup(target);
ce27a443
JZ
1938 if (tev->point.module == NULL) {
1939 ret = -ENOMEM;
1940 goto error;
1941 }
190b57fc 1942 }
ce27a443 1943
4235b045 1944 tev->point.offset = pev->point.offset;
04ddd04b 1945 tev->point.retprobe = pev->point.retprobe;
4235b045 1946 tev->nargs = pev->nargs;
225466f1
SD
1947 tev->uprobes = pev->uprobes;
1948
4235b045 1949 if (tev->nargs) {
0e60836b 1950 tev->args = zalloc(sizeof(struct probe_trace_arg)
e334016f
MH
1951 * tev->nargs);
1952 if (tev->args == NULL) {
02b95dad
MH
1953 ret = -ENOMEM;
1954 goto error;
e334016f 1955 }
48481938 1956 for (i = 0; i < tev->nargs; i++) {
02b95dad
MH
1957 if (pev->args[i].name) {
1958 tev->args[i].name = strdup(pev->args[i].name);
1959 if (tev->args[i].name == NULL) {
1960 ret = -ENOMEM;
1961 goto error;
1962 }
1963 }
1964 tev->args[i].value = strdup(pev->args[i].var);
1965 if (tev->args[i].value == NULL) {
1966 ret = -ENOMEM;
1967 goto error;
1968 }
1969 if (pev->args[i].type) {
1970 tev->args[i].type = strdup(pev->args[i].type);
1971 if (tev->args[i].type == NULL) {
1972 ret = -ENOMEM;
1973 goto error;
1974 }
1975 }
48481938 1976 }
4235b045
MH
1977 }
1978
225466f1
SD
1979 if (pev->uprobes)
1980 return 1;
1981
4235b045 1982 /* Currently just checking function name from symbol map */
469b9b88 1983 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
146a1439
MH
1984 if (!sym) {
1985 pr_warning("Kernel symbol \'%s\' not found.\n",
1986 tev->point.symbol);
02b95dad
MH
1987 ret = -ENOENT;
1988 goto error;
1c1bc922
PN
1989 } else if (tev->point.offset > sym->end - sym->start) {
1990 pr_warning("Offset specified is greater than size of %s\n",
1991 tev->point.symbol);
1992 ret = -ENOENT;
1993 goto error;
1994
02b95dad 1995 }
e334016f 1996
02b95dad
MH
1997 return 1;
1998error:
0e60836b 1999 clear_probe_trace_event(tev);
02b95dad
MH
2000 free(tev);
2001 *tevs = NULL;
e334016f 2002 return ret;
4235b045
MH
2003}
2004
2005struct __event_package {
2006 struct perf_probe_event *pev;
0e60836b 2007 struct probe_trace_event *tevs;
4235b045
MH
2008 int ntevs;
2009};
2010
146a1439 2011int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
4eced234 2012 int max_tevs, const char *target, bool force_add)
4235b045 2013{
146a1439 2014 int i, j, ret;
4235b045
MH
2015 struct __event_package *pkgs;
2016
225466f1 2017 ret = 0;
e334016f 2018 pkgs = zalloc(sizeof(struct __event_package) * npevs);
225466f1 2019
e334016f
MH
2020 if (pkgs == NULL)
2021 return -ENOMEM;
4235b045 2022
225466f1
SD
2023 if (!pevs->uprobes)
2024 /* Init vmlinux path */
2025 ret = init_vmlinux();
2026 else
2027 ret = init_user_exec();
2028
449e5b24
MH
2029 if (ret < 0) {
2030 free(pkgs);
146a1439 2031 return ret;
449e5b24 2032 }
4235b045
MH
2033
2034 /* Loop 1: convert all events */
2035 for (i = 0; i < npevs; i++) {
2036 pkgs[i].pev = &pevs[i];
2037 /* Convert with or without debuginfo */
0e60836b 2038 ret = convert_to_probe_trace_events(pkgs[i].pev,
469b9b88
MH
2039 &pkgs[i].tevs,
2040 max_tevs,
4eced234 2041 target);
146a1439
MH
2042 if (ret < 0)
2043 goto end;
2044 pkgs[i].ntevs = ret;
e0faa8d3
MH
2045 }
2046
4235b045 2047 /* Loop 2: add all events */
8635bf6e 2048 for (i = 0; i < npevs; i++) {
0e60836b 2049 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
146a1439 2050 pkgs[i].ntevs, force_add);
fbee632d
ACM
2051 if (ret < 0)
2052 break;
2053 }
146a1439 2054end:
449e5b24
MH
2055 /* Loop 3: cleanup and free trace events */
2056 for (i = 0; i < npevs; i++) {
146a1439 2057 for (j = 0; j < pkgs[i].ntevs; j++)
0e60836b 2058 clear_probe_trace_event(&pkgs[i].tevs[j]);
449e5b24
MH
2059 free(pkgs[i].tevs);
2060 }
2061 free(pkgs);
146a1439
MH
2062
2063 return ret;
e0faa8d3
MH
2064}
2065
0e60836b 2066static int __del_trace_probe_event(int fd, struct str_node *ent)
bbbb521b
MH
2067{
2068 char *p;
2069 char buf[128];
4235b045 2070 int ret;
bbbb521b 2071
0e60836b 2072 /* Convert from perf-probe event to trace-probe event */
146a1439
MH
2073 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2074 if (ret < 0)
2075 goto error;
2076
bbbb521b 2077 p = strchr(buf + 2, ':');
146a1439
MH
2078 if (!p) {
2079 pr_debug("Internal error: %s should have ':' but not.\n",
2080 ent->s);
2081 ret = -ENOTSUP;
2082 goto error;
2083 }
bbbb521b
MH
2084 *p = '/';
2085
4235b045
MH
2086 pr_debug("Writing event: %s\n", buf);
2087 ret = write(fd, buf, strlen(buf));
44a56040
MH
2088 if (ret < 0) {
2089 ret = -errno;
146a1439 2090 goto error;
44a56040 2091 }
146a1439 2092
a844d1ef 2093 printf("Removed event: %s\n", ent->s);
146a1439
MH
2094 return 0;
2095error:
2096 pr_warning("Failed to delete event: %s\n", strerror(-ret));
2097 return ret;
bbbb521b
MH
2098}
2099
225466f1
SD
2100static int del_trace_probe_event(int fd, const char *buf,
2101 struct strlist *namelist)
fa28244d 2102{
bbbb521b 2103 struct str_node *ent, *n;
225466f1 2104 int ret = -1;
fa28244d 2105
bbbb521b
MH
2106 if (strpbrk(buf, "*?")) { /* Glob-exp */
2107 strlist__for_each_safe(ent, n, namelist)
2108 if (strglobmatch(ent->s, buf)) {
0e60836b 2109 ret = __del_trace_probe_event(fd, ent);
146a1439
MH
2110 if (ret < 0)
2111 break;
bbbb521b
MH
2112 strlist__remove(namelist, ent);
2113 }
2114 } else {
2115 ent = strlist__find(namelist, buf);
2116 if (ent) {
0e60836b 2117 ret = __del_trace_probe_event(fd, ent);
146a1439
MH
2118 if (ret >= 0)
2119 strlist__remove(namelist, ent);
bbbb521b
MH
2120 }
2121 }
146a1439
MH
2122
2123 return ret;
fa28244d
MH
2124}
2125
146a1439 2126int del_perf_probe_events(struct strlist *dellist)
fa28244d 2127{
225466f1
SD
2128 int ret = -1, ufd = -1, kfd = -1;
2129 char buf[128];
fa28244d
MH
2130 const char *group, *event;
2131 char *p, *str;
2132 struct str_node *ent;
225466f1 2133 struct strlist *namelist = NULL, *unamelist = NULL;
146a1439 2134
fa28244d 2135 /* Get current event names */
225466f1
SD
2136 kfd = open_kprobe_events(true);
2137 if (kfd < 0)
2138 return kfd;
2139
2140 namelist = get_probe_trace_event_names(kfd, true);
2141 ufd = open_uprobe_events(true);
2142
2143 if (ufd >= 0)
2144 unamelist = get_probe_trace_event_names(ufd, true);
2145
2146 if (namelist == NULL && unamelist == NULL)
2147 goto error;
fa28244d 2148
adf365f4 2149 strlist__for_each(ent, dellist) {
02b95dad
MH
2150 str = strdup(ent->s);
2151 if (str == NULL) {
2152 ret = -ENOMEM;
225466f1 2153 goto error;
02b95dad 2154 }
bbbb521b 2155 pr_debug("Parsing: %s\n", str);
fa28244d
MH
2156 p = strchr(str, ':');
2157 if (p) {
2158 group = str;
2159 *p = '\0';
2160 event = p + 1;
2161 } else {
bbbb521b 2162 group = "*";
fa28244d
MH
2163 event = str;
2164 }
225466f1
SD
2165
2166 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2167 if (ret < 0) {
2168 pr_err("Failed to copy event.");
2169 free(str);
2170 goto error;
2171 }
2172
bbbb521b 2173 pr_debug("Group: %s, Event: %s\n", group, event);
225466f1
SD
2174
2175 if (namelist)
2176 ret = del_trace_probe_event(kfd, buf, namelist);
2177
2178 if (unamelist && ret != 0)
2179 ret = del_trace_probe_event(ufd, buf, unamelist);
2180
2181 if (ret != 0)
2182 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2183
fa28244d
MH
2184 free(str);
2185 }
225466f1
SD
2186
2187error:
2188 if (kfd >= 0) {
a23c4dc4 2189 strlist__delete(namelist);
225466f1
SD
2190 close(kfd);
2191 }
2192
2193 if (ufd >= 0) {
a23c4dc4 2194 strlist__delete(unamelist);
225466f1
SD
2195 close(ufd);
2196 }
146a1439
MH
2197
2198 return ret;
fa28244d 2199}
225466f1 2200
3c42258c
MH
2201/* TODO: don't use a global variable for filter ... */
2202static struct strfilter *available_func_filter;
fa28244d 2203
e80711ca 2204/*
3c42258c
MH
2205 * If a symbol corresponds to a function with global binding and
2206 * matches filter return 0. For all others return 1.
e80711ca 2207 */
1d037ca1 2208static int filter_available_functions(struct map *map __maybe_unused,
3c42258c 2209 struct symbol *sym)
e80711ca 2210{
3c42258c
MH
2211 if (sym->binding == STB_GLOBAL &&
2212 strfilter__compare(available_func_filter, sym->name))
2213 return 0;
2214 return 1;
e80711ca
MH
2215}
2216
225466f1
SD
2217static int __show_available_funcs(struct map *map)
2218{
2219 if (map__load(map, filter_available_functions)) {
2220 pr_err("Failed to load map.\n");
2221 return -EINVAL;
2222 }
2223 if (!dso__sorted_by_name(map->dso, map->type))
2224 dso__sort_by_name(map->dso, map->type);
2225
2226 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2227 return 0;
2228}
2229
2230static int available_kernel_funcs(const char *module)
e80711ca
MH
2231{
2232 struct map *map;
2233 int ret;
2234
e80711ca
MH
2235 ret = init_vmlinux();
2236 if (ret < 0)
2237 return ret;
2238
225466f1 2239 map = kernel_get_module_map(module);
e80711ca 2240 if (!map) {
225466f1 2241 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
e80711ca
MH
2242 return -EINVAL;
2243 }
225466f1
SD
2244 return __show_available_funcs(map);
2245}
2246
2247static int available_user_funcs(const char *target)
2248{
2249 struct map *map;
2250 int ret;
2251
2252 ret = init_user_exec();
2253 if (ret < 0)
2254 return ret;
2255
2256 map = dso__new_map(target);
2257 ret = __show_available_funcs(map);
2258 dso__delete(map->dso);
2259 map__delete(map);
2260 return ret;
2261}
2262
2263int show_available_funcs(const char *target, struct strfilter *_filter,
2264 bool user)
2265{
2266 setup_pager();
3c42258c 2267 available_func_filter = _filter;
225466f1
SD
2268
2269 if (!user)
2270 return available_kernel_funcs(target);
2271
2272 return available_user_funcs(target);
2273}
2274
2275/*
2276 * uprobe_events only accepts address:
2277 * Convert function and any offset to address
2278 */
2279static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2280{
2281 struct perf_probe_point *pp = &pev->point;
2282 struct symbol *sym;
2283 struct map *map = NULL;
2284 char *function = NULL, *name = NULL;
2285 int ret = -EINVAL;
2286 unsigned long long vaddr = 0;
2287
2288 if (!pp->function) {
2289 pr_warning("No function specified for uprobes");
2290 goto out;
2291 }
2292
2293 function = strdup(pp->function);
2294 if (!function) {
2295 pr_warning("Failed to allocate memory by strdup.\n");
2296 ret = -ENOMEM;
2297 goto out;
2298 }
2299
2300 name = realpath(exec, NULL);
2301 if (!name) {
2302 pr_warning("Cannot find realpath for %s.\n", exec);
2303 goto out;
2304 }
2305 map = dso__new_map(name);
2306 if (!map) {
2307 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2308 goto out;
2309 }
2310 available_func_filter = strfilter__new(function, NULL);
3c42258c 2311 if (map__load(map, filter_available_functions)) {
e80711ca 2312 pr_err("Failed to load map.\n");
225466f1 2313 goto out;
e80711ca 2314 }
e80711ca 2315
225466f1
SD
2316 sym = map__find_symbol_by_name(map, function, NULL);
2317 if (!sym) {
2318 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2319 goto out;
2320 }
2321
2322 if (map->start > sym->start)
2323 vaddr = map->start;
2324 vaddr += sym->start + pp->offset + map->pgoff;
2325 pp->offset = 0;
2326
2327 if (!pev->event) {
2328 pev->event = function;
2329 function = NULL;
2330 }
2331 if (!pev->group) {
1fb89448 2332 char *ptr1, *ptr2, *exec_copy;
225466f1
SD
2333
2334 pev->group = zalloc(sizeof(char *) * 64);
1fb89448
DA
2335 exec_copy = strdup(exec);
2336 if (!exec_copy) {
2337 ret = -ENOMEM;
2338 pr_warning("Failed to copy exec string.\n");
2339 goto out;
2340 }
2341
2342 ptr1 = strdup(basename(exec_copy));
225466f1
SD
2343 if (ptr1) {
2344 ptr2 = strpbrk(ptr1, "-._");
2345 if (ptr2)
2346 *ptr2 = '\0';
2347 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2348 ptr1);
2349 free(ptr1);
2350 }
1fb89448 2351 free(exec_copy);
225466f1
SD
2352 }
2353 free(pp->function);
2354 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2355 if (!pp->function) {
2356 ret = -ENOMEM;
2357 pr_warning("Failed to allocate memory by zalloc.\n");
2358 goto out;
2359 }
2360 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2361 ret = 0;
2362
2363out:
2364 if (map) {
2365 dso__delete(map->dso);
2366 map__delete(map);
2367 }
2368 if (function)
2369 free(function);
2370 if (name)
2371 free(name);
2372 return ret;
e80711ca 2373}
This page took 0.271869 seconds and 5 git commands to generate.