blob: b8f45782126a8333ac624a1e41624580404a1f7b [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 Hiramatsuff741782011-06-27 16:27:39 +0900356/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000357static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900358{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000359 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000360 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900361
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000362 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900363 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900364 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000365 if (!silent)
366 pr_err("Failed to find path of %s module.\n",
367 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900368 return NULL;
369 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900370 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000371 ret = debuginfo__new(path);
372 if (!ret && !silent) {
373 pr_warning("The %s file has no debug information.\n", path);
374 if (!module || !strtailcmp(path, ".ko"))
375 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
376 else
377 pr_warning("Rebuild with -g, ");
378 pr_warning("or install an appropriate debuginfo package.\n");
379 }
380 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400381}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300382
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000383
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000384static int get_text_start_address(const char *exec, unsigned long *address)
385{
386 Elf *elf;
387 GElf_Ehdr ehdr;
388 GElf_Shdr shdr;
389 int fd, ret = -ENOENT;
390
391 fd = open(exec, O_RDONLY);
392 if (fd < 0)
393 return -errno;
394
395 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
396 if (elf == NULL)
397 return -EINVAL;
398
399 if (gelf_getehdr(elf, &ehdr) == NULL)
400 goto out;
401
402 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
403 goto out;
404
405 *address = shdr.sh_addr - shdr.sh_offset;
406 ret = 0;
407out:
408 elf_end(elf);
409 return ret;
410}
411
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000412/*
413 * Convert trace point to probe point with debuginfo
414 */
415static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
416 struct perf_probe_point *pp,
417 bool is_kprobe)
418{
419 struct debuginfo *dinfo = NULL;
420 unsigned long stext = 0;
421 u64 addr = tp->address;
422 int ret = -ENOENT;
423
424 /* convert the address to dwarf address */
425 if (!is_kprobe) {
426 if (!addr) {
427 ret = -EINVAL;
428 goto error;
429 }
430 ret = get_text_start_address(tp->module, &stext);
431 if (ret < 0)
432 goto error;
433 addr += stext;
434 } else {
435 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
436 if (addr == 0)
437 goto error;
438 addr += tp->offset;
439 }
440
441 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
442 tp->module ? : "kernel");
443
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000444 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000445 if (dinfo) {
446 ret = debuginfo__find_probe_point(dinfo,
447 (unsigned long)addr, pp);
448 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000449 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000450 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000451
452 if (ret > 0) {
453 pp->retprobe = tp->retprobe;
454 return 0;
455 }
456error:
457 pr_debug("Failed to find corresponding probes from debuginfo.\n");
458 return ret ? : -ENOENT;
459}
460
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000461static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
462 int ntevs, const char *exec)
463{
464 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000465 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000466
467 if (!exec)
468 return 0;
469
470 ret = get_text_start_address(exec, &stext);
471 if (ret < 0)
472 return ret;
473
474 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000475 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000476 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000477 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000478 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000479 ret = -ENOMEM;
480 break;
481 }
482 tevs[i].uprobes = true;
483 }
484
485 return ret;
486}
487
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900488static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
489 int ntevs, const char *module)
490{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900491 int i, ret = 0;
492 char *tmp;
493
494 if (!module)
495 return 0;
496
497 tmp = strrchr(module, '/');
498 if (tmp) {
499 /* This is a module path -- get the module name */
500 module = strdup(tmp + 1);
501 if (!module)
502 return -ENOMEM;
503 tmp = strchr(module, '.');
504 if (tmp)
505 *tmp = '\0';
506 tmp = (char *)module; /* For free() */
507 }
508
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900509 for (i = 0; i < ntevs; i++) {
510 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900511 if (!tevs[i].point.module) {
512 ret = -ENOMEM;
513 break;
514 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900515 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900516
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300517 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900518 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900519}
520
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000521/* Post processing the probe events */
522static int post_process_probe_trace_events(struct probe_trace_event *tevs,
523 int ntevs, const char *module,
524 bool uprobe)
525{
526 struct ref_reloc_sym *reloc_sym;
527 char *tmp;
528 int i;
529
530 if (uprobe)
531 return add_exec_to_probe_trace_events(tevs, ntevs, module);
532
533 /* Note that currently ref_reloc_sym based probe is not for drivers */
534 if (module)
535 return add_module_to_probe_trace_events(tevs, ntevs, module);
536
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000537 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000538 if (!reloc_sym) {
539 pr_warning("Relocated base symbol is not found!\n");
540 return -EINVAL;
541 }
542
543 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900544 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000545 tmp = strdup(reloc_sym->name);
546 if (!tmp)
547 return -ENOMEM;
548 free(tevs[i].point.symbol);
549 tevs[i].point.symbol = tmp;
550 tevs[i].point.offset = tevs[i].point.address -
551 reloc_sym->unrelocated_addr;
552 }
553 }
554 return 0;
555}
556
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300557/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530558static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900559 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +0530560 int max_tevs, const char *target)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300561{
562 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900563 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530564 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900565 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300566
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000567 dinfo = open_debuginfo(target, !need_dwarf);
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530568
Masami Hiramatsuff741782011-06-27 16:27:39 +0900569 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000570 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900571 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900572 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300573 return 0;
574 }
575
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000576 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900577 /* Searching trace events corresponding to a probe event */
578 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
579
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900580 if (ntevs == 0) { /* Not found, retry with an alternative */
581 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
582 if (!ret) {
583 ntevs = debuginfo__find_trace_events(dinfo, pev,
584 tevs, max_tevs);
585 /*
586 * Write back to the original probe_event for
587 * setting appropriate (user given) event name
588 */
589 clear_perf_probe_point(&pev->point);
590 memcpy(&pev->point, &tmp, sizeof(tmp));
591 }
592 }
593
Masami Hiramatsuff741782011-06-27 16:27:39 +0900594 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300595
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400596 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000597 pr_debug("Found %d probe_trace_events.\n", ntevs);
598 ret = post_process_probe_trace_events(*tevs, ntevs,
599 target, pev->uprobes);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000600 if (ret < 0) {
601 clear_probe_trace_events(*tevs, ntevs);
602 zfree(tevs);
603 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900604 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400605 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300606
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400607 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900608 pr_warning("Probe point '%s' not found in debuginfo.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400609 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu906451b2014-12-31 15:27:47 +0900610 if (need_dwarf)
611 return -ENOENT;
612 return 0;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400613 }
614 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400615 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
616 if (ntevs == -EBADF) {
617 pr_warning("Warning: No dwarf info found in the vmlinux - "
618 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
619 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900620 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400621 return 0;
622 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300623 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400624 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300625}
626
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900627/*
628 * Find a src file from a DWARF tag path. Prepend optional source path prefix
629 * and chop off leading directories that do not exist. Result is passed back as
630 * a newly allocated path on success.
631 * Return 0 if file was found and readable, -errno otherwise.
632 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900633static int get_real_path(const char *raw_path, const char *comp_dir,
634 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900635{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900636 const char *prefix = symbol_conf.source_prefix;
637
638 if (!prefix) {
639 if (raw_path[0] != '/' && comp_dir)
640 /* If not an absolute path, try to use comp_dir */
641 prefix = comp_dir;
642 else {
643 if (access(raw_path, R_OK) == 0) {
644 *new_path = strdup(raw_path);
Arnaldo Carvalho de Melo38ae5022015-02-26 11:47:18 -0300645 return *new_path ? 0 : -ENOMEM;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900646 } else
647 return -errno;
648 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900649 }
650
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900651 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900652 if (!*new_path)
653 return -ENOMEM;
654
655 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900656 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900657
658 if (access(*new_path, R_OK) == 0)
659 return 0;
660
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900661 if (!symbol_conf.source_prefix) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900662 /* In case of searching comp_dir, don't retry */
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900663 zfree(new_path);
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900664 return -errno;
Masami Hiramatsueb47cb22015-02-26 17:25:04 +0900665 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900666
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900667 switch (errno) {
668 case ENAMETOOLONG:
669 case ENOENT:
670 case EROFS:
671 case EFAULT:
672 raw_path = strchr(++raw_path, '/');
673 if (!raw_path) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300674 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900675 return -ENOENT;
676 }
677 continue;
678
679 default:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300680 zfree(new_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900681 return -errno;
682 }
683 }
684}
685
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300686#define LINEBUF_SIZE 256
687#define NR_ADDITIONAL_LINES 2
688
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100689static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300690{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000691 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100692 const char *color = show_num ? "" : PERF_COLOR_BLUE;
693 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300694
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100695 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300696 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
697 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100698 if (skip)
699 continue;
700 if (!prefix) {
701 prefix = show_num ? "%7d " : " ";
702 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300703 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100704 color_fprintf(stdout, color, "%s", buf);
705
706 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100708 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300709error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100710 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000711 pr_warning("File read error: %s\n",
712 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100713 return -1;
714 }
715 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300716}
717
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100718static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
719{
720 int rv = __show_one_line(fp, l, skip, show_num);
721 if (rv == 0) {
722 pr_warning("Source file is shorter than expected.\n");
723 rv = -1;
724 }
725 return rv;
726}
727
728#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
729#define show_one_line(f,l) _show_one_line(f,l,false,false)
730#define skip_one_line(f,l) _show_one_line(f,l,true,false)
731#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
732
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300733/*
734 * Show line-range always requires debuginfo to find source file and
735 * line number.
736 */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000737static int __show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400739 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000740 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900741 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300742 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900743 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900744 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000745 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300746
747 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000748 dinfo = open_debuginfo(module, false);
749 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900750 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400751
Masami Hiramatsuff741782011-06-27 16:27:39 +0900752 ret = debuginfo__find_line_range(dinfo, lr);
753 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000754 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400755 pr_warning("Specified source line is not found.\n");
756 return -ENOENT;
757 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000758 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400759 return ret;
760 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300761
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900762 /* Convert source file path */
763 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900764 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900765 free(tmp); /* Free old path */
766 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000767 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900768 return ret;
769 }
770
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300771 setup_pager();
772
773 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900774 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300775 lr->start - lr->offset);
776 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100777 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300778
779 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400780 if (fp == NULL) {
781 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000782 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400783 return -errno;
784 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300785 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100786 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100787 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100788 if (ret < 0)
789 goto end;
790 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300791
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000792 intlist__for_each(ln, lr->line_list) {
793 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100794 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100795 if (ret < 0)
796 goto end;
797 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100798 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400799 if (ret < 0)
800 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300801 }
802
803 if (lr->end == INT_MAX)
804 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100805 while (l <= lr->end) {
806 ret = show_one_line_or_eof(fp, l++ - lr->offset);
807 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100808 break;
809 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400810end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300811 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400812 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813}
814
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000815int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000816{
817 int ret;
818
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000819 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000820 if (ret < 0)
821 return ret;
822 ret = __show_line_range(lr, module);
823 exit_symbol_maps();
824
825 return ret;
826}
827
Masami Hiramatsuff741782011-06-27 16:27:39 +0900828static int show_available_vars_at(struct debuginfo *dinfo,
829 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900830 int max_vls, struct strfilter *_filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900831 bool externs, const char *target)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900832{
833 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900834 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900835 struct str_node *node;
836 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900837 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900838 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900839
840 buf = synthesize_perf_probe_point(&pev->point);
841 if (!buf)
842 return -EINVAL;
843 pr_debug("Searching variables at %s\n", buf);
844
Masami Hiramatsuff741782011-06-27 16:27:39 +0900845 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
846 max_vls, externs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900847 if (!ret) { /* Not found, retry with an alternative */
848 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
849 if (!ret) {
850 ret = debuginfo__find_available_vars_at(dinfo, pev,
851 &vls, max_vls, externs);
852 /* Release the old probe_point */
853 clear_perf_probe_point(&tmp);
854 }
855 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900856 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000857 if (ret == 0 || ret == -ENOENT) {
858 pr_err("Failed to find the address of %s\n", buf);
859 ret = -ENOENT;
860 } else
861 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900862 goto end;
863 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000864
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900865 /* Some variables are found */
866 fprintf(stdout, "Available variables at %s\n", buf);
867 for (i = 0; i < ret; i++) {
868 vl = &vls[i];
869 /*
870 * A probe point might be converted to
871 * several trace points.
872 */
873 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
874 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300875 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900876 nvars = 0;
877 if (vl->vars) {
878 strlist__for_each(node, vl->vars) {
879 var = strchr(node->s, '\t') + 1;
880 if (strfilter__compare(_filter, var)) {
881 fprintf(stdout, "\t\t%s\n", node->s);
882 nvars++;
883 }
884 }
885 strlist__delete(vl->vars);
886 }
887 if (nvars == 0)
888 fprintf(stdout, "\t\t(No matched variables)\n");
889 }
890 free(vls);
891end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900892 free(buf);
893 return ret;
894}
895
896/* Show available variables on given probe point */
897int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900898 int max_vls, const char *module,
899 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900900{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900901 int i, ret = 0;
902 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900903
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000904 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900905 if (ret < 0)
906 return ret;
907
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000908 dinfo = open_debuginfo(module, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900909 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000910 ret = -ENOENT;
911 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900912 }
913
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900914 setup_pager();
915
Masami Hiramatsuff741782011-06-27 16:27:39 +0900916 for (i = 0; i < npevs && ret >= 0; i++)
917 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900918 externs, module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900919
920 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000921out:
922 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900923 return ret;
924}
925
Ingo Molnar89fe8082013-09-30 12:07:11 +0200926#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300927
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000928static int
929find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
930 struct perf_probe_point *pp __maybe_unused,
931 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300932{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000933 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300934}
935
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530936static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300937 struct probe_trace_event **tevs __maybe_unused,
Arnaldo Carvalho de Melo1d027ee2014-01-13 15:15:25 -0300938 int max_tevs __maybe_unused,
939 const char *target __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300940{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400941 if (perf_probe_event_need_dwarf(pev)) {
942 pr_warning("Debuginfo-analysis is not supported.\n");
943 return -ENOSYS;
944 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530945
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300946 return 0;
947}
948
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300949int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000950 const char *module __maybe_unused,
951 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300952{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400953 pr_warning("Debuginfo-analysis is not supported.\n");
954 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300955}
956
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300957int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
958 int npevs __maybe_unused, int max_vls __maybe_unused,
959 const char *module __maybe_unused,
960 struct strfilter *filter __maybe_unused,
961 bool externs __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900962{
963 pr_warning("Debuginfo-analysis is not supported.\n");
964 return -ENOSYS;
965}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400966#endif
967
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000968void line_range__clear(struct line_range *lr)
969{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000970 free(lr->function);
971 free(lr->file);
972 free(lr->path);
973 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000974 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000975 memset(lr, 0, sizeof(*lr));
976}
977
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000978int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000979{
980 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000981 lr->line_list = intlist__new(NULL);
982 if (!lr->line_list)
983 return -ENOMEM;
984 else
985 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000986}
987
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100988static int parse_line_num(char **ptr, int *val, const char *what)
989{
990 const char *start = *ptr;
991
992 errno = 0;
993 *val = strtol(*ptr, ptr, 0);
994 if (errno || *ptr == start) {
995 semantic_error("'%s' is not a valid number.\n", what);
996 return -EINVAL;
997 }
998 return 0;
999}
1000
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001001/*
1002 * Stuff 'lr' according to the line range described by 'arg'.
1003 * The line range syntax is described by:
1004 *
1005 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001006 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001007 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001008int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001009{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001010 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001011 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001012
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001013 if (!name)
1014 return -ENOMEM;
1015
1016 lr->start = 0;
1017 lr->end = INT_MAX;
1018
1019 range = strchr(name, ':');
1020 if (range) {
1021 *range++ = '\0';
1022
1023 err = parse_line_num(&range, &lr->start, "start line");
1024 if (err)
1025 goto err;
1026
1027 if (*range == '+' || *range == '-') {
1028 const char c = *range++;
1029
1030 err = parse_line_num(&range, &lr->end, "end line");
1031 if (err)
1032 goto err;
1033
1034 if (c == '+') {
1035 lr->end += lr->start;
1036 /*
1037 * Adjust the number of lines here.
1038 * If the number of lines == 1, the
1039 * the end of line should be equal to
1040 * the start of line.
1041 */
1042 lr->end--;
1043 }
1044 }
1045
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001046 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001047
1048 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001049 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001050 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001052 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001054 if (*range != '\0') {
1055 semantic_error("Tailing with invalid str '%s'.\n", range);
1056 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001057 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001058 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001059
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001060 file = strchr(name, '@');
1061 if (file) {
1062 *file = '\0';
1063 lr->file = strdup(++file);
1064 if (lr->file == NULL) {
1065 err = -ENOMEM;
1066 goto err;
1067 }
1068 lr->function = name;
1069 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001070 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001071 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001072 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001073
1074 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001075err:
1076 free(name);
1077 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001078}
1079
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001080/* Check the name is good for event/group */
1081static bool check_event_name(const char *name)
1082{
1083 if (!isalpha(*name) && *name != '_')
1084 return false;
1085 while (*++name != '\0') {
1086 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1087 return false;
1088 }
1089 return true;
1090}
1091
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001092/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001093static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001094{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001095 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001096 char *ptr, *tmp;
1097 char c, nc = 0;
1098 /*
1099 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001100 * perf probe [EVENT=]SRC[:LN|;PTN]
1101 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001102 *
1103 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001104 */
1105
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001106 ptr = strpbrk(arg, ";=@+%");
1107 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001108 *ptr = '\0';
1109 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 if (strchr(arg, ':')) {
1111 semantic_error("Group name is not supported yet.\n");
1112 return -ENOTSUP;
1113 }
1114 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001115 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001116 "follow C symbol-naming rule.\n", arg);
1117 return -EINVAL;
1118 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001119 pev->event = strdup(arg);
1120 if (pev->event == NULL)
1121 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001122 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001123 arg = tmp;
1124 }
1125
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001126 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001127 if (ptr) {
1128 nc = *ptr;
1129 *ptr++ = '\0';
1130 }
1131
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001132 tmp = strdup(arg);
1133 if (tmp == NULL)
1134 return -ENOMEM;
1135
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001136 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001137 if (strchr(tmp, '.')) /* File */
1138 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001139 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001140 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001141
1142 /* Parse other options */
1143 while (ptr) {
1144 arg = ptr;
1145 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001146 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001147 pp->lazy_line = strdup(arg);
1148 if (pp->lazy_line == NULL)
1149 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001150 break;
1151 }
1152 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001153 if (ptr) {
1154 nc = *ptr;
1155 *ptr++ = '\0';
1156 }
1157 switch (c) {
1158 case ':': /* Line number */
1159 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001160 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001161 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001162 " in line number.\n");
1163 return -EINVAL;
1164 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001165 break;
1166 case '+': /* Byte offset from a symbol */
1167 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001168 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001169 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001170 " in offset.\n");
1171 return -EINVAL;
1172 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001173 break;
1174 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 if (pp->file) {
1176 semantic_error("SRC@SRC is not allowed.\n");
1177 return -EINVAL;
1178 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001179 pp->file = strdup(arg);
1180 if (pp->file == NULL)
1181 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001182 break;
1183 case '%': /* Probe places */
1184 if (strcmp(arg, "return") == 0) {
1185 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001186 } else { /* Others not supported yet */
1187 semantic_error("%%%s is not supported.\n", arg);
1188 return -ENOTSUP;
1189 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001190 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191 default: /* Buggy case */
1192 pr_err("This program has a bug at %s:%d.\n",
1193 __FILE__, __LINE__);
1194 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001195 break;
1196 }
1197 }
1198
1199 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001200 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001201 semantic_error("Lazy pattern can't be used with"
1202 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001203 return -EINVAL;
1204 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001205
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001207 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001208 return -EINVAL;
1209 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001210
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001212 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001213 return -EINVAL;
1214 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001215
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001216 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001217 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001218 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 return -EINVAL;
1220 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001221
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001223 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 return -EINVAL;
1225 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001226
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001228 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 return -EINVAL;
1230 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001231
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001232 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001233 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001234 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 return -EINVAL;
1236 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001237
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001238 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001239 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1240 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001242}
1243
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001244/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001246{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001247 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001248 struct perf_probe_arg_field **fieldp;
1249
1250 pr_debug("parsing arg: %s into ", str);
1251
Masami Hiramatsu48481932010-04-12 13:16:53 -04001252 tmp = strchr(str, '=');
1253 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001254 arg->name = strndup(str, tmp - str);
1255 if (arg->name == NULL)
1256 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001257 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001258 str = tmp + 1;
1259 }
1260
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001261 tmp = strchr(str, ':');
1262 if (tmp) { /* Type setting */
1263 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001264 arg->type = strdup(tmp + 1);
1265 if (arg->type == NULL)
1266 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001267 pr_debug("type:%s ", arg->type);
1268 }
1269
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001270 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001271 if (!is_c_varname(str) || !tmp) {
1272 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001273 arg->var = strdup(str);
1274 if (arg->var == NULL)
1275 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001276 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001278 }
1279
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001280 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001281 arg->var = strndup(str, tmp - str);
1282 if (arg->var == NULL)
1283 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001284 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001285 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001286 fieldp = &arg->field;
1287
1288 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001289 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1290 if (*fieldp == NULL)
1291 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001292 if (*tmp == '[') { /* Array */
1293 str = tmp;
1294 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001295 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001296 if (*tmp != ']' || tmp == str + 1) {
1297 semantic_error("Array index must be a"
1298 " number.\n");
1299 return -EINVAL;
1300 }
1301 tmp++;
1302 if (*tmp == '\0')
1303 tmp = NULL;
1304 } else { /* Structure */
1305 if (*tmp == '.') {
1306 str = tmp + 1;
1307 (*fieldp)->ref = false;
1308 } else if (tmp[1] == '>') {
1309 str = tmp + 2;
1310 (*fieldp)->ref = true;
1311 } else {
1312 semantic_error("Argument parse error: %s\n",
1313 str);
1314 return -EINVAL;
1315 }
1316 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001318 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001319 (*fieldp)->name = strndup(str, tmp - str);
1320 if ((*fieldp)->name == NULL)
1321 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001322 if (*str != '[')
1323 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001324 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1325 fieldp = &(*fieldp)->next;
1326 }
1327 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001328 (*fieldp)->name = strdup(str);
1329 if ((*fieldp)->name == NULL)
1330 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001331 if (*str != '[')
1332 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001333 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001334
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001335 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001336 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001337 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001338 if (arg->name == NULL)
1339 return -ENOMEM;
1340 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001341 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001342}
1343
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001344/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001345int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001346{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001347 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001348 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001349
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001350 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001351 if (!argv) {
1352 pr_debug("Failed to split arguments.\n");
1353 return -ENOMEM;
1354 }
1355 if (argc - 1 > MAX_PROBE_ARGS) {
1356 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1357 ret = -ERANGE;
1358 goto out;
1359 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001360 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001361 ret = parse_perf_probe_point(argv[0], pev);
1362 if (ret < 0)
1363 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001364
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001365 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001366 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001367 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1368 if (pev->args == NULL) {
1369 ret = -ENOMEM;
1370 goto out;
1371 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001372 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1373 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1374 if (ret >= 0 &&
1375 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001376 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 " kretprobe.\n");
1378 ret = -EINVAL;
1379 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001380 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001382 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001383
1384 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001385}
1386
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001387/* Return true if this perf_probe_event requires debuginfo */
1388bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001389{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390 int i;
1391
1392 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1393 return true;
1394
1395 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001396 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001397 return true;
1398
1399 return false;
1400}
1401
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301402/* Parse probe_events event into struct probe_point */
1403static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001404 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001405{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301406 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001407 char pr;
1408 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001409 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001410 int ret, i, argc;
1411 char **argv;
1412
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301413 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001414 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415 if (!argv) {
1416 pr_debug("Failed to split arguments.\n");
1417 return -ENOMEM;
1418 }
1419 if (argc < 2) {
1420 semantic_error("Too few probe arguments.\n");
1421 ret = -ERANGE;
1422 goto out;
1423 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001424
1425 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001426 argv0_str = strdup(argv[0]);
1427 if (argv0_str == NULL) {
1428 ret = -ENOMEM;
1429 goto out;
1430 }
1431 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1432 fmt2_str = strtok_r(NULL, "/", &fmt);
1433 fmt3_str = strtok_r(NULL, " \t", &fmt);
1434 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1435 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001436 semantic_error("Failed to parse event name: %s\n", argv[0]);
1437 ret = -EINVAL;
1438 goto out;
1439 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001440 pr = fmt1_str[0];
1441 tev->group = strdup(fmt2_str);
1442 tev->event = strdup(fmt3_str);
1443 if (tev->group == NULL || tev->event == NULL) {
1444 ret = -ENOMEM;
1445 goto out;
1446 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001447 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001448
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001449 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001450
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001451 /* Scan module name(if there), function name and offset */
1452 p = strchr(argv[1], ':');
1453 if (p) {
1454 tp->module = strndup(argv[1], p - argv[1]);
1455 p++;
1456 } else
1457 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001458 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001459 if (fmt1_str[0] == '0') /* only the address started with 0x */
1460 tp->address = strtoul(fmt1_str, NULL, 0);
1461 else {
1462 /* Only the symbol-based probe has offset */
1463 tp->symbol = strdup(fmt1_str);
1464 if (tp->symbol == NULL) {
1465 ret = -ENOMEM;
1466 goto out;
1467 }
1468 fmt2_str = strtok_r(NULL, "", &fmt);
1469 if (fmt2_str == NULL)
1470 tp->offset = 0;
1471 else
1472 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001473 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301476 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001477 if (tev->args == NULL) {
1478 ret = -ENOMEM;
1479 goto out;
1480 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482 p = strchr(argv[i + 2], '=');
1483 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001484 *p++ = '\0';
1485 else
1486 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001487 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001488 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001489 tev->args[i].value = strdup(p);
1490 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1491 ret = -ENOMEM;
1492 goto out;
1493 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001494 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001495 ret = 0;
1496out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001497 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001498 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001500}
1501
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001502/* Compose only probe arg */
1503int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1504{
1505 struct perf_probe_arg_field *field = pa->field;
1506 int ret;
1507 char *tmp = buf;
1508
Masami Hiramatsu48481932010-04-12 13:16:53 -04001509 if (pa->name && pa->var)
1510 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1511 else
1512 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001513 if (ret <= 0)
1514 goto error;
1515 tmp += ret;
1516 len -= ret;
1517
1518 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001519 if (field->name[0] == '[')
1520 ret = e_snprintf(tmp, len, "%s", field->name);
1521 else
1522 ret = e_snprintf(tmp, len, "%s%s",
1523 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001524 if (ret <= 0)
1525 goto error;
1526 tmp += ret;
1527 len -= ret;
1528 field = field->next;
1529 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001530
1531 if (pa->type) {
1532 ret = e_snprintf(tmp, len, ":%s", pa->type);
1533 if (ret <= 0)
1534 goto error;
1535 tmp += ret;
1536 len -= ret;
1537 }
1538
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001539 return tmp - buf;
1540error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001541 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001542 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001543}
1544
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001545/* Compose only probe point (not argument) */
1546static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001547{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001548 char *buf, *tmp;
1549 char offs[32] = "", line[32] = "", file[32] = "";
1550 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001551
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001552 buf = zalloc(MAX_CMDLEN);
1553 if (buf == NULL) {
1554 ret = -ENOMEM;
1555 goto error;
1556 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001557 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001558 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001559 if (ret <= 0)
1560 goto error;
1561 }
1562 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001563 ret = e_snprintf(line, 32, ":%d", pp->line);
1564 if (ret <= 0)
1565 goto error;
1566 }
1567 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001568 tmp = pp->file;
1569 len = strlen(tmp);
1570 if (len > 30) {
1571 tmp = strchr(pp->file + len - 30, '/');
1572 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1573 }
1574 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001575 if (ret <= 0)
1576 goto error;
1577 }
1578
1579 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001580 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1581 offs, pp->retprobe ? "%return" : "", line,
1582 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001583 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001584 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001585 if (ret <= 0)
1586 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001588 return buf;
1589error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001590 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001591 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001592 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001593}
1594
1595#if 0
1596char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1597{
1598 char *buf;
1599 int i, len, ret;
1600
1601 buf = synthesize_perf_probe_point(&pev->point);
1602 if (!buf)
1603 return NULL;
1604
1605 len = strlen(buf);
1606 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001607 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001608 pev->args[i].name);
1609 if (ret <= 0) {
1610 free(buf);
1611 return NULL;
1612 }
1613 len += ret;
1614 }
1615
1616 return buf;
1617}
1618#endif
1619
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301620static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621 char **buf, size_t *buflen,
1622 int depth)
1623{
1624 int ret;
1625 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301626 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001627 buflen, depth + 1);
1628 if (depth < 0)
1629 goto out;
1630 }
1631
1632 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1633 if (ret < 0)
1634 depth = ret;
1635 else {
1636 *buf += ret;
1637 *buflen -= ret;
1638 }
1639out:
1640 return depth;
1641
1642}
1643
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301644static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001645 char *buf, size_t buflen)
1646{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301647 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001648 int ret, depth = 0;
1649 char *tmp = buf;
1650
1651 /* Argument name or separator */
1652 if (arg->name)
1653 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1654 else
1655 ret = e_snprintf(buf, buflen, " ");
1656 if (ret < 0)
1657 return ret;
1658 buf += ret;
1659 buflen -= ret;
1660
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001661 /* Special case: @XXX */
1662 if (arg->value[0] == '@' && arg->ref)
1663 ref = ref->next;
1664
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001665 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001666 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301667 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001668 &buflen, 1);
1669 if (depth < 0)
1670 return depth;
1671 }
1672
1673 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001674 if (arg->value[0] == '@' && arg->ref)
1675 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1676 arg->ref->offset);
1677 else
1678 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001679 if (ret < 0)
1680 return ret;
1681 buf += ret;
1682 buflen -= ret;
1683
1684 /* Closing */
1685 while (depth--) {
1686 ret = e_snprintf(buf, buflen, ")");
1687 if (ret < 0)
1688 return ret;
1689 buf += ret;
1690 buflen -= ret;
1691 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001692 /* Print argument type */
1693 if (arg->type) {
1694 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1695 if (ret <= 0)
1696 return ret;
1697 buf += ret;
1698 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001699
1700 return buf - tmp;
1701}
1702
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301703char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001704{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301705 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001706 char *buf;
1707 int i, len, ret;
1708
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001709 buf = zalloc(MAX_CMDLEN);
1710 if (buf == NULL)
1711 return NULL;
1712
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001713 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1714 tev->group, tev->event);
1715 if (len <= 0)
1716 goto error;
1717
1718 /* Uprobes must have tp->address and tp->module */
1719 if (tev->uprobes && (!tp->address || !tp->module))
1720 goto error;
1721
1722 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301723 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001724 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1725 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301726 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001727 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301728 tp->module ?: "", tp->module ? ":" : "",
1729 tp->symbol, tp->offset);
1730
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001731 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001732 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001733 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734
1735 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301736 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001737 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001738 if (ret <= 0)
1739 goto error;
1740 len += ret;
1741 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001742
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001743 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001744error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001745 free(buf);
1746 return NULL;
1747}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001748
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001749static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1750 struct perf_probe_point *pp,
1751 bool is_kprobe)
1752{
1753 struct symbol *sym = NULL;
1754 struct map *map;
1755 u64 addr;
1756 int ret = -ENOENT;
1757
1758 if (!is_kprobe) {
1759 map = dso__new_map(tp->module);
1760 if (!map)
1761 goto out;
1762 addr = tp->address;
1763 sym = map__find_symbol(map, addr, NULL);
1764 } else {
1765 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1766 if (addr) {
1767 addr += tp->offset;
1768 sym = __find_kernel_function(addr, &map);
1769 }
1770 }
1771 if (!sym)
1772 goto out;
1773
1774 pp->retprobe = tp->retprobe;
1775 pp->offset = addr - map->unmap_ip(map, sym->start);
1776 pp->function = strdup(sym->name);
1777 ret = pp->function ? 0 : -ENOMEM;
1778
1779out:
1780 if (map && !is_kprobe) {
1781 dso__delete(map->dso);
1782 map__delete(map);
1783 }
1784
1785 return ret;
1786}
1787
1788static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1789 struct perf_probe_point *pp,
1790 bool is_kprobe)
1791{
1792 char buf[128];
1793 int ret;
1794
1795 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1796 if (!ret)
1797 return 0;
1798 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1799 if (!ret)
1800 return 0;
1801
1802 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1803
1804 if (tp->symbol) {
1805 pp->function = strdup(tp->symbol);
1806 pp->offset = tp->offset;
1807 } else if (!tp->module && !is_kprobe) {
1808 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1809 if (ret < 0)
1810 return ret;
1811 pp->function = strdup(buf);
1812 pp->offset = 0;
1813 }
1814 if (pp->function == NULL)
1815 return -ENOMEM;
1816
1817 pp->retprobe = tp->retprobe;
1818
1819 return 0;
1820}
1821
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301822static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301823 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001824{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001825 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001826 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001828 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001829 pev->event = strdup(tev->event);
1830 pev->group = strdup(tev->group);
1831 if (pev->event == NULL || pev->group == NULL)
1832 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001833
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001834 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001835 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001836 if (ret < 0)
1837 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001838
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001839 /* Convert trace_arg to probe_arg */
1840 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001841 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1842 if (pev->args == NULL)
1843 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001844 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001845 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001846 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001847 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301848 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001850 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001851 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001852 if (pev->args[i].name == NULL && ret >= 0)
1853 ret = -ENOMEM;
1854 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001855
1856 if (ret < 0)
1857 clear_perf_probe_event(pev);
1858
1859 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001860}
1861
1862void clear_perf_probe_event(struct perf_probe_event *pev)
1863{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001864 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001865 int i;
1866
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001867 free(pev->event);
1868 free(pev->group);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001869 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001870
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001871 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001872 free(pev->args[i].name);
1873 free(pev->args[i].var);
1874 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001875 field = pev->args[i].field;
1876 while (field) {
1877 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001878 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001879 free(field);
1880 field = next;
1881 }
1882 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001883 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884 memset(pev, 0, sizeof(*pev));
1885}
1886
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301887static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301889 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890 int i;
1891
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001892 free(tev->event);
1893 free(tev->group);
1894 free(tev->point.symbol);
1895 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001896 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001897 free(tev->args[i].name);
1898 free(tev->args[i].value);
1899 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900 ref = tev->args[i].ref;
1901 while (ref) {
1902 next = ref->next;
1903 free(ref);
1904 ref = next;
1905 }
1906 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001907 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001909}
1910
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001911static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301912{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001913 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301914
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001915 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301916 const char *config;
1917
1918 if (!is_kprobe)
1919 config = "CONFIG_UPROBE_EVENTS";
1920 else
1921 config = "CONFIG_KPROBE_EVENTS";
1922
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001923 pr_warning("%cprobe_events file does not exist"
1924 " - please rebuild kernel with %s.\n",
1925 is_kprobe ? 'k' : 'u', config);
1926 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001927 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001928 else
1929 pr_warning("Failed to open %cprobe_events: %s\n",
1930 is_kprobe ? 'k' : 'u',
1931 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301932}
1933
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001934static void print_both_open_warning(int kerr, int uerr)
1935{
1936 /* Both kprobes and uprobes are disabled, warn it. */
1937 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001938 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001939 else if (kerr == -ENOENT && uerr == -ENOENT)
1940 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1941 "or/and CONFIG_UPROBE_EVENTS.\n");
1942 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001943 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001944 pr_warning("Failed to open kprobe events: %s.\n",
1945 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1946 pr_warning("Failed to open uprobe events: %s.\n",
1947 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1948 }
1949}
1950
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001951static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001952{
1953 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001954 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001955 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001956 int ret;
1957
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001958 __debugfs = tracefs_find_mountpoint();
1959 if (__debugfs == NULL) {
1960 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001961
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001962 __debugfs = debugfs_find_mountpoint();
1963 if (__debugfs == NULL)
1964 return -ENOTSUP;
1965 }
1966
1967 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1968 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001969 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001970 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001971 if (readwrite && !probe_event_dry_run)
1972 ret = open(buf, O_RDWR, O_APPEND);
1973 else
1974 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001975
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301976 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001977 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001978 }
1979 return ret;
1980}
1981
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301982static int open_kprobe_events(bool readwrite)
1983{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001984 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301985}
1986
1987static int open_uprobe_events(bool readwrite)
1988{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001989 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301990}
1991
1992/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301993static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001994{
1995 int ret, idx;
1996 FILE *fp;
1997 char buf[MAX_CMDLEN];
1998 char *p;
1999 struct strlist *sl;
2000
2001 sl = strlist__new(true, NULL);
2002
2003 fp = fdopen(dup(fd), "r");
2004 while (!feof(fp)) {
2005 p = fgets(buf, MAX_CMDLEN, fp);
2006 if (!p)
2007 break;
2008
2009 idx = strlen(p) - 1;
2010 if (p[idx] == '\n')
2011 p[idx] = '\0';
2012 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002013 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002014 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002015 strlist__delete(sl);
2016 return NULL;
2017 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002018 }
2019 fclose(fp);
2020
2021 return sl;
2022}
2023
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002024struct kprobe_blacklist_node {
2025 struct list_head list;
2026 unsigned long start;
2027 unsigned long end;
2028 char *symbol;
2029};
2030
2031static void kprobe_blacklist__delete(struct list_head *blacklist)
2032{
2033 struct kprobe_blacklist_node *node;
2034
2035 while (!list_empty(blacklist)) {
2036 node = list_first_entry(blacklist,
2037 struct kprobe_blacklist_node, list);
2038 list_del(&node->list);
2039 free(node->symbol);
2040 free(node);
2041 }
2042}
2043
2044static int kprobe_blacklist__load(struct list_head *blacklist)
2045{
2046 struct kprobe_blacklist_node *node;
2047 const char *__debugfs = debugfs_find_mountpoint();
2048 char buf[PATH_MAX], *p;
2049 FILE *fp;
2050 int ret;
2051
2052 if (__debugfs == NULL)
2053 return -ENOTSUP;
2054
2055 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2056 if (ret < 0)
2057 return ret;
2058
2059 fp = fopen(buf, "r");
2060 if (!fp)
2061 return -errno;
2062
2063 ret = 0;
2064 while (fgets(buf, PATH_MAX, fp)) {
2065 node = zalloc(sizeof(*node));
2066 if (!node) {
2067 ret = -ENOMEM;
2068 break;
2069 }
2070 INIT_LIST_HEAD(&node->list);
2071 list_add_tail(&node->list, blacklist);
2072 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2073 ret = -EINVAL;
2074 break;
2075 }
2076 p = strchr(buf, '\t');
2077 if (p) {
2078 p++;
2079 if (p[strlen(p) - 1] == '\n')
2080 p[strlen(p) - 1] = '\0';
2081 } else
2082 p = (char *)"unknown";
2083 node->symbol = strdup(p);
2084 if (!node->symbol) {
2085 ret = -ENOMEM;
2086 break;
2087 }
2088 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2089 node->start, node->end, node->symbol);
2090 ret++;
2091 }
2092 if (ret < 0)
2093 kprobe_blacklist__delete(blacklist);
2094 fclose(fp);
2095
2096 return ret;
2097}
2098
2099static struct kprobe_blacklist_node *
2100kprobe_blacklist__find_by_address(struct list_head *blacklist,
2101 unsigned long address)
2102{
2103 struct kprobe_blacklist_node *node;
2104
2105 list_for_each_entry(node, blacklist, list) {
2106 if (node->start <= address && address <= node->end)
2107 return node;
2108 }
2109
2110 return NULL;
2111}
2112
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002113/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002114static int show_perf_probe_event(struct perf_probe_event *pev,
2115 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002116{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002117 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002118 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002119 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002120
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002121 /* Synthesize only event probe point */
2122 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002123 if (!place)
2124 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002125
2126 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002127 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002128 return ret;
2129
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002130 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002131 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002132 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002133
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002134 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002135 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002136 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002137 ret = synthesize_perf_probe_arg(&pev->args[i],
2138 buf, 128);
2139 if (ret < 0)
2140 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002141 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002142 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002143 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002144 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002145 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002146 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002147}
2148
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302149static int __show_perf_probe_events(int fd, bool is_kprobe)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002150{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302151 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302152 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002153 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002154 struct strlist *rawlist;
2155 struct str_node *ent;
2156
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002157 memset(&tev, 0, sizeof(tev));
2158 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002159
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302160 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002161 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002162 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002163
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002164 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302165 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002166 if (ret >= 0) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302167 ret = convert_to_perf_probe_event(&tev, &pev,
2168 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002170 ret = show_perf_probe_event(&pev,
2171 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002172 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002173 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302174 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002175 if (ret < 0)
2176 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002177 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002178 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002179
2180 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002181}
2182
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302183/* List up current perf-probe events */
2184int show_perf_probe_events(void)
2185{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002186 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302187
2188 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302189
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002190 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302191 if (ret < 0)
2192 return ret;
2193
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002194 kp_fd = open_kprobe_events(false);
2195 if (kp_fd >= 0) {
2196 ret = __show_perf_probe_events(kp_fd, true);
2197 close(kp_fd);
2198 if (ret < 0)
2199 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302200 }
2201
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002202 up_fd = open_uprobe_events(false);
2203 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002204 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002205 ret = kp_fd;
2206 goto out;
2207 }
2208
2209 if (up_fd >= 0) {
2210 ret = __show_perf_probe_events(up_fd, false);
2211 close(up_fd);
2212 }
2213out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002214 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215 return ret;
2216}
2217
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002218/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302219static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002220{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002221 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002222 struct strlist *sl, *rawlist;
2223 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302224 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002225 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002226
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002227 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302228 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002229 if (!rawlist)
2230 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002231 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002232 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302233 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002234 if (ret < 0)
2235 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002236 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2238 tev.event);
2239 if (ret >= 0)
2240 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002241 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002242 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302243 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002244 if (ret < 0)
2245 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002246 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002247 strlist__delete(rawlist);
2248
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249 if (ret < 0) {
2250 strlist__delete(sl);
2251 return NULL;
2252 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002253 return sl;
2254}
2255
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302256static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002257{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002258 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302259 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002260 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002261
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002262 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302263 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002264 return -EINVAL;
2265 }
2266
Masami Hiramatsufa282442009-12-08 17:03:23 -05002267 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002268 if (!probe_event_dry_run) {
2269 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002270 if (ret <= 0) {
2271 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002273 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002274 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002275 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002276 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002277 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002278}
2279
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002280static int get_new_event_name(char *buf, size_t len, const char *base,
2281 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002282{
2283 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002284
2285 /* Try no suffix */
2286 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002287 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002288 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002289 return ret;
2290 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002291 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002292 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002293
Masami Hiramatsud761b082009-12-15 10:32:25 -05002294 if (!allow_suffix) {
2295 pr_warning("Error: event \"%s\" already exists. "
2296 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002297 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002298 }
2299
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002300 /* Try to add suffix */
2301 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002302 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002303 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002304 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002305 return ret;
2306 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002307 if (!strlist__has_entry(namelist, buf))
2308 break;
2309 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002310 if (i == MAX_EVENT_INDEX) {
2311 pr_warning("Too many events are on the same function.\n");
2312 ret = -ERANGE;
2313 }
2314
2315 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002316}
2317
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002318/* Warn if the current kernel's uprobe implementation is old */
2319static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2320{
2321 int i;
2322 char *buf = synthesize_probe_trace_command(tev);
2323
2324 /* Old uprobe event doesn't support memory dereference */
2325 if (!tev->uprobes || tev->nargs == 0 || !buf)
2326 goto out;
2327
2328 for (i = 0; i < tev->nargs; i++)
2329 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2330 pr_warning("Please upgrade your kernel to at least "
2331 "3.14 to have access to feature %s\n",
2332 tev->args[i].value);
2333 break;
2334 }
2335out:
2336 free(buf);
2337}
2338
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302339static int __add_probe_trace_events(struct perf_probe_event *pev,
2340 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002341 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002342{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002343 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302344 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002345 char buf[64];
2346 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002347 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002348 LIST_HEAD(blacklist);
2349 struct kprobe_blacklist_node *node;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002350
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302351 if (pev->uprobes)
2352 fd = open_uprobe_events(true);
2353 else
2354 fd = open_kprobe_events(true);
2355
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002356 if (fd < 0) {
2357 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002358 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002359 }
2360
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002361 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302362 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002363 if (!namelist) {
2364 pr_debug("Failed to get current event list.\n");
2365 return -EIO;
2366 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002367 /* Get kprobe blacklist if exists */
2368 if (!pev->uprobes) {
2369 ret = kprobe_blacklist__load(&blacklist);
2370 if (ret < 0)
2371 pr_debug("No kprobe blacklist support, ignored\n");
2372 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002373
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002374 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002375 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002376 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002377 tev = &tevs[i];
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002378 /* Ensure that the address is NOT blacklisted */
2379 node = kprobe_blacklist__find_by_address(&blacklist,
2380 tev->point.address);
2381 if (node) {
2382 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2383 continue;
2384 }
2385
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002386 if (pev->event)
2387 event = pev->event;
2388 else
2389 if (pev->point.function)
2390 event = pev->point.function;
2391 else
2392 event = tev->point.symbol;
2393 if (pev->group)
2394 group = pev->group;
2395 else
2396 group = PERFPROBE_GROUP;
2397
2398 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002399 ret = get_new_event_name(buf, 64, event,
2400 namelist, allow_suffix);
2401 if (ret < 0)
2402 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002403 event = buf;
2404
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002405 tev->event = strdup(event);
2406 tev->group = strdup(group);
2407 if (tev->event == NULL || tev->group == NULL) {
2408 ret = -ENOMEM;
2409 break;
2410 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302411 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002412 if (ret < 0)
2413 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002414 /* Add added event name to namelist */
2415 strlist__add(namelist, event);
2416
2417 /* Trick here - save current event/group */
2418 event = pev->event;
2419 group = pev->group;
2420 pev->event = tev->event;
2421 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002422 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002423 /* Trick here - restore current event/group */
2424 pev->event = (char *)event;
2425 pev->group = (char *)group;
2426
2427 /*
2428 * Probes after the first probe which comes from same
2429 * user input are always allowed to add suffix, because
2430 * there might be several addresses corresponding to
2431 * one code line.
2432 */
2433 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002434 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002435 if (ret == -EINVAL && pev->uprobes)
2436 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002437
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002438 /* Note that it is possible to skip all events because of blacklist */
2439 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002440 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002441 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2442 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 tev->event);
2444 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002445
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002446 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002447 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002448 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002449 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002450}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002451
Namhyung Kim564c62a2015-01-14 20:18:07 +09002452static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002453{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002454 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002455 struct symbol *sym;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002456
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002457 map__for_each_symbol_by_name(map, name, sym) {
Namhyung Kim564c62a2015-01-14 20:18:07 +09002458 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2459 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002460 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002461
2462 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002463}
2464
2465#define strdup_or_goto(str, label) \
2466 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2467
2468/*
2469 * Find probe function addresses from map.
2470 * Return an error or the number of found probe_trace_event
2471 */
2472static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2473 struct probe_trace_event **tevs,
2474 int max_tevs, const char *target)
2475{
2476 struct map *map = NULL;
2477 struct kmap *kmap = NULL;
2478 struct ref_reloc_sym *reloc_sym = NULL;
2479 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002480 struct probe_trace_event *tev;
2481 struct perf_probe_point *pp = &pev->point;
2482 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002483 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002484 int ret, i;
2485
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002486 map = get_target_map(target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002487 if (!map) {
2488 ret = -EINVAL;
2489 goto out;
2490 }
2491
2492 /*
2493 * Load matched symbols: Since the different local symbols may have
2494 * same name but different addresses, this lists all the symbols.
2495 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002496 num_matched_functions = find_probe_functions(map, pp->function);
2497 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002498 pr_err("Failed to find symbol %s in %s\n", pp->function,
2499 target ? : "kernel");
2500 ret = -ENOENT;
2501 goto out;
2502 } else if (num_matched_functions > max_tevs) {
2503 pr_err("Too many functions matched in %s\n",
2504 target ? : "kernel");
2505 ret = -E2BIG;
2506 goto out;
2507 }
2508
Namhyung Kim25dd9172015-01-14 20:18:08 +09002509 if (!pev->uprobes && !pp->retprobe) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002510 kmap = map__kmap(map);
2511 reloc_sym = kmap->ref_reloc_sym;
2512 if (!reloc_sym) {
2513 pr_warning("Relocated base symbol is not found!\n");
2514 ret = -EINVAL;
2515 goto out;
2516 }
2517 }
2518
2519 /* Setup result trace-probe-events */
2520 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2521 if (!*tevs) {
2522 ret = -ENOMEM;
2523 goto out;
2524 }
2525
2526 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002527
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002528 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002529 tev = (*tevs) + ret;
2530 tp = &tev->point;
2531 if (ret == num_matched_functions) {
2532 pr_warning("Too many symbols are listed. Skip it.\n");
2533 break;
2534 }
2535 ret++;
2536
2537 if (pp->offset > sym->end - sym->start) {
2538 pr_warning("Offset %ld is bigger than the size of %s\n",
2539 pp->offset, sym->name);
2540 ret = -ENOENT;
2541 goto err_out;
2542 }
2543 /* Add one probe point */
2544 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2545 if (reloc_sym) {
2546 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2547 tp->offset = tp->address - reloc_sym->addr;
2548 } else {
2549 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2550 tp->offset = pp->offset;
2551 }
2552 tp->retprobe = pp->retprobe;
2553 if (target)
2554 tev->point.module = strdup_or_goto(target, nomem_out);
2555 tev->uprobes = pev->uprobes;
2556 tev->nargs = pev->nargs;
2557 if (tev->nargs) {
2558 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2559 tev->nargs);
2560 if (tev->args == NULL)
2561 goto nomem_out;
2562 }
2563 for (i = 0; i < tev->nargs; i++) {
2564 if (pev->args[i].name)
2565 tev->args[i].name =
2566 strdup_or_goto(pev->args[i].name,
2567 nomem_out);
2568
2569 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2570 nomem_out);
2571 if (pev->args[i].type)
2572 tev->args[i].type =
2573 strdup_or_goto(pev->args[i].type,
2574 nomem_out);
2575 }
2576 }
2577
2578out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002579 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002580 return ret;
2581
2582nomem_out:
2583 ret = -ENOMEM;
2584err_out:
2585 clear_probe_trace_events(*tevs, num_matched_functions);
2586 zfree(tevs);
2587 goto out;
2588}
2589
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302590static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2591 struct probe_trace_event **tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302592 int max_tevs, const char *target)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002593{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002594 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002595
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002596 if (pev->uprobes && !pev->group) {
2597 /* Replace group name if not given */
2598 ret = convert_exec_to_group(target, &pev->group);
2599 if (ret != 0) {
2600 pr_warning("Failed to make a group name.\n");
2601 return ret;
2602 }
2603 }
2604
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002605 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302606 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002607 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002608 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002609
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002610 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002611}
2612
2613struct __event_package {
2614 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302615 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002616 int ntevs;
2617};
2618
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002619int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302620 int max_tevs, const char *target, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002621{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002622 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002623 struct __event_package *pkgs;
2624
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302625 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002626 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302627
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002628 if (pkgs == NULL)
2629 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002630
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002631 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002632 if (ret < 0) {
2633 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002634 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002635 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002636
2637 /* Loop 1: convert all events */
2638 for (i = 0; i < npevs; i++) {
2639 pkgs[i].pev = &pevs[i];
2640 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302641 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002642 &pkgs[i].tevs,
2643 max_tevs,
Srikar Dronamraju4eced232012-02-02 19:50:40 +05302644 target);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002645 if (ret < 0)
2646 goto end;
2647 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002648 }
2649
2650 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002651 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302652 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002653 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002654 if (ret < 0)
2655 break;
2656 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002657end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002658 /* Loop 3: cleanup and free trace events */
2659 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002660 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302661 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002662 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002663 }
2664 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002665 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002666
2667 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002668}
2669
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302670static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002671{
2672 char *p;
2673 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002674 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002675
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302676 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002677 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2678 if (ret < 0)
2679 goto error;
2680
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002681 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002682 if (!p) {
2683 pr_debug("Internal error: %s should have ':' but not.\n",
2684 ent->s);
2685 ret = -ENOTSUP;
2686 goto error;
2687 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002688 *p = '/';
2689
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002690 pr_debug("Writing event: %s\n", buf);
2691 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002692 if (ret < 0) {
2693 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002694 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002695 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002697 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002698 return 0;
2699error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002700 pr_warning("Failed to delete event: %s\n",
2701 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002702 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002703}
2704
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302705static int del_trace_probe_event(int fd, const char *buf,
2706 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002707{
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002708 struct str_node *ent, *n;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302709 int ret = -1;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002710
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002711 if (strpbrk(buf, "*?")) { /* Glob-exp */
2712 strlist__for_each_safe(ent, n, namelist)
2713 if (strglobmatch(ent->s, buf)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302714 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002715 if (ret < 0)
2716 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002717 strlist__remove(namelist, ent);
2718 }
2719 } else {
2720 ent = strlist__find(namelist, buf);
2721 if (ent) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302722 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002723 if (ret >= 0)
2724 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002725 }
2726 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002727
2728 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002729}
2730
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002731int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002732{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302733 int ret = -1, ufd = -1, kfd = -1;
2734 char buf[128];
Masami Hiramatsufa282442009-12-08 17:03:23 -05002735 const char *group, *event;
2736 char *p, *str;
2737 struct str_node *ent;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302738 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002739
Masami Hiramatsufa282442009-12-08 17:03:23 -05002740 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302741 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002742 if (kfd >= 0)
2743 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302744
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302745 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002746 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302747 unamelist = get_probe_trace_event_names(ufd, true);
2748
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002749 if (kfd < 0 && ufd < 0) {
2750 print_both_open_warning(kfd, ufd);
2751 goto error;
2752 }
2753
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302754 if (namelist == NULL && unamelist == NULL)
2755 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002756
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002757 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002758 str = strdup(ent->s);
2759 if (str == NULL) {
2760 ret = -ENOMEM;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302761 goto error;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002762 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002763 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002764 p = strchr(str, ':');
2765 if (p) {
2766 group = str;
2767 *p = '\0';
2768 event = p + 1;
2769 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002770 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05002771 event = str;
2772 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302773
2774 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2775 if (ret < 0) {
2776 pr_err("Failed to copy event.");
2777 free(str);
2778 goto error;
2779 }
2780
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002781 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302782
2783 if (namelist)
2784 ret = del_trace_probe_event(kfd, buf, namelist);
2785
2786 if (unamelist && ret != 0)
2787 ret = del_trace_probe_event(ufd, buf, unamelist);
2788
2789 if (ret != 0)
2790 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2791
Masami Hiramatsufa282442009-12-08 17:03:23 -05002792 free(str);
2793 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302794
2795error:
2796 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302797 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302798 close(kfd);
2799 }
2800
2801 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302802 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302803 close(ufd);
2804 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002805
2806 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002807}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302808
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002809/* TODO: don't use a global variable for filter ... */
2810static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002811
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002812/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002813 * If a symbol corresponds to a function with global binding and
2814 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002815 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002816static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002817 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002818{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002819 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002820 strfilter__compare(available_func_filter, sym->name))
2821 return 0;
2822 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002823}
2824
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002825int show_available_funcs(const char *target, struct strfilter *_filter,
2826 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002827{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002828 struct map *map;
2829 int ret;
2830
2831 ret = init_symbol_maps(user);
2832 if (ret < 0)
2833 return ret;
2834
2835 /* Get a symbol map */
2836 if (user)
2837 map = dso__new_map(target);
2838 else
2839 map = kernel_get_module_map(target);
2840 if (!map) {
2841 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002842 return -EINVAL;
2843 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002844
2845 /* Load symbols with given filter */
2846 available_func_filter = _filter;
2847 if (map__load(map, filter_available_functions)) {
2848 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2849 goto end;
2850 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002851 if (!dso__sorted_by_name(map->dso, map->type))
2852 dso__sort_by_name(map->dso, map->type);
2853
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002854 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302855 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002856 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2857end:
2858 if (user) {
2859 dso__delete(map->dso);
2860 map__delete(map);
2861 }
2862 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302863
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002864 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302865}
2866