blob: eb5f18b754028e863e3b72970842cc33d2fb997d [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090048#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053049#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050
51#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050052#define PERFPROBE_GROUP "probe"
53
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040054bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090055struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040056
Masami Hiramatsu146a1432010-04-12 13:17:42 -040057#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050058
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090059int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050060{
61 int ret;
62 va_list ap;
63 va_start(ap, format);
64 ret = vsnprintf(str, size, format, ap);
65 va_end(ap);
66 if (ret >= (int)size)
67 ret = -E2BIG;
68 return ret;
69}
70
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030071static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000072static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040073
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090074/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000075static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040077 int ret;
78
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090080 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090081 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040082 if (ret < 0) {
83 pr_debug("Failed to init symbol map.\n");
84 goto out;
85 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040086
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000087 if (host_machine || user_only) /* already initialized */
88 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030089
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000090 if (symbol_conf.vmlinux_name)
91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92
93 host_machine = machine__new_host();
94 if (!host_machine) {
95 pr_debug("machine__new_host() failed.\n");
96 symbol__exit();
97 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090098 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040099out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400103}
104
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000105static void exit_symbol_maps(void)
106{
107 if (host_machine) {
108 machine__delete(host_machine);
109 host_machine = NULL;
110 }
111 symbol__exit();
112}
113
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900114static struct symbol *__find_kernel_function_by_name(const char *name,
115 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400116{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000117 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900118 NULL);
119}
120
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000121static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
122{
123 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
124}
125
126static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
127{
128 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
129 struct kmap *kmap;
130
131 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
132 return NULL;
133
134 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
Wang Nanba927322015-04-07 08:22:45 +0000135 if (!kmap)
136 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800154 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000161 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300162 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300163 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900164
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300167 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900169 if (!module)
170 module = "kernel";
171
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900181static struct map *get_target_map(const char *target, bool user)
182{
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300194 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900195 }
196}
197
198
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000199static int convert_exec_to_group(const char *exec, char **result)
200{
201 char *ptr1, *ptr2, *exec_copy;
202 char buf[64];
203 int ret;
204
205 exec_copy = strdup(exec);
206 if (!exec_copy)
207 return -ENOMEM;
208
209 ptr1 = basename(exec_copy);
210 if (!ptr1) {
211 ret = -EINVAL;
212 goto out;
213 }
214
215 ptr2 = strpbrk(ptr1, "-._");
216 if (ptr2)
217 *ptr2 = '\0';
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219 if (ret < 0)
220 goto out;
221
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
224
225out:
226 free(exec_copy);
227 return ret;
228}
229
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900230static void clear_perf_probe_point(struct perf_probe_point *pp)
231{
232 free(pp->file);
233 free(pp->function);
234 free(pp->lazy_line);
235}
236
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000237static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238{
239 int i;
240
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
243}
244
Masami Hiramatsub0312202015-06-16 20:50:55 +0900245static bool kprobe_blacklist__listed(unsigned long address);
246static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247{
He Kuang7c31bb82015-06-18 02:49:10 +0000248 u64 etext_addr;
249
Masami Hiramatsub0312202015-06-16 20:50:55 +0900250 /* Get the address of _etext for checking non-probable text symbol */
He Kuang7c31bb82015-06-18 02:49:10 +0000251 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
252
253 if (etext_addr != 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900254 pr_warning("%s is out of .text, skip it.\n", symbol);
255 else if (kprobe_blacklist__listed(address))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol);
257 else
258 return false;
259
260 return true;
261}
262
Ingo Molnar89fe8082013-09-30 12:07:11 +0200263#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000264
265static int kernel_get_module_dso(const char *module, struct dso **pdso)
266{
267 struct dso *dso;
268 struct map *map;
269 const char *vmlinux_name;
270 int ret = 0;
271
272 if (module) {
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300273 list_for_each_entry(dso, &host_machine->dsos.head, node) {
274 if (!dso->kernel)
275 continue;
Wang Nan60fb7742015-05-28 02:25:05 +0000276 if (strncmp(dso->short_name + 1, module,
277 dso->short_name_len - 2) == 0)
278 goto found;
279 }
280 pr_debug("Failed to find module %s.\n", module);
281 return -ENOENT;
282 }
283
284 map = host_machine->vmlinux_maps[MAP__FUNCTION];
285 dso = map->dso;
286
287 vmlinux_name = symbol_conf.vmlinux_name;
288 dso->load_errno = 0;
289 if (vmlinux_name)
290 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
291 else
292 ret = dso__load_vmlinux_path(dso, map, NULL);
293found:
294 *pdso = dso;
295 return ret;
296}
297
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900298/*
299 * Some binaries like glibc have special symbols which are on the symbol
300 * table, but not in the debuginfo. If we can find the address of the
301 * symbol from map, we can translate the address back to the probe point.
302 */
303static int find_alternative_probe_point(struct debuginfo *dinfo,
304 struct perf_probe_point *pp,
305 struct perf_probe_point *result,
306 const char *target, bool uprobes)
307{
308 struct map *map = NULL;
309 struct symbol *sym;
310 u64 address = 0;
311 int ret = -ENOENT;
312
313 /* This can work only for function-name based one */
314 if (!pp->function || pp->file)
315 return -ENOTSUP;
316
317 map = get_target_map(target, uprobes);
318 if (!map)
319 return -EINVAL;
320
321 /* Find the address of given function */
322 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900323 if (uprobes)
324 address = sym->start;
325 else
326 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900327 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900328 }
329 if (!address) {
330 ret = -ENOENT;
331 goto out;
332 }
Wang Nanf6c15622015-04-08 02:14:34 +0000333 pr_debug("Symbol %s address found : %" PRIx64 "\n",
334 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900335
336 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
337 result);
338 if (ret <= 0)
339 ret = (!ret) ? -ENOENT : ret;
340 else {
341 result->offset += pp->offset;
342 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800343 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900344 ret = 0;
345 }
346
347out:
348 put_target_map(map, uprobes);
349 return ret;
350
351}
352
353static int get_alternative_probe_event(struct debuginfo *dinfo,
354 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900355 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900356{
357 int ret;
358
359 memcpy(tmp, &pev->point, sizeof(*tmp));
360 memset(&pev->point, 0, sizeof(pev->point));
361 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900362 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900363 if (ret < 0)
364 memcpy(&pev->point, tmp, sizeof(*tmp));
365
366 return ret;
367}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000368
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900369static int get_alternative_line_range(struct debuginfo *dinfo,
370 struct line_range *lr,
371 const char *target, bool user)
372{
David Ahern6d4a4892015-03-11 10:36:20 -0400373 struct perf_probe_point pp = { .function = lr->function,
374 .file = lr->file,
375 .line = lr->start };
376 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900377 int ret, len = 0;
378
David Ahern6d4a4892015-03-11 10:36:20 -0400379 memset(&result, 0, sizeof(result));
380
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900381 if (lr->end != INT_MAX)
382 len = lr->end - lr->start;
383 ret = find_alternative_probe_point(dinfo, &pp, &result,
384 target, user);
385 if (!ret) {
386 lr->function = result.function;
387 lr->file = result.file;
388 lr->start = result.line;
389 if (lr->end != INT_MAX)
390 lr->end = lr->start + len;
391 clear_perf_probe_point(&pp);
392 }
393 return ret;
394}
395
Masami Hiramatsuff741782011-06-27 16:27:39 +0900396/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000397static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900398{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000399 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900400 char reason[STRERR_BUFSIZE];
401 struct debuginfo *ret = NULL;
402 struct dso *dso = NULL;
403 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900404
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000405 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900406 err = kernel_get_module_dso(module, &dso);
407 if (err < 0) {
408 if (!dso || dso->load_errno == 0) {
409 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
410 strcpy(reason, "(unknown)");
411 } else
412 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000413 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900414 pr_err("Failed to find the path for %s: %s\n",
415 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900416 return NULL;
417 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900418 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900419 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000420 ret = debuginfo__new(path);
421 if (!ret && !silent) {
422 pr_warning("The %s file has no debug information.\n", path);
423 if (!module || !strtailcmp(path, ".ko"))
424 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
425 else
426 pr_warning("Rebuild with -g, ");
427 pr_warning("or install an appropriate debuginfo package.\n");
428 }
429 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400430}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300431
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900432/* For caching the last debuginfo */
433static struct debuginfo *debuginfo_cache;
434static char *debuginfo_cache_path;
435
436static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
437{
438 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
439 (!debuginfo_cache_path && !module && debuginfo_cache))
440 goto out;
441
442 /* Copy module path */
443 free(debuginfo_cache_path);
444 if (module) {
445 debuginfo_cache_path = strdup(module);
446 if (!debuginfo_cache_path) {
447 debuginfo__delete(debuginfo_cache);
448 debuginfo_cache = NULL;
449 goto out;
450 }
451 }
452
453 debuginfo_cache = open_debuginfo(module, silent);
454 if (!debuginfo_cache)
455 zfree(&debuginfo_cache_path);
456out:
457 return debuginfo_cache;
458}
459
460static void debuginfo_cache__exit(void)
461{
462 debuginfo__delete(debuginfo_cache);
463 debuginfo_cache = NULL;
464 zfree(&debuginfo_cache_path);
465}
466
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000467
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000468static int get_text_start_address(const char *exec, unsigned long *address)
469{
470 Elf *elf;
471 GElf_Ehdr ehdr;
472 GElf_Shdr shdr;
473 int fd, ret = -ENOENT;
474
475 fd = open(exec, O_RDONLY);
476 if (fd < 0)
477 return -errno;
478
479 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
480 if (elf == NULL)
481 return -EINVAL;
482
483 if (gelf_getehdr(elf, &ehdr) == NULL)
484 goto out;
485
486 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
487 goto out;
488
489 *address = shdr.sh_addr - shdr.sh_offset;
490 ret = 0;
491out:
492 elf_end(elf);
493 return ret;
494}
495
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000496/*
497 * Convert trace point to probe point with debuginfo
498 */
499static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
500 struct perf_probe_point *pp,
501 bool is_kprobe)
502{
503 struct debuginfo *dinfo = NULL;
504 unsigned long stext = 0;
505 u64 addr = tp->address;
506 int ret = -ENOENT;
507
508 /* convert the address to dwarf address */
509 if (!is_kprobe) {
510 if (!addr) {
511 ret = -EINVAL;
512 goto error;
513 }
514 ret = get_text_start_address(tp->module, &stext);
515 if (ret < 0)
516 goto error;
517 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000518 } else if (tp->symbol) {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000519 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
520 if (addr == 0)
521 goto error;
522 addr += tp->offset;
523 }
524
525 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
526 tp->module ? : "kernel");
527
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900528 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
529 if (dinfo)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000530 ret = debuginfo__find_probe_point(dinfo,
531 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900532 else
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000533 ret = -ENOENT;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000534
535 if (ret > 0) {
536 pp->retprobe = tp->retprobe;
537 return 0;
538 }
539error:
540 pr_debug("Failed to find corresponding probes from debuginfo.\n");
541 return ret ? : -ENOENT;
542}
543
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000544static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
545 int ntevs, const char *exec)
546{
547 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000548 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000549
550 if (!exec)
551 return 0;
552
553 ret = get_text_start_address(exec, &stext);
554 if (ret < 0)
555 return ret;
556
557 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000558 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000559 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000560 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000561 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000562 ret = -ENOMEM;
563 break;
564 }
565 tevs[i].uprobes = true;
566 }
567
568 return ret;
569}
570
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900571static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
572 int ntevs, const char *module)
573{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900574 int i, ret = 0;
575 char *tmp;
576
577 if (!module)
578 return 0;
579
580 tmp = strrchr(module, '/');
581 if (tmp) {
582 /* This is a module path -- get the module name */
583 module = strdup(tmp + 1);
584 if (!module)
585 return -ENOMEM;
586 tmp = strchr(module, '.');
587 if (tmp)
588 *tmp = '\0';
589 tmp = (char *)module; /* For free() */
590 }
591
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900592 for (i = 0; i < ntevs; i++) {
593 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900594 if (!tevs[i].point.module) {
595 ret = -ENOMEM;
596 break;
597 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900598 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900599
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300600 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900601 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900602}
603
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000604/* Post processing the probe events */
605static int post_process_probe_trace_events(struct probe_trace_event *tevs,
606 int ntevs, const char *module,
607 bool uprobe)
608{
609 struct ref_reloc_sym *reloc_sym;
610 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900611 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000612
613 if (uprobe)
614 return add_exec_to_probe_trace_events(tevs, ntevs, module);
615
616 /* Note that currently ref_reloc_sym based probe is not for drivers */
617 if (module)
618 return add_module_to_probe_trace_events(tevs, ntevs, module);
619
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000620 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000621 if (!reloc_sym) {
622 pr_warning("Relocated base symbol is not found!\n");
623 return -EINVAL;
624 }
625
626 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900627 if (!tevs[i].point.address || tevs[i].point.retprobe)
628 continue;
629 /* If we found a wrong one, mark it by NULL symbol */
630 if (kprobe_warn_out_range(tevs[i].point.symbol,
631 tevs[i].point.address)) {
632 tmp = NULL;
633 skipped++;
634 } else {
635 tmp = strdup(reloc_sym->name);
636 if (!tmp)
637 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000638 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900639 /* If we have no realname, use symbol for it */
640 if (!tevs[i].point.realname)
641 tevs[i].point.realname = tevs[i].point.symbol;
642 else
643 free(tevs[i].point.symbol);
644 tevs[i].point.symbol = tmp;
645 tevs[i].point.offset = tevs[i].point.address -
646 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000647 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900648 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000649}
650
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300651/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530652static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900653 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300654{
655 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900656 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530657 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900658 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300659
Masami Hiramatsu44225522015-05-08 10:03:28 +0900660 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900661 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000662 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900663 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900664 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300665 return 0;
666 }
667
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000668 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900669 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900670 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900671
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900672 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900673 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900674 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900675 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900676 /*
677 * Write back to the original probe_event for
678 * setting appropriate (user given) event name
679 */
680 clear_perf_probe_point(&pev->point);
681 memcpy(&pev->point, &tmp, sizeof(tmp));
682 }
683 }
684
Masami Hiramatsuff741782011-06-27 16:27:39 +0900685 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300686
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400687 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000688 pr_debug("Found %d probe_trace_events.\n", ntevs);
689 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900690 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900691 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000692 clear_probe_trace_events(*tevs, ntevs);
693 zfree(tevs);
694 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900695 if (ret != ntevs)
696 return ret < 0 ? ret : ntevs;
697 ntevs = 0;
698 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400699 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300700
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400701 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900702 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400703 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900704 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400705 }
706 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400707 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000708 if (ntevs < 0) {
709 if (ntevs == -EBADF)
710 pr_warning("Warning: No dwarf info found in the vmlinux - "
711 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400712 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900713 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400714 return 0;
715 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300716 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400717 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300718}
719
720#define LINEBUF_SIZE 256
721#define NR_ADDITIONAL_LINES 2
722
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100723static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300724{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000725 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100726 const char *color = show_num ? "" : PERF_COLOR_BLUE;
727 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300728
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100729 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300730 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
731 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100732 if (skip)
733 continue;
734 if (!prefix) {
735 prefix = show_num ? "%7d " : " ";
736 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300737 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100738 color_fprintf(stdout, color, "%s", buf);
739
740 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400741
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100742 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100744 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000745 pr_warning("File read error: %s\n",
746 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100747 return -1;
748 }
749 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300750}
751
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100752static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
753{
754 int rv = __show_one_line(fp, l, skip, show_num);
755 if (rv == 0) {
756 pr_warning("Source file is shorter than expected.\n");
757 rv = -1;
758 }
759 return rv;
760}
761
762#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
763#define show_one_line(f,l) _show_one_line(f,l,false,false)
764#define skip_one_line(f,l) _show_one_line(f,l,true,false)
765#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
766
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300767/*
768 * Show line-range always requires debuginfo to find source file and
769 * line number.
770 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900771static int __show_line_range(struct line_range *lr, const char *module,
772 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300773{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400774 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000775 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900776 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300777 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900778 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900779 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000780 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300781
782 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000783 dinfo = open_debuginfo(module, false);
784 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900785 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400786
Masami Hiramatsuff741782011-06-27 16:27:39 +0900787 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900788 if (!ret) { /* Not found, retry with an alternative */
789 ret = get_alternative_line_range(dinfo, lr, module, user);
790 if (!ret)
791 ret = debuginfo__find_line_range(dinfo, lr);
792 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900793 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000794 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400795 pr_warning("Specified source line is not found.\n");
796 return -ENOENT;
797 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000798 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400799 return ret;
800 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300801
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900802 /* Convert source file path */
803 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900804 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800805
806 /* Free old path when new path is assigned */
807 if (tmp != lr->path)
808 free(tmp);
809
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900810 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000811 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900812 return ret;
813 }
814
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300815 setup_pager();
816
817 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900818 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300819 lr->start - lr->offset);
820 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100821 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822
823 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400824 if (fp == NULL) {
825 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000826 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 return -errno;
828 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300829 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100830 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100831 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100832 if (ret < 0)
833 goto end;
834 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300835
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000836 intlist__for_each(ln, lr->line_list) {
837 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100838 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100839 if (ret < 0)
840 goto end;
841 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100842 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400843 if (ret < 0)
844 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300845 }
846
847 if (lr->end == INT_MAX)
848 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100849 while (l <= lr->end) {
850 ret = show_one_line_or_eof(fp, l++ - lr->offset);
851 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100852 break;
853 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400854end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300855 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400856 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300857}
858
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000859int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000860{
861 int ret;
862
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000863 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000864 if (ret < 0)
865 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900866 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000867 exit_symbol_maps();
868
869 return ret;
870}
871
Masami Hiramatsuff741782011-06-27 16:27:39 +0900872static int show_available_vars_at(struct debuginfo *dinfo,
873 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900874 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900875{
876 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900877 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900878 struct str_node *node;
879 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900880 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900881 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900882
883 buf = synthesize_perf_probe_point(&pev->point);
884 if (!buf)
885 return -EINVAL;
886 pr_debug("Searching variables at %s\n", buf);
887
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900888 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900889 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900890 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900891 if (!ret) {
892 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900893 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900894 /* Release the old probe_point */
895 clear_perf_probe_point(&tmp);
896 }
897 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900898 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000899 if (ret == 0 || ret == -ENOENT) {
900 pr_err("Failed to find the address of %s\n", buf);
901 ret = -ENOENT;
902 } else
903 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900904 goto end;
905 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000906
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900907 /* Some variables are found */
908 fprintf(stdout, "Available variables at %s\n", buf);
909 for (i = 0; i < ret; i++) {
910 vl = &vls[i];
911 /*
912 * A probe point might be converted to
913 * several trace points.
914 */
915 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
916 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300917 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900918 nvars = 0;
919 if (vl->vars) {
920 strlist__for_each(node, vl->vars) {
921 var = strchr(node->s, '\t') + 1;
922 if (strfilter__compare(_filter, var)) {
923 fprintf(stdout, "\t\t%s\n", node->s);
924 nvars++;
925 }
926 }
927 strlist__delete(vl->vars);
928 }
929 if (nvars == 0)
930 fprintf(stdout, "\t\t(No matched variables)\n");
931 }
932 free(vls);
933end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900934 free(buf);
935 return ret;
936}
937
938/* Show available variables on given probe point */
939int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900940 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900941{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900942 int i, ret = 0;
943 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000945 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900946 if (ret < 0)
947 return ret;
948
Masami Hiramatsu44225522015-05-08 10:03:28 +0900949 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900950 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000951 ret = -ENOENT;
952 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900953 }
954
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900955 setup_pager();
956
Masami Hiramatsuff741782011-06-27 16:27:39 +0900957 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900958 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900959
960 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000961out:
962 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900963 return ret;
964}
965
Ingo Molnar89fe8082013-09-30 12:07:11 +0200966#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300967
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900968static void debuginfo_cache__exit(void)
969{
970}
971
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000972static int
973find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
974 struct perf_probe_point *pp __maybe_unused,
975 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300976{
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000977 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300978}
979
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530980static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900981 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300982{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400983 if (perf_probe_event_need_dwarf(pev)) {
984 pr_warning("Debuginfo-analysis is not supported.\n");
985 return -ENOSYS;
986 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530987
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300988 return 0;
989}
990
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300991int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000992 const char *module __maybe_unused,
993 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300994{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400995 pr_warning("Debuginfo-analysis is not supported.\n");
996 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300997}
998
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300999int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001000 int npevs __maybe_unused,
1001 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001002{
1003 pr_warning("Debuginfo-analysis is not supported.\n");
1004 return -ENOSYS;
1005}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001006#endif
1007
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001008void line_range__clear(struct line_range *lr)
1009{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001010 free(lr->function);
1011 free(lr->file);
1012 free(lr->path);
1013 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001014 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001015 memset(lr, 0, sizeof(*lr));
1016}
1017
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001018int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001019{
1020 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001021 lr->line_list = intlist__new(NULL);
1022 if (!lr->line_list)
1023 return -ENOMEM;
1024 else
1025 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001026}
1027
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001028static int parse_line_num(char **ptr, int *val, const char *what)
1029{
1030 const char *start = *ptr;
1031
1032 errno = 0;
1033 *val = strtol(*ptr, ptr, 0);
1034 if (errno || *ptr == start) {
1035 semantic_error("'%s' is not a valid number.\n", what);
1036 return -EINVAL;
1037 }
1038 return 0;
1039}
1040
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001041/* Check the name is good for event, group or function */
1042static bool is_c_func_name(const char *name)
1043{
1044 if (!isalpha(*name) && *name != '_')
1045 return false;
1046 while (*++name != '\0') {
1047 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1048 return false;
1049 }
1050 return true;
1051}
1052
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001053/*
1054 * Stuff 'lr' according to the line range described by 'arg'.
1055 * The line range syntax is described by:
1056 *
1057 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001058 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001059 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001060int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001061{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001062 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001063 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001064
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001065 if (!name)
1066 return -ENOMEM;
1067
1068 lr->start = 0;
1069 lr->end = INT_MAX;
1070
1071 range = strchr(name, ':');
1072 if (range) {
1073 *range++ = '\0';
1074
1075 err = parse_line_num(&range, &lr->start, "start line");
1076 if (err)
1077 goto err;
1078
1079 if (*range == '+' || *range == '-') {
1080 const char c = *range++;
1081
1082 err = parse_line_num(&range, &lr->end, "end line");
1083 if (err)
1084 goto err;
1085
1086 if (c == '+') {
1087 lr->end += lr->start;
1088 /*
1089 * Adjust the number of lines here.
1090 * If the number of lines == 1, the
1091 * the end of line should be equal to
1092 * the start of line.
1093 */
1094 lr->end--;
1095 }
1096 }
1097
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001098 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001099
1100 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001101 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001102 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001104 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001105 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001106 if (*range != '\0') {
1107 semantic_error("Tailing with invalid str '%s'.\n", range);
1108 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001109 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001110 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001111
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001112 file = strchr(name, '@');
1113 if (file) {
1114 *file = '\0';
1115 lr->file = strdup(++file);
1116 if (lr->file == NULL) {
1117 err = -ENOMEM;
1118 goto err;
1119 }
1120 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001121 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001122 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001123 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001124 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001125 else { /* Invalid name */
1126 semantic_error("'%s' is not a valid function name.\n", name);
1127 err = -EINVAL;
1128 goto err;
1129 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001130
1131 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001132err:
1133 free(name);
1134 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001135}
1136
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001137/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001138static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001139{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001140 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001141 char *ptr, *tmp;
1142 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301143 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001144 /*
1145 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001146 * perf probe [EVENT=]SRC[:LN|;PTN]
1147 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001148 *
1149 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001150 */
Wang Nane59d29e2015-04-28 08:46:09 +00001151 if (!arg)
1152 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001153
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001154 ptr = strpbrk(arg, ";=@+%");
1155 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001156 *ptr = '\0';
1157 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001158 if (strchr(arg, ':')) {
1159 semantic_error("Group name is not supported yet.\n");
1160 return -ENOTSUP;
1161 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001162 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001163 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001164 "follow C symbol-naming rule.\n", arg);
1165 return -EINVAL;
1166 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001167 pev->event = strdup(arg);
1168 if (pev->event == NULL)
1169 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001170 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001171 arg = tmp;
1172 }
1173
Naveen N. Rao3099c022015-04-28 17:35:34 +05301174 /*
1175 * Check arg is function or file name and copy it.
1176 *
1177 * We consider arg to be a file spec if and only if it satisfies
1178 * all of the below criteria::
1179 * - it does not include any of "+@%",
1180 * - it includes one of ":;", and
1181 * - it has a period '.' in the name.
1182 *
1183 * Otherwise, we consider arg to be a function specification.
1184 */
1185 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1186 /* This is a file spec if it includes a '.' before ; or : */
1187 if (memchr(arg, '.', ptr - arg))
1188 file_spec = true;
1189 }
1190
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001191 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001192 if (ptr) {
1193 nc = *ptr;
1194 *ptr++ = '\0';
1195 }
1196
Wang Nan6c6e0242015-08-26 10:57:44 +00001197 if (arg[0] == '\0')
1198 tmp = NULL;
1199 else {
1200 tmp = strdup(arg);
1201 if (tmp == NULL)
1202 return -ENOMEM;
1203 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001204
Naveen N. Rao3099c022015-04-28 17:35:34 +05301205 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001206 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001207 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001208 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001209
Wang Nanda15bd92015-08-26 10:57:45 +00001210 /*
1211 * Keep pp->function even if this is absolute address,
1212 * so it can mark whether abs_address is valid.
1213 * Which make 'perf probe lib.bin 0x0' possible.
1214 *
1215 * Note that checking length of tmp is not needed
1216 * because when we access tmp[1] we know tmp[0] is '0',
1217 * so tmp[1] should always valid (but could be '\0').
1218 */
1219 if (tmp && !strncmp(tmp, "0x", 2)) {
1220 pp->abs_address = strtoul(pp->function, &tmp, 0);
1221 if (*tmp != '\0') {
1222 semantic_error("Invalid absolute address.\n");
1223 return -EINVAL;
1224 }
1225 }
1226 }
1227
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001228 /* Parse other options */
1229 while (ptr) {
1230 arg = ptr;
1231 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001232 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001233 pp->lazy_line = strdup(arg);
1234 if (pp->lazy_line == NULL)
1235 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001236 break;
1237 }
1238 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001239 if (ptr) {
1240 nc = *ptr;
1241 *ptr++ = '\0';
1242 }
1243 switch (c) {
1244 case ':': /* Line number */
1245 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001246 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001247 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001248 " in line number.\n");
1249 return -EINVAL;
1250 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001251 break;
1252 case '+': /* Byte offset from a symbol */
1253 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001255 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 " in offset.\n");
1257 return -EINVAL;
1258 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001259 break;
1260 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 if (pp->file) {
1262 semantic_error("SRC@SRC is not allowed.\n");
1263 return -EINVAL;
1264 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001265 pp->file = strdup(arg);
1266 if (pp->file == NULL)
1267 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001268 break;
1269 case '%': /* Probe places */
1270 if (strcmp(arg, "return") == 0) {
1271 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001272 } else { /* Others not supported yet */
1273 semantic_error("%%%s is not supported.\n", arg);
1274 return -ENOTSUP;
1275 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001276 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 default: /* Buggy case */
1278 pr_err("This program has a bug at %s:%d.\n",
1279 __FILE__, __LINE__);
1280 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001281 break;
1282 }
1283 }
1284
1285 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001286 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001287 semantic_error("Lazy pattern can't be used with"
1288 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001289 return -EINVAL;
1290 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001291
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001292 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001293 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001294 return -EINVAL;
1295 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001296
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001297 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001298 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001299 return -EINVAL;
1300 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001301
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001302 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001303 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001304 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001305 return -EINVAL;
1306 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001307
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001309 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001310 return -EINVAL;
1311 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001312
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001313 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001314 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001315 return -EINVAL;
1316 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001317
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001318 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001319 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001320 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001321 return -EINVAL;
1322 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001323
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001324 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001325 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1326 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001328}
1329
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001330/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001331static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001332{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001333 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001334 struct perf_probe_arg_field **fieldp;
1335
1336 pr_debug("parsing arg: %s into ", str);
1337
Masami Hiramatsu48481932010-04-12 13:16:53 -04001338 tmp = strchr(str, '=');
1339 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001340 arg->name = strndup(str, tmp - str);
1341 if (arg->name == NULL)
1342 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001343 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001344 str = tmp + 1;
1345 }
1346
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001347 tmp = strchr(str, ':');
1348 if (tmp) { /* Type setting */
1349 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001350 arg->type = strdup(tmp + 1);
1351 if (arg->type == NULL)
1352 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001353 pr_debug("type:%s ", arg->type);
1354 }
1355
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001356 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001357 if (!is_c_varname(str) || !tmp) {
1358 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001359 arg->var = strdup(str);
1360 if (arg->var == NULL)
1361 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001362 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001364 }
1365
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001366 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001367 arg->var = strndup(str, tmp - str);
1368 if (arg->var == NULL)
1369 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001370 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001371 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001372 fieldp = &arg->field;
1373
1374 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001375 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1376 if (*fieldp == NULL)
1377 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001378 if (*tmp == '[') { /* Array */
1379 str = tmp;
1380 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001381 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001382 if (*tmp != ']' || tmp == str + 1) {
1383 semantic_error("Array index must be a"
1384 " number.\n");
1385 return -EINVAL;
1386 }
1387 tmp++;
1388 if (*tmp == '\0')
1389 tmp = NULL;
1390 } else { /* Structure */
1391 if (*tmp == '.') {
1392 str = tmp + 1;
1393 (*fieldp)->ref = false;
1394 } else if (tmp[1] == '>') {
1395 str = tmp + 2;
1396 (*fieldp)->ref = true;
1397 } else {
1398 semantic_error("Argument parse error: %s\n",
1399 str);
1400 return -EINVAL;
1401 }
1402 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001404 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001405 (*fieldp)->name = strndup(str, tmp - str);
1406 if ((*fieldp)->name == NULL)
1407 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001408 if (*str != '[')
1409 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001410 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1411 fieldp = &(*fieldp)->next;
1412 }
1413 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001414 (*fieldp)->name = strdup(str);
1415 if ((*fieldp)->name == NULL)
1416 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001417 if (*str != '[')
1418 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001419 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001420
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001421 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001422 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001423 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001424 if (arg->name == NULL)
1425 return -ENOMEM;
1426 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001427 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001428}
1429
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001430/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001431int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001432{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001433 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001434 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001435
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001436 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 if (!argv) {
1438 pr_debug("Failed to split arguments.\n");
1439 return -ENOMEM;
1440 }
1441 if (argc - 1 > MAX_PROBE_ARGS) {
1442 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1443 ret = -ERANGE;
1444 goto out;
1445 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001446 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001447 ret = parse_perf_probe_point(argv[0], pev);
1448 if (ret < 0)
1449 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001450
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001451 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001452 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001453 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1454 if (pev->args == NULL) {
1455 ret = -ENOMEM;
1456 goto out;
1457 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1459 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1460 if (ret >= 0 &&
1461 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001462 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001463 " kretprobe.\n");
1464 ret = -EINVAL;
1465 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001466 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001467out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001468 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469
1470 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001471}
1472
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001473/* Return true if this perf_probe_event requires debuginfo */
1474bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001475{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001476 int i;
1477
1478 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1479 return true;
1480
1481 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001482 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001483 return true;
1484
1485 return false;
1486}
1487
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301488/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001489int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301491 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001492 char pr;
1493 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001494 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001495 int ret, i, argc;
1496 char **argv;
1497
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301498 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001499 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001500 if (!argv) {
1501 pr_debug("Failed to split arguments.\n");
1502 return -ENOMEM;
1503 }
1504 if (argc < 2) {
1505 semantic_error("Too few probe arguments.\n");
1506 ret = -ERANGE;
1507 goto out;
1508 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001509
1510 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001511 argv0_str = strdup(argv[0]);
1512 if (argv0_str == NULL) {
1513 ret = -ENOMEM;
1514 goto out;
1515 }
1516 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1517 fmt2_str = strtok_r(NULL, "/", &fmt);
1518 fmt3_str = strtok_r(NULL, " \t", &fmt);
1519 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1520 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001521 semantic_error("Failed to parse event name: %s\n", argv[0]);
1522 ret = -EINVAL;
1523 goto out;
1524 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001525 pr = fmt1_str[0];
1526 tev->group = strdup(fmt2_str);
1527 tev->event = strdup(fmt3_str);
1528 if (tev->group == NULL || tev->event == NULL) {
1529 ret = -ENOMEM;
1530 goto out;
1531 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001532 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001533
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001534 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001535
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001536 /* Scan module name(if there), function name and offset */
1537 p = strchr(argv[1], ':');
1538 if (p) {
1539 tp->module = strndup(argv[1], p - argv[1]);
1540 p++;
1541 } else
1542 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001543 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001544 /* only the address started with 0x */
1545 if (fmt1_str[0] == '0') {
1546 /*
1547 * Fix a special case:
1548 * if address == 0, kernel reports something like:
1549 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1550 * Newer kernel may fix that, but we want to
1551 * support old kernel also.
1552 */
1553 if (strcmp(fmt1_str, "0x") == 0) {
1554 if (!argv[2] || strcmp(argv[2], "(null)")) {
1555 ret = -EINVAL;
1556 goto out;
1557 }
1558 tp->address = 0;
1559
1560 free(argv[2]);
1561 for (i = 2; argv[i + 1] != NULL; i++)
1562 argv[i] = argv[i + 1];
1563
1564 argv[i] = NULL;
1565 argc -= 1;
1566 } else
1567 tp->address = strtoul(fmt1_str, NULL, 0);
1568 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001569 /* Only the symbol-based probe has offset */
1570 tp->symbol = strdup(fmt1_str);
1571 if (tp->symbol == NULL) {
1572 ret = -ENOMEM;
1573 goto out;
1574 }
1575 fmt2_str = strtok_r(NULL, "", &fmt);
1576 if (fmt2_str == NULL)
1577 tp->offset = 0;
1578 else
1579 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001580 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001581
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001582 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301583 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001584 if (tev->args == NULL) {
1585 ret = -ENOMEM;
1586 goto out;
1587 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001588 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589 p = strchr(argv[i + 2], '=');
1590 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001591 *p++ = '\0';
1592 else
1593 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001594 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001595 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001596 tev->args[i].value = strdup(p);
1597 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1598 ret = -ENOMEM;
1599 goto out;
1600 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001601 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001602 ret = 0;
1603out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001604 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001605 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001606 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001607}
1608
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001609/* Compose only probe arg */
1610int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1611{
1612 struct perf_probe_arg_field *field = pa->field;
1613 int ret;
1614 char *tmp = buf;
1615
Masami Hiramatsu48481932010-04-12 13:16:53 -04001616 if (pa->name && pa->var)
1617 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1618 else
1619 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001620 if (ret <= 0)
1621 goto error;
1622 tmp += ret;
1623 len -= ret;
1624
1625 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001626 if (field->name[0] == '[')
1627 ret = e_snprintf(tmp, len, "%s", field->name);
1628 else
1629 ret = e_snprintf(tmp, len, "%s%s",
1630 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001631 if (ret <= 0)
1632 goto error;
1633 tmp += ret;
1634 len -= ret;
1635 field = field->next;
1636 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001637
1638 if (pa->type) {
1639 ret = e_snprintf(tmp, len, ":%s", pa->type);
1640 if (ret <= 0)
1641 goto error;
1642 tmp += ret;
1643 len -= ret;
1644 }
1645
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001646 return tmp - buf;
1647error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001648 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001649 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001650}
1651
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652/* Compose only probe point (not argument) */
1653static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001654{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001655 char *buf, *tmp;
1656 char offs[32] = "", line[32] = "", file[32] = "";
1657 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001658
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001659 buf = zalloc(MAX_CMDLEN);
1660 if (buf == NULL) {
1661 ret = -ENOMEM;
1662 goto error;
1663 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001664 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001665 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001666 if (ret <= 0)
1667 goto error;
1668 }
1669 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001670 ret = e_snprintf(line, 32, ":%d", pp->line);
1671 if (ret <= 0)
1672 goto error;
1673 }
1674 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001675 tmp = pp->file;
1676 len = strlen(tmp);
1677 if (len > 30) {
1678 tmp = strchr(pp->file + len - 30, '/');
1679 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1680 }
1681 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001682 if (ret <= 0)
1683 goto error;
1684 }
1685
1686 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001687 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1688 offs, pp->retprobe ? "%return" : "", line,
1689 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001690 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001691 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001692 if (ret <= 0)
1693 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001694
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695 return buf;
1696error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001697 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001698 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001699 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700}
1701
1702#if 0
1703char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1704{
1705 char *buf;
1706 int i, len, ret;
1707
1708 buf = synthesize_perf_probe_point(&pev->point);
1709 if (!buf)
1710 return NULL;
1711
1712 len = strlen(buf);
1713 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001714 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001715 pev->args[i].name);
1716 if (ret <= 0) {
1717 free(buf);
1718 return NULL;
1719 }
1720 len += ret;
1721 }
1722
1723 return buf;
1724}
1725#endif
1726
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301727static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001728 char **buf, size_t *buflen,
1729 int depth)
1730{
1731 int ret;
1732 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301733 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734 buflen, depth + 1);
1735 if (depth < 0)
1736 goto out;
1737 }
1738
1739 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1740 if (ret < 0)
1741 depth = ret;
1742 else {
1743 *buf += ret;
1744 *buflen -= ret;
1745 }
1746out:
1747 return depth;
1748
1749}
1750
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301751static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001752 char *buf, size_t buflen)
1753{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301754 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001755 int ret, depth = 0;
1756 char *tmp = buf;
1757
1758 /* Argument name or separator */
1759 if (arg->name)
1760 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1761 else
1762 ret = e_snprintf(buf, buflen, " ");
1763 if (ret < 0)
1764 return ret;
1765 buf += ret;
1766 buflen -= ret;
1767
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001768 /* Special case: @XXX */
1769 if (arg->value[0] == '@' && arg->ref)
1770 ref = ref->next;
1771
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001773 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301774 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001775 &buflen, 1);
1776 if (depth < 0)
1777 return depth;
1778 }
1779
1780 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001781 if (arg->value[0] == '@' && arg->ref)
1782 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1783 arg->ref->offset);
1784 else
1785 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001786 if (ret < 0)
1787 return ret;
1788 buf += ret;
1789 buflen -= ret;
1790
1791 /* Closing */
1792 while (depth--) {
1793 ret = e_snprintf(buf, buflen, ")");
1794 if (ret < 0)
1795 return ret;
1796 buf += ret;
1797 buflen -= ret;
1798 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001799 /* Print argument type */
1800 if (arg->type) {
1801 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1802 if (ret <= 0)
1803 return ret;
1804 buf += ret;
1805 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806
1807 return buf - tmp;
1808}
1809
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301810char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001811{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301812 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001813 char *buf;
1814 int i, len, ret;
1815
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001816 buf = zalloc(MAX_CMDLEN);
1817 if (buf == NULL)
1818 return NULL;
1819
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001820 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1821 tev->group, tev->event);
1822 if (len <= 0)
1823 goto error;
1824
Wang Nanda15bd92015-08-26 10:57:45 +00001825 /* Uprobes must have tp->module */
1826 if (tev->uprobes && !tp->module)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001827 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001828 /*
1829 * If tp->address == 0, then this point must be a
1830 * absolute address uprobe.
1831 * try_to_find_absolute_address() should have made
1832 * tp->symbol to "0x0".
1833 */
1834 if (tev->uprobes && !tp->address) {
1835 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1836 goto error;
1837 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001838
1839 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301840 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001841 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1842 tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001843 else if (!strncmp(tp->symbol, "0x", 2))
1844 /* Absolute address. See try_to_find_absolute_address() */
1845 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1846 tp->module ?: "", tp->module ? ":" : "",
1847 tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301848 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001849 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301850 tp->module ?: "", tp->module ? ":" : "",
1851 tp->symbol, tp->offset);
1852
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001853 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001854 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001855 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001856
1857 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301858 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001860 if (ret <= 0)
1861 goto error;
1862 len += ret;
1863 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001864
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001865 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001866error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001867 free(buf);
1868 return NULL;
1869}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001870
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001871static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1872 struct perf_probe_point *pp,
1873 bool is_kprobe)
1874{
1875 struct symbol *sym = NULL;
1876 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001877 u64 addr = tp->address;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001878 int ret = -ENOENT;
1879
1880 if (!is_kprobe) {
1881 map = dso__new_map(tp->module);
1882 if (!map)
1883 goto out;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001884 sym = map__find_symbol(map, addr, NULL);
1885 } else {
Wang Nane4863672015-08-25 13:27:35 +00001886 if (tp->symbol)
1887 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001888 if (addr) {
1889 addr += tp->offset;
1890 sym = __find_kernel_function(addr, &map);
1891 }
1892 }
1893 if (!sym)
1894 goto out;
1895
1896 pp->retprobe = tp->retprobe;
1897 pp->offset = addr - map->unmap_ip(map, sym->start);
1898 pp->function = strdup(sym->name);
1899 ret = pp->function ? 0 : -ENOMEM;
1900
1901out:
1902 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001903 map__put(map);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001904 }
1905
1906 return ret;
1907}
1908
1909static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001910 struct perf_probe_point *pp,
1911 bool is_kprobe)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001912{
1913 char buf[128];
1914 int ret;
1915
1916 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1917 if (!ret)
1918 return 0;
1919 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1920 if (!ret)
1921 return 0;
1922
1923 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1924
1925 if (tp->symbol) {
1926 pp->function = strdup(tp->symbol);
1927 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001928 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001929 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1930 if (ret < 0)
1931 return ret;
1932 pp->function = strdup(buf);
1933 pp->offset = 0;
1934 }
1935 if (pp->function == NULL)
1936 return -ENOMEM;
1937
1938 pp->retprobe = tp->retprobe;
1939
1940 return 0;
1941}
1942
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301943static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301944 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001945{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001946 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001947 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001948
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001949 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001950 pev->event = strdup(tev->event);
1951 pev->group = strdup(tev->group);
1952 if (pev->event == NULL || pev->group == NULL)
1953 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001954
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001955 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001956 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001957 if (ret < 0)
1958 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001959
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001960 /* Convert trace_arg to probe_arg */
1961 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001962 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1963 if (pev->args == NULL)
1964 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001965 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001966 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001967 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001968 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301969 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001970 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001971 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001972 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001973 if (pev->args[i].name == NULL && ret >= 0)
1974 ret = -ENOMEM;
1975 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001976
1977 if (ret < 0)
1978 clear_perf_probe_event(pev);
1979
1980 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001981}
1982
1983void clear_perf_probe_event(struct perf_probe_event *pev)
1984{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001985 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001986 int i;
1987
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001988 free(pev->event);
1989 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001990 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001991 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001992
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001993 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001994 free(pev->args[i].name);
1995 free(pev->args[i].var);
1996 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001997 field = pev->args[i].field;
1998 while (field) {
1999 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002000 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002001 free(field);
2002 field = next;
2003 }
2004 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002005 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002006 memset(pev, 0, sizeof(*pev));
2007}
2008
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002009void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002010{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302011 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002012 int i;
2013
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002014 free(tev->event);
2015 free(tev->group);
2016 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002017 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002018 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002019 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002020 free(tev->args[i].name);
2021 free(tev->args[i].value);
2022 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002023 ref = tev->args[i].ref;
2024 while (ref) {
2025 next = ref->next;
2026 free(ref);
2027 ref = next;
2028 }
2029 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002030 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002031 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002032}
2033
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002034struct kprobe_blacklist_node {
2035 struct list_head list;
2036 unsigned long start;
2037 unsigned long end;
2038 char *symbol;
2039};
2040
2041static void kprobe_blacklist__delete(struct list_head *blacklist)
2042{
2043 struct kprobe_blacklist_node *node;
2044
2045 while (!list_empty(blacklist)) {
2046 node = list_first_entry(blacklist,
2047 struct kprobe_blacklist_node, list);
2048 list_del(&node->list);
2049 free(node->symbol);
2050 free(node);
2051 }
2052}
2053
2054static int kprobe_blacklist__load(struct list_head *blacklist)
2055{
2056 struct kprobe_blacklist_node *node;
2057 const char *__debugfs = debugfs_find_mountpoint();
2058 char buf[PATH_MAX], *p;
2059 FILE *fp;
2060 int ret;
2061
2062 if (__debugfs == NULL)
2063 return -ENOTSUP;
2064
2065 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2066 if (ret < 0)
2067 return ret;
2068
2069 fp = fopen(buf, "r");
2070 if (!fp)
2071 return -errno;
2072
2073 ret = 0;
2074 while (fgets(buf, PATH_MAX, fp)) {
2075 node = zalloc(sizeof(*node));
2076 if (!node) {
2077 ret = -ENOMEM;
2078 break;
2079 }
2080 INIT_LIST_HEAD(&node->list);
2081 list_add_tail(&node->list, blacklist);
2082 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2083 ret = -EINVAL;
2084 break;
2085 }
2086 p = strchr(buf, '\t');
2087 if (p) {
2088 p++;
2089 if (p[strlen(p) - 1] == '\n')
2090 p[strlen(p) - 1] = '\0';
2091 } else
2092 p = (char *)"unknown";
2093 node->symbol = strdup(p);
2094 if (!node->symbol) {
2095 ret = -ENOMEM;
2096 break;
2097 }
2098 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2099 node->start, node->end, node->symbol);
2100 ret++;
2101 }
2102 if (ret < 0)
2103 kprobe_blacklist__delete(blacklist);
2104 fclose(fp);
2105
2106 return ret;
2107}
2108
2109static struct kprobe_blacklist_node *
2110kprobe_blacklist__find_by_address(struct list_head *blacklist,
2111 unsigned long address)
2112{
2113 struct kprobe_blacklist_node *node;
2114
2115 list_for_each_entry(node, blacklist, list) {
2116 if (node->start <= address && address <= node->end)
2117 return node;
2118 }
2119
2120 return NULL;
2121}
2122
Masami Hiramatsub0312202015-06-16 20:50:55 +09002123static LIST_HEAD(kprobe_blacklist);
2124
2125static void kprobe_blacklist__init(void)
2126{
2127 if (!list_empty(&kprobe_blacklist))
2128 return;
2129
2130 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2131 pr_debug("No kprobe blacklist support, ignored\n");
2132}
2133
2134static void kprobe_blacklist__release(void)
2135{
2136 kprobe_blacklist__delete(&kprobe_blacklist);
2137}
2138
2139static bool kprobe_blacklist__listed(unsigned long address)
2140{
2141 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2142}
2143
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002144static int perf_probe_event__sprintf(const char *group, const char *event,
2145 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002146 const char *module,
2147 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002148{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002149 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002150 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002151 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002152
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002153 /* Synthesize only event probe point */
2154 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002155 if (!place)
2156 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002157
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002158 ret = e_snprintf(buf, 128, "%s:%s", group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002159 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002160 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002161
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002162 strbuf_addf(result, " %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002163 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002164 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002165
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002166 if (pev->nargs > 0) {
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002167 strbuf_addstr(result, " with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002168 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002169 ret = synthesize_perf_probe_arg(&pev->args[i],
2170 buf, 128);
2171 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002172 goto out;
2173 strbuf_addf(result, " %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002174 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002175 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002176 strbuf_addch(result, ')');
2177out:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002178 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002179 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002180}
2181
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002182/* Show an event */
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002183static int show_perf_probe_event(const char *group, const char *event,
2184 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002185 const char *module, bool use_stdout)
2186{
2187 struct strbuf buf = STRBUF_INIT;
2188 int ret;
2189
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002190 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002191 if (ret >= 0) {
2192 if (use_stdout)
2193 printf("%s\n", buf.buf);
2194 else
2195 pr_info("%s\n", buf.buf);
2196 }
2197 strbuf_release(&buf);
2198
2199 return ret;
2200}
2201
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002202static bool filter_probe_trace_event(struct probe_trace_event *tev,
2203 struct strfilter *filter)
2204{
2205 char tmp[128];
2206
2207 /* At first, check the event name itself */
2208 if (strfilter__compare(filter, tev->event))
2209 return true;
2210
2211 /* Next, check the combination of name and group */
2212 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2213 return false;
2214 return strfilter__compare(filter, tmp);
2215}
2216
2217static int __show_perf_probe_events(int fd, bool is_kprobe,
2218 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002219{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302220 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302221 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002222 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002223 struct strlist *rawlist;
2224 struct str_node *ent;
2225
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002226 memset(&tev, 0, sizeof(tev));
2227 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002228
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002229 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002230 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002231 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002232
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002233 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302234 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002235 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002236 if (!filter_probe_trace_event(&tev, filter))
2237 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302238 ret = convert_to_perf_probe_event(&tev, &pev,
2239 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002240 if (ret < 0)
2241 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002242 ret = show_perf_probe_event(pev.group, pev.event,
2243 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002244 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002245 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002246next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002247 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249 if (ret < 0)
2250 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002251 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002252 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002253 /* Cleanup cached debuginfo if needed */
2254 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002255
2256 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002257}
2258
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302259/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002260int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302261{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002262 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302263
2264 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302265
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002266 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302267 if (ret < 0)
2268 return ret;
2269
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002270 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2271 if (ret < 0)
2272 return ret;
2273
2274 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002275 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002276 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002277 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002278 if (kp_fd > 0)
2279 close(kp_fd);
2280 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002281 close(up_fd);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002282 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302283
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002284 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002285}
2286
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002287static int get_new_event_name(char *buf, size_t len, const char *base,
2288 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002289{
2290 int i, ret;
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002291 char *p;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002292
Naveen N. Rao3099c022015-04-28 17:35:34 +05302293 if (*base == '.')
2294 base++;
2295
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002296 /* Try no suffix */
2297 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002298 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002299 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002300 return ret;
2301 }
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002302 /* Cut off the postfixes (e.g. .const, .isra)*/
2303 p = strchr(buf, '.');
2304 if (p && p != buf)
2305 *p = '\0';
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002306 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002307 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002308
Masami Hiramatsud761b082009-12-15 10:32:25 -05002309 if (!allow_suffix) {
2310 pr_warning("Error: event \"%s\" already exists. "
2311 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002312 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002313 }
2314
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002315 /* Try to add suffix */
2316 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002317 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002318 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002319 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002320 return ret;
2321 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002322 if (!strlist__has_entry(namelist, buf))
2323 break;
2324 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002325 if (i == MAX_EVENT_INDEX) {
2326 pr_warning("Too many events are on the same function.\n");
2327 ret = -ERANGE;
2328 }
2329
2330 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002331}
2332
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002333/* Warn if the current kernel's uprobe implementation is old */
2334static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2335{
2336 int i;
2337 char *buf = synthesize_probe_trace_command(tev);
2338
2339 /* Old uprobe event doesn't support memory dereference */
2340 if (!tev->uprobes || tev->nargs == 0 || !buf)
2341 goto out;
2342
2343 for (i = 0; i < tev->nargs; i++)
2344 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2345 pr_warning("Please upgrade your kernel to at least "
2346 "3.14 to have access to feature %s\n",
2347 tev->args[i].value);
2348 break;
2349 }
2350out:
2351 free(buf);
2352}
2353
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002354/* Set new name from original perf_probe_event and namelist */
2355static int probe_trace_event__set_name(struct probe_trace_event *tev,
2356 struct perf_probe_event *pev,
2357 struct strlist *namelist,
2358 bool allow_suffix)
2359{
2360 const char *event, *group;
2361 char buf[64];
2362 int ret;
2363
2364 if (pev->event)
2365 event = pev->event;
2366 else
Wang Nanda15bd92015-08-26 10:57:45 +00002367 if (pev->point.function &&
2368 (strncmp(pev->point.function, "0x", 2) != 0) &&
2369 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002370 event = pev->point.function;
2371 else
2372 event = tev->point.realname;
2373 if (pev->group)
2374 group = pev->group;
2375 else
2376 group = PERFPROBE_GROUP;
2377
2378 /* Get an unused new event name */
2379 ret = get_new_event_name(buf, 64, event,
2380 namelist, allow_suffix);
2381 if (ret < 0)
2382 return ret;
2383
2384 event = buf;
2385
2386 tev->event = strdup(event);
2387 tev->group = strdup(group);
2388 if (tev->event == NULL || tev->group == NULL)
2389 return -ENOMEM;
2390
2391 /* Add added event name to namelist */
2392 strlist__add(namelist, event);
2393 return 0;
2394}
2395
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302396static int __add_probe_trace_events(struct perf_probe_event *pev,
2397 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002398 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002399{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002400 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302401 struct probe_trace_event *tev = NULL;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002402 const char *event = NULL, *group = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002403 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002404
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002405 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2406 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002407 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002408
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002409 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002410 namelist = probe_file__get_namelist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002411 if (!namelist) {
2412 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002413 ret = -ENOMEM;
2414 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002415 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002416
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002417 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002418 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002419 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002420 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002421 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002422 if (!tev->point.symbol)
2423 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002424
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002425 /* Set new name for tev (and update namelist) */
2426 ret = probe_trace_event__set_name(tev, pev, namelist,
2427 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002428 if (ret < 0)
2429 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002430
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002431 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002432 if (ret < 0)
2433 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002435 /* We use tev's name for showing new events */
2436 show_perf_probe_event(tev->group, tev->event, pev,
2437 tev->point.module, false);
2438 /* Save the last valid name */
2439 event = tev->event;
2440 group = tev->group;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002441
2442 /*
2443 * Probes after the first probe which comes from same
2444 * user input are always allowed to add suffix, because
2445 * there might be several addresses corresponding to
2446 * one code line.
2447 */
2448 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002449 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002450 if (ret == -EINVAL && pev->uprobes)
2451 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002452
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002453 /* Note that it is possible to skip all events because of blacklist */
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002454 if (ret >= 0 && event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002455 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002456 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002457 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002458 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002459
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002460 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002461close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002462 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002463 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002464}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002465
Wang Nan6bb536c2015-05-29 09:45:47 +00002466static int find_probe_functions(struct map *map, char *name,
2467 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002468{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002469 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002470 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002471 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002472
Wang Nan75e4a2a2015-05-15 12:14:44 +00002473 if (map__load(map, NULL) < 0)
2474 return 0;
2475
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002476 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002477 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002478 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002479 if (syms && found < probe_conf.max_probes)
2480 syms[found - 1] = sym;
2481 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002482 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002483
2484 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002485}
2486
2487#define strdup_or_goto(str, label) \
2488 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2489
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302490void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2491 struct probe_trace_event *tev __maybe_unused,
2492 struct map *map __maybe_unused) { }
2493
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002494/*
2495 * Find probe function addresses from map.
2496 * Return an error or the number of found probe_trace_event
2497 */
2498static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002499 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002500{
2501 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002502 struct ref_reloc_sym *reloc_sym = NULL;
2503 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002504 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002505 struct probe_trace_event *tev;
2506 struct perf_probe_point *pp = &pev->point;
2507 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002508 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002509 int ret, i, j, skipped = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002510
Masami Hiramatsu44225522015-05-08 10:03:28 +09002511 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002512 if (!map) {
2513 ret = -EINVAL;
2514 goto out;
2515 }
2516
Wang Nan6bb536c2015-05-29 09:45:47 +00002517 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2518 if (!syms) {
2519 ret = -ENOMEM;
2520 goto out;
2521 }
2522
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002523 /*
2524 * Load matched symbols: Since the different local symbols may have
2525 * same name but different addresses, this lists all the symbols.
2526 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002527 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002528 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002529 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002530 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002531 ret = -ENOENT;
2532 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002533 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002534 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002535 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002536 ret = -E2BIG;
2537 goto out;
2538 }
2539
Namhyung Kim25dd9172015-01-14 20:18:08 +09002540 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002541 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002542 if (!reloc_sym) {
2543 pr_warning("Relocated base symbol is not found!\n");
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547 }
2548
2549 /* Setup result trace-probe-events */
2550 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2551 if (!*tevs) {
2552 ret = -ENOMEM;
2553 goto out;
2554 }
2555
2556 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002557
Wang Nan6bb536c2015-05-29 09:45:47 +00002558 for (j = 0; j < num_matched_functions; j++) {
2559 sym = syms[j];
2560
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002561 tev = (*tevs) + ret;
2562 tp = &tev->point;
2563 if (ret == num_matched_functions) {
2564 pr_warning("Too many symbols are listed. Skip it.\n");
2565 break;
2566 }
2567 ret++;
2568
2569 if (pp->offset > sym->end - sym->start) {
2570 pr_warning("Offset %ld is bigger than the size of %s\n",
2571 pp->offset, sym->name);
2572 ret = -ENOENT;
2573 goto err_out;
2574 }
2575 /* Add one probe point */
2576 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002577 /* If we found a wrong one, mark it by NULL symbol */
2578 if (!pev->uprobes &&
2579 kprobe_warn_out_range(sym->name, tp->address)) {
2580 tp->symbol = NULL; /* Skip it */
2581 skipped++;
2582 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002583 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2584 tp->offset = tp->address - reloc_sym->addr;
2585 } else {
2586 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2587 tp->offset = pp->offset;
2588 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002589 tp->realname = strdup_or_goto(sym->name, nomem_out);
2590
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002591 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002592 if (pev->target)
2593 tev->point.module = strdup_or_goto(pev->target,
2594 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002595 tev->uprobes = pev->uprobes;
2596 tev->nargs = pev->nargs;
2597 if (tev->nargs) {
2598 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2599 tev->nargs);
2600 if (tev->args == NULL)
2601 goto nomem_out;
2602 }
2603 for (i = 0; i < tev->nargs; i++) {
2604 if (pev->args[i].name)
2605 tev->args[i].name =
2606 strdup_or_goto(pev->args[i].name,
2607 nomem_out);
2608
2609 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2610 nomem_out);
2611 if (pev->args[i].type)
2612 tev->args[i].type =
2613 strdup_or_goto(pev->args[i].type,
2614 nomem_out);
2615 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302616 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002617 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002618 if (ret == skipped) {
2619 ret = -ENOENT;
2620 goto err_out;
2621 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002622
2623out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002624 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002625 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002626 return ret;
2627
2628nomem_out:
2629 ret = -ENOMEM;
2630err_out:
2631 clear_probe_trace_events(*tevs, num_matched_functions);
2632 zfree(tevs);
2633 goto out;
2634}
2635
Wang Nanda15bd92015-08-26 10:57:45 +00002636static int try_to_find_absolute_address(struct perf_probe_event *pev,
2637 struct probe_trace_event **tevs)
2638{
2639 struct perf_probe_point *pp = &pev->point;
2640 struct probe_trace_event *tev;
2641 struct probe_trace_point *tp;
2642 int i, err;
2643
2644 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2645 return -EINVAL;
2646 if (perf_probe_event_need_dwarf(pev))
2647 return -EINVAL;
2648
2649 /*
2650 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2651 * absolute address.
2652 *
2653 * Only one tev can be generated by this.
2654 */
2655 *tevs = zalloc(sizeof(*tev));
2656 if (!*tevs)
2657 return -ENOMEM;
2658
2659 tev = *tevs;
2660 tp = &tev->point;
2661
2662 /*
2663 * Don't use tp->offset, use address directly, because
2664 * in synthesize_probe_trace_command() address cannot be
2665 * zero.
2666 */
2667 tp->address = pev->point.abs_address;
2668 tp->retprobe = pp->retprobe;
2669 tev->uprobes = pev->uprobes;
2670
2671 err = -ENOMEM;
2672 /*
2673 * Give it a '0x' leading symbol name.
2674 * In __add_probe_trace_events, a NULL symbol is interpreted as
2675 * invalud.
2676 */
2677 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2678 goto errout;
2679
2680 /* For kprobe, check range */
2681 if ((!tev->uprobes) &&
2682 (kprobe_warn_out_range(tev->point.symbol,
2683 tev->point.address))) {
2684 err = -EACCES;
2685 goto errout;
2686 }
2687
2688 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2689 goto errout;
2690
2691 if (pev->target) {
2692 tp->module = strdup(pev->target);
2693 if (!tp->module)
2694 goto errout;
2695 }
2696
2697 if (tev->group) {
2698 tev->group = strdup(pev->group);
2699 if (!tev->group)
2700 goto errout;
2701 }
2702
2703 if (pev->event) {
2704 tev->event = strdup(pev->event);
2705 if (!tev->event)
2706 goto errout;
2707 }
2708
2709 tev->nargs = pev->nargs;
2710 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2711 if (!tev->args) {
2712 err = -ENOMEM;
2713 goto errout;
2714 }
2715 for (i = 0; i < tev->nargs; i++)
2716 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2717
2718 return 1;
2719
2720errout:
2721 if (*tevs) {
2722 clear_probe_trace_events(*tevs, 1);
2723 *tevs = NULL;
2724 }
2725 return err;
2726}
2727
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302728bool __weak arch__prefers_symtab(void) { return false; }
2729
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302730static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002731 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002732{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002733 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002734
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002735 if (pev->uprobes && !pev->group) {
2736 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002737 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002738 if (ret != 0) {
2739 pr_warning("Failed to make a group name.\n");
2740 return ret;
2741 }
2742 }
2743
Wang Nanda15bd92015-08-26 10:57:45 +00002744 ret = try_to_find_absolute_address(pev, tevs);
2745 if (ret > 0)
2746 return ret;
2747
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302748 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002749 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302750 if (ret > 0)
2751 return ret; /* Found in symbol table */
2752 }
2753
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002754 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002755 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002756 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002757 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002758
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002759 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002760}
2761
2762struct __event_package {
2763 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302764 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002765 int ntevs;
2766};
2767
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002768int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002769{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002770 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002771 struct __event_package *pkgs;
2772
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302773 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002774 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302775
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002776 if (pkgs == NULL)
2777 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002778
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002779 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002780 if (ret < 0) {
2781 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002782 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002783 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002784
2785 /* Loop 1: convert all events */
2786 for (i = 0; i < npevs; i++) {
2787 pkgs[i].pev = &pevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002788 /* Init kprobe blacklist if needed */
2789 if (!pkgs[i].pev->uprobes)
2790 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002791 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302792 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002793 &pkgs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002794 if (ret < 0)
2795 goto end;
2796 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002797 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002798 /* This just release blacklist only if allocated */
2799 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002800
2801 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002802 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302803 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002804 pkgs[i].ntevs,
2805 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002806 if (ret < 0)
2807 break;
2808 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002809end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002810 /* Loop 3: cleanup and free trace events */
2811 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002812 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302813 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002814 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002815 }
2816 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002817 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002818
2819 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002820}
2821
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002822int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002823{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002824 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002825 char *str = strfilter__string(filter);
2826
2827 if (!str)
2828 return -EINVAL;
2829
2830 pr_debug("Delete filter: \'%s\'\n", str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002831
Masami Hiramatsufa282442009-12-08 17:03:23 -05002832 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002833 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2834 if (ret < 0)
2835 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302836
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002837 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002838 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302839 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002840
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002841 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002842 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002843 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002844 goto error;
2845 }
2846 if (ret == -ENOENT && ret2 == -ENOENT)
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002847 pr_debug("\"%s\" does not hit any event.\n", str);
2848 /* Note that this is silently ignored */
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002849 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302850
2851error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002852 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302853 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002854 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302855 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002856out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002857 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002858
2859 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002860}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302861
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002862/* TODO: don't use a global variable for filter ... */
2863static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002864
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002865/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002866 * If a symbol corresponds to a function with global binding and
2867 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002868 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002869static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002870 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002871{
Namhyung Kime578da32015-03-06 16:31:29 +09002872 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002873 return 0;
2874 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002875}
2876
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002877int show_available_funcs(const char *target, struct strfilter *_filter,
2878 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002879{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002880 struct map *map;
2881 int ret;
2882
2883 ret = init_symbol_maps(user);
2884 if (ret < 0)
2885 return ret;
2886
2887 /* Get a symbol map */
2888 if (user)
2889 map = dso__new_map(target);
2890 else
2891 map = kernel_get_module_map(target);
2892 if (!map) {
2893 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002894 return -EINVAL;
2895 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002896
2897 /* Load symbols with given filter */
2898 available_func_filter = _filter;
2899 if (map__load(map, filter_available_functions)) {
2900 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2901 goto end;
2902 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002903 if (!dso__sorted_by_name(map->dso, map->type))
2904 dso__sort_by_name(map->dso, map->type);
2905
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002906 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302907 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002908 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2909end:
2910 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002911 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002912 }
2913 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302914
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002915 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302916}
2917
Wang Nanda15bd92015-08-26 10:57:45 +00002918int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2919 struct perf_probe_arg *pvar)
2920{
2921 tvar->value = strdup(pvar->var);
2922 if (tvar->value == NULL)
2923 return -ENOMEM;
2924 if (pvar->type) {
2925 tvar->type = strdup(pvar->type);
2926 if (tvar->type == NULL)
2927 return -ENOMEM;
2928 }
2929 if (pvar->name) {
2930 tvar->name = strdup(pvar->name);
2931 if (tvar->name == NULL)
2932 return -ENOMEM;
2933 } else
2934 tvar->name = NULL;
2935 return 0;
2936}