blob: 4cfd1211a2ae0ad166d435a8857304618b2bda3a [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
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#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>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
54
Masami Hiramatsu146a1432010-04-12 13:17:42 -040055#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050056
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050057/* If there is no space to write, returns -E2BIG. */
58static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050059 __attribute__((format(printf, 3, 4)));
60
61static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050062{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030073static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000074static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000075static struct machine *host_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 Hiramatsuee45b6c2014-02-06 05:32:04 +000078static int init_symbol_maps(bool user_only)
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;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090083 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040084 if (ret < 0) {
85 pr_debug("Failed to init symbol map.\n");
86 goto out;
87 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (host_machine || user_only) /* already initialized */
90 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030091
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000092 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94
95 host_machine = machine__new_host();
96 if (!host_machine) {
97 pr_debug("machine__new_host() failed.\n");
98 symbol__exit();
99 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 }
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 Hiramatsuee45b6c2014-02-06 05:32:04 +0000107static void exit_symbol_maps(void)
108{
109 if (host_machine) {
110 machine__delete(host_machine);
111 host_machine = NULL;
112 }
113 symbol__exit();
114}
115
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900116static struct symbol *__find_kernel_function_by_name(const char *name,
117 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400118{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000119 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120 NULL);
121}
122
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000123static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
124{
125 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
126}
127
128static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
129{
130 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 struct kmap *kmap;
132
133 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
134 return NULL;
135
136 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800154 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
161 struct rb_node *nd;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000162 struct map_groups *grp = &host_machine->kmaps;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000166 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
171 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
172 struct map *pos = rb_entry(nd, struct map, rb_node);
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900181static struct map *get_target_map(const char *target, bool user)
182{
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193 /* Only the user map needs to be released */
194 dso__delete(map->dso);
195 map__delete(map);
196 }
197}
198
199
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900200static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900201{
202 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100203 struct map *map;
204 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900205
206 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400207 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
208 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900209 if (strncmp(dso->short_name + 1, module,
210 dso->short_name_len - 2) == 0)
211 goto found;
212 }
213 pr_debug("Failed to find module %s.\n", module);
214 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100215 }
216
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000217 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100218 dso = map->dso;
219
220 vmlinux_name = symbol_conf.vmlinux_name;
221 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300222 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100223 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900224 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100225 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900226 pr_debug("Failed to load kernel map.\n");
227 return NULL;
228 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400229 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900230found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900231 return dso;
232}
233
234const char *kernel_get_module_path(const char *module)
235{
236 struct dso *dso = kernel_get_module_dso(module);
237 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900238}
239
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000240static int convert_exec_to_group(const char *exec, char **result)
241{
242 char *ptr1, *ptr2, *exec_copy;
243 char buf[64];
244 int ret;
245
246 exec_copy = strdup(exec);
247 if (!exec_copy)
248 return -ENOMEM;
249
250 ptr1 = basename(exec_copy);
251 if (!ptr1) {
252 ret = -EINVAL;
253 goto out;
254 }
255
256 ptr2 = strpbrk(ptr1, "-._");
257 if (ptr2)
258 *ptr2 = '\0';
259 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
260 if (ret < 0)
261 goto out;
262
263 *result = strdup(buf);
264 ret = *result ? 0 : -ENOMEM;
265
266out:
267 free(exec_copy);
268 return ret;
269}
270
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900271static void clear_perf_probe_point(struct perf_probe_point *pp)
272{
273 free(pp->file);
274 free(pp->function);
275 free(pp->lazy_line);
276}
277
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000278static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
279{
280 int i;
281
282 for (i = 0; i < ntevs; i++)
283 clear_probe_trace_event(tevs + i);
284}
285
Ingo Molnar89fe8082013-09-30 12:07:11 +0200286#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900287/*
288 * Some binaries like glibc have special symbols which are on the symbol
289 * table, but not in the debuginfo. If we can find the address of the
290 * symbol from map, we can translate the address back to the probe point.
291 */
292static int find_alternative_probe_point(struct debuginfo *dinfo,
293 struct perf_probe_point *pp,
294 struct perf_probe_point *result,
295 const char *target, bool uprobes)
296{
297 struct map *map = NULL;
298 struct symbol *sym;
299 u64 address = 0;
300 int ret = -ENOENT;
301
302 /* This can work only for function-name based one */
303 if (!pp->function || pp->file)
304 return -ENOTSUP;
305
306 map = get_target_map(target, uprobes);
307 if (!map)
308 return -EINVAL;
309
310 /* Find the address of given function */
311 map__for_each_symbol_by_name(map, pp->function, sym) {
312 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) {
313 address = sym->start;
314 break;
315 }
316 }
317 if (!address) {
318 ret = -ENOENT;
319 goto out;
320 }
321 pr_debug("Symbol %s address found : %lx\n", pp->function, address);
322
323 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
324 result);
325 if (ret <= 0)
326 ret = (!ret) ? -ENOENT : ret;
327 else {
328 result->offset += pp->offset;
329 result->line += pp->line;
330 ret = 0;
331 }
332
333out:
334 put_target_map(map, uprobes);
335 return ret;
336
337}
338
339static int get_alternative_probe_event(struct debuginfo *dinfo,
340 struct perf_probe_event *pev,
341 struct perf_probe_point *tmp,
342 const char *target)
343{
344 int ret;
345
346 memcpy(tmp, &pev->point, sizeof(*tmp));
347 memset(&pev->point, 0, sizeof(pev->point));
348 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
349 target, pev->uprobes);
350 if (ret < 0)
351 memcpy(&pev->point, tmp, sizeof(*tmp));
352
353 return ret;
354}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000355
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900356static int get_alternative_line_range(struct debuginfo *dinfo,
357 struct line_range *lr,
358 const char *target, bool user)
359{
360 struct perf_probe_point pp = { 0 }, result = { 0 };
361 int ret, len = 0;
362
363 pp.function = lr->function;
364 pp.file = lr->file;
365 pp.line = lr->start;
366 if (lr->end != INT_MAX)
367 len = lr->end - lr->start;
368 ret = find_alternative_probe_point(dinfo, &pp, &result,
369 target, user);
370 if (!ret) {
371 lr->function = result.function;
372 lr->file = result.file;
373 lr->start = result.line;
374 if (lr->end != INT_MAX)
375 lr->end = lr->start + len;
376 clear_perf_probe_point(&pp);
377 }
378 return ret;
379}
380
Masami Hiramatsuff741782011-06-27 16:27:39 +0900381/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000382static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900383{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000384 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000385 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900386
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000387 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900388 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900389 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000390 if (!silent)
391 pr_err("Failed to find path of %s module.\n",
392 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900393 return NULL;
394 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900395 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000396 ret = debuginfo__new(path);
397 if (!ret && !silent) {
398 pr_warning("The %s file has no debug information.\n", path);
399 if (!module || !strtailcmp(path, ".ko"))
400 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
401 else
402 pr_warning("Rebuild with -g, ");
403 pr_warning("or install an appropriate debuginfo package.\n");
404 }
405 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400406}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300407
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000408
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000409static int get_text_start_address(const char *exec, unsigned long *address)
410{
411 Elf *elf;
412 GElf_Ehdr ehdr;
413 GElf_Shdr shdr;
414 int fd, ret = -ENOENT;
415
416 fd = open(exec, O_RDONLY);
417 if (fd < 0)
418 return -errno;
419
420 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
421 if (elf == NULL)
422 return -EINVAL;
423
424 if (gelf_getehdr(elf, &ehdr) == NULL)
425 goto out;
426
427 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
428 goto out;
429
430 *address = shdr.sh_addr - shdr.sh_offset;
431 ret = 0;
432out:
433 elf_end(elf);
434 return ret;
435}
436
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000437/*
438 * Convert trace point to probe point with debuginfo
439 */
440static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
441 struct perf_probe_point *pp,
442 bool is_kprobe)
443{
444 struct debuginfo *dinfo = NULL;
445 unsigned long stext = 0;
446 u64 addr = tp->address;
447 int ret = -ENOENT;
448
449 /* convert the address to dwarf address */
450 if (!is_kprobe) {
451 if (!addr) {
452 ret = -EINVAL;
453 goto error;
454 }
455 ret = get_text_start_address(tp->module, &stext);
456 if (ret < 0)
457 goto error;
458 addr += stext;
459 } else {
460 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
461 if (addr == 0)
462 goto error;
463 addr += tp->offset;
464 }
465
466 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
467 tp->module ? : "kernel");
468
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000469 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000470 if (dinfo) {
471 ret = debuginfo__find_probe_point(dinfo,
472 (unsigned long)addr, pp);
473 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000474 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000475 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000476
477 if (ret > 0) {
478 pp->retprobe = tp->retprobe;
479 return 0;
480 }
481error:
482 pr_debug("Failed to find corresponding probes from debuginfo.\n");
483 return ret ? : -ENOENT;
484}
485
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000486static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
487 int ntevs, const char *exec)
488{
489 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000490 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000491
492 if (!exec)
493 return 0;
494
495 ret = get_text_start_address(exec, &stext);
496 if (ret < 0)
497 return ret;
498
499 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000500 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000501 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000502 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000503 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000504 ret = -ENOMEM;
505 break;
506 }
507 tevs[i].uprobes = true;
508 }
509
510 return ret;
511}
512
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900513static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
514 int ntevs, const char *module)
515{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900516 int i, ret = 0;
517 char *tmp;
518
519 if (!module)
520 return 0;
521
522 tmp = strrchr(module, '/');
523 if (tmp) {
524 /* This is a module path -- get the module name */
525 module = strdup(tmp + 1);
526 if (!module)
527 return -ENOMEM;
528 tmp = strchr(module, '.');
529 if (tmp)
530 *tmp = '\0';
531 tmp = (char *)module; /* For free() */
532 }
533
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900534 for (i = 0; i < ntevs; i++) {
535 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900536 if (!tevs[i].point.module) {
537 ret = -ENOMEM;
538 break;
539 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900540 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900541
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300542 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900543 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900544}
545
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000546/* Post processing the probe events */
547static int post_process_probe_trace_events(struct probe_trace_event *tevs,
548 int ntevs, const char *module,
549 bool uprobe)
550{
551 struct ref_reloc_sym *reloc_sym;
552 char *tmp;
553 int i;
554
555 if (uprobe)
556 return add_exec_to_probe_trace_events(tevs, ntevs, module);
557
558 /* Note that currently ref_reloc_sym based probe is not for drivers */
559 if (module)
560 return add_module_to_probe_trace_events(tevs, ntevs, module);
561
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000562 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000563 if (!reloc_sym) {
564 pr_warning("Relocated base symbol is not found!\n");
565 return -EINVAL;
566 }
567
568 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900569 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000570 tmp = strdup(reloc_sym->name);
571 if (!tmp)
572 return -ENOMEM;
573 free(tevs[i].point.symbol);
574 tevs[i].point.symbol = tmp;
575 tevs[i].point.offset = tevs[i].point.address -
576 reloc_sym->unrelocated_addr;
577 }
578 }
579 return 0;
580}
581
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300582/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530583static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900584 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530585 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300586{
587 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900588 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530589 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900590 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300591
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000592 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530593
Masami Hiramatsuff741782011-06-27 16:27:39 +0900594 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000595 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900596 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900597 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300598 return 0;
599 }
600
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000601 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900602 /* Searching trace events corresponding to a probe event */
603 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
604
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900605 if (ntevs == 0) { /* Not found, retry with an alternative */
606 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
607 if (!ret) {
608 ntevs = debuginfo__find_trace_events(dinfo, pev,
609 tevs, max_tevs);
610 /*
611 * Write back to the original probe_event for
612 * setting appropriate (user given) event name
613 */
614 clear_perf_probe_point(&pev->point);
615 memcpy(&pev->point, &tmp, sizeof(tmp));
616 }
617 }
618
Masami Hiramatsuff741782011-06-27 16:27:39 +0900619 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300620
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400621 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000622 pr_debug("Found %d probe_trace_events.\n", ntevs);
623 ret = post_process_probe_trace_events(*tevs, ntevs,
624 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000625 if (ret < 0) {
626 clear_probe_trace_events(*tevs, ntevs);
627 zfree(tevs);
628 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900629 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300631
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400632 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900633 pr_warning("Probe point '%s' not found in debuginfo.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400634 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900635 if (need_dwarf)
636 return -ENOENT;
637 return 0;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400638 }
639 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400640 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
641 if (ntevs == -EBADF) {
642 pr_warning("Warning: No dwarf info found in the vmlinux - "
643 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
644 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900645 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400646 return 0;
647 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300648 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400649 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300650}
651
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900652/*
653 * Find a src file from a DWARF tag path. Prepend optional source path prefix
654 * and chop off leading directories that do not exist. Result is passed back as
655 * a newly allocated path on success.
656 * Return 0 if file was found and readable, -errno otherwise.
657 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900658static int get_real_path(const char *raw_path, const char *comp_dir,
659 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900660{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900661 const char *prefix = symbol_conf.source_prefix;
662
663 if (!prefix) {
664 if (raw_path[0] != '/' && comp_dir)
665 /* If not an absolute path, try to use comp_dir */
666 prefix = comp_dir;
667 else {
668 if (access(raw_path, R_OK) == 0) {
669 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300670 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900671 } else
672 return -errno;
673 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900674 }
675
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900676 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900677 if (!*new_path)
678 return -ENOMEM;
679
680 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900681 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900682
683 if (access(*new_path, R_OK) == 0)
684 return 0;
685
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900686 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900687 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900688 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900689 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900690 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900691
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900692 switch (errno) {
693 case ENAMETOOLONG:
694 case ENOENT:
695 case EROFS:
696 case EFAULT:
697 raw_path = strchr(++raw_path, '/');
698 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300699 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900700 return -ENOENT;
701 }
702 continue;
703
704 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300705 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900706 return -errno;
707 }
708 }
709}
710
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300711#define LINEBUF_SIZE 256
712#define NR_ADDITIONAL_LINES 2
713
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100714static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300715{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000716 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100717 const char *color = show_num ? "" : PERF_COLOR_BLUE;
718 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100720 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300721 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
722 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100723 if (skip)
724 continue;
725 if (!prefix) {
726 prefix = show_num ? "%7d " : " ";
727 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300728 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100729 color_fprintf(stdout, color, "%s", buf);
730
731 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400732
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100733 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300734error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100735 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000736 pr_warning("File read error: %s\n",
737 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100738 return -1;
739 }
740 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300741}
742
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100743static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
744{
745 int rv = __show_one_line(fp, l, skip, show_num);
746 if (rv == 0) {
747 pr_warning("Source file is shorter than expected.\n");
748 rv = -1;
749 }
750 return rv;
751}
752
753#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
754#define show_one_line(f,l) _show_one_line(f,l,false,false)
755#define skip_one_line(f,l) _show_one_line(f,l,true,false)
756#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
757
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300758/*
759 * Show line-range always requires debuginfo to find source file and
760 * line number.
761 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900762static int __show_line_range(struct line_range *lr, const char *module,
763 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300764{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400765 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000766 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900767 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300768 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900769 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900770 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000771 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300772
773 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000774 dinfo = open_debuginfo(module, false);
775 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900776 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400777
Masami Hiramatsuff741782011-06-27 16:27:39 +0900778 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900779 if (!ret) { /* Not found, retry with an alternative */
780 ret = get_alternative_line_range(dinfo, lr, module, user);
781 if (!ret)
782 ret = debuginfo__find_line_range(dinfo, lr);
783 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900784 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000785 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400786 pr_warning("Specified source line is not found.\n");
787 return -ENOENT;
788 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000789 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400790 return ret;
791 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300792
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900793 /* Convert source file path */
794 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900795 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900796 free(tmp); /* Free old path */
797 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000798 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900799 return ret;
800 }
801
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300802 setup_pager();
803
804 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900805 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806 lr->start - lr->offset);
807 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100808 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300809
810 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400811 if (fp == NULL) {
812 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000813 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400814 return -errno;
815 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100817 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100818 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100819 if (ret < 0)
820 goto end;
821 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000823 intlist__for_each(ln, lr->line_list) {
824 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100825 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100826 if (ret < 0)
827 goto end;
828 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100829 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400830 if (ret < 0)
831 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300832 }
833
834 if (lr->end == INT_MAX)
835 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100836 while (l <= lr->end) {
837 ret = show_one_line_or_eof(fp, l++ - lr->offset);
838 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100839 break;
840 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400841end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300842 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400843 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300844}
845
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000846int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000847{
848 int ret;
849
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000850 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000851 if (ret < 0)
852 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900853 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000854 exit_symbol_maps();
855
856 return ret;
857}
858
Masami Hiramatsuff741782011-06-27 16:27:39 +0900859static int show_available_vars_at(struct debuginfo *dinfo,
860 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900861 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900862 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900863{
864 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900865 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900866 struct str_node *node;
867 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900868 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900869 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900870
871 buf = synthesize_perf_probe_point(&pev->point);
872 if (!buf)
873 return -EINVAL;
874 pr_debug("Searching variables at %s\n", buf);
875
Masami Hiramatsuff741782011-06-27 16:27:39 +0900876 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
877 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900878 if (!ret) { /* Not found, retry with an alternative */
879 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
880 if (!ret) {
881 ret = debuginfo__find_available_vars_at(dinfo, pev,
882 &vls, max_vls, externs);
883 /* Release the old probe_point */
884 clear_perf_probe_point(&tmp);
885 }
886 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900887 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000888 if (ret == 0 || ret == -ENOENT) {
889 pr_err("Failed to find the address of %s\n", buf);
890 ret = -ENOENT;
891 } else
892 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900893 goto end;
894 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000895
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900896 /* Some variables are found */
897 fprintf(stdout, "Available variables at %s\n", buf);
898 for (i = 0; i < ret; i++) {
899 vl = &vls[i];
900 /*
901 * A probe point might be converted to
902 * several trace points.
903 */
904 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
905 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300906 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900907 nvars = 0;
908 if (vl->vars) {
909 strlist__for_each(node, vl->vars) {
910 var = strchr(node->s, '\t') + 1;
911 if (strfilter__compare(_filter, var)) {
912 fprintf(stdout, "\t\t%s\n", node->s);
913 nvars++;
914 }
915 }
916 strlist__delete(vl->vars);
917 }
918 if (nvars == 0)
919 fprintf(stdout, "\t\t(No matched variables)\n");
920 }
921 free(vls);
922end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900923 free(buf);
924 return ret;
925}
926
927/* Show available variables on given probe point */
928int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900929 int max_vls, const char *module,
930 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900931{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900932 int i, ret = 0;
933 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900934
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000935 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900936 if (ret < 0)
937 return ret;
938
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000939 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900940 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000941 ret = -ENOENT;
942 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900943 }
944
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900945 setup_pager();
946
Masami Hiramatsuff741782011-06-27 16:27:39 +0900947 for (i = 0; i < npevs && ret >= 0; i++)
948 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900949 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900950
951 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000952out:
953 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900954 return ret;
955}
956
Ingo Molnar89fe8082013-09-30 12:07:11 +0200957#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300958
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000959static int
960find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
961 struct perf_probe_point *pp __maybe_unused,
962 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300963{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000964 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300965}
966
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530967static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300968 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300969 int max_tevs __maybe_unused,
970 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300971{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 if (perf_probe_event_need_dwarf(pev)) {
973 pr_warning("Debuginfo-analysis is not supported.\n");
974 return -ENOSYS;
975 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530976
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300977 return 0;
978}
979
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300980int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000981 const char *module __maybe_unused,
982 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300983{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400984 pr_warning("Debuginfo-analysis is not supported.\n");
985 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300986}
987
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300988int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
989 int npevs __maybe_unused, int max_vls __maybe_unused,
990 const char *module __maybe_unused,
991 struct strfilter *filter __maybe_unused,
992 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900993{
994 pr_warning("Debuginfo-analysis is not supported.\n");
995 return -ENOSYS;
996}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400997#endif
998
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000999void line_range__clear(struct line_range *lr)
1000{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001001 free(lr->function);
1002 free(lr->file);
1003 free(lr->path);
1004 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001005 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001006 memset(lr, 0, sizeof(*lr));
1007}
1008
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001009int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001010{
1011 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001012 lr->line_list = intlist__new(NULL);
1013 if (!lr->line_list)
1014 return -ENOMEM;
1015 else
1016 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001017}
1018
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001019static int parse_line_num(char **ptr, int *val, const char *what)
1020{
1021 const char *start = *ptr;
1022
1023 errno = 0;
1024 *val = strtol(*ptr, ptr, 0);
1025 if (errno || *ptr == start) {
1026 semantic_error("'%s' is not a valid number.\n", what);
1027 return -EINVAL;
1028 }
1029 return 0;
1030}
1031
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001032/*
1033 * Stuff 'lr' according to the line range described by 'arg'.
1034 * The line range syntax is described by:
1035 *
1036 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001037 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001038 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001039int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001040{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001041 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001042 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001043
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001044 if (!name)
1045 return -ENOMEM;
1046
1047 lr->start = 0;
1048 lr->end = INT_MAX;
1049
1050 range = strchr(name, ':');
1051 if (range) {
1052 *range++ = '\0';
1053
1054 err = parse_line_num(&range, &lr->start, "start line");
1055 if (err)
1056 goto err;
1057
1058 if (*range == '+' || *range == '-') {
1059 const char c = *range++;
1060
1061 err = parse_line_num(&range, &lr->end, "end line");
1062 if (err)
1063 goto err;
1064
1065 if (c == '+') {
1066 lr->end += lr->start;
1067 /*
1068 * Adjust the number of lines here.
1069 * If the number of lines == 1, the
1070 * the end of line should be equal to
1071 * the start of line.
1072 */
1073 lr->end--;
1074 }
1075 }
1076
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001077 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001078
1079 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001080 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001081 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001082 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001083 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001085 if (*range != '\0') {
1086 semantic_error("Tailing with invalid str '%s'.\n", range);
1087 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001088 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001089 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001090
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001091 file = strchr(name, '@');
1092 if (file) {
1093 *file = '\0';
1094 lr->file = strdup(++file);
1095 if (lr->file == NULL) {
1096 err = -ENOMEM;
1097 goto err;
1098 }
1099 lr->function = name;
1100 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001101 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001102 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001103 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001104
1105 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001106err:
1107 free(name);
1108 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001109}
1110
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001111/* Check the name is good for event/group */
1112static bool check_event_name(const char *name)
1113{
1114 if (!isalpha(*name) && *name != '_')
1115 return false;
1116 while (*++name != '\0') {
1117 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1118 return false;
1119 }
1120 return true;
1121}
1122
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001123/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001124static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001125{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001126 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001127 char *ptr, *tmp;
1128 char c, nc = 0;
1129 /*
1130 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001131 * perf probe [EVENT=]SRC[:LN|;PTN]
1132 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001133 *
1134 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001135 */
1136
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001137 ptr = strpbrk(arg, ";=@+%");
1138 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001139 *ptr = '\0';
1140 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001141 if (strchr(arg, ':')) {
1142 semantic_error("Group name is not supported yet.\n");
1143 return -ENOTSUP;
1144 }
1145 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001146 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001147 "follow C symbol-naming rule.\n", arg);
1148 return -EINVAL;
1149 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001150 pev->event = strdup(arg);
1151 if (pev->event == NULL)
1152 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001153 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001154 arg = tmp;
1155 }
1156
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001157 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001158 if (ptr) {
1159 nc = *ptr;
1160 *ptr++ = '\0';
1161 }
1162
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001163 tmp = strdup(arg);
1164 if (tmp == NULL)
1165 return -ENOMEM;
1166
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001167 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001168 if (strchr(tmp, '.')) /* File */
1169 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001170 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001171 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001172
1173 /* Parse other options */
1174 while (ptr) {
1175 arg = ptr;
1176 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001177 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001178 pp->lazy_line = strdup(arg);
1179 if (pp->lazy_line == NULL)
1180 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001181 break;
1182 }
1183 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001184 if (ptr) {
1185 nc = *ptr;
1186 *ptr++ = '\0';
1187 }
1188 switch (c) {
1189 case ':': /* Line number */
1190 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001192 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193 " in line number.\n");
1194 return -EINVAL;
1195 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001196 break;
1197 case '+': /* Byte offset from a symbol */
1198 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001199 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001200 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 " in offset.\n");
1202 return -EINVAL;
1203 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001204 break;
1205 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 if (pp->file) {
1207 semantic_error("SRC@SRC is not allowed.\n");
1208 return -EINVAL;
1209 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001210 pp->file = strdup(arg);
1211 if (pp->file == NULL)
1212 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001213 break;
1214 case '%': /* Probe places */
1215 if (strcmp(arg, "return") == 0) {
1216 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 } else { /* Others not supported yet */
1218 semantic_error("%%%s is not supported.\n", arg);
1219 return -ENOTSUP;
1220 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001221 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 default: /* Buggy case */
1223 pr_err("This program has a bug at %s:%d.\n",
1224 __FILE__, __LINE__);
1225 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001226 break;
1227 }
1228 }
1229
1230 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001231 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001232 semantic_error("Lazy pattern can't be used with"
1233 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 return -EINVAL;
1235 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001236
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001238 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 return -EINVAL;
1240 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001241
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001243 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244 return -EINVAL;
1245 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001246
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001248 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001249 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 return -EINVAL;
1251 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001252
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001254 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 return -EINVAL;
1256 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001259 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260 return -EINVAL;
1261 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001262
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001264 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001265 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001266 return -EINVAL;
1267 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001268
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001270 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1271 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001272 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001273}
1274
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001275/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001276static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001277{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001278 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001279 struct perf_probe_arg_field **fieldp;
1280
1281 pr_debug("parsing arg: %s into ", str);
1282
Masami Hiramatsu48481932010-04-12 13:16:53 -04001283 tmp = strchr(str, '=');
1284 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001285 arg->name = strndup(str, tmp - str);
1286 if (arg->name == NULL)
1287 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001288 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001289 str = tmp + 1;
1290 }
1291
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001292 tmp = strchr(str, ':');
1293 if (tmp) { /* Type setting */
1294 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001295 arg->type = strdup(tmp + 1);
1296 if (arg->type == NULL)
1297 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001298 pr_debug("type:%s ", arg->type);
1299 }
1300
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001301 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001302 if (!is_c_varname(str) || !tmp) {
1303 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001304 arg->var = strdup(str);
1305 if (arg->var == NULL)
1306 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001307 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001309 }
1310
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001311 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001312 arg->var = strndup(str, tmp - str);
1313 if (arg->var == NULL)
1314 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001315 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001316 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001317 fieldp = &arg->field;
1318
1319 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001320 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1321 if (*fieldp == NULL)
1322 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001323 if (*tmp == '[') { /* Array */
1324 str = tmp;
1325 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001326 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001327 if (*tmp != ']' || tmp == str + 1) {
1328 semantic_error("Array index must be a"
1329 " number.\n");
1330 return -EINVAL;
1331 }
1332 tmp++;
1333 if (*tmp == '\0')
1334 tmp = NULL;
1335 } else { /* Structure */
1336 if (*tmp == '.') {
1337 str = tmp + 1;
1338 (*fieldp)->ref = false;
1339 } else if (tmp[1] == '>') {
1340 str = tmp + 2;
1341 (*fieldp)->ref = true;
1342 } else {
1343 semantic_error("Argument parse error: %s\n",
1344 str);
1345 return -EINVAL;
1346 }
1347 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001348 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001349 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001350 (*fieldp)->name = strndup(str, tmp - str);
1351 if ((*fieldp)->name == NULL)
1352 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001353 if (*str != '[')
1354 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001355 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1356 fieldp = &(*fieldp)->next;
1357 }
1358 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001359 (*fieldp)->name = strdup(str);
1360 if ((*fieldp)->name == NULL)
1361 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001362 if (*str != '[')
1363 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001364 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001365
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001366 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001367 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001368 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001369 if (arg->name == NULL)
1370 return -ENOMEM;
1371 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001372 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001373}
1374
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001375/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001377{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001378 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001380
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001381 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001382 if (!argv) {
1383 pr_debug("Failed to split arguments.\n");
1384 return -ENOMEM;
1385 }
1386 if (argc - 1 > MAX_PROBE_ARGS) {
1387 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1388 ret = -ERANGE;
1389 goto out;
1390 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001391 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001392 ret = parse_perf_probe_point(argv[0], pev);
1393 if (ret < 0)
1394 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001395
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001396 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001397 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001398 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1399 if (pev->args == NULL) {
1400 ret = -ENOMEM;
1401 goto out;
1402 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1404 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1405 if (ret >= 0 &&
1406 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001407 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 " kretprobe.\n");
1409 ret = -EINVAL;
1410 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001411 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001413 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001414
1415 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001416}
1417
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001418/* Return true if this perf_probe_event requires debuginfo */
1419bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001420{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001421 int i;
1422
1423 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1424 return true;
1425
1426 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001427 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001428 return true;
1429
1430 return false;
1431}
1432
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301433/* Parse probe_events event into struct probe_point */
1434static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001435 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001436{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301437 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 char pr;
1439 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001440 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001441 int ret, i, argc;
1442 char **argv;
1443
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301444 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001445 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001446 if (!argv) {
1447 pr_debug("Failed to split arguments.\n");
1448 return -ENOMEM;
1449 }
1450 if (argc < 2) {
1451 semantic_error("Too few probe arguments.\n");
1452 ret = -ERANGE;
1453 goto out;
1454 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001455
1456 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001457 argv0_str = strdup(argv[0]);
1458 if (argv0_str == NULL) {
1459 ret = -ENOMEM;
1460 goto out;
1461 }
1462 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1463 fmt2_str = strtok_r(NULL, "/", &fmt);
1464 fmt3_str = strtok_r(NULL, " \t", &fmt);
1465 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1466 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001467 semantic_error("Failed to parse event name: %s\n", argv[0]);
1468 ret = -EINVAL;
1469 goto out;
1470 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001471 pr = fmt1_str[0];
1472 tev->group = strdup(fmt2_str);
1473 tev->event = strdup(fmt3_str);
1474 if (tev->group == NULL || tev->event == NULL) {
1475 ret = -ENOMEM;
1476 goto out;
1477 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001478 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001479
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001480 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001481
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001482 /* Scan module name(if there), function name and offset */
1483 p = strchr(argv[1], ':');
1484 if (p) {
1485 tp->module = strndup(argv[1], p - argv[1]);
1486 p++;
1487 } else
1488 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001489 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001490 if (fmt1_str[0] == '0') /* only the address started with 0x */
1491 tp->address = strtoul(fmt1_str, NULL, 0);
1492 else {
1493 /* Only the symbol-based probe has offset */
1494 tp->symbol = strdup(fmt1_str);
1495 if (tp->symbol == NULL) {
1496 ret = -ENOMEM;
1497 goto out;
1498 }
1499 fmt2_str = strtok_r(NULL, "", &fmt);
1500 if (fmt2_str == NULL)
1501 tp->offset = 0;
1502 else
1503 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001504 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001505
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001506 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301507 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001508 if (tev->args == NULL) {
1509 ret = -ENOMEM;
1510 goto out;
1511 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001512 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001513 p = strchr(argv[i + 2], '=');
1514 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 *p++ = '\0';
1516 else
1517 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001518 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001519 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001520 tev->args[i].value = strdup(p);
1521 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1522 ret = -ENOMEM;
1523 goto out;
1524 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001525 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001526 ret = 0;
1527out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001528 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001529 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001530 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001531}
1532
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001533/* Compose only probe arg */
1534int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1535{
1536 struct perf_probe_arg_field *field = pa->field;
1537 int ret;
1538 char *tmp = buf;
1539
Masami Hiramatsu48481932010-04-12 13:16:53 -04001540 if (pa->name && pa->var)
1541 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1542 else
1543 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001544 if (ret <= 0)
1545 goto error;
1546 tmp += ret;
1547 len -= ret;
1548
1549 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001550 if (field->name[0] == '[')
1551 ret = e_snprintf(tmp, len, "%s", field->name);
1552 else
1553 ret = e_snprintf(tmp, len, "%s%s",
1554 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001555 if (ret <= 0)
1556 goto error;
1557 tmp += ret;
1558 len -= ret;
1559 field = field->next;
1560 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001561
1562 if (pa->type) {
1563 ret = e_snprintf(tmp, len, ":%s", pa->type);
1564 if (ret <= 0)
1565 goto error;
1566 tmp += ret;
1567 len -= ret;
1568 }
1569
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001570 return tmp - buf;
1571error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001572 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001573 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001574}
1575
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001576/* Compose only probe point (not argument) */
1577static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001578{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001579 char *buf, *tmp;
1580 char offs[32] = "", line[32] = "", file[32] = "";
1581 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001582
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001583 buf = zalloc(MAX_CMDLEN);
1584 if (buf == NULL) {
1585 ret = -ENOMEM;
1586 goto error;
1587 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001588 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001589 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001590 if (ret <= 0)
1591 goto error;
1592 }
1593 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001594 ret = e_snprintf(line, 32, ":%d", pp->line);
1595 if (ret <= 0)
1596 goto error;
1597 }
1598 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001599 tmp = pp->file;
1600 len = strlen(tmp);
1601 if (len > 30) {
1602 tmp = strchr(pp->file + len - 30, '/');
1603 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1604 }
1605 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001606 if (ret <= 0)
1607 goto error;
1608 }
1609
1610 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001611 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1612 offs, pp->retprobe ? "%return" : "", line,
1613 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001615 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001616 if (ret <= 0)
1617 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001618
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001619 return buf;
1620error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001621 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001622 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001623 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001624}
1625
1626#if 0
1627char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1628{
1629 char *buf;
1630 int i, len, ret;
1631
1632 buf = synthesize_perf_probe_point(&pev->point);
1633 if (!buf)
1634 return NULL;
1635
1636 len = strlen(buf);
1637 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001638 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001639 pev->args[i].name);
1640 if (ret <= 0) {
1641 free(buf);
1642 return NULL;
1643 }
1644 len += ret;
1645 }
1646
1647 return buf;
1648}
1649#endif
1650
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 char **buf, size_t *buflen,
1653 int depth)
1654{
1655 int ret;
1656 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301657 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001658 buflen, depth + 1);
1659 if (depth < 0)
1660 goto out;
1661 }
1662
1663 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1664 if (ret < 0)
1665 depth = ret;
1666 else {
1667 *buf += ret;
1668 *buflen -= ret;
1669 }
1670out:
1671 return depth;
1672
1673}
1674
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301675static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001676 char *buf, size_t buflen)
1677{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301678 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001679 int ret, depth = 0;
1680 char *tmp = buf;
1681
1682 /* Argument name or separator */
1683 if (arg->name)
1684 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1685 else
1686 ret = e_snprintf(buf, buflen, " ");
1687 if (ret < 0)
1688 return ret;
1689 buf += ret;
1690 buflen -= ret;
1691
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001692 /* Special case: @XXX */
1693 if (arg->value[0] == '@' && arg->ref)
1694 ref = ref->next;
1695
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001697 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301698 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001699 &buflen, 1);
1700 if (depth < 0)
1701 return depth;
1702 }
1703
1704 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001705 if (arg->value[0] == '@' && arg->ref)
1706 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1707 arg->ref->offset);
1708 else
1709 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001710 if (ret < 0)
1711 return ret;
1712 buf += ret;
1713 buflen -= ret;
1714
1715 /* Closing */
1716 while (depth--) {
1717 ret = e_snprintf(buf, buflen, ")");
1718 if (ret < 0)
1719 return ret;
1720 buf += ret;
1721 buflen -= ret;
1722 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001723 /* Print argument type */
1724 if (arg->type) {
1725 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1726 if (ret <= 0)
1727 return ret;
1728 buf += ret;
1729 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001730
1731 return buf - tmp;
1732}
1733
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301736 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001737 char *buf;
1738 int i, len, ret;
1739
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001740 buf = zalloc(MAX_CMDLEN);
1741 if (buf == NULL)
1742 return NULL;
1743
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001744 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1745 tev->group, tev->event);
1746 if (len <= 0)
1747 goto error;
1748
1749 /* Uprobes must have tp->address and tp->module */
1750 if (tev->uprobes && (!tp->address || !tp->module))
1751 goto error;
1752
1753 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301754 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001755 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1756 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301757 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001758 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301759 tp->module ?: "", tp->module ? ":" : "",
1760 tp->symbol, tp->offset);
1761
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001762 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001763 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001764 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765
1766 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301767 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001768 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001769 if (ret <= 0)
1770 goto error;
1771 len += ret;
1772 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001773
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001775error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001776 free(buf);
1777 return NULL;
1778}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001779
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001780static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1781 struct perf_probe_point *pp,
1782 bool is_kprobe)
1783{
1784 struct symbol *sym = NULL;
1785 struct map *map;
1786 u64 addr;
1787 int ret = -ENOENT;
1788
1789 if (!is_kprobe) {
1790 map = dso__new_map(tp->module);
1791 if (!map)
1792 goto out;
1793 addr = tp->address;
1794 sym = map__find_symbol(map, addr, NULL);
1795 } else {
1796 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1797 if (addr) {
1798 addr += tp->offset;
1799 sym = __find_kernel_function(addr, &map);
1800 }
1801 }
1802 if (!sym)
1803 goto out;
1804
1805 pp->retprobe = tp->retprobe;
1806 pp->offset = addr - map->unmap_ip(map, sym->start);
1807 pp->function = strdup(sym->name);
1808 ret = pp->function ? 0 : -ENOMEM;
1809
1810out:
1811 if (map && !is_kprobe) {
1812 dso__delete(map->dso);
1813 map__delete(map);
1814 }
1815
1816 return ret;
1817}
1818
1819static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1820 struct perf_probe_point *pp,
1821 bool is_kprobe)
1822{
1823 char buf[128];
1824 int ret;
1825
1826 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1827 if (!ret)
1828 return 0;
1829 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1830 if (!ret)
1831 return 0;
1832
1833 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1834
1835 if (tp->symbol) {
1836 pp->function = strdup(tp->symbol);
1837 pp->offset = tp->offset;
1838 } else if (!tp->module && !is_kprobe) {
1839 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1840 if (ret < 0)
1841 return ret;
1842 pp->function = strdup(buf);
1843 pp->offset = 0;
1844 }
1845 if (pp->function == NULL)
1846 return -ENOMEM;
1847
1848 pp->retprobe = tp->retprobe;
1849
1850 return 0;
1851}
1852
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301853static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301854 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001856 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001857 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001858
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001859 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001860 pev->event = strdup(tev->event);
1861 pev->group = strdup(tev->group);
1862 if (pev->event == NULL || pev->group == NULL)
1863 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001864
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001865 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001866 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001867 if (ret < 0)
1868 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001869
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001870 /* Convert trace_arg to probe_arg */
1871 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001872 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1873 if (pev->args == NULL)
1874 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001875 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001877 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001878 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301879 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001880 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001881 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001882 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001883 if (pev->args[i].name == NULL && ret >= 0)
1884 ret = -ENOMEM;
1885 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001886
1887 if (ret < 0)
1888 clear_perf_probe_event(pev);
1889
1890 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001891}
1892
1893void clear_perf_probe_event(struct perf_probe_event *pev)
1894{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001895 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001896 int i;
1897
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001898 free(pev->event);
1899 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001900 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001901
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001902 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001903 free(pev->args[i].name);
1904 free(pev->args[i].var);
1905 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001906 field = pev->args[i].field;
1907 while (field) {
1908 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001909 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001910 free(field);
1911 field = next;
1912 }
1913 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001914 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001915 memset(pev, 0, sizeof(*pev));
1916}
1917
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301918static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001919{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301920 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001921 int i;
1922
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001923 free(tev->event);
1924 free(tev->group);
1925 free(tev->point.symbol);
1926 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001928 free(tev->args[i].name);
1929 free(tev->args[i].value);
1930 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001931 ref = tev->args[i].ref;
1932 while (ref) {
1933 next = ref->next;
1934 free(ref);
1935 ref = next;
1936 }
1937 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001938 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001939 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001940}
1941
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001942static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301943{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001944 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301945
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001946 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301947 const char *config;
1948
1949 if (!is_kprobe)
1950 config = "CONFIG_UPROBE_EVENTS";
1951 else
1952 config = "CONFIG_KPROBE_EVENTS";
1953
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001954 pr_warning("%cprobe_events file does not exist"
1955 " - please rebuild kernel with %s.\n",
1956 is_kprobe ? 'k' : 'u', config);
1957 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001958 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001959 else
1960 pr_warning("Failed to open %cprobe_events: %s\n",
1961 is_kprobe ? 'k' : 'u',
1962 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301963}
1964
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001965static void print_both_open_warning(int kerr, int uerr)
1966{
1967 /* Both kprobes and uprobes are disabled, warn it. */
1968 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001969 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001970 else if (kerr == -ENOENT && uerr == -ENOENT)
1971 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1972 "or/and CONFIG_UPROBE_EVENTS.\n");
1973 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001974 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001975 pr_warning("Failed to open kprobe events: %s.\n",
1976 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1977 pr_warning("Failed to open uprobe events: %s.\n",
1978 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1979 }
1980}
1981
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001982static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001983{
1984 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001985 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001986 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001987 int ret;
1988
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001989 __debugfs = tracefs_find_mountpoint();
1990 if (__debugfs == NULL) {
1991 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001992
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001993 __debugfs = debugfs_find_mountpoint();
1994 if (__debugfs == NULL)
1995 return -ENOTSUP;
1996 }
1997
1998 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1999 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002000 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002001 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002002 if (readwrite && !probe_event_dry_run)
2003 ret = open(buf, O_RDWR, O_APPEND);
2004 else
2005 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002006
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302007 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002008 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002009 }
2010 return ret;
2011}
2012
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302013static int open_kprobe_events(bool readwrite)
2014{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002015 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302016}
2017
2018static int open_uprobe_events(bool readwrite)
2019{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002020 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302021}
2022
2023/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302024static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002025{
2026 int ret, idx;
2027 FILE *fp;
2028 char buf[MAX_CMDLEN];
2029 char *p;
2030 struct strlist *sl;
2031
2032 sl = strlist__new(true, NULL);
2033
2034 fp = fdopen(dup(fd), "r");
2035 while (!feof(fp)) {
2036 p = fgets(buf, MAX_CMDLEN, fp);
2037 if (!p)
2038 break;
2039
2040 idx = strlen(p) - 1;
2041 if (p[idx] == '\n')
2042 p[idx] = '\0';
2043 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002044 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002045 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002046 strlist__delete(sl);
2047 return NULL;
2048 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002049 }
2050 fclose(fp);
2051
2052 return sl;
2053}
2054
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002055struct kprobe_blacklist_node {
2056 struct list_head list;
2057 unsigned long start;
2058 unsigned long end;
2059 char *symbol;
2060};
2061
2062static void kprobe_blacklist__delete(struct list_head *blacklist)
2063{
2064 struct kprobe_blacklist_node *node;
2065
2066 while (!list_empty(blacklist)) {
2067 node = list_first_entry(blacklist,
2068 struct kprobe_blacklist_node, list);
2069 list_del(&node->list);
2070 free(node->symbol);
2071 free(node);
2072 }
2073}
2074
2075static int kprobe_blacklist__load(struct list_head *blacklist)
2076{
2077 struct kprobe_blacklist_node *node;
2078 const char *__debugfs = debugfs_find_mountpoint();
2079 char buf[PATH_MAX], *p;
2080 FILE *fp;
2081 int ret;
2082
2083 if (__debugfs == NULL)
2084 return -ENOTSUP;
2085
2086 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2087 if (ret < 0)
2088 return ret;
2089
2090 fp = fopen(buf, "r");
2091 if (!fp)
2092 return -errno;
2093
2094 ret = 0;
2095 while (fgets(buf, PATH_MAX, fp)) {
2096 node = zalloc(sizeof(*node));
2097 if (!node) {
2098 ret = -ENOMEM;
2099 break;
2100 }
2101 INIT_LIST_HEAD(&node->list);
2102 list_add_tail(&node->list, blacklist);
2103 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2104 ret = -EINVAL;
2105 break;
2106 }
2107 p = strchr(buf, '\t');
2108 if (p) {
2109 p++;
2110 if (p[strlen(p) - 1] == '\n')
2111 p[strlen(p) - 1] = '\0';
2112 } else
2113 p = (char *)"unknown";
2114 node->symbol = strdup(p);
2115 if (!node->symbol) {
2116 ret = -ENOMEM;
2117 break;
2118 }
2119 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2120 node->start, node->end, node->symbol);
2121 ret++;
2122 }
2123 if (ret < 0)
2124 kprobe_blacklist__delete(blacklist);
2125 fclose(fp);
2126
2127 return ret;
2128}
2129
2130static struct kprobe_blacklist_node *
2131kprobe_blacklist__find_by_address(struct list_head *blacklist,
2132 unsigned long address)
2133{
2134 struct kprobe_blacklist_node *node;
2135
2136 list_for_each_entry(node, blacklist, list) {
2137 if (node->start <= address && address <= node->end)
2138 return node;
2139 }
2140
2141 return NULL;
2142}
2143
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002144/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002145static int show_perf_probe_event(struct perf_probe_event *pev,
2146 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002147{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002148 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002149 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002151
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002152 /* Synthesize only event probe point */
2153 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002154 if (!place)
2155 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002156
2157 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002158 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002159 return ret;
2160
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002161 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002162 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002163 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002164
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002165 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002166 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002167 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002168 ret = synthesize_perf_probe_arg(&pev->args[i],
2169 buf, 128);
2170 if (ret < 0)
2171 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002172 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002173 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002174 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002175 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002176 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002177 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002178}
2179
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302180static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002181{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302182 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302183 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002184 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002185 struct strlist *rawlist;
2186 struct str_node *ent;
2187
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002188 memset(&tev, 0, sizeof(tev));
2189 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002190
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302191 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002192 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002193 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002194
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002195 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302196 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002197 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302198 ret = convert_to_perf_probe_event(&tev, &pev,
2199 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002200 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002201 ret = show_perf_probe_event(&pev,
2202 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002204 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302205 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002206 if (ret < 0)
2207 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002208 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002209 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002210
2211 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002212}
2213
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302214/* List up current perf-probe events */
2215int show_perf_probe_events(void)
2216{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002217 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302218
2219 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302220
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002221 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302222 if (ret < 0)
2223 return ret;
2224
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002225 kp_fd = open_kprobe_events(false);
2226 if (kp_fd >= 0) {
2227 ret = __show_perf_probe_events(kp_fd, true);
2228 close(kp_fd);
2229 if (ret < 0)
2230 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302231 }
2232
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002233 up_fd = open_uprobe_events(false);
2234 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002235 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002236 ret = kp_fd;
2237 goto out;
2238 }
2239
2240 if (up_fd >= 0) {
2241 ret = __show_perf_probe_events(up_fd, false);
2242 close(up_fd);
2243 }
2244out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002245 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302246 return ret;
2247}
2248
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002249/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302250static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002251{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002252 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002253 struct strlist *sl, *rawlist;
2254 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302255 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002256 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002257
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002258 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302259 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002260 if (!rawlist)
2261 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002262 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002263 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302264 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002265 if (ret < 0)
2266 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002267 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002268 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2269 tev.event);
2270 if (ret >= 0)
2271 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002272 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002273 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302274 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002275 if (ret < 0)
2276 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002277 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002278 strlist__delete(rawlist);
2279
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002280 if (ret < 0) {
2281 strlist__delete(sl);
2282 return NULL;
2283 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002284 return sl;
2285}
2286
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302287static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002288{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002289 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302290 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002291 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002292
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002293 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302294 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002295 return -EINVAL;
2296 }
2297
Masami Hiramatsufa282442009-12-08 17:03:23 -05002298 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002299 if (!probe_event_dry_run) {
2300 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002301 if (ret <= 0) {
2302 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002303 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002304 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002305 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002306 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002307 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002308 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002309}
2310
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002311static int get_new_event_name(char *buf, size_t len, const char *base,
2312 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002313{
2314 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002315
2316 /* Try no suffix */
2317 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002318 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002319 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002320 return ret;
2321 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002322 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002323 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002324
Masami Hiramatsud761b082009-12-15 10:32:25 -05002325 if (!allow_suffix) {
2326 pr_warning("Error: event \"%s\" already exists. "
2327 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002328 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002329 }
2330
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002331 /* Try to add suffix */
2332 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002333 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002334 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002335 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002336 return ret;
2337 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002338 if (!strlist__has_entry(namelist, buf))
2339 break;
2340 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002341 if (i == MAX_EVENT_INDEX) {
2342 pr_warning("Too many events are on the same function.\n");
2343 ret = -ERANGE;
2344 }
2345
2346 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002347}
2348
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002349/* Warn if the current kernel's uprobe implementation is old */
2350static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2351{
2352 int i;
2353 char *buf = synthesize_probe_trace_command(tev);
2354
2355 /* Old uprobe event doesn't support memory dereference */
2356 if (!tev->uprobes || tev->nargs == 0 || !buf)
2357 goto out;
2358
2359 for (i = 0; i < tev->nargs; i++)
2360 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2361 pr_warning("Please upgrade your kernel to at least "
2362 "3.14 to have access to feature %s\n",
2363 tev->args[i].value);
2364 break;
2365 }
2366out:
2367 free(buf);
2368}
2369
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302370static int __add_probe_trace_events(struct perf_probe_event *pev,
2371 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002372 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002373{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002374 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302375 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002376 char buf[64];
2377 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002378 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002379 LIST_HEAD(blacklist);
2380 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002381
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302382 if (pev->uprobes)
2383 fd = open_uprobe_events(true);
2384 else
2385 fd = open_kprobe_events(true);
2386
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002387 if (fd < 0) {
2388 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002389 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002390 }
2391
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002392 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302393 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002394 if (!namelist) {
2395 pr_debug("Failed to get current event list.\n");
2396 return -EIO;
2397 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002398 /* Get kprobe blacklist if exists */
2399 if (!pev->uprobes) {
2400 ret = kprobe_blacklist__load(&blacklist);
2401 if (ret < 0)
2402 pr_debug("No kprobe blacklist support, ignored\n");
2403 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002404
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002405 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002406 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002407 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002408 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002409 /* Ensure that the address is NOT blacklisted */
2410 node = kprobe_blacklist__find_by_address(&blacklist,
2411 tev->point.address);
2412 if (node) {
2413 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2414 continue;
2415 }
2416
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002417 if (pev->event)
2418 event = pev->event;
2419 else
2420 if (pev->point.function)
2421 event = pev->point.function;
2422 else
2423 event = tev->point.symbol;
2424 if (pev->group)
2425 group = pev->group;
2426 else
2427 group = PERFPROBE_GROUP;
2428
2429 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002430 ret = get_new_event_name(buf, 64, event,
2431 namelist, allow_suffix);
2432 if (ret < 0)
2433 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434 event = buf;
2435
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002436 tev->event = strdup(event);
2437 tev->group = strdup(group);
2438 if (tev->event == NULL || tev->group == NULL) {
2439 ret = -ENOMEM;
2440 break;
2441 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302442 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 if (ret < 0)
2444 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002445 /* Add added event name to namelist */
2446 strlist__add(namelist, event);
2447
2448 /* Trick here - save current event/group */
2449 event = pev->event;
2450 group = pev->group;
2451 pev->event = tev->event;
2452 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002453 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002454 /* Trick here - restore current event/group */
2455 pev->event = (char *)event;
2456 pev->group = (char *)group;
2457
2458 /*
2459 * Probes after the first probe which comes from same
2460 * user input are always allowed to add suffix, because
2461 * there might be several addresses corresponding to
2462 * one code line.
2463 */
2464 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002465 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002466 if (ret == -EINVAL && pev->uprobes)
2467 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002468
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002469 /* Note that it is possible to skip all events because of blacklist */
2470 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002471 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002472 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2473 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002474 tev->event);
2475 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002476
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002477 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002478 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002479 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002480 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002481}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002482
Namhyung Kim564c62a2015-01-14 20:18:07 +09002483static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002484{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002485 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002486 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002487
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002488 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002489 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2490 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002491 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002492
2493 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002494}
2495
2496#define strdup_or_goto(str, label) \
2497 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2498
2499/*
2500 * Find probe function addresses from map.
2501 * Return an error or the number of found probe_trace_event
2502 */
2503static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2504 struct probe_trace_event **tevs,
2505 int max_tevs, const char *target)
2506{
2507 struct map *map = NULL;
2508 struct kmap *kmap = NULL;
2509 struct ref_reloc_sym *reloc_sym = NULL;
2510 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002511 struct probe_trace_event *tev;
2512 struct perf_probe_point *pp = &pev->point;
2513 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002514 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002515 int ret, i;
2516
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002517 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002518 if (!map) {
2519 ret = -EINVAL;
2520 goto out;
2521 }
2522
2523 /*
2524 * Load matched symbols: Since the different local symbols may have
2525 * same name but different addresses, this lists all the symbols.
2526 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002527 num_matched_functions = find_probe_functions(map, pp->function);
2528 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002529 pr_err("Failed to find symbol %s in %s\n", pp->function,
2530 target ? : "kernel");
2531 ret = -ENOENT;
2532 goto out;
2533 } else if (num_matched_functions > max_tevs) {
2534 pr_err("Too many functions matched in %s\n",
2535 target ? : "kernel");
2536 ret = -E2BIG;
2537 goto out;
2538 }
2539
Namhyung Kim25dd9172015-01-14 20:18:08 +09002540 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002541 kmap = map__kmap(map);
2542 reloc_sym = kmap->ref_reloc_sym;
2543 if (!reloc_sym) {
2544 pr_warning("Relocated base symbol is not found!\n");
2545 ret = -EINVAL;
2546 goto out;
2547 }
2548 }
2549
2550 /* Setup result trace-probe-events */
2551 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2552 if (!*tevs) {
2553 ret = -ENOMEM;
2554 goto out;
2555 }
2556
2557 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002558
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002559 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002560 tev = (*tevs) + ret;
2561 tp = &tev->point;
2562 if (ret == num_matched_functions) {
2563 pr_warning("Too many symbols are listed. Skip it.\n");
2564 break;
2565 }
2566 ret++;
2567
2568 if (pp->offset > sym->end - sym->start) {
2569 pr_warning("Offset %ld is bigger than the size of %s\n",
2570 pp->offset, sym->name);
2571 ret = -ENOENT;
2572 goto err_out;
2573 }
2574 /* Add one probe point */
2575 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2576 if (reloc_sym) {
2577 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2578 tp->offset = tp->address - reloc_sym->addr;
2579 } else {
2580 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2581 tp->offset = pp->offset;
2582 }
2583 tp->retprobe = pp->retprobe;
2584 if (target)
2585 tev->point.module = strdup_or_goto(target, nomem_out);
2586 tev->uprobes = pev->uprobes;
2587 tev->nargs = pev->nargs;
2588 if (tev->nargs) {
2589 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2590 tev->nargs);
2591 if (tev->args == NULL)
2592 goto nomem_out;
2593 }
2594 for (i = 0; i < tev->nargs; i++) {
2595 if (pev->args[i].name)
2596 tev->args[i].name =
2597 strdup_or_goto(pev->args[i].name,
2598 nomem_out);
2599
2600 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2601 nomem_out);
2602 if (pev->args[i].type)
2603 tev->args[i].type =
2604 strdup_or_goto(pev->args[i].type,
2605 nomem_out);
2606 }
2607 }
2608
2609out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002610 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002611 return ret;
2612
2613nomem_out:
2614 ret = -ENOMEM;
2615err_out:
2616 clear_probe_trace_events(*tevs, num_matched_functions);
2617 zfree(tevs);
2618 goto out;
2619}
2620
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302621static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2622 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302623 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002624{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002625 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002626
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002627 if (pev->uprobes && !pev->group) {
2628 /* Replace group name if not given */
2629 ret = convert_exec_to_group(target, &pev->group);
2630 if (ret != 0) {
2631 pr_warning("Failed to make a group name.\n");
2632 return ret;
2633 }
2634 }
2635
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002636 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302637 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002638 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002639 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002640
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002641 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002642}
2643
2644struct __event_package {
2645 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302646 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002647 int ntevs;
2648};
2649
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002650int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302651 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002652{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002653 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002654 struct __event_package *pkgs;
2655
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302656 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002657 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302658
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002659 if (pkgs == NULL)
2660 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002661
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002662 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002663 if (ret < 0) {
2664 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002665 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002666 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002667
2668 /* Loop 1: convert all events */
2669 for (i = 0; i < npevs; i++) {
2670 pkgs[i].pev = &pevs[i];
2671 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302672 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002673 &pkgs[i].tevs,
2674 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302675 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002676 if (ret < 0)
2677 goto end;
2678 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002679 }
2680
2681 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002682 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302683 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002684 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002685 if (ret < 0)
2686 break;
2687 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002688end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002689 /* Loop 3: cleanup and free trace events */
2690 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002691 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302692 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002693 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002694 }
2695 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002696 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002697
2698 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002699}
2700
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302701static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002702{
2703 char *p;
2704 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002705 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002706
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302707 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002708 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2709 if (ret < 0)
2710 goto error;
2711
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002712 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002713 if (!p) {
2714 pr_debug("Internal error: %s should have ':' but not.\n",
2715 ent->s);
2716 ret = -ENOTSUP;
2717 goto error;
2718 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002719 *p = '/';
2720
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002721 pr_debug("Writing event: %s\n", buf);
2722 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002723 if (ret < 0) {
2724 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002725 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002726 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002727
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002728 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002729 return 0;
2730error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002731 pr_warning("Failed to delete event: %s\n",
2732 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002733 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002734}
2735
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302736static int del_trace_probe_event(int fd, const char *buf,
2737 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002738{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002739 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302740 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002741
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002742 if (strpbrk(buf, "*?")) { /* Glob-exp */
2743 strlist__for_each_safe(ent, n, namelist)
2744 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302745 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002746 if (ret < 0)
2747 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002748 strlist__remove(namelist, ent);
2749 }
2750 } else {
2751 ent = strlist__find(namelist, buf);
2752 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302753 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002754 if (ret >= 0)
2755 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002756 }
2757 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002758
2759 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002760}
2761
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002762int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002763{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302764 int ret = -1, ufd = -1, kfd = -1;
2765 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002766 const char *group, *event;
2767 char *p, *str;
2768 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302769 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002770
Masami Hiramatsufa282442009-12-08 17:03:23 -05002771 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302772 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002773 if (kfd >= 0)
2774 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302775
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302776 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002777 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302778 unamelist = get_probe_trace_event_names(ufd, true);
2779
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002780 if (kfd < 0 && ufd < 0) {
2781 print_both_open_warning(kfd, ufd);
2782 goto error;
2783 }
2784
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785 if (namelist == NULL && unamelist == NULL)
2786 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002787
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002788 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002789 str = strdup(ent->s);
2790 if (str == NULL) {
2791 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302792 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002793 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002794 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002795 p = strchr(str, ':');
2796 if (p) {
2797 group = str;
2798 *p = '\0';
2799 event = p + 1;
2800 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002801 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002802 event = str;
2803 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302804
2805 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2806 if (ret < 0) {
2807 pr_err("Failed to copy event.");
2808 free(str);
2809 goto error;
2810 }
2811
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002812 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302813
2814 if (namelist)
2815 ret = del_trace_probe_event(kfd, buf, namelist);
2816
2817 if (unamelist && ret != 0)
2818 ret = del_trace_probe_event(ufd, buf, unamelist);
2819
2820 if (ret != 0)
2821 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2822
Masami Hiramatsufa282442009-12-08 17:03:23 -05002823 free(str);
2824 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302825
2826error:
2827 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302828 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302829 close(kfd);
2830 }
2831
2832 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302833 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302834 close(ufd);
2835 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002836
2837 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002838}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302839
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002840/* TODO: don't use a global variable for filter ... */
2841static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002842
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002843/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002844 * If a symbol corresponds to a function with global binding and
2845 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002846 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002847static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002848 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002849{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002850 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002851 strfilter__compare(available_func_filter, sym->name))
2852 return 0;
2853 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002854}
2855
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002856int show_available_funcs(const char *target, struct strfilter *_filter,
2857 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002858{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002859 struct map *map;
2860 int ret;
2861
2862 ret = init_symbol_maps(user);
2863 if (ret < 0)
2864 return ret;
2865
2866 /* Get a symbol map */
2867 if (user)
2868 map = dso__new_map(target);
2869 else
2870 map = kernel_get_module_map(target);
2871 if (!map) {
2872 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002873 return -EINVAL;
2874 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002875
2876 /* Load symbols with given filter */
2877 available_func_filter = _filter;
2878 if (map__load(map, filter_available_functions)) {
2879 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2880 goto end;
2881 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002882 if (!dso__sorted_by_name(map->dso, map->type))
2883 dso__sort_by_name(map->dso, map->type);
2884
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002885 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302886 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002887 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2888end:
2889 if (user) {
2890 dso__delete(map->dso);
2891 map__delete(map);
2892 }
2893 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302894
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002895 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302896}
2897