blob: 3d7d60cc6f162ba95ff3c43f676294dbc3739bde [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"
Jiri Olsa4605eab2015-09-02 09:56:43 +020043#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090047#include "probe-file.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 */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090054struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090058int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059{
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68}
69
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030070static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000071static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040072
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090073/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000074static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040076 int ret;
77
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090079 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090080 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 if (ret < 0) {
82 pr_debug("Failed to init symbol map.\n");
83 goto out;
84 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040085
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000086 if (host_machine || user_only) /* already initialized */
87 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91
92 host_machine = machine__new_host();
93 if (!host_machine) {
94 pr_debug("machine__new_host() failed.\n");
95 symbol__exit();
96 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040098out:
99 if (ret < 0)
100 pr_warning("Failed to init vmlinux path.\n");
101 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400102}
103
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000104static void exit_symbol_maps(void)
105{
106 if (host_machine) {
107 machine__delete(host_machine);
108 host_machine = NULL;
109 }
110 symbol__exit();
111}
112
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900113static struct symbol *__find_kernel_function_by_name(const char *name,
114 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400115{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000116 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900117 NULL);
118}
119
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000120static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
121{
122 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
123}
124
125static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
126{
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
128 struct kmap *kmap;
129
130 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
131 return NULL;
132
133 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
Wang Nanba927322015-04-07 08:22:45 +0000134 if (!kmap)
135 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000136 return kmap->ref_reloc_sym;
137}
138
139static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
140{
141 struct ref_reloc_sym *reloc_sym;
142 struct symbol *sym;
143 struct map *map;
144
145 /* ref_reloc_sym is just a label. Need a special fix*/
146 reloc_sym = kernel_get_ref_reloc_sym();
147 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
148 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
149 else {
150 sym = __find_kernel_function_by_name(name, &map);
151 if (sym)
152 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800153 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000154 }
155 return 0;
156}
157
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900158static struct map *kernel_get_module_map(const char *module)
159{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000160 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300161 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300162 struct map *pos;
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, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300166 return machine__findnew_module_map(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
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300171 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900172 if (strncmp(pos->dso->short_name + 1, module,
173 pos->dso->short_name_len - 2) == 0) {
174 return pos;
175 }
176 }
177 return NULL;
178}
179
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900180static struct map *get_target_map(const char *target, bool user)
181{
182 /* Init maps of given executable or kernel */
183 if (user)
184 return dso__new_map(target);
185 else
186 return kernel_get_module_map(target);
187}
188
189static void put_target_map(struct map *map, bool user)
190{
191 if (map && user) {
192 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300193 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900194 }
195}
196
197
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000198static int convert_exec_to_group(const char *exec, char **result)
199{
200 char *ptr1, *ptr2, *exec_copy;
201 char buf[64];
202 int ret;
203
204 exec_copy = strdup(exec);
205 if (!exec_copy)
206 return -ENOMEM;
207
208 ptr1 = basename(exec_copy);
209 if (!ptr1) {
210 ret = -EINVAL;
211 goto out;
212 }
213
214 ptr2 = strpbrk(ptr1, "-._");
215 if (ptr2)
216 *ptr2 = '\0';
217 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
218 if (ret < 0)
219 goto out;
220
221 *result = strdup(buf);
222 ret = *result ? 0 : -ENOMEM;
223
224out:
225 free(exec_copy);
226 return ret;
227}
228
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900229static void clear_perf_probe_point(struct perf_probe_point *pp)
230{
231 free(pp->file);
232 free(pp->function);
233 free(pp->lazy_line);
234}
235
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000236static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
237{
238 int i;
239
240 for (i = 0; i < ntevs; i++)
241 clear_probe_trace_event(tevs + i);
242}
243
Masami Hiramatsub0312202015-06-16 20:50:55 +0900244static bool kprobe_blacklist__listed(unsigned long address);
245static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
246{
He Kuang7c31bb82015-06-18 02:49:10 +0000247 u64 etext_addr;
248
Masami Hiramatsub0312202015-06-16 20:50:55 +0900249 /* Get the address of _etext for checking non-probable text symbol */
He Kuang7c31bb82015-06-18 02:49:10 +0000250 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
251
252 if (etext_addr != 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900253 pr_warning("%s is out of .text, skip it.\n", symbol);
254 else if (kprobe_blacklist__listed(address))
255 pr_warning("%s is blacklisted function, skip it.\n", symbol);
256 else
257 return false;
258
259 return true;
260}
261
Ingo Molnar89fe8082013-09-30 12:07:11 +0200262#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000263
264static int kernel_get_module_dso(const char *module, struct dso **pdso)
265{
266 struct dso *dso;
267 struct map *map;
268 const char *vmlinux_name;
269 int ret = 0;
270
271 if (module) {
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300272 list_for_each_entry(dso, &host_machine->dsos.head, node) {
273 if (!dso->kernel)
274 continue;
Wang Nan60fb7742015-05-28 02:25:05 +0000275 if (strncmp(dso->short_name + 1, module,
276 dso->short_name_len - 2) == 0)
277 goto found;
278 }
279 pr_debug("Failed to find module %s.\n", module);
280 return -ENOENT;
281 }
282
283 map = host_machine->vmlinux_maps[MAP__FUNCTION];
284 dso = map->dso;
285
286 vmlinux_name = symbol_conf.vmlinux_name;
287 dso->load_errno = 0;
288 if (vmlinux_name)
289 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
290 else
291 ret = dso__load_vmlinux_path(dso, map, NULL);
292found:
293 *pdso = dso;
294 return ret;
295}
296
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900297/*
298 * Some binaries like glibc have special symbols which are on the symbol
299 * table, but not in the debuginfo. If we can find the address of the
300 * symbol from map, we can translate the address back to the probe point.
301 */
302static int find_alternative_probe_point(struct debuginfo *dinfo,
303 struct perf_probe_point *pp,
304 struct perf_probe_point *result,
305 const char *target, bool uprobes)
306{
307 struct map *map = NULL;
308 struct symbol *sym;
309 u64 address = 0;
310 int ret = -ENOENT;
311
312 /* This can work only for function-name based one */
313 if (!pp->function || pp->file)
314 return -ENOTSUP;
315
316 map = get_target_map(target, uprobes);
317 if (!map)
318 return -EINVAL;
319
320 /* Find the address of given function */
321 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900322 if (uprobes)
323 address = sym->start;
324 else
325 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900326 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900327 }
328 if (!address) {
329 ret = -ENOENT;
330 goto out;
331 }
Wang Nanf6c15622015-04-08 02:14:34 +0000332 pr_debug("Symbol %s address found : %" PRIx64 "\n",
333 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900334
335 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
336 result);
337 if (ret <= 0)
338 ret = (!ret) ? -ENOENT : ret;
339 else {
340 result->offset += pp->offset;
341 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800342 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900343 ret = 0;
344 }
345
346out:
347 put_target_map(map, uprobes);
348 return ret;
349
350}
351
352static int get_alternative_probe_event(struct debuginfo *dinfo,
353 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900354 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900355{
356 int ret;
357
358 memcpy(tmp, &pev->point, sizeof(*tmp));
359 memset(&pev->point, 0, sizeof(pev->point));
360 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900361 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900362 if (ret < 0)
363 memcpy(&pev->point, tmp, sizeof(*tmp));
364
365 return ret;
366}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000367
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900368static int get_alternative_line_range(struct debuginfo *dinfo,
369 struct line_range *lr,
370 const char *target, bool user)
371{
David Ahern6d4a4892015-03-11 10:36:20 -0400372 struct perf_probe_point pp = { .function = lr->function,
373 .file = lr->file,
374 .line = lr->start };
375 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900376 int ret, len = 0;
377
David Ahern6d4a4892015-03-11 10:36:20 -0400378 memset(&result, 0, sizeof(result));
379
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900380 if (lr->end != INT_MAX)
381 len = lr->end - lr->start;
382 ret = find_alternative_probe_point(dinfo, &pp, &result,
383 target, user);
384 if (!ret) {
385 lr->function = result.function;
386 lr->file = result.file;
387 lr->start = result.line;
388 if (lr->end != INT_MAX)
389 lr->end = lr->start + len;
390 clear_perf_probe_point(&pp);
391 }
392 return ret;
393}
394
Masami Hiramatsuff741782011-06-27 16:27:39 +0900395/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000396static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900397{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000398 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900399 char reason[STRERR_BUFSIZE];
400 struct debuginfo *ret = NULL;
401 struct dso *dso = NULL;
402 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900403
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000404 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900405 err = kernel_get_module_dso(module, &dso);
406 if (err < 0) {
407 if (!dso || dso->load_errno == 0) {
408 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
409 strcpy(reason, "(unknown)");
410 } else
411 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000412 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900413 pr_err("Failed to find the path for %s: %s\n",
414 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900415 return NULL;
416 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900417 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900418 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000419 ret = debuginfo__new(path);
420 if (!ret && !silent) {
421 pr_warning("The %s file has no debug information.\n", path);
422 if (!module || !strtailcmp(path, ".ko"))
423 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
424 else
425 pr_warning("Rebuild with -g, ");
426 pr_warning("or install an appropriate debuginfo package.\n");
427 }
428 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400429}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300430
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900431/* For caching the last debuginfo */
432static struct debuginfo *debuginfo_cache;
433static char *debuginfo_cache_path;
434
435static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
436{
437 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
438 (!debuginfo_cache_path && !module && debuginfo_cache))
439 goto out;
440
441 /* Copy module path */
442 free(debuginfo_cache_path);
443 if (module) {
444 debuginfo_cache_path = strdup(module);
445 if (!debuginfo_cache_path) {
446 debuginfo__delete(debuginfo_cache);
447 debuginfo_cache = NULL;
448 goto out;
449 }
450 }
451
452 debuginfo_cache = open_debuginfo(module, silent);
453 if (!debuginfo_cache)
454 zfree(&debuginfo_cache_path);
455out:
456 return debuginfo_cache;
457}
458
459static void debuginfo_cache__exit(void)
460{
461 debuginfo__delete(debuginfo_cache);
462 debuginfo_cache = NULL;
463 zfree(&debuginfo_cache_path);
464}
465
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000466
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000467static int get_text_start_address(const char *exec, unsigned long *address)
468{
469 Elf *elf;
470 GElf_Ehdr ehdr;
471 GElf_Shdr shdr;
472 int fd, ret = -ENOENT;
473
474 fd = open(exec, O_RDONLY);
475 if (fd < 0)
476 return -errno;
477
478 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
479 if (elf == NULL)
480 return -EINVAL;
481
482 if (gelf_getehdr(elf, &ehdr) == NULL)
483 goto out;
484
485 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
486 goto out;
487
488 *address = shdr.sh_addr - shdr.sh_offset;
489 ret = 0;
490out:
491 elf_end(elf);
492 return ret;
493}
494
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000495/*
496 * Convert trace point to probe point with debuginfo
497 */
498static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
499 struct perf_probe_point *pp,
500 bool is_kprobe)
501{
502 struct debuginfo *dinfo = NULL;
503 unsigned long stext = 0;
504 u64 addr = tp->address;
505 int ret = -ENOENT;
506
507 /* convert the address to dwarf address */
508 if (!is_kprobe) {
509 if (!addr) {
510 ret = -EINVAL;
511 goto error;
512 }
513 ret = get_text_start_address(tp->module, &stext);
514 if (ret < 0)
515 goto error;
516 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000517 } else if (tp->symbol) {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000518 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
519 if (addr == 0)
520 goto error;
521 addr += tp->offset;
522 }
523
524 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
525 tp->module ? : "kernel");
526
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900527 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
528 if (dinfo)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000529 ret = debuginfo__find_probe_point(dinfo,
530 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900531 else
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000532 ret = -ENOENT;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000533
534 if (ret > 0) {
535 pp->retprobe = tp->retprobe;
536 return 0;
537 }
538error:
539 pr_debug("Failed to find corresponding probes from debuginfo.\n");
540 return ret ? : -ENOENT;
541}
542
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000543static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
544 int ntevs, const char *exec)
545{
546 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000547 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000548
549 if (!exec)
550 return 0;
551
552 ret = get_text_start_address(exec, &stext);
553 if (ret < 0)
554 return ret;
555
556 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000557 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000558 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000559 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000560 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000561 ret = -ENOMEM;
562 break;
563 }
564 tevs[i].uprobes = true;
565 }
566
567 return ret;
568}
569
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900570static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
571 int ntevs, const char *module)
572{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900573 int i, ret = 0;
574 char *tmp;
575
576 if (!module)
577 return 0;
578
579 tmp = strrchr(module, '/');
580 if (tmp) {
581 /* This is a module path -- get the module name */
582 module = strdup(tmp + 1);
583 if (!module)
584 return -ENOMEM;
585 tmp = strchr(module, '.');
586 if (tmp)
587 *tmp = '\0';
588 tmp = (char *)module; /* For free() */
589 }
590
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900591 for (i = 0; i < ntevs; i++) {
592 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900593 if (!tevs[i].point.module) {
594 ret = -ENOMEM;
595 break;
596 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900597 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900598
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300599 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900600 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900601}
602
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000603/* Post processing the probe events */
604static int post_process_probe_trace_events(struct probe_trace_event *tevs,
605 int ntevs, const char *module,
606 bool uprobe)
607{
608 struct ref_reloc_sym *reloc_sym;
609 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900610 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000611
612 if (uprobe)
613 return add_exec_to_probe_trace_events(tevs, ntevs, module);
614
615 /* Note that currently ref_reloc_sym based probe is not for drivers */
616 if (module)
617 return add_module_to_probe_trace_events(tevs, ntevs, module);
618
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000619 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000620 if (!reloc_sym) {
621 pr_warning("Relocated base symbol is not found!\n");
622 return -EINVAL;
623 }
624
625 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900626 if (!tevs[i].point.address || tevs[i].point.retprobe)
627 continue;
628 /* If we found a wrong one, mark it by NULL symbol */
629 if (kprobe_warn_out_range(tevs[i].point.symbol,
630 tevs[i].point.address)) {
631 tmp = NULL;
632 skipped++;
633 } else {
634 tmp = strdup(reloc_sym->name);
635 if (!tmp)
636 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000637 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900638 /* If we have no realname, use symbol for it */
639 if (!tevs[i].point.realname)
640 tevs[i].point.realname = tevs[i].point.symbol;
641 else
642 free(tevs[i].point.symbol);
643 tevs[i].point.symbol = tmp;
644 tevs[i].point.offset = tevs[i].point.address -
645 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000646 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900647 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000648}
649
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300650/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530651static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900652 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300653{
654 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900655 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530656 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900657 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300658
Masami Hiramatsu44225522015-05-08 10:03:28 +0900659 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900660 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000661 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900662 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900663 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300664 return 0;
665 }
666
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000667 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900668 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900669 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900670
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900671 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900672 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900673 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900674 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900675 /*
676 * Write back to the original probe_event for
677 * setting appropriate (user given) event name
678 */
679 clear_perf_probe_point(&pev->point);
680 memcpy(&pev->point, &tmp, sizeof(tmp));
681 }
682 }
683
Masami Hiramatsuff741782011-06-27 16:27:39 +0900684 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300685
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400686 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000687 pr_debug("Found %d probe_trace_events.\n", ntevs);
688 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900689 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900690 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000691 clear_probe_trace_events(*tevs, ntevs);
692 zfree(tevs);
693 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900694 if (ret != ntevs)
695 return ret < 0 ? ret : ntevs;
696 ntevs = 0;
697 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400698 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300699
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400700 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900701 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900703 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 }
705 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400706 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000707 if (ntevs < 0) {
708 if (ntevs == -EBADF)
709 pr_warning("Warning: No dwarf info found in the vmlinux - "
710 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400711 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900712 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400713 return 0;
714 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300715 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400716 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717}
718
719#define LINEBUF_SIZE 256
720#define NR_ADDITIONAL_LINES 2
721
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100722static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300723{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000724 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100725 const char *color = show_num ? "" : PERF_COLOR_BLUE;
726 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300727
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100728 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300729 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
730 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100731 if (skip)
732 continue;
733 if (!prefix) {
734 prefix = show_num ? "%7d " : " ";
735 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300736 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100737 color_fprintf(stdout, color, "%s", buf);
738
739 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400740
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100741 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300742error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100743 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000744 pr_warning("File read error: %s\n",
745 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100746 return -1;
747 }
748 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300749}
750
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100751static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
752{
753 int rv = __show_one_line(fp, l, skip, show_num);
754 if (rv == 0) {
755 pr_warning("Source file is shorter than expected.\n");
756 rv = -1;
757 }
758 return rv;
759}
760
761#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
762#define show_one_line(f,l) _show_one_line(f,l,false,false)
763#define skip_one_line(f,l) _show_one_line(f,l,true,false)
764#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
765
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300766/*
767 * Show line-range always requires debuginfo to find source file and
768 * line number.
769 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900770static int __show_line_range(struct line_range *lr, const char *module,
771 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300772{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400773 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000774 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900775 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300776 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900777 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900778 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000779 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300780
781 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000782 dinfo = open_debuginfo(module, false);
783 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900784 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400785
Masami Hiramatsuff741782011-06-27 16:27:39 +0900786 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900787 if (!ret) { /* Not found, retry with an alternative */
788 ret = get_alternative_line_range(dinfo, lr, module, user);
789 if (!ret)
790 ret = debuginfo__find_line_range(dinfo, lr);
791 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900792 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000793 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400794 pr_warning("Specified source line is not found.\n");
795 return -ENOENT;
796 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000797 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 return ret;
799 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900801 /* Convert source file path */
802 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900803 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800804
805 /* Free old path when new path is assigned */
806 if (tmp != lr->path)
807 free(tmp);
808
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900809 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000810 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900811 return ret;
812 }
813
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300814 setup_pager();
815
816 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900817 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300818 lr->start - lr->offset);
819 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100820 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300821
822 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400823 if (fp == NULL) {
824 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000825 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400826 return -errno;
827 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300828 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100829 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100830 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100831 if (ret < 0)
832 goto end;
833 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300834
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000835 intlist__for_each(ln, lr->line_list) {
836 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100837 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100838 if (ret < 0)
839 goto end;
840 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100841 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400842 if (ret < 0)
843 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300844 }
845
846 if (lr->end == INT_MAX)
847 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100848 while (l <= lr->end) {
849 ret = show_one_line_or_eof(fp, l++ - lr->offset);
850 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100851 break;
852 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400853end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300854 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300856}
857
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000858int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000859{
860 int ret;
861
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000862 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000863 if (ret < 0)
864 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900865 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000866 exit_symbol_maps();
867
868 return ret;
869}
870
Masami Hiramatsuff741782011-06-27 16:27:39 +0900871static int show_available_vars_at(struct debuginfo *dinfo,
872 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900873 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900874{
875 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900876 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900877 struct str_node *node;
878 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900879 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900880 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900881
882 buf = synthesize_perf_probe_point(&pev->point);
883 if (!buf)
884 return -EINVAL;
885 pr_debug("Searching variables at %s\n", buf);
886
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900887 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900888 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900889 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900890 if (!ret) {
891 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900892 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900893 /* Release the old probe_point */
894 clear_perf_probe_point(&tmp);
895 }
896 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900897 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000898 if (ret == 0 || ret == -ENOENT) {
899 pr_err("Failed to find the address of %s\n", buf);
900 ret = -ENOENT;
901 } else
902 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900903 goto end;
904 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000905
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900906 /* Some variables are found */
907 fprintf(stdout, "Available variables at %s\n", buf);
908 for (i = 0; i < ret; i++) {
909 vl = &vls[i];
910 /*
911 * A probe point might be converted to
912 * several trace points.
913 */
914 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
915 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300916 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900917 nvars = 0;
918 if (vl->vars) {
919 strlist__for_each(node, vl->vars) {
920 var = strchr(node->s, '\t') + 1;
921 if (strfilter__compare(_filter, var)) {
922 fprintf(stdout, "\t\t%s\n", node->s);
923 nvars++;
924 }
925 }
926 strlist__delete(vl->vars);
927 }
928 if (nvars == 0)
929 fprintf(stdout, "\t\t(No matched variables)\n");
930 }
931 free(vls);
932end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900933 free(buf);
934 return ret;
935}
936
937/* Show available variables on given probe point */
938int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900939 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900940{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900941 int i, ret = 0;
942 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900943
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000944 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900945 if (ret < 0)
946 return ret;
947
Masami Hiramatsu44225522015-05-08 10:03:28 +0900948 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900949 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000950 ret = -ENOENT;
951 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900952 }
953
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900954 setup_pager();
955
Masami Hiramatsuff741782011-06-27 16:27:39 +0900956 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900957 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900958
959 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000960out:
961 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900962 return ret;
963}
964
Ingo Molnar89fe8082013-09-30 12:07:11 +0200965#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300966
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900967static void debuginfo_cache__exit(void)
968{
969}
970
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000971static int
972find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
973 struct perf_probe_point *pp __maybe_unused,
974 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300975{
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000976 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300977}
978
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530979static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900980 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300981{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400982 if (perf_probe_event_need_dwarf(pev)) {
983 pr_warning("Debuginfo-analysis is not supported.\n");
984 return -ENOSYS;
985 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530986
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300987 return 0;
988}
989
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300990int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000991 const char *module __maybe_unused,
992 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300993{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400994 pr_warning("Debuginfo-analysis is not supported.\n");
995 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300996}
997
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300998int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900999 int npevs __maybe_unused,
1000 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001001{
1002 pr_warning("Debuginfo-analysis is not supported.\n");
1003 return -ENOSYS;
1004}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001005#endif
1006
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001007void line_range__clear(struct line_range *lr)
1008{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001009 free(lr->function);
1010 free(lr->file);
1011 free(lr->path);
1012 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001013 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001014 memset(lr, 0, sizeof(*lr));
1015}
1016
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001017int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001018{
1019 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001020 lr->line_list = intlist__new(NULL);
1021 if (!lr->line_list)
1022 return -ENOMEM;
1023 else
1024 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001025}
1026
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001027static int parse_line_num(char **ptr, int *val, const char *what)
1028{
1029 const char *start = *ptr;
1030
1031 errno = 0;
1032 *val = strtol(*ptr, ptr, 0);
1033 if (errno || *ptr == start) {
1034 semantic_error("'%s' is not a valid number.\n", what);
1035 return -EINVAL;
1036 }
1037 return 0;
1038}
1039
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001040/* Check the name is good for event, group or function */
1041static bool is_c_func_name(const char *name)
1042{
1043 if (!isalpha(*name) && *name != '_')
1044 return false;
1045 while (*++name != '\0') {
1046 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1047 return false;
1048 }
1049 return true;
1050}
1051
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001052/*
1053 * Stuff 'lr' according to the line range described by 'arg'.
1054 * The line range syntax is described by:
1055 *
1056 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001057 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001058 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001059int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001060{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001061 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001062 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001063
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001064 if (!name)
1065 return -ENOMEM;
1066
1067 lr->start = 0;
1068 lr->end = INT_MAX;
1069
1070 range = strchr(name, ':');
1071 if (range) {
1072 *range++ = '\0';
1073
1074 err = parse_line_num(&range, &lr->start, "start line");
1075 if (err)
1076 goto err;
1077
1078 if (*range == '+' || *range == '-') {
1079 const char c = *range++;
1080
1081 err = parse_line_num(&range, &lr->end, "end line");
1082 if (err)
1083 goto err;
1084
1085 if (c == '+') {
1086 lr->end += lr->start;
1087 /*
1088 * Adjust the number of lines here.
1089 * If the number of lines == 1, the
1090 * the end of line should be equal to
1091 * the start of line.
1092 */
1093 lr->end--;
1094 }
1095 }
1096
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001097 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001098
1099 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001100 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001101 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001102 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001103 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001104 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001105 if (*range != '\0') {
1106 semantic_error("Tailing with invalid str '%s'.\n", range);
1107 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001109 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001110
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001111 file = strchr(name, '@');
1112 if (file) {
1113 *file = '\0';
1114 lr->file = strdup(++file);
1115 if (lr->file == NULL) {
1116 err = -ENOMEM;
1117 goto err;
1118 }
1119 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001120 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001121 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001122 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001123 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001124 else { /* Invalid name */
1125 semantic_error("'%s' is not a valid function name.\n", name);
1126 err = -EINVAL;
1127 goto err;
1128 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001129
1130 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001131err:
1132 free(name);
1133 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001134}
1135
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001136/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001137static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001138{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001139 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001140 char *ptr, *tmp;
1141 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301142 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001143 /*
1144 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001145 * perf probe [EVENT=]SRC[:LN|;PTN]
1146 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001147 *
1148 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001149 */
Wang Nane59d29e2015-04-28 08:46:09 +00001150 if (!arg)
1151 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001152
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001153 ptr = strpbrk(arg, ";=@+%");
1154 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001155 *ptr = '\0';
1156 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001157 if (strchr(arg, ':')) {
1158 semantic_error("Group name is not supported yet.\n");
1159 return -ENOTSUP;
1160 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001161 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001162 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001163 "follow C symbol-naming rule.\n", arg);
1164 return -EINVAL;
1165 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001166 pev->event = strdup(arg);
1167 if (pev->event == NULL)
1168 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001170 arg = tmp;
1171 }
1172
Naveen N. Rao3099c022015-04-28 17:35:34 +05301173 /*
1174 * Check arg is function or file name and copy it.
1175 *
1176 * We consider arg to be a file spec if and only if it satisfies
1177 * all of the below criteria::
1178 * - it does not include any of "+@%",
1179 * - it includes one of ":;", and
1180 * - it has a period '.' in the name.
1181 *
1182 * Otherwise, we consider arg to be a function specification.
1183 */
1184 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1185 /* This is a file spec if it includes a '.' before ; or : */
1186 if (memchr(arg, '.', ptr - arg))
1187 file_spec = true;
1188 }
1189
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001190 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001191 if (ptr) {
1192 nc = *ptr;
1193 *ptr++ = '\0';
1194 }
1195
Wang Nan6c6e0242015-08-26 10:57:44 +00001196 if (arg[0] == '\0')
1197 tmp = NULL;
1198 else {
1199 tmp = strdup(arg);
1200 if (tmp == NULL)
1201 return -ENOMEM;
1202 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001203
Naveen N. Rao3099c022015-04-28 17:35:34 +05301204 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001205 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001206 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001207 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001208
Wang Nanda15bd92015-08-26 10:57:45 +00001209 /*
1210 * Keep pp->function even if this is absolute address,
1211 * so it can mark whether abs_address is valid.
1212 * Which make 'perf probe lib.bin 0x0' possible.
1213 *
1214 * Note that checking length of tmp is not needed
1215 * because when we access tmp[1] we know tmp[0] is '0',
1216 * so tmp[1] should always valid (but could be '\0').
1217 */
1218 if (tmp && !strncmp(tmp, "0x", 2)) {
1219 pp->abs_address = strtoul(pp->function, &tmp, 0);
1220 if (*tmp != '\0') {
1221 semantic_error("Invalid absolute address.\n");
1222 return -EINVAL;
1223 }
1224 }
1225 }
1226
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001227 /* Parse other options */
1228 while (ptr) {
1229 arg = ptr;
1230 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001231 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001232 pp->lazy_line = strdup(arg);
1233 if (pp->lazy_line == NULL)
1234 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001235 break;
1236 }
1237 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001238 if (ptr) {
1239 nc = *ptr;
1240 *ptr++ = '\0';
1241 }
1242 switch (c) {
1243 case ':': /* Line number */
1244 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001245 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001246 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 " in line number.\n");
1248 return -EINVAL;
1249 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001250 break;
1251 case '+': /* Byte offset from a symbol */
1252 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001254 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 " in offset.\n");
1256 return -EINVAL;
1257 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 break;
1259 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260 if (pp->file) {
1261 semantic_error("SRC@SRC is not allowed.\n");
1262 return -EINVAL;
1263 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001264 pp->file = strdup(arg);
1265 if (pp->file == NULL)
1266 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001267 break;
1268 case '%': /* Probe places */
1269 if (strcmp(arg, "return") == 0) {
1270 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001271 } else { /* Others not supported yet */
1272 semantic_error("%%%s is not supported.\n", arg);
1273 return -ENOTSUP;
1274 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001275 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001276 default: /* Buggy case */
1277 pr_err("This program has a bug at %s:%d.\n",
1278 __FILE__, __LINE__);
1279 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001280 break;
1281 }
1282 }
1283
1284 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001285 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001286 semantic_error("Lazy pattern can't be used with"
1287 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001288 return -EINVAL;
1289 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001290
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001292 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001293 return -EINVAL;
1294 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001295
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001297 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001298 return -EINVAL;
1299 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001300
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001301 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001302 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001303 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001304 return -EINVAL;
1305 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001306
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001307 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001308 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001309 return -EINVAL;
1310 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001311
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001313 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001314 return -EINVAL;
1315 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001316
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001318 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001319 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001320 return -EINVAL;
1321 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001322
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001323 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001324 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1325 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001326 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001327}
1328
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001329/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001330static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001331{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001332 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001333 struct perf_probe_arg_field **fieldp;
1334
1335 pr_debug("parsing arg: %s into ", str);
1336
Masami Hiramatsu48481932010-04-12 13:16:53 -04001337 tmp = strchr(str, '=');
1338 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001339 arg->name = strndup(str, tmp - str);
1340 if (arg->name == NULL)
1341 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001342 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001343 str = tmp + 1;
1344 }
1345
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001346 tmp = strchr(str, ':');
1347 if (tmp) { /* Type setting */
1348 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001349 arg->type = strdup(tmp + 1);
1350 if (arg->type == NULL)
1351 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001352 pr_debug("type:%s ", arg->type);
1353 }
1354
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001355 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001356 if (!is_c_varname(str) || !tmp) {
1357 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001358 arg->var = strdup(str);
1359 if (arg->var == NULL)
1360 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001361 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001362 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001363 }
1364
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001365 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 arg->var = strndup(str, tmp - str);
1367 if (arg->var == NULL)
1368 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001369 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001370 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001371 fieldp = &arg->field;
1372
1373 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001374 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1375 if (*fieldp == NULL)
1376 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001377 if (*tmp == '[') { /* Array */
1378 str = tmp;
1379 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001380 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001381 if (*tmp != ']' || tmp == str + 1) {
1382 semantic_error("Array index must be a"
1383 " number.\n");
1384 return -EINVAL;
1385 }
1386 tmp++;
1387 if (*tmp == '\0')
1388 tmp = NULL;
1389 } else { /* Structure */
1390 if (*tmp == '.') {
1391 str = tmp + 1;
1392 (*fieldp)->ref = false;
1393 } else if (tmp[1] == '>') {
1394 str = tmp + 2;
1395 (*fieldp)->ref = true;
1396 } else {
1397 semantic_error("Argument parse error: %s\n",
1398 str);
1399 return -EINVAL;
1400 }
1401 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001402 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001403 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001404 (*fieldp)->name = strndup(str, tmp - str);
1405 if ((*fieldp)->name == NULL)
1406 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001407 if (*str != '[')
1408 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001409 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1410 fieldp = &(*fieldp)->next;
1411 }
1412 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001413 (*fieldp)->name = strdup(str);
1414 if ((*fieldp)->name == NULL)
1415 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001416 if (*str != '[')
1417 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001418 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001419
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001420 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001421 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001422 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001423 if (arg->name == NULL)
1424 return -ENOMEM;
1425 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001426 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001427}
1428
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001430int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001431{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001432 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001434
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001435 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001436 if (!argv) {
1437 pr_debug("Failed to split arguments.\n");
1438 return -ENOMEM;
1439 }
1440 if (argc - 1 > MAX_PROBE_ARGS) {
1441 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1442 ret = -ERANGE;
1443 goto out;
1444 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001445 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001446 ret = parse_perf_probe_point(argv[0], pev);
1447 if (ret < 0)
1448 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001449
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001450 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001451 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001452 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1453 if (pev->args == NULL) {
1454 ret = -ENOMEM;
1455 goto out;
1456 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001457 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1458 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1459 if (ret >= 0 &&
1460 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001461 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001462 " kretprobe.\n");
1463 ret = -EINVAL;
1464 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001465 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001467 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468
1469 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001470}
1471
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472/* Return true if this perf_probe_event requires debuginfo */
1473bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475 int i;
1476
1477 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1478 return true;
1479
1480 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001481 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001482 return true;
1483
1484 return false;
1485}
1486
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301487/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001488int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001489{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301490 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001491 char pr;
1492 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001493 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001494 int ret, i, argc;
1495 char **argv;
1496
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301497 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001498 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 if (!argv) {
1500 pr_debug("Failed to split arguments.\n");
1501 return -ENOMEM;
1502 }
1503 if (argc < 2) {
1504 semantic_error("Too few probe arguments.\n");
1505 ret = -ERANGE;
1506 goto out;
1507 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001508
1509 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001510 argv0_str = strdup(argv[0]);
1511 if (argv0_str == NULL) {
1512 ret = -ENOMEM;
1513 goto out;
1514 }
1515 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1516 fmt2_str = strtok_r(NULL, "/", &fmt);
1517 fmt3_str = strtok_r(NULL, " \t", &fmt);
1518 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1519 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001520 semantic_error("Failed to parse event name: %s\n", argv[0]);
1521 ret = -EINVAL;
1522 goto out;
1523 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001524 pr = fmt1_str[0];
1525 tev->group = strdup(fmt2_str);
1526 tev->event = strdup(fmt3_str);
1527 if (tev->group == NULL || tev->event == NULL) {
1528 ret = -ENOMEM;
1529 goto out;
1530 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001531 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001532
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001533 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001534
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001535 /* Scan module name(if there), function name and offset */
1536 p = strchr(argv[1], ':');
1537 if (p) {
1538 tp->module = strndup(argv[1], p - argv[1]);
1539 p++;
1540 } else
1541 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001542 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001543 /* only the address started with 0x */
1544 if (fmt1_str[0] == '0') {
1545 /*
1546 * Fix a special case:
1547 * if address == 0, kernel reports something like:
1548 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1549 * Newer kernel may fix that, but we want to
1550 * support old kernel also.
1551 */
1552 if (strcmp(fmt1_str, "0x") == 0) {
1553 if (!argv[2] || strcmp(argv[2], "(null)")) {
1554 ret = -EINVAL;
1555 goto out;
1556 }
1557 tp->address = 0;
1558
1559 free(argv[2]);
1560 for (i = 2; argv[i + 1] != NULL; i++)
1561 argv[i] = argv[i + 1];
1562
1563 argv[i] = NULL;
1564 argc -= 1;
1565 } else
1566 tp->address = strtoul(fmt1_str, NULL, 0);
1567 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001568 /* Only the symbol-based probe has offset */
1569 tp->symbol = strdup(fmt1_str);
1570 if (tp->symbol == NULL) {
1571 ret = -ENOMEM;
1572 goto out;
1573 }
1574 fmt2_str = strtok_r(NULL, "", &fmt);
1575 if (fmt2_str == NULL)
1576 tp->offset = 0;
1577 else
1578 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001579 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001580
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001581 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301582 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001583 if (tev->args == NULL) {
1584 ret = -ENOMEM;
1585 goto out;
1586 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001587 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001588 p = strchr(argv[i + 2], '=');
1589 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001590 *p++ = '\0';
1591 else
1592 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001593 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001594 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001595 tev->args[i].value = strdup(p);
1596 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1597 ret = -ENOMEM;
1598 goto out;
1599 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001600 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 ret = 0;
1602out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001603 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001604 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001606}
1607
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001608/* Compose only probe arg */
1609int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1610{
1611 struct perf_probe_arg_field *field = pa->field;
1612 int ret;
1613 char *tmp = buf;
1614
Masami Hiramatsu48481932010-04-12 13:16:53 -04001615 if (pa->name && pa->var)
1616 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1617 else
1618 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001619 if (ret <= 0)
1620 goto error;
1621 tmp += ret;
1622 len -= ret;
1623
1624 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001625 if (field->name[0] == '[')
1626 ret = e_snprintf(tmp, len, "%s", field->name);
1627 else
1628 ret = e_snprintf(tmp, len, "%s%s",
1629 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001630 if (ret <= 0)
1631 goto error;
1632 tmp += ret;
1633 len -= ret;
1634 field = field->next;
1635 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001636
1637 if (pa->type) {
1638 ret = e_snprintf(tmp, len, ":%s", pa->type);
1639 if (ret <= 0)
1640 goto error;
1641 tmp += ret;
1642 len -= ret;
1643 }
1644
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001645 return tmp - buf;
1646error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001647 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001648 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001649}
1650
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651/* Compose only probe point (not argument) */
1652static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001653{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001654 char *buf, *tmp;
1655 char offs[32] = "", line[32] = "", file[32] = "";
1656 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001657
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001658 buf = zalloc(MAX_CMDLEN);
1659 if (buf == NULL) {
1660 ret = -ENOMEM;
1661 goto error;
1662 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001663 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001664 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001665 if (ret <= 0)
1666 goto error;
1667 }
1668 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001669 ret = e_snprintf(line, 32, ":%d", pp->line);
1670 if (ret <= 0)
1671 goto error;
1672 }
1673 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001674 tmp = pp->file;
1675 len = strlen(tmp);
1676 if (len > 30) {
1677 tmp = strchr(pp->file + len - 30, '/');
1678 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1679 }
1680 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001681 if (ret <= 0)
1682 goto error;
1683 }
1684
1685 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001686 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1687 offs, pp->retprobe ? "%return" : "", line,
1688 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001689 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001690 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001691 if (ret <= 0)
1692 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001693
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694 return buf;
1695error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001696 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001697 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001698 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001699}
1700
1701#if 0
1702char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1703{
1704 char *buf;
1705 int i, len, ret;
1706
1707 buf = synthesize_perf_probe_point(&pev->point);
1708 if (!buf)
1709 return NULL;
1710
1711 len = strlen(buf);
1712 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001713 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714 pev->args[i].name);
1715 if (ret <= 0) {
1716 free(buf);
1717 return NULL;
1718 }
1719 len += ret;
1720 }
1721
1722 return buf;
1723}
1724#endif
1725
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301726static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727 char **buf, size_t *buflen,
1728 int depth)
1729{
1730 int ret;
1731 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301732 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 buflen, depth + 1);
1734 if (depth < 0)
1735 goto out;
1736 }
1737
1738 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1739 if (ret < 0)
1740 depth = ret;
1741 else {
1742 *buf += ret;
1743 *buflen -= ret;
1744 }
1745out:
1746 return depth;
1747
1748}
1749
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301750static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001751 char *buf, size_t buflen)
1752{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301753 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001754 int ret, depth = 0;
1755 char *tmp = buf;
1756
1757 /* Argument name or separator */
1758 if (arg->name)
1759 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1760 else
1761 ret = e_snprintf(buf, buflen, " ");
1762 if (ret < 0)
1763 return ret;
1764 buf += ret;
1765 buflen -= ret;
1766
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001767 /* Special case: @XXX */
1768 if (arg->value[0] == '@' && arg->ref)
1769 ref = ref->next;
1770
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001772 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301773 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774 &buflen, 1);
1775 if (depth < 0)
1776 return depth;
1777 }
1778
1779 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001780 if (arg->value[0] == '@' && arg->ref)
1781 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1782 arg->ref->offset);
1783 else
1784 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001785 if (ret < 0)
1786 return ret;
1787 buf += ret;
1788 buflen -= ret;
1789
1790 /* Closing */
1791 while (depth--) {
1792 ret = e_snprintf(buf, buflen, ")");
1793 if (ret < 0)
1794 return ret;
1795 buf += ret;
1796 buflen -= ret;
1797 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001798 /* Print argument type */
1799 if (arg->type) {
1800 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1801 if (ret <= 0)
1802 return ret;
1803 buf += ret;
1804 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001805
1806 return buf - tmp;
1807}
1808
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301809char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001810{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301811 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001812 char *buf;
1813 int i, len, ret;
1814
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001815 buf = zalloc(MAX_CMDLEN);
1816 if (buf == NULL)
1817 return NULL;
1818
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001819 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1820 tev->group, tev->event);
1821 if (len <= 0)
1822 goto error;
1823
Wang Nanda15bd92015-08-26 10:57:45 +00001824 /* Uprobes must have tp->module */
1825 if (tev->uprobes && !tp->module)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001826 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001827 /*
1828 * If tp->address == 0, then this point must be a
1829 * absolute address uprobe.
1830 * try_to_find_absolute_address() should have made
1831 * tp->symbol to "0x0".
1832 */
1833 if (tev->uprobes && !tp->address) {
1834 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1835 goto error;
1836 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001837
1838 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301839 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001840 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1841 tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001842 else if (!strncmp(tp->symbol, "0x", 2))
1843 /* Absolute address. See try_to_find_absolute_address() */
1844 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1845 tp->module ?: "", tp->module ? ":" : "",
1846 tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301847 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001848 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301849 tp->module ?: "", tp->module ? ":" : "",
1850 tp->symbol, tp->offset);
1851
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001852 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001853 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001854 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855
1856 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301857 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001858 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001859 if (ret <= 0)
1860 goto error;
1861 len += ret;
1862 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001863
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001864 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001865error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866 free(buf);
1867 return NULL;
1868}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001869
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001870static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1871 struct perf_probe_point *pp,
1872 bool is_kprobe)
1873{
1874 struct symbol *sym = NULL;
1875 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001876 u64 addr = tp->address;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001877 int ret = -ENOENT;
1878
1879 if (!is_kprobe) {
1880 map = dso__new_map(tp->module);
1881 if (!map)
1882 goto out;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001883 sym = map__find_symbol(map, addr, NULL);
1884 } else {
Wang Nane4863672015-08-25 13:27:35 +00001885 if (tp->symbol)
1886 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001887 if (addr) {
1888 addr += tp->offset;
1889 sym = __find_kernel_function(addr, &map);
1890 }
1891 }
1892 if (!sym)
1893 goto out;
1894
1895 pp->retprobe = tp->retprobe;
1896 pp->offset = addr - map->unmap_ip(map, sym->start);
1897 pp->function = strdup(sym->name);
1898 ret = pp->function ? 0 : -ENOMEM;
1899
1900out:
1901 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001902 map__put(map);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001903 }
1904
1905 return ret;
1906}
1907
1908static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001909 struct perf_probe_point *pp,
1910 bool is_kprobe)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001911{
1912 char buf[128];
1913 int ret;
1914
1915 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1916 if (!ret)
1917 return 0;
1918 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1919 if (!ret)
1920 return 0;
1921
1922 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1923
1924 if (tp->symbol) {
1925 pp->function = strdup(tp->symbol);
1926 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001927 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001928 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1929 if (ret < 0)
1930 return ret;
1931 pp->function = strdup(buf);
1932 pp->offset = 0;
1933 }
1934 if (pp->function == NULL)
1935 return -ENOMEM;
1936
1937 pp->retprobe = tp->retprobe;
1938
1939 return 0;
1940}
1941
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301942static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301943 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001944{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001945 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001946 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001947
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001948 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001949 pev->event = strdup(tev->event);
1950 pev->group = strdup(tev->group);
1951 if (pev->event == NULL || pev->group == NULL)
1952 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001953
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001954 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001955 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001956 if (ret < 0)
1957 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001958
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001959 /* Convert trace_arg to probe_arg */
1960 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001961 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1962 if (pev->args == NULL)
1963 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001964 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001965 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001966 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001967 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301968 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001969 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001970 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001972 if (pev->args[i].name == NULL && ret >= 0)
1973 ret = -ENOMEM;
1974 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001975
1976 if (ret < 0)
1977 clear_perf_probe_event(pev);
1978
1979 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001980}
1981
1982void clear_perf_probe_event(struct perf_probe_event *pev)
1983{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001984 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001985 int i;
1986
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001987 free(pev->event);
1988 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001989 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001990 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001991
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001992 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001993 free(pev->args[i].name);
1994 free(pev->args[i].var);
1995 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001996 field = pev->args[i].field;
1997 while (field) {
1998 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001999 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002000 free(field);
2001 field = next;
2002 }
2003 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002004 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002005 memset(pev, 0, sizeof(*pev));
2006}
2007
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002008void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002009{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302010 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002011 int i;
2012
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002013 free(tev->event);
2014 free(tev->group);
2015 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002016 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002017 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002018 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002019 free(tev->args[i].name);
2020 free(tev->args[i].value);
2021 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002022 ref = tev->args[i].ref;
2023 while (ref) {
2024 next = ref->next;
2025 free(ref);
2026 ref = next;
2027 }
2028 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002029 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002030 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002031}
2032
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002033struct kprobe_blacklist_node {
2034 struct list_head list;
2035 unsigned long start;
2036 unsigned long end;
2037 char *symbol;
2038};
2039
2040static void kprobe_blacklist__delete(struct list_head *blacklist)
2041{
2042 struct kprobe_blacklist_node *node;
2043
2044 while (!list_empty(blacklist)) {
2045 node = list_first_entry(blacklist,
2046 struct kprobe_blacklist_node, list);
2047 list_del(&node->list);
2048 free(node->symbol);
2049 free(node);
2050 }
2051}
2052
2053static int kprobe_blacklist__load(struct list_head *blacklist)
2054{
2055 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002056 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002057 char buf[PATH_MAX], *p;
2058 FILE *fp;
2059 int ret;
2060
2061 if (__debugfs == NULL)
2062 return -ENOTSUP;
2063
2064 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2065 if (ret < 0)
2066 return ret;
2067
2068 fp = fopen(buf, "r");
2069 if (!fp)
2070 return -errno;
2071
2072 ret = 0;
2073 while (fgets(buf, PATH_MAX, fp)) {
2074 node = zalloc(sizeof(*node));
2075 if (!node) {
2076 ret = -ENOMEM;
2077 break;
2078 }
2079 INIT_LIST_HEAD(&node->list);
2080 list_add_tail(&node->list, blacklist);
2081 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2082 ret = -EINVAL;
2083 break;
2084 }
2085 p = strchr(buf, '\t');
2086 if (p) {
2087 p++;
2088 if (p[strlen(p) - 1] == '\n')
2089 p[strlen(p) - 1] = '\0';
2090 } else
2091 p = (char *)"unknown";
2092 node->symbol = strdup(p);
2093 if (!node->symbol) {
2094 ret = -ENOMEM;
2095 break;
2096 }
2097 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2098 node->start, node->end, node->symbol);
2099 ret++;
2100 }
2101 if (ret < 0)
2102 kprobe_blacklist__delete(blacklist);
2103 fclose(fp);
2104
2105 return ret;
2106}
2107
2108static struct kprobe_blacklist_node *
2109kprobe_blacklist__find_by_address(struct list_head *blacklist,
2110 unsigned long address)
2111{
2112 struct kprobe_blacklist_node *node;
2113
2114 list_for_each_entry(node, blacklist, list) {
2115 if (node->start <= address && address <= node->end)
2116 return node;
2117 }
2118
2119 return NULL;
2120}
2121
Masami Hiramatsub0312202015-06-16 20:50:55 +09002122static LIST_HEAD(kprobe_blacklist);
2123
2124static void kprobe_blacklist__init(void)
2125{
2126 if (!list_empty(&kprobe_blacklist))
2127 return;
2128
2129 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2130 pr_debug("No kprobe blacklist support, ignored\n");
2131}
2132
2133static void kprobe_blacklist__release(void)
2134{
2135 kprobe_blacklist__delete(&kprobe_blacklist);
2136}
2137
2138static bool kprobe_blacklist__listed(unsigned long address)
2139{
2140 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2141}
2142
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002143static int perf_probe_event__sprintf(const char *group, const char *event,
2144 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002145 const char *module,
2146 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002147{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002148 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002149 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002151
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002152 /* Synthesize only event probe point */
2153 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002154 if (!place)
2155 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002156
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002157 ret = e_snprintf(buf, 128, "%s:%s", group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002158 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002159 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002160
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002161 strbuf_addf(result, " %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002162 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002163 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002164
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002165 if (pev->nargs > 0) {
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002166 strbuf_addstr(result, " with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002167 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002168 ret = synthesize_perf_probe_arg(&pev->args[i],
2169 buf, 128);
2170 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002171 goto out;
2172 strbuf_addf(result, " %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002173 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002174 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002175 strbuf_addch(result, ')');
2176out:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002177 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002178 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002179}
2180
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002181/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002182int show_perf_probe_event(const char *group, const char *event,
2183 struct perf_probe_event *pev,
2184 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002185{
2186 struct strbuf buf = STRBUF_INIT;
2187 int ret;
2188
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002189 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002190 if (ret >= 0) {
2191 if (use_stdout)
2192 printf("%s\n", buf.buf);
2193 else
2194 pr_info("%s\n", buf.buf);
2195 }
2196 strbuf_release(&buf);
2197
2198 return ret;
2199}
2200
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002201static bool filter_probe_trace_event(struct probe_trace_event *tev,
2202 struct strfilter *filter)
2203{
2204 char tmp[128];
2205
2206 /* At first, check the event name itself */
2207 if (strfilter__compare(filter, tev->event))
2208 return true;
2209
2210 /* Next, check the combination of name and group */
2211 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2212 return false;
2213 return strfilter__compare(filter, tmp);
2214}
2215
2216static int __show_perf_probe_events(int fd, bool is_kprobe,
2217 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002218{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302220 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002221 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002222 struct strlist *rawlist;
2223 struct str_node *ent;
2224
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002225 memset(&tev, 0, sizeof(tev));
2226 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002227
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002228 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002229 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002230 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002231
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) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002235 if (!filter_probe_trace_event(&tev, filter))
2236 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302237 ret = convert_to_perf_probe_event(&tev, &pev,
2238 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002239 if (ret < 0)
2240 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002241 ret = show_perf_probe_event(pev.group, pev.event,
2242 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002243 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002244 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002245next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002246 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302247 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002248 if (ret < 0)
2249 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002250 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002251 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002252 /* Cleanup cached debuginfo if needed */
2253 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002254
2255 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002256}
2257
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302258/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002259int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302260{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002261 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302262
2263 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302264
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002265 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302266 if (ret < 0)
2267 return ret;
2268
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002269 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2270 if (ret < 0)
2271 return ret;
2272
2273 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002274 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002275 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002276 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002277 if (kp_fd > 0)
2278 close(kp_fd);
2279 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002280 close(up_fd);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002281 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302282
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002283 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002284}
2285
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002286static int get_new_event_name(char *buf, size_t len, const char *base,
2287 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002288{
2289 int i, ret;
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002290 char *p;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002291
Naveen N. Rao3099c022015-04-28 17:35:34 +05302292 if (*base == '.')
2293 base++;
2294
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002295 /* Try no suffix */
2296 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002297 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002298 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002299 return ret;
2300 }
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002301 /* Cut off the postfixes (e.g. .const, .isra)*/
2302 p = strchr(buf, '.');
2303 if (p && p != buf)
2304 *p = '\0';
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002305 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002306 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002307
Masami Hiramatsud761b082009-12-15 10:32:25 -05002308 if (!allow_suffix) {
2309 pr_warning("Error: event \"%s\" already exists. "
2310 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002311 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002312 }
2313
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002314 /* Try to add suffix */
2315 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002316 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002317 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002318 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319 return ret;
2320 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002321 if (!strlist__has_entry(namelist, buf))
2322 break;
2323 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002324 if (i == MAX_EVENT_INDEX) {
2325 pr_warning("Too many events are on the same function.\n");
2326 ret = -ERANGE;
2327 }
2328
2329 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002330}
2331
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002332/* Warn if the current kernel's uprobe implementation is old */
2333static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2334{
2335 int i;
2336 char *buf = synthesize_probe_trace_command(tev);
2337
2338 /* Old uprobe event doesn't support memory dereference */
2339 if (!tev->uprobes || tev->nargs == 0 || !buf)
2340 goto out;
2341
2342 for (i = 0; i < tev->nargs; i++)
2343 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2344 pr_warning("Please upgrade your kernel to at least "
2345 "3.14 to have access to feature %s\n",
2346 tev->args[i].value);
2347 break;
2348 }
2349out:
2350 free(buf);
2351}
2352
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002353/* Set new name from original perf_probe_event and namelist */
2354static int probe_trace_event__set_name(struct probe_trace_event *tev,
2355 struct perf_probe_event *pev,
2356 struct strlist *namelist,
2357 bool allow_suffix)
2358{
2359 const char *event, *group;
2360 char buf[64];
2361 int ret;
2362
2363 if (pev->event)
2364 event = pev->event;
2365 else
Wang Nanda15bd92015-08-26 10:57:45 +00002366 if (pev->point.function &&
2367 (strncmp(pev->point.function, "0x", 2) != 0) &&
2368 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002369 event = pev->point.function;
2370 else
2371 event = tev->point.realname;
2372 if (pev->group)
2373 group = pev->group;
2374 else
2375 group = PERFPROBE_GROUP;
2376
2377 /* Get an unused new event name */
2378 ret = get_new_event_name(buf, 64, event,
2379 namelist, allow_suffix);
2380 if (ret < 0)
2381 return ret;
2382
2383 event = buf;
2384
2385 tev->event = strdup(event);
2386 tev->group = strdup(group);
2387 if (tev->event == NULL || tev->group == NULL)
2388 return -ENOMEM;
2389
2390 /* Add added event name to namelist */
2391 strlist__add(namelist, event);
2392 return 0;
2393}
2394
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302395static int __add_probe_trace_events(struct perf_probe_event *pev,
2396 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002398{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002399 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302400 struct probe_trace_event *tev = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002401 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002402
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002403 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2404 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002405 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002406
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002407 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002408 namelist = probe_file__get_namelist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002409 if (!namelist) {
2410 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002411 ret = -ENOMEM;
2412 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002413 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002414
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002415 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002416 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002417 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002418 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002419 if (!tev->point.symbol)
2420 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002421
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002422 /* Set new name for tev (and update namelist) */
2423 ret = probe_trace_event__set_name(tev, pev, namelist,
2424 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002425 if (ret < 0)
2426 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002427
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002428 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002429 if (ret < 0)
2430 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002431
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002432 /*
2433 * Probes after the first probe which comes from same
2434 * user input are always allowed to add suffix, because
2435 * there might be several addresses corresponding to
2436 * one code line.
2437 */
2438 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002439 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002440 if (ret == -EINVAL && pev->uprobes)
2441 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002442
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002443 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002444close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002445 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002446 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002447}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002448
Wang Nan6bb536c2015-05-29 09:45:47 +00002449static int find_probe_functions(struct map *map, char *name,
2450 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002451{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002452 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002453 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002454 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002455
Wang Nan75e4a2a2015-05-15 12:14:44 +00002456 if (map__load(map, NULL) < 0)
2457 return 0;
2458
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002459 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002460 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002461 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002462 if (syms && found < probe_conf.max_probes)
2463 syms[found - 1] = sym;
2464 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002465 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002466
2467 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002468}
2469
2470#define strdup_or_goto(str, label) \
2471 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2472
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302473void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2474 struct probe_trace_event *tev __maybe_unused,
2475 struct map *map __maybe_unused) { }
2476
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002477/*
2478 * Find probe function addresses from map.
2479 * Return an error or the number of found probe_trace_event
2480 */
2481static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002482 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002483{
2484 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002485 struct ref_reloc_sym *reloc_sym = NULL;
2486 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002487 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002488 struct probe_trace_event *tev;
2489 struct perf_probe_point *pp = &pev->point;
2490 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002491 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002492 int ret, i, j, skipped = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002493
Masami Hiramatsu44225522015-05-08 10:03:28 +09002494 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002495 if (!map) {
2496 ret = -EINVAL;
2497 goto out;
2498 }
2499
Wang Nan6bb536c2015-05-29 09:45:47 +00002500 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2501 if (!syms) {
2502 ret = -ENOMEM;
2503 goto out;
2504 }
2505
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002506 /*
2507 * Load matched symbols: Since the different local symbols may have
2508 * same name but different addresses, this lists all the symbols.
2509 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002510 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002511 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002512 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002513 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002514 ret = -ENOENT;
2515 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002516 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002517 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002518 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002519 ret = -E2BIG;
2520 goto out;
2521 }
2522
Namhyung Kim25dd9172015-01-14 20:18:08 +09002523 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002524 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002525 if (!reloc_sym) {
2526 pr_warning("Relocated base symbol is not found!\n");
2527 ret = -EINVAL;
2528 goto out;
2529 }
2530 }
2531
2532 /* Setup result trace-probe-events */
2533 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2534 if (!*tevs) {
2535 ret = -ENOMEM;
2536 goto out;
2537 }
2538
2539 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002540
Wang Nan6bb536c2015-05-29 09:45:47 +00002541 for (j = 0; j < num_matched_functions; j++) {
2542 sym = syms[j];
2543
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002544 tev = (*tevs) + ret;
2545 tp = &tev->point;
2546 if (ret == num_matched_functions) {
2547 pr_warning("Too many symbols are listed. Skip it.\n");
2548 break;
2549 }
2550 ret++;
2551
2552 if (pp->offset > sym->end - sym->start) {
2553 pr_warning("Offset %ld is bigger than the size of %s\n",
2554 pp->offset, sym->name);
2555 ret = -ENOENT;
2556 goto err_out;
2557 }
2558 /* Add one probe point */
2559 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002560 /* If we found a wrong one, mark it by NULL symbol */
2561 if (!pev->uprobes &&
2562 kprobe_warn_out_range(sym->name, tp->address)) {
2563 tp->symbol = NULL; /* Skip it */
2564 skipped++;
2565 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002566 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2567 tp->offset = tp->address - reloc_sym->addr;
2568 } else {
2569 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2570 tp->offset = pp->offset;
2571 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002572 tp->realname = strdup_or_goto(sym->name, nomem_out);
2573
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002574 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002575 if (pev->target)
2576 tev->point.module = strdup_or_goto(pev->target,
2577 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002578 tev->uprobes = pev->uprobes;
2579 tev->nargs = pev->nargs;
2580 if (tev->nargs) {
2581 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2582 tev->nargs);
2583 if (tev->args == NULL)
2584 goto nomem_out;
2585 }
2586 for (i = 0; i < tev->nargs; i++) {
2587 if (pev->args[i].name)
2588 tev->args[i].name =
2589 strdup_or_goto(pev->args[i].name,
2590 nomem_out);
2591
2592 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2593 nomem_out);
2594 if (pev->args[i].type)
2595 tev->args[i].type =
2596 strdup_or_goto(pev->args[i].type,
2597 nomem_out);
2598 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302599 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002600 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002601 if (ret == skipped) {
2602 ret = -ENOENT;
2603 goto err_out;
2604 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002605
2606out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002607 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002608 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002609 return ret;
2610
2611nomem_out:
2612 ret = -ENOMEM;
2613err_out:
2614 clear_probe_trace_events(*tevs, num_matched_functions);
2615 zfree(tevs);
2616 goto out;
2617}
2618
Wang Nanda15bd92015-08-26 10:57:45 +00002619static int try_to_find_absolute_address(struct perf_probe_event *pev,
2620 struct probe_trace_event **tevs)
2621{
2622 struct perf_probe_point *pp = &pev->point;
2623 struct probe_trace_event *tev;
2624 struct probe_trace_point *tp;
2625 int i, err;
2626
2627 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2628 return -EINVAL;
2629 if (perf_probe_event_need_dwarf(pev))
2630 return -EINVAL;
2631
2632 /*
2633 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2634 * absolute address.
2635 *
2636 * Only one tev can be generated by this.
2637 */
2638 *tevs = zalloc(sizeof(*tev));
2639 if (!*tevs)
2640 return -ENOMEM;
2641
2642 tev = *tevs;
2643 tp = &tev->point;
2644
2645 /*
2646 * Don't use tp->offset, use address directly, because
2647 * in synthesize_probe_trace_command() address cannot be
2648 * zero.
2649 */
2650 tp->address = pev->point.abs_address;
2651 tp->retprobe = pp->retprobe;
2652 tev->uprobes = pev->uprobes;
2653
2654 err = -ENOMEM;
2655 /*
2656 * Give it a '0x' leading symbol name.
2657 * In __add_probe_trace_events, a NULL symbol is interpreted as
2658 * invalud.
2659 */
2660 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2661 goto errout;
2662
2663 /* For kprobe, check range */
2664 if ((!tev->uprobes) &&
2665 (kprobe_warn_out_range(tev->point.symbol,
2666 tev->point.address))) {
2667 err = -EACCES;
2668 goto errout;
2669 }
2670
2671 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2672 goto errout;
2673
2674 if (pev->target) {
2675 tp->module = strdup(pev->target);
2676 if (!tp->module)
2677 goto errout;
2678 }
2679
2680 if (tev->group) {
2681 tev->group = strdup(pev->group);
2682 if (!tev->group)
2683 goto errout;
2684 }
2685
2686 if (pev->event) {
2687 tev->event = strdup(pev->event);
2688 if (!tev->event)
2689 goto errout;
2690 }
2691
2692 tev->nargs = pev->nargs;
2693 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2694 if (!tev->args) {
2695 err = -ENOMEM;
2696 goto errout;
2697 }
2698 for (i = 0; i < tev->nargs; i++)
2699 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2700
2701 return 1;
2702
2703errout:
2704 if (*tevs) {
2705 clear_probe_trace_events(*tevs, 1);
2706 *tevs = NULL;
2707 }
2708 return err;
2709}
2710
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302711bool __weak arch__prefers_symtab(void) { return false; }
2712
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302713static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002714 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002715{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002716 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002717
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002718 if (pev->uprobes && !pev->group) {
2719 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002720 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002721 if (ret != 0) {
2722 pr_warning("Failed to make a group name.\n");
2723 return ret;
2724 }
2725 }
2726
Wang Nanda15bd92015-08-26 10:57:45 +00002727 ret = try_to_find_absolute_address(pev, tevs);
2728 if (ret > 0)
2729 return ret;
2730
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302731 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002732 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302733 if (ret > 0)
2734 return ret; /* Found in symbol table */
2735 }
2736
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002737 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002738 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002739 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002740 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002741
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002742 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002743}
2744
Wang Nan12fae5e2015-09-04 21:16:00 +09002745int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002746{
Namhyung Kim844dffa2015-09-04 21:15:59 +09002747 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002748
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002749 ret = init_symbol_maps(pevs->uprobes);
Wang Nan12fae5e2015-09-04 21:16:00 +09002750 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002751 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002752
2753 /* Loop 1: convert all events */
2754 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09002755 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09002756 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09002757 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002758 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09002759 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002760 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002761 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09002762 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002763 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002764 /* This just release blacklist only if allocated */
2765 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002766
Namhyung Kim844dffa2015-09-04 21:15:59 +09002767 return 0;
2768}
2769
Wang Nan12fae5e2015-09-04 21:16:00 +09002770int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002771{
2772 int i, ret = 0;
2773
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002774 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002775 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002776 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2777 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002778 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002779 if (ret < 0)
2780 break;
2781 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002782 return ret;
2783}
2784
Wang Nan12fae5e2015-09-04 21:16:00 +09002785void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002786{
2787 int i, j;
2788
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002789 /* Loop 3: cleanup and free trace events */
2790 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002791 for (j = 0; j < pevs[i].ntevs; j++)
2792 clear_probe_trace_event(&pevs[i].tevs[j]);
2793 zfree(&pevs[i].tevs);
2794 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09002795 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002796 }
Wang Nan12fae5e2015-09-04 21:16:00 +09002797
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002798 exit_symbol_maps();
Namhyung Kim844dffa2015-09-04 21:15:59 +09002799}
2800
2801int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2802{
2803 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09002804
Wang Nan12fae5e2015-09-04 21:16:00 +09002805 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002806 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09002807 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002808
Wang Nan12fae5e2015-09-04 21:16:00 +09002809 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002810
2811 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002812}
2813
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002814int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002815{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002816 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002817 char *str = strfilter__string(filter);
2818
2819 if (!str)
2820 return -EINVAL;
2821
Masami Hiramatsufa282442009-12-08 17:03:23 -05002822 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002823 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2824 if (ret < 0)
2825 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302826
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002827 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002828 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302829 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002830
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002831 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002832 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002833 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002834 goto error;
2835 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002836 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302837
2838error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002839 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302840 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002841 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302842 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002843out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002844 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002845
2846 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002847}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302848
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002849/* TODO: don't use a global variable for filter ... */
2850static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002851
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002852/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002853 * If a symbol corresponds to a function with global binding and
2854 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002855 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002856static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002857 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002858{
Namhyung Kime578da32015-03-06 16:31:29 +09002859 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002860 return 0;
2861 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002862}
2863
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002864int show_available_funcs(const char *target, struct strfilter *_filter,
2865 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002866{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002867 struct map *map;
2868 int ret;
2869
2870 ret = init_symbol_maps(user);
2871 if (ret < 0)
2872 return ret;
2873
2874 /* Get a symbol map */
2875 if (user)
2876 map = dso__new_map(target);
2877 else
2878 map = kernel_get_module_map(target);
2879 if (!map) {
2880 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002881 return -EINVAL;
2882 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002883
2884 /* Load symbols with given filter */
2885 available_func_filter = _filter;
2886 if (map__load(map, filter_available_functions)) {
2887 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2888 goto end;
2889 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002890 if (!dso__sorted_by_name(map->dso, map->type))
2891 dso__sort_by_name(map->dso, map->type);
2892
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002893 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302894 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002895 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2896end:
2897 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002898 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002899 }
2900 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302901
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002902 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302903}
2904
Wang Nanda15bd92015-08-26 10:57:45 +00002905int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2906 struct perf_probe_arg *pvar)
2907{
2908 tvar->value = strdup(pvar->var);
2909 if (tvar->value == NULL)
2910 return -ENOMEM;
2911 if (pvar->type) {
2912 tvar->type = strdup(pvar->type);
2913 if (tvar->type == NULL)
2914 return -ENOMEM;
2915 }
2916 if (pvar->name) {
2917 tvar->name = strdup(pvar->name);
2918 if (tvar->name == NULL)
2919 return -ENOMEM;
2920 } else
2921 tvar->name = NULL;
2922 return 0;
2923}