blob: a0b3f3c886f0377863a43af5a6d3f8b58fef4bb3 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020043#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090047#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090054struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090058int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059{
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68}
69
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030070static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000071static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040072
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090073/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090074int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040076 int ret;
77
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090079 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090080 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 if (ret < 0) {
82 pr_debug("Failed to init symbol map.\n");
83 goto out;
84 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040085
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000086 if (host_machine || user_only) /* already initialized */
87 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91
92 host_machine = machine__new_host();
93 if (!host_machine) {
94 pr_debug("machine__new_host() failed.\n");
95 symbol__exit();
96 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040098out:
99 if (ret < 0)
100 pr_warning("Failed to init vmlinux path.\n");
101 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400102}
103
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900104void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000105{
106 if (host_machine) {
107 machine__delete(host_machine);
108 host_machine = NULL;
109 }
110 symbol__exit();
111}
112
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900113static struct symbol *__find_kernel_function_by_name(const char *name,
114 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400115{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000116 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900117 NULL);
118}
119
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000120static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
121{
122 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
123}
124
125static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
126{
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
128 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300129 struct map *map = machine__kernel_map(host_machine, MAP__FUNCTION);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000130
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300131 if (map__load(map, NULL) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000132 return NULL;
133
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300134 kmap = map__kmap(map);
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 Melo266fa2b2015-09-24 11:24:18 -0300273 char module_name[128];
274
275 snprintf(module_name, sizeof(module_name), "[%s]", module);
276 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
277 if (map) {
278 dso = map->dso;
279 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000280 }
281 pr_debug("Failed to find module %s.\n", module);
282 return -ENOENT;
283 }
284
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300285 map = machine__kernel_map(host_machine, MAP__FUNCTION);
Wang Nan60fb7742015-05-28 02:25:05 +0000286 dso = map->dso;
287
288 vmlinux_name = symbol_conf.vmlinux_name;
289 dso->load_errno = 0;
290 if (vmlinux_name)
291 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
292 else
293 ret = dso__load_vmlinux_path(dso, map, NULL);
294found:
295 *pdso = dso;
296 return ret;
297}
298
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900299/*
300 * Some binaries like glibc have special symbols which are on the symbol
301 * table, but not in the debuginfo. If we can find the address of the
302 * symbol from map, we can translate the address back to the probe point.
303 */
304static int find_alternative_probe_point(struct debuginfo *dinfo,
305 struct perf_probe_point *pp,
306 struct perf_probe_point *result,
307 const char *target, bool uprobes)
308{
309 struct map *map = NULL;
310 struct symbol *sym;
311 u64 address = 0;
312 int ret = -ENOENT;
313
314 /* This can work only for function-name based one */
315 if (!pp->function || pp->file)
316 return -ENOTSUP;
317
318 map = get_target_map(target, uprobes);
319 if (!map)
320 return -EINVAL;
321
322 /* Find the address of given function */
323 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900324 if (uprobes)
325 address = sym->start;
326 else
327 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900328 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900329 }
330 if (!address) {
331 ret = -ENOENT;
332 goto out;
333 }
Wang Nanf6c15622015-04-08 02:14:34 +0000334 pr_debug("Symbol %s address found : %" PRIx64 "\n",
335 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900336
337 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
338 result);
339 if (ret <= 0)
340 ret = (!ret) ? -ENOENT : ret;
341 else {
342 result->offset += pp->offset;
343 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800344 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900345 ret = 0;
346 }
347
348out:
349 put_target_map(map, uprobes);
350 return ret;
351
352}
353
354static int get_alternative_probe_event(struct debuginfo *dinfo,
355 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900356 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900357{
358 int ret;
359
360 memcpy(tmp, &pev->point, sizeof(*tmp));
361 memset(&pev->point, 0, sizeof(pev->point));
362 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900363 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900364 if (ret < 0)
365 memcpy(&pev->point, tmp, sizeof(*tmp));
366
367 return ret;
368}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000369
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900370static int get_alternative_line_range(struct debuginfo *dinfo,
371 struct line_range *lr,
372 const char *target, bool user)
373{
David Ahern6d4a4892015-03-11 10:36:20 -0400374 struct perf_probe_point pp = { .function = lr->function,
375 .file = lr->file,
376 .line = lr->start };
377 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900378 int ret, len = 0;
379
David Ahern6d4a4892015-03-11 10:36:20 -0400380 memset(&result, 0, sizeof(result));
381
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900382 if (lr->end != INT_MAX)
383 len = lr->end - lr->start;
384 ret = find_alternative_probe_point(dinfo, &pp, &result,
385 target, user);
386 if (!ret) {
387 lr->function = result.function;
388 lr->file = result.file;
389 lr->start = result.line;
390 if (lr->end != INT_MAX)
391 lr->end = lr->start + len;
392 clear_perf_probe_point(&pp);
393 }
394 return ret;
395}
396
Masami Hiramatsuff741782011-06-27 16:27:39 +0900397/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000398static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900399{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000400 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900401 char reason[STRERR_BUFSIZE];
402 struct debuginfo *ret = NULL;
403 struct dso *dso = NULL;
404 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900405
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000406 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900407 err = kernel_get_module_dso(module, &dso);
408 if (err < 0) {
409 if (!dso || dso->load_errno == 0) {
410 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
411 strcpy(reason, "(unknown)");
412 } else
413 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000414 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900415 pr_err("Failed to find the path for %s: %s\n",
416 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900417 return NULL;
418 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900419 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900420 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000421 ret = debuginfo__new(path);
422 if (!ret && !silent) {
423 pr_warning("The %s file has no debug information.\n", path);
424 if (!module || !strtailcmp(path, ".ko"))
425 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
426 else
427 pr_warning("Rebuild with -g, ");
428 pr_warning("or install an appropriate debuginfo package.\n");
429 }
430 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400431}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300432
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900433/* For caching the last debuginfo */
434static struct debuginfo *debuginfo_cache;
435static char *debuginfo_cache_path;
436
437static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
438{
439 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
440 (!debuginfo_cache_path && !module && debuginfo_cache))
441 goto out;
442
443 /* Copy module path */
444 free(debuginfo_cache_path);
445 if (module) {
446 debuginfo_cache_path = strdup(module);
447 if (!debuginfo_cache_path) {
448 debuginfo__delete(debuginfo_cache);
449 debuginfo_cache = NULL;
450 goto out;
451 }
452 }
453
454 debuginfo_cache = open_debuginfo(module, silent);
455 if (!debuginfo_cache)
456 zfree(&debuginfo_cache_path);
457out:
458 return debuginfo_cache;
459}
460
461static void debuginfo_cache__exit(void)
462{
463 debuginfo__delete(debuginfo_cache);
464 debuginfo_cache = NULL;
465 zfree(&debuginfo_cache_path);
466}
467
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000468
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000469static int get_text_start_address(const char *exec, unsigned long *address)
470{
471 Elf *elf;
472 GElf_Ehdr ehdr;
473 GElf_Shdr shdr;
474 int fd, ret = -ENOENT;
475
476 fd = open(exec, O_RDONLY);
477 if (fd < 0)
478 return -errno;
479
480 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
481 if (elf == NULL)
482 return -EINVAL;
483
484 if (gelf_getehdr(elf, &ehdr) == NULL)
485 goto out;
486
487 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
488 goto out;
489
490 *address = shdr.sh_addr - shdr.sh_offset;
491 ret = 0;
492out:
493 elf_end(elf);
494 return ret;
495}
496
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000497/*
498 * Convert trace point to probe point with debuginfo
499 */
500static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
501 struct perf_probe_point *pp,
502 bool is_kprobe)
503{
504 struct debuginfo *dinfo = NULL;
505 unsigned long stext = 0;
506 u64 addr = tp->address;
507 int ret = -ENOENT;
508
509 /* convert the address to dwarf address */
510 if (!is_kprobe) {
511 if (!addr) {
512 ret = -EINVAL;
513 goto error;
514 }
515 ret = get_text_start_address(tp->module, &stext);
516 if (ret < 0)
517 goto error;
518 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000519 } else if (tp->symbol) {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000520 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
521 if (addr == 0)
522 goto error;
523 addr += tp->offset;
524 }
525
526 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
527 tp->module ? : "kernel");
528
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900529 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
530 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000531 ret = debuginfo__find_probe_point(dinfo,
532 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900533 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000534 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000535
536 if (ret > 0) {
537 pp->retprobe = tp->retprobe;
538 return 0;
539 }
540error:
541 pr_debug("Failed to find corresponding probes from debuginfo.\n");
542 return ret ? : -ENOENT;
543}
544
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000545static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
546 int ntevs, const char *exec)
547{
548 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000549 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000550
551 if (!exec)
552 return 0;
553
554 ret = get_text_start_address(exec, &stext);
555 if (ret < 0)
556 return ret;
557
558 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000559 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000560 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000561 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000562 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000563 ret = -ENOMEM;
564 break;
565 }
566 tevs[i].uprobes = true;
567 }
568
569 return ret;
570}
571
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900572static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
573 int ntevs, const char *module)
574{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900575 int i, ret = 0;
576 char *tmp;
577
578 if (!module)
579 return 0;
580
581 tmp = strrchr(module, '/');
582 if (tmp) {
583 /* This is a module path -- get the module name */
584 module = strdup(tmp + 1);
585 if (!module)
586 return -ENOMEM;
587 tmp = strchr(module, '.');
588 if (tmp)
589 *tmp = '\0';
590 tmp = (char *)module; /* For free() */
591 }
592
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900593 for (i = 0; i < ntevs; i++) {
594 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900595 if (!tevs[i].point.module) {
596 ret = -ENOMEM;
597 break;
598 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900599 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900600
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300601 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900602 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900603}
604
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000605/* Post processing the probe events */
606static int post_process_probe_trace_events(struct probe_trace_event *tevs,
607 int ntevs, const char *module,
608 bool uprobe)
609{
610 struct ref_reloc_sym *reloc_sym;
611 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900612 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000613
614 if (uprobe)
615 return add_exec_to_probe_trace_events(tevs, ntevs, module);
616
617 /* Note that currently ref_reloc_sym based probe is not for drivers */
618 if (module)
619 return add_module_to_probe_trace_events(tevs, ntevs, module);
620
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000621 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000622 if (!reloc_sym) {
623 pr_warning("Relocated base symbol is not found!\n");
624 return -EINVAL;
625 }
626
627 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900628 if (!tevs[i].point.address || tevs[i].point.retprobe)
629 continue;
630 /* If we found a wrong one, mark it by NULL symbol */
631 if (kprobe_warn_out_range(tevs[i].point.symbol,
632 tevs[i].point.address)) {
633 tmp = NULL;
634 skipped++;
635 } else {
636 tmp = strdup(reloc_sym->name);
637 if (!tmp)
638 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000639 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900640 /* If we have no realname, use symbol for it */
641 if (!tevs[i].point.realname)
642 tevs[i].point.realname = tevs[i].point.symbol;
643 else
644 free(tevs[i].point.symbol);
645 tevs[i].point.symbol = tmp;
646 tevs[i].point.offset = tevs[i].point.address -
647 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000648 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900649 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000650}
651
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300652/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530653static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900654 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300655{
656 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900657 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530658 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900659 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300660
Masami Hiramatsu44225522015-05-08 10:03:28 +0900661 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900662 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000663 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900664 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900665 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300666 return 0;
667 }
668
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000669 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900670 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900671 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900672
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900673 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900674 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900675 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900676 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900677 /*
678 * Write back to the original probe_event for
679 * setting appropriate (user given) event name
680 */
681 clear_perf_probe_point(&pev->point);
682 memcpy(&pev->point, &tmp, sizeof(tmp));
683 }
684 }
685
Masami Hiramatsuff741782011-06-27 16:27:39 +0900686 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300687
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400688 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000689 pr_debug("Found %d probe_trace_events.\n", ntevs);
690 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900691 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900692 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000693 clear_probe_trace_events(*tevs, ntevs);
694 zfree(tevs);
695 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900696 if (ret != ntevs)
697 return ret < 0 ? ret : ntevs;
698 ntevs = 0;
699 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400700 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300701
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900703 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900705 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400706 }
707 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400708 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000709 if (ntevs < 0) {
710 if (ntevs == -EBADF)
711 pr_warning("Warning: No dwarf info found in the vmlinux - "
712 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400713 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900714 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400715 return 0;
716 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400718 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719}
720
721#define LINEBUF_SIZE 256
722#define NR_ADDITIONAL_LINES 2
723
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100724static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000726 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100727 const char *color = show_num ? "" : PERF_COLOR_BLUE;
728 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300729
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100730 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
732 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100733 if (skip)
734 continue;
735 if (!prefix) {
736 prefix = show_num ? "%7d " : " ";
737 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100739 color_fprintf(stdout, color, "%s", buf);
740
741 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400742
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100743 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300744error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100745 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000746 pr_warning("File read error: %s\n",
747 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100748 return -1;
749 }
750 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300751}
752
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100753static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
754{
755 int rv = __show_one_line(fp, l, skip, show_num);
756 if (rv == 0) {
757 pr_warning("Source file is shorter than expected.\n");
758 rv = -1;
759 }
760 return rv;
761}
762
763#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
764#define show_one_line(f,l) _show_one_line(f,l,false,false)
765#define skip_one_line(f,l) _show_one_line(f,l,true,false)
766#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
767
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300768/*
769 * Show line-range always requires debuginfo to find source file and
770 * line number.
771 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900772static int __show_line_range(struct line_range *lr, const char *module,
773 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300774{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400775 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000776 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900777 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300778 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900779 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900780 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000781 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300782
783 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000784 dinfo = open_debuginfo(module, false);
785 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900786 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400787
Masami Hiramatsuff741782011-06-27 16:27:39 +0900788 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900789 if (!ret) { /* Not found, retry with an alternative */
790 ret = get_alternative_line_range(dinfo, lr, module, user);
791 if (!ret)
792 ret = debuginfo__find_line_range(dinfo, lr);
793 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900794 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000795 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400796 pr_warning("Specified source line is not found.\n");
797 return -ENOENT;
798 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000799 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400800 return ret;
801 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300802
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900803 /* Convert source file path */
804 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900805 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800806
807 /* Free old path when new path is assigned */
808 if (tmp != lr->path)
809 free(tmp);
810
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900811 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000812 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900813 return ret;
814 }
815
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816 setup_pager();
817
818 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900819 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300820 lr->start - lr->offset);
821 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100822 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300823
824 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400825 if (fp == NULL) {
826 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000827 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400828 return -errno;
829 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100831 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100832 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100833 if (ret < 0)
834 goto end;
835 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300836
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000837 intlist__for_each(ln, lr->line_list) {
838 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100839 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100840 if (ret < 0)
841 goto end;
842 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100843 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844 if (ret < 0)
845 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300846 }
847
848 if (lr->end == INT_MAX)
849 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100850 while (l <= lr->end) {
851 ret = show_one_line_or_eof(fp, l++ - lr->offset);
852 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100853 break;
854 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300856 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400857 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300858}
859
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000860int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000861{
862 int ret;
863
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900864 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000865 if (ret < 0)
866 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900867 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900868 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000869
870 return ret;
871}
872
Masami Hiramatsuff741782011-06-27 16:27:39 +0900873static int show_available_vars_at(struct debuginfo *dinfo,
874 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900875 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900876{
877 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900878 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900879 struct str_node *node;
880 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900881 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900882 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900883
884 buf = synthesize_perf_probe_point(&pev->point);
885 if (!buf)
886 return -EINVAL;
887 pr_debug("Searching variables at %s\n", buf);
888
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900889 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900890 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900891 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900892 if (!ret) {
893 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900894 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900895 /* Release the old probe_point */
896 clear_perf_probe_point(&tmp);
897 }
898 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900899 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000900 if (ret == 0 || ret == -ENOENT) {
901 pr_err("Failed to find the address of %s\n", buf);
902 ret = -ENOENT;
903 } else
904 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900905 goto end;
906 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000907
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900908 /* Some variables are found */
909 fprintf(stdout, "Available variables at %s\n", buf);
910 for (i = 0; i < ret; i++) {
911 vl = &vls[i];
912 /*
913 * A probe point might be converted to
914 * several trace points.
915 */
916 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
917 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300918 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900919 nvars = 0;
920 if (vl->vars) {
921 strlist__for_each(node, vl->vars) {
922 var = strchr(node->s, '\t') + 1;
923 if (strfilter__compare(_filter, var)) {
924 fprintf(stdout, "\t\t%s\n", node->s);
925 nvars++;
926 }
927 }
928 strlist__delete(vl->vars);
929 }
930 if (nvars == 0)
931 fprintf(stdout, "\t\t(No matched variables)\n");
932 }
933 free(vls);
934end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900935 free(buf);
936 return ret;
937}
938
939/* Show available variables on given probe point */
940int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900941 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900942{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900943 int i, ret = 0;
944 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900945
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900946 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900947 if (ret < 0)
948 return ret;
949
Masami Hiramatsu44225522015-05-08 10:03:28 +0900950 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900951 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000952 ret = -ENOENT;
953 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900954 }
955
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900956 setup_pager();
957
Masami Hiramatsuff741782011-06-27 16:27:39 +0900958 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900959 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900960
961 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000962out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900963 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900964 return ret;
965}
966
Ingo Molnar89fe8082013-09-30 12:07:11 +0200967#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300968
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900969static void debuginfo_cache__exit(void)
970{
971}
972
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000973static int
974find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
975 struct perf_probe_point *pp __maybe_unused,
976 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300977{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000978 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300979}
980
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530981static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900982 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300983{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400984 if (perf_probe_event_need_dwarf(pev)) {
985 pr_warning("Debuginfo-analysis is not supported.\n");
986 return -ENOSYS;
987 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530988
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300989 return 0;
990}
991
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300992int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000993 const char *module __maybe_unused,
994 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300995{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400996 pr_warning("Debuginfo-analysis is not supported.\n");
997 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300998}
999
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001000int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001001 int npevs __maybe_unused,
1002 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001003{
1004 pr_warning("Debuginfo-analysis is not supported.\n");
1005 return -ENOSYS;
1006}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001007#endif
1008
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001009void line_range__clear(struct line_range *lr)
1010{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001011 free(lr->function);
1012 free(lr->file);
1013 free(lr->path);
1014 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001015 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001016 memset(lr, 0, sizeof(*lr));
1017}
1018
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001019int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001020{
1021 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001022 lr->line_list = intlist__new(NULL);
1023 if (!lr->line_list)
1024 return -ENOMEM;
1025 else
1026 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001027}
1028
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001029static int parse_line_num(char **ptr, int *val, const char *what)
1030{
1031 const char *start = *ptr;
1032
1033 errno = 0;
1034 *val = strtol(*ptr, ptr, 0);
1035 if (errno || *ptr == start) {
1036 semantic_error("'%s' is not a valid number.\n", what);
1037 return -EINVAL;
1038 }
1039 return 0;
1040}
1041
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001042/* Check the name is good for event, group or function */
1043static bool is_c_func_name(const char *name)
1044{
1045 if (!isalpha(*name) && *name != '_')
1046 return false;
1047 while (*++name != '\0') {
1048 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1049 return false;
1050 }
1051 return true;
1052}
1053
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001054/*
1055 * Stuff 'lr' according to the line range described by 'arg'.
1056 * The line range syntax is described by:
1057 *
1058 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001059 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001060 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001061int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001062{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001063 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001064 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001065
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001066 if (!name)
1067 return -ENOMEM;
1068
1069 lr->start = 0;
1070 lr->end = INT_MAX;
1071
1072 range = strchr(name, ':');
1073 if (range) {
1074 *range++ = '\0';
1075
1076 err = parse_line_num(&range, &lr->start, "start line");
1077 if (err)
1078 goto err;
1079
1080 if (*range == '+' || *range == '-') {
1081 const char c = *range++;
1082
1083 err = parse_line_num(&range, &lr->end, "end line");
1084 if (err)
1085 goto err;
1086
1087 if (c == '+') {
1088 lr->end += lr->start;
1089 /*
1090 * Adjust the number of lines here.
1091 * If the number of lines == 1, the
1092 * the end of line should be equal to
1093 * the start of line.
1094 */
1095 lr->end--;
1096 }
1097 }
1098
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001099 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001100
1101 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001102 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001103 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001104 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001105 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001107 if (*range != '\0') {
1108 semantic_error("Tailing with invalid str '%s'.\n", range);
1109 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001111 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001112
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001113 file = strchr(name, '@');
1114 if (file) {
1115 *file = '\0';
1116 lr->file = strdup(++file);
1117 if (lr->file == NULL) {
1118 err = -ENOMEM;
1119 goto err;
1120 }
1121 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001122 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001123 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001124 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001125 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001126 else { /* Invalid name */
1127 semantic_error("'%s' is not a valid function name.\n", name);
1128 err = -EINVAL;
1129 goto err;
1130 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001131
1132 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001133err:
1134 free(name);
1135 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001136}
1137
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001138/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001139static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001140{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001141 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001142 char *ptr, *tmp;
1143 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301144 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001145 /*
1146 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001147 * perf probe [EVENT=]SRC[:LN|;PTN]
1148 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001149 *
1150 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001151 */
Wang Nane59d29e2015-04-28 08:46:09 +00001152 if (!arg)
1153 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001154
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001155 ptr = strpbrk(arg, ";=@+%");
1156 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001157 *ptr = '\0';
1158 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001159 if (strchr(arg, ':')) {
1160 semantic_error("Group name is not supported yet.\n");
1161 return -ENOTSUP;
1162 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001163 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001164 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 "follow C symbol-naming rule.\n", arg);
1166 return -EINVAL;
1167 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001168 pev->event = strdup(arg);
1169 if (pev->event == NULL)
1170 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001171 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001172 arg = tmp;
1173 }
1174
Naveen N. Rao3099c022015-04-28 17:35:34 +05301175 /*
1176 * Check arg is function or file name and copy it.
1177 *
1178 * We consider arg to be a file spec if and only if it satisfies
1179 * all of the below criteria::
1180 * - it does not include any of "+@%",
1181 * - it includes one of ":;", and
1182 * - it has a period '.' in the name.
1183 *
1184 * Otherwise, we consider arg to be a function specification.
1185 */
1186 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1187 /* This is a file spec if it includes a '.' before ; or : */
1188 if (memchr(arg, '.', ptr - arg))
1189 file_spec = true;
1190 }
1191
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001192 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001193 if (ptr) {
1194 nc = *ptr;
1195 *ptr++ = '\0';
1196 }
1197
Wang Nan6c6e0242015-08-26 10:57:44 +00001198 if (arg[0] == '\0')
1199 tmp = NULL;
1200 else {
1201 tmp = strdup(arg);
1202 if (tmp == NULL)
1203 return -ENOMEM;
1204 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001205
Naveen N. Rao3099c022015-04-28 17:35:34 +05301206 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001207 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001208 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001209 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210
Wang Nanda15bd92015-08-26 10:57:45 +00001211 /*
1212 * Keep pp->function even if this is absolute address,
1213 * so it can mark whether abs_address is valid.
1214 * Which make 'perf probe lib.bin 0x0' possible.
1215 *
1216 * Note that checking length of tmp is not needed
1217 * because when we access tmp[1] we know tmp[0] is '0',
1218 * so tmp[1] should always valid (but could be '\0').
1219 */
1220 if (tmp && !strncmp(tmp, "0x", 2)) {
1221 pp->abs_address = strtoul(pp->function, &tmp, 0);
1222 if (*tmp != '\0') {
1223 semantic_error("Invalid absolute address.\n");
1224 return -EINVAL;
1225 }
1226 }
1227 }
1228
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001229 /* Parse other options */
1230 while (ptr) {
1231 arg = ptr;
1232 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001233 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001234 pp->lazy_line = strdup(arg);
1235 if (pp->lazy_line == NULL)
1236 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001237 break;
1238 }
1239 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001240 if (ptr) {
1241 nc = *ptr;
1242 *ptr++ = '\0';
1243 }
1244 switch (c) {
1245 case ':': /* Line number */
1246 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001248 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001249 " in line number.\n");
1250 return -EINVAL;
1251 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001252 break;
1253 case '+': /* Byte offset from a symbol */
1254 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001256 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257 " in offset.\n");
1258 return -EINVAL;
1259 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001260 break;
1261 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001262 if (pp->file) {
1263 semantic_error("SRC@SRC is not allowed.\n");
1264 return -EINVAL;
1265 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001266 pp->file = strdup(arg);
1267 if (pp->file == NULL)
1268 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001269 break;
1270 case '%': /* Probe places */
1271 if (strcmp(arg, "return") == 0) {
1272 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273 } else { /* Others not supported yet */
1274 semantic_error("%%%s is not supported.\n", arg);
1275 return -ENOTSUP;
1276 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001277 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001278 default: /* Buggy case */
1279 pr_err("This program has a bug at %s:%d.\n",
1280 __FILE__, __LINE__);
1281 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001282 break;
1283 }
1284 }
1285
1286 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001288 semantic_error("Lazy pattern can't be used with"
1289 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001290 return -EINVAL;
1291 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001292
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001293 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001294 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001295 return -EINVAL;
1296 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001297
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001298 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001299 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001300 return -EINVAL;
1301 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001302
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001303 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001304 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001305 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001306 return -EINVAL;
1307 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001308
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001309 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001310 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001311 return -EINVAL;
1312 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001313
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001314 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001315 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001316 return -EINVAL;
1317 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001318
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001319 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001320 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001321 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 return -EINVAL;
1323 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001324
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001325 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001326 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1327 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001328 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001329}
1330
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001331/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001333{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001334 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001335 struct perf_probe_arg_field **fieldp;
1336
1337 pr_debug("parsing arg: %s into ", str);
1338
Masami Hiramatsu48481932010-04-12 13:16:53 -04001339 tmp = strchr(str, '=');
1340 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001341 arg->name = strndup(str, tmp - str);
1342 if (arg->name == NULL)
1343 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001344 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001345 str = tmp + 1;
1346 }
1347
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001348 tmp = strchr(str, ':');
1349 if (tmp) { /* Type setting */
1350 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001351 arg->type = strdup(tmp + 1);
1352 if (arg->type == NULL)
1353 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001354 pr_debug("type:%s ", arg->type);
1355 }
1356
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001357 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001358 if (!is_c_varname(str) || !tmp) {
1359 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001360 arg->var = strdup(str);
1361 if (arg->var == NULL)
1362 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001363 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001364 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001365 }
1366
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001367 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001368 arg->var = strndup(str, tmp - str);
1369 if (arg->var == NULL)
1370 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001371 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001372 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001373 fieldp = &arg->field;
1374
1375 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001376 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1377 if (*fieldp == NULL)
1378 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001379 if (*tmp == '[') { /* Array */
1380 str = tmp;
1381 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001382 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001383 if (*tmp != ']' || tmp == str + 1) {
1384 semantic_error("Array index must be a"
1385 " number.\n");
1386 return -EINVAL;
1387 }
1388 tmp++;
1389 if (*tmp == '\0')
1390 tmp = NULL;
1391 } else { /* Structure */
1392 if (*tmp == '.') {
1393 str = tmp + 1;
1394 (*fieldp)->ref = false;
1395 } else if (tmp[1] == '>') {
1396 str = tmp + 2;
1397 (*fieldp)->ref = true;
1398 } else {
1399 semantic_error("Argument parse error: %s\n",
1400 str);
1401 return -EINVAL;
1402 }
1403 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001405 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001406 (*fieldp)->name = strndup(str, tmp - str);
1407 if ((*fieldp)->name == NULL)
1408 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001409 if (*str != '[')
1410 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001411 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1412 fieldp = &(*fieldp)->next;
1413 }
1414 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001415 (*fieldp)->name = strdup(str);
1416 if ((*fieldp)->name == NULL)
1417 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001418 if (*str != '[')
1419 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001420 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001421
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001422 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001423 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001424 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001425 if (arg->name == NULL)
1426 return -ENOMEM;
1427 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001428 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001429}
1430
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001431/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001432int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001433{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001434 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001436
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001438 if (!argv) {
1439 pr_debug("Failed to split arguments.\n");
1440 return -ENOMEM;
1441 }
1442 if (argc - 1 > MAX_PROBE_ARGS) {
1443 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1444 ret = -ERANGE;
1445 goto out;
1446 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001447 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448 ret = parse_perf_probe_point(argv[0], pev);
1449 if (ret < 0)
1450 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001451
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001452 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001454 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1455 if (pev->args == NULL) {
1456 ret = -ENOMEM;
1457 goto out;
1458 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1460 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1461 if (ret >= 0 &&
1462 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001463 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 " kretprobe.\n");
1465 ret = -EINVAL;
1466 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001467 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001469 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470
1471 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001472}
1473
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001474/* Return true if this perf_probe_event requires debuginfo */
1475bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001476{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001477 int i;
1478
1479 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1480 return true;
1481
1482 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001483 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001484 return true;
1485
1486 return false;
1487}
1488
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301489/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001490int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001491{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301492 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001493 char pr;
1494 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001495 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001496 int ret, i, argc;
1497 char **argv;
1498
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301499 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001500 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501 if (!argv) {
1502 pr_debug("Failed to split arguments.\n");
1503 return -ENOMEM;
1504 }
1505 if (argc < 2) {
1506 semantic_error("Too few probe arguments.\n");
1507 ret = -ERANGE;
1508 goto out;
1509 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001510
1511 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001512 argv0_str = strdup(argv[0]);
1513 if (argv0_str == NULL) {
1514 ret = -ENOMEM;
1515 goto out;
1516 }
1517 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1518 fmt2_str = strtok_r(NULL, "/", &fmt);
1519 fmt3_str = strtok_r(NULL, " \t", &fmt);
1520 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1521 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522 semantic_error("Failed to parse event name: %s\n", argv[0]);
1523 ret = -EINVAL;
1524 goto out;
1525 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001526 pr = fmt1_str[0];
1527 tev->group = strdup(fmt2_str);
1528 tev->event = strdup(fmt3_str);
1529 if (tev->group == NULL || tev->event == NULL) {
1530 ret = -ENOMEM;
1531 goto out;
1532 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001533 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001534
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001535 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001536
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001537 /* Scan module name(if there), function name and offset */
1538 p = strchr(argv[1], ':');
1539 if (p) {
1540 tp->module = strndup(argv[1], p - argv[1]);
1541 p++;
1542 } else
1543 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001544 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001545 /* only the address started with 0x */
1546 if (fmt1_str[0] == '0') {
1547 /*
1548 * Fix a special case:
1549 * if address == 0, kernel reports something like:
1550 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1551 * Newer kernel may fix that, but we want to
1552 * support old kernel also.
1553 */
1554 if (strcmp(fmt1_str, "0x") == 0) {
1555 if (!argv[2] || strcmp(argv[2], "(null)")) {
1556 ret = -EINVAL;
1557 goto out;
1558 }
1559 tp->address = 0;
1560
1561 free(argv[2]);
1562 for (i = 2; argv[i + 1] != NULL; i++)
1563 argv[i] = argv[i + 1];
1564
1565 argv[i] = NULL;
1566 argc -= 1;
1567 } else
1568 tp->address = strtoul(fmt1_str, NULL, 0);
1569 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001570 /* Only the symbol-based probe has offset */
1571 tp->symbol = strdup(fmt1_str);
1572 if (tp->symbol == NULL) {
1573 ret = -ENOMEM;
1574 goto out;
1575 }
1576 fmt2_str = strtok_r(NULL, "", &fmt);
1577 if (fmt2_str == NULL)
1578 tp->offset = 0;
1579 else
1580 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001581 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001582
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001583 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301584 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001585 if (tev->args == NULL) {
1586 ret = -ENOMEM;
1587 goto out;
1588 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001589 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001590 p = strchr(argv[i + 2], '=');
1591 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001592 *p++ = '\0';
1593 else
1594 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001595 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001596 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001597 tev->args[i].value = strdup(p);
1598 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1599 ret = -ENOMEM;
1600 goto out;
1601 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001602 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001603 ret = 0;
1604out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001605 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001606 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001607 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001608}
1609
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001610/* Compose only probe arg */
1611int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1612{
1613 struct perf_probe_arg_field *field = pa->field;
1614 int ret;
1615 char *tmp = buf;
1616
Masami Hiramatsu48481932010-04-12 13:16:53 -04001617 if (pa->name && pa->var)
1618 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1619 else
1620 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001621 if (ret <= 0)
1622 goto error;
1623 tmp += ret;
1624 len -= ret;
1625
1626 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001627 if (field->name[0] == '[')
1628 ret = e_snprintf(tmp, len, "%s", field->name);
1629 else
1630 ret = e_snprintf(tmp, len, "%s%s",
1631 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001632 if (ret <= 0)
1633 goto error;
1634 tmp += ret;
1635 len -= ret;
1636 field = field->next;
1637 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001638
1639 if (pa->type) {
1640 ret = e_snprintf(tmp, len, ":%s", pa->type);
1641 if (ret <= 0)
1642 goto error;
1643 tmp += ret;
1644 len -= ret;
1645 }
1646
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001647 return tmp - buf;
1648error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001649 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001650 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001651}
1652
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001653/* Compose only probe point (not argument) */
1654static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001655{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001656 char *buf, *tmp;
1657 char offs[32] = "", line[32] = "", file[32] = "";
1658 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001659
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001660 buf = zalloc(MAX_CMDLEN);
1661 if (buf == NULL) {
1662 ret = -ENOMEM;
1663 goto error;
1664 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001665 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001666 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001667 if (ret <= 0)
1668 goto error;
1669 }
1670 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001671 ret = e_snprintf(line, 32, ":%d", pp->line);
1672 if (ret <= 0)
1673 goto error;
1674 }
1675 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001676 tmp = pp->file;
1677 len = strlen(tmp);
1678 if (len > 30) {
1679 tmp = strchr(pp->file + len - 30, '/');
1680 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1681 }
1682 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001683 if (ret <= 0)
1684 goto error;
1685 }
1686
1687 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001688 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1689 offs, pp->retprobe ? "%return" : "", line,
1690 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001691 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001692 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001693 if (ret <= 0)
1694 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001695
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 return buf;
1697error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001698 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001699 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001700 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001701}
1702
1703#if 0
1704char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1705{
1706 char *buf;
1707 int i, len, ret;
1708
1709 buf = synthesize_perf_probe_point(&pev->point);
1710 if (!buf)
1711 return NULL;
1712
1713 len = strlen(buf);
1714 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001715 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716 pev->args[i].name);
1717 if (ret <= 0) {
1718 free(buf);
1719 return NULL;
1720 }
1721 len += ret;
1722 }
1723
1724 return buf;
1725}
1726#endif
1727
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301728static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001729 char **buf, size_t *buflen,
1730 int depth)
1731{
1732 int ret;
1733 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 buflen, depth + 1);
1736 if (depth < 0)
1737 goto out;
1738 }
1739
1740 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1741 if (ret < 0)
1742 depth = ret;
1743 else {
1744 *buf += ret;
1745 *buflen -= ret;
1746 }
1747out:
1748 return depth;
1749
1750}
1751
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301752static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001753 char *buf, size_t buflen)
1754{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301755 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001756 int ret, depth = 0;
1757 char *tmp = buf;
1758
1759 /* Argument name or separator */
1760 if (arg->name)
1761 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1762 else
1763 ret = e_snprintf(buf, buflen, " ");
1764 if (ret < 0)
1765 return ret;
1766 buf += ret;
1767 buflen -= ret;
1768
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001769 /* Special case: @XXX */
1770 if (arg->value[0] == '@' && arg->ref)
1771 ref = ref->next;
1772
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001774 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301775 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001776 &buflen, 1);
1777 if (depth < 0)
1778 return depth;
1779 }
1780
1781 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001782 if (arg->value[0] == '@' && arg->ref)
1783 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1784 arg->ref->offset);
1785 else
1786 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001787 if (ret < 0)
1788 return ret;
1789 buf += ret;
1790 buflen -= ret;
1791
1792 /* Closing */
1793 while (depth--) {
1794 ret = e_snprintf(buf, buflen, ")");
1795 if (ret < 0)
1796 return ret;
1797 buf += ret;
1798 buflen -= ret;
1799 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001800 /* Print argument type */
1801 if (arg->type) {
1802 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1803 if (ret <= 0)
1804 return ret;
1805 buf += ret;
1806 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001807
1808 return buf - tmp;
1809}
1810
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301811char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001812{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301813 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001814 char *buf;
1815 int i, len, ret;
1816
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001817 buf = zalloc(MAX_CMDLEN);
1818 if (buf == NULL)
1819 return NULL;
1820
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001821 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1822 tev->group, tev->event);
1823 if (len <= 0)
1824 goto error;
1825
Wang Nanda15bd92015-08-26 10:57:45 +00001826 /* Uprobes must have tp->module */
1827 if (tev->uprobes && !tp->module)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001828 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001829 /*
1830 * If tp->address == 0, then this point must be a
1831 * absolute address uprobe.
1832 * try_to_find_absolute_address() should have made
1833 * tp->symbol to "0x0".
1834 */
1835 if (tev->uprobes && !tp->address) {
1836 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1837 goto error;
1838 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001839
1840 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301841 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001842 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1843 tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001844 else if (!strncmp(tp->symbol, "0x", 2))
1845 /* Absolute address. See try_to_find_absolute_address() */
1846 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1847 tp->module ?: "", tp->module ? ":" : "",
1848 tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301849 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001850 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301851 tp->module ?: "", tp->module ? ":" : "",
1852 tp->symbol, tp->offset);
1853
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001854 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001856 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001857
1858 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301859 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001860 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001861 if (ret <= 0)
1862 goto error;
1863 len += ret;
1864 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001865
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001867error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001868 free(buf);
1869 return NULL;
1870}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001871
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001872static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1873 struct perf_probe_point *pp,
1874 bool is_kprobe)
1875{
1876 struct symbol *sym = NULL;
1877 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001878 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001879 int ret = -ENOENT;
1880
1881 if (!is_kprobe) {
1882 map = dso__new_map(tp->module);
1883 if (!map)
1884 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001885 sym = map__find_symbol(map, addr, NULL);
1886 } else {
Wang Nane4863672015-08-25 13:27:35 +00001887 if (tp->symbol)
1888 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001889 if (addr) {
1890 addr += tp->offset;
1891 sym = __find_kernel_function(addr, &map);
1892 }
1893 }
1894 if (!sym)
1895 goto out;
1896
1897 pp->retprobe = tp->retprobe;
1898 pp->offset = addr - map->unmap_ip(map, sym->start);
1899 pp->function = strdup(sym->name);
1900 ret = pp->function ? 0 : -ENOMEM;
1901
1902out:
1903 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001904 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001905 }
1906
1907 return ret;
1908}
1909
1910static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001911 struct perf_probe_point *pp,
1912 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001913{
1914 char buf[128];
1915 int ret;
1916
1917 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1918 if (!ret)
1919 return 0;
1920 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1921 if (!ret)
1922 return 0;
1923
1924 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1925
1926 if (tp->symbol) {
1927 pp->function = strdup(tp->symbol);
1928 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001929 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001930 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1931 if (ret < 0)
1932 return ret;
1933 pp->function = strdup(buf);
1934 pp->offset = 0;
1935 }
1936 if (pp->function == NULL)
1937 return -ENOMEM;
1938
1939 pp->retprobe = tp->retprobe;
1940
1941 return 0;
1942}
1943
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301944static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301945 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001946{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001947 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001948 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001949
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001950 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001951 pev->event = strdup(tev->event);
1952 pev->group = strdup(tev->group);
1953 if (pev->event == NULL || pev->group == NULL)
1954 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001955
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001956 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001957 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001958 if (ret < 0)
1959 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001960
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001961 /* Convert trace_arg to probe_arg */
1962 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001963 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1964 if (pev->args == NULL)
1965 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001966 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001967 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001968 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001969 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301970 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001971 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001972 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001973 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001974 if (pev->args[i].name == NULL && ret >= 0)
1975 ret = -ENOMEM;
1976 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001977
1978 if (ret < 0)
1979 clear_perf_probe_event(pev);
1980
1981 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001982}
1983
1984void clear_perf_probe_event(struct perf_probe_event *pev)
1985{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001986 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001987 int i;
1988
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001989 free(pev->event);
1990 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001991 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001992 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001993
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001994 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001995 free(pev->args[i].name);
1996 free(pev->args[i].var);
1997 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001998 field = pev->args[i].field;
1999 while (field) {
2000 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002001 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002002 free(field);
2003 field = next;
2004 }
2005 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002006 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002007 memset(pev, 0, sizeof(*pev));
2008}
2009
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002010void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002011{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302012 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002013 int i;
2014
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002015 free(tev->event);
2016 free(tev->group);
2017 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002018 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002019 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002020 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002021 free(tev->args[i].name);
2022 free(tev->args[i].value);
2023 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002024 ref = tev->args[i].ref;
2025 while (ref) {
2026 next = ref->next;
2027 free(ref);
2028 ref = next;
2029 }
2030 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002031 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002032 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002033}
2034
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002035struct kprobe_blacklist_node {
2036 struct list_head list;
2037 unsigned long start;
2038 unsigned long end;
2039 char *symbol;
2040};
2041
2042static void kprobe_blacklist__delete(struct list_head *blacklist)
2043{
2044 struct kprobe_blacklist_node *node;
2045
2046 while (!list_empty(blacklist)) {
2047 node = list_first_entry(blacklist,
2048 struct kprobe_blacklist_node, list);
2049 list_del(&node->list);
2050 free(node->symbol);
2051 free(node);
2052 }
2053}
2054
2055static int kprobe_blacklist__load(struct list_head *blacklist)
2056{
2057 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002058 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002059 char buf[PATH_MAX], *p;
2060 FILE *fp;
2061 int ret;
2062
2063 if (__debugfs == NULL)
2064 return -ENOTSUP;
2065
2066 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2067 if (ret < 0)
2068 return ret;
2069
2070 fp = fopen(buf, "r");
2071 if (!fp)
2072 return -errno;
2073
2074 ret = 0;
2075 while (fgets(buf, PATH_MAX, fp)) {
2076 node = zalloc(sizeof(*node));
2077 if (!node) {
2078 ret = -ENOMEM;
2079 break;
2080 }
2081 INIT_LIST_HEAD(&node->list);
2082 list_add_tail(&node->list, blacklist);
2083 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2084 ret = -EINVAL;
2085 break;
2086 }
2087 p = strchr(buf, '\t');
2088 if (p) {
2089 p++;
2090 if (p[strlen(p) - 1] == '\n')
2091 p[strlen(p) - 1] = '\0';
2092 } else
2093 p = (char *)"unknown";
2094 node->symbol = strdup(p);
2095 if (!node->symbol) {
2096 ret = -ENOMEM;
2097 break;
2098 }
2099 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2100 node->start, node->end, node->symbol);
2101 ret++;
2102 }
2103 if (ret < 0)
2104 kprobe_blacklist__delete(blacklist);
2105 fclose(fp);
2106
2107 return ret;
2108}
2109
2110static struct kprobe_blacklist_node *
2111kprobe_blacklist__find_by_address(struct list_head *blacklist,
2112 unsigned long address)
2113{
2114 struct kprobe_blacklist_node *node;
2115
2116 list_for_each_entry(node, blacklist, list) {
2117 if (node->start <= address && address <= node->end)
2118 return node;
2119 }
2120
2121 return NULL;
2122}
2123
Masami Hiramatsub0312202015-06-16 20:50:55 +09002124static LIST_HEAD(kprobe_blacklist);
2125
2126static void kprobe_blacklist__init(void)
2127{
2128 if (!list_empty(&kprobe_blacklist))
2129 return;
2130
2131 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2132 pr_debug("No kprobe blacklist support, ignored\n");
2133}
2134
2135static void kprobe_blacklist__release(void)
2136{
2137 kprobe_blacklist__delete(&kprobe_blacklist);
2138}
2139
2140static bool kprobe_blacklist__listed(unsigned long address)
2141{
2142 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2143}
2144
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002145static int perf_probe_event__sprintf(const char *group, const char *event,
2146 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002147 const char *module,
2148 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002149{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002150 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002151 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002152 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002153
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002154 /* Synthesize only event probe point */
2155 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002156 if (!place)
2157 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002158
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002159 ret = e_snprintf(buf, 128, "%s:%s", group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002160 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002161 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002163 strbuf_addf(result, " %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002164 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002165 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002166
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002167 if (pev->nargs > 0) {
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002168 strbuf_addstr(result, " with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002169 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002170 ret = synthesize_perf_probe_arg(&pev->args[i],
2171 buf, 128);
2172 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002173 goto out;
2174 strbuf_addf(result, " %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002175 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002176 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002177 strbuf_addch(result, ')');
2178out:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002179 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002180 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002181}
2182
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002183/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002184int show_perf_probe_event(const char *group, const char *event,
2185 struct perf_probe_event *pev,
2186 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002187{
2188 struct strbuf buf = STRBUF_INIT;
2189 int ret;
2190
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002191 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002192 if (ret >= 0) {
2193 if (use_stdout)
2194 printf("%s\n", buf.buf);
2195 else
2196 pr_info("%s\n", buf.buf);
2197 }
2198 strbuf_release(&buf);
2199
2200 return ret;
2201}
2202
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002203static bool filter_probe_trace_event(struct probe_trace_event *tev,
2204 struct strfilter *filter)
2205{
2206 char tmp[128];
2207
2208 /* At first, check the event name itself */
2209 if (strfilter__compare(filter, tev->event))
2210 return true;
2211
2212 /* Next, check the combination of name and group */
2213 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2214 return false;
2215 return strfilter__compare(filter, tmp);
2216}
2217
2218static int __show_perf_probe_events(int fd, bool is_kprobe,
2219 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002220{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302221 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302222 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002223 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002224 struct strlist *rawlist;
2225 struct str_node *ent;
2226
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002227 memset(&tev, 0, sizeof(tev));
2228 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002229
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002230 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002231 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002232 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002233
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002234 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302235 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002236 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002237 if (!filter_probe_trace_event(&tev, filter))
2238 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302239 ret = convert_to_perf_probe_event(&tev, &pev,
2240 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002241 if (ret < 0)
2242 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002243 ret = show_perf_probe_event(pev.group, pev.event,
2244 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002245 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002246 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002247next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002248 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302249 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002250 if (ret < 0)
2251 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002252 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002253 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002254 /* Cleanup cached debuginfo if needed */
2255 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002256
2257 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002258}
2259
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302260/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002261int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302262{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002263 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302264
2265 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302266
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002267 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302268 if (ret < 0)
2269 return ret;
2270
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002271 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2272 if (ret < 0)
2273 return ret;
2274
2275 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002276 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002277 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002278 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002279 if (kp_fd > 0)
2280 close(kp_fd);
2281 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002282 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002283 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302284
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002285 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002286}
2287
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288static int get_new_event_name(char *buf, size_t len, const char *base,
2289 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002290{
2291 int i, ret;
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002292 char *p;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002293
Naveen N. Rao3099c022015-04-28 17:35:34 +05302294 if (*base == '.')
2295 base++;
2296
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002297 /* Try no suffix */
2298 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002299 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002300 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002301 return ret;
2302 }
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002303 /* Cut off the postfixes (e.g. .const, .isra)*/
2304 p = strchr(buf, '.');
2305 if (p && p != buf)
2306 *p = '\0';
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002307 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002308 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002309
Masami Hiramatsud761b082009-12-15 10:32:25 -05002310 if (!allow_suffix) {
2311 pr_warning("Error: event \"%s\" already exists. "
2312 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002313 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002314 }
2315
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002316 /* Try to add suffix */
2317 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002318 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002319 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002320 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002321 return ret;
2322 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002323 if (!strlist__has_entry(namelist, buf))
2324 break;
2325 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002326 if (i == MAX_EVENT_INDEX) {
2327 pr_warning("Too many events are on the same function.\n");
2328 ret = -ERANGE;
2329 }
2330
2331 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002332}
2333
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002334/* Warn if the current kernel's uprobe implementation is old */
2335static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2336{
2337 int i;
2338 char *buf = synthesize_probe_trace_command(tev);
2339
2340 /* Old uprobe event doesn't support memory dereference */
2341 if (!tev->uprobes || tev->nargs == 0 || !buf)
2342 goto out;
2343
2344 for (i = 0; i < tev->nargs; i++)
2345 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2346 pr_warning("Please upgrade your kernel to at least "
2347 "3.14 to have access to feature %s\n",
2348 tev->args[i].value);
2349 break;
2350 }
2351out:
2352 free(buf);
2353}
2354
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002355/* Set new name from original perf_probe_event and namelist */
2356static int probe_trace_event__set_name(struct probe_trace_event *tev,
2357 struct perf_probe_event *pev,
2358 struct strlist *namelist,
2359 bool allow_suffix)
2360{
2361 const char *event, *group;
2362 char buf[64];
2363 int ret;
2364
2365 if (pev->event)
2366 event = pev->event;
2367 else
Wang Nanda15bd92015-08-26 10:57:45 +00002368 if (pev->point.function &&
2369 (strncmp(pev->point.function, "0x", 2) != 0) &&
2370 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002371 event = pev->point.function;
2372 else
2373 event = tev->point.realname;
2374 if (pev->group)
2375 group = pev->group;
2376 else
2377 group = PERFPROBE_GROUP;
2378
2379 /* Get an unused new event name */
2380 ret = get_new_event_name(buf, 64, event,
2381 namelist, allow_suffix);
2382 if (ret < 0)
2383 return ret;
2384
2385 event = buf;
2386
2387 tev->event = strdup(event);
2388 tev->group = strdup(group);
2389 if (tev->event == NULL || tev->group == NULL)
2390 return -ENOMEM;
2391
2392 /* Add added event name to namelist */
2393 strlist__add(namelist, event);
2394 return 0;
2395}
2396
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302397static int __add_probe_trace_events(struct perf_probe_event *pev,
2398 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002399 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002400{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002401 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302402 struct probe_trace_event *tev = 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 Hiramatsu02b95da2010-04-12 13:17:56 -04002418 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002419 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002420 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002421 if (!tev->point.symbol)
2422 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002423
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002424 /* Set new name for tev (and update namelist) */
2425 ret = probe_trace_event__set_name(tev, pev, namelist,
2426 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 if (ret < 0)
2428 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002429
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002430 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002431 if (ret < 0)
2432 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002433
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434 /*
2435 * Probes after the first probe which comes from same
2436 * user input are always allowed to add suffix, because
2437 * there might be several addresses corresponding to
2438 * one code line.
2439 */
2440 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002441 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002442 if (ret == -EINVAL && pev->uprobes)
2443 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002444
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002445 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002446close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002447 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002448 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002449}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002450
Wang Nan6bb536c2015-05-29 09:45:47 +00002451static int find_probe_functions(struct map *map, char *name,
2452 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002453{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002454 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002455 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002456 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002457
Wang Nan75e4a2a2015-05-15 12:14:44 +00002458 if (map__load(map, NULL) < 0)
2459 return 0;
2460
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002461 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002462 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002463 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002464 if (syms && found < probe_conf.max_probes)
2465 syms[found - 1] = sym;
2466 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002467 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002468
2469 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002470}
2471
2472#define strdup_or_goto(str, label) \
2473 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2474
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302475void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2476 struct probe_trace_event *tev __maybe_unused,
2477 struct map *map __maybe_unused) { }
2478
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002479/*
2480 * Find probe function addresses from map.
2481 * Return an error or the number of found probe_trace_event
2482 */
2483static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002484 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002485{
2486 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002487 struct ref_reloc_sym *reloc_sym = NULL;
2488 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002489 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002490 struct probe_trace_event *tev;
2491 struct perf_probe_point *pp = &pev->point;
2492 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002493 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002494 int ret, i, j, skipped = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002495
Masami Hiramatsu44225522015-05-08 10:03:28 +09002496 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002497 if (!map) {
2498 ret = -EINVAL;
2499 goto out;
2500 }
2501
Wang Nan6bb536c2015-05-29 09:45:47 +00002502 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2503 if (!syms) {
2504 ret = -ENOMEM;
2505 goto out;
2506 }
2507
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002508 /*
2509 * Load matched symbols: Since the different local symbols may have
2510 * same name but different addresses, this lists all the symbols.
2511 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002512 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002513 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002514 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002515 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002516 ret = -ENOENT;
2517 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002518 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002519 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002520 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002521 ret = -E2BIG;
2522 goto out;
2523 }
2524
Namhyung Kim25dd9172015-01-14 20:18:08 +09002525 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002526 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002527 if (!reloc_sym) {
2528 pr_warning("Relocated base symbol is not found!\n");
2529 ret = -EINVAL;
2530 goto out;
2531 }
2532 }
2533
2534 /* Setup result trace-probe-events */
2535 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2536 if (!*tevs) {
2537 ret = -ENOMEM;
2538 goto out;
2539 }
2540
2541 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002542
Wang Nan6bb536c2015-05-29 09:45:47 +00002543 for (j = 0; j < num_matched_functions; j++) {
2544 sym = syms[j];
2545
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002546 tev = (*tevs) + ret;
2547 tp = &tev->point;
2548 if (ret == num_matched_functions) {
2549 pr_warning("Too many symbols are listed. Skip it.\n");
2550 break;
2551 }
2552 ret++;
2553
2554 if (pp->offset > sym->end - sym->start) {
2555 pr_warning("Offset %ld is bigger than the size of %s\n",
2556 pp->offset, sym->name);
2557 ret = -ENOENT;
2558 goto err_out;
2559 }
2560 /* Add one probe point */
2561 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002562 /* If we found a wrong one, mark it by NULL symbol */
2563 if (!pev->uprobes &&
2564 kprobe_warn_out_range(sym->name, tp->address)) {
2565 tp->symbol = NULL; /* Skip it */
2566 skipped++;
2567 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002568 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2569 tp->offset = tp->address - reloc_sym->addr;
2570 } else {
2571 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2572 tp->offset = pp->offset;
2573 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002574 tp->realname = strdup_or_goto(sym->name, nomem_out);
2575
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002576 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002577 if (pev->target)
2578 tev->point.module = strdup_or_goto(pev->target,
2579 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002580 tev->uprobes = pev->uprobes;
2581 tev->nargs = pev->nargs;
2582 if (tev->nargs) {
2583 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2584 tev->nargs);
2585 if (tev->args == NULL)
2586 goto nomem_out;
2587 }
2588 for (i = 0; i < tev->nargs; i++) {
2589 if (pev->args[i].name)
2590 tev->args[i].name =
2591 strdup_or_goto(pev->args[i].name,
2592 nomem_out);
2593
2594 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2595 nomem_out);
2596 if (pev->args[i].type)
2597 tev->args[i].type =
2598 strdup_or_goto(pev->args[i].type,
2599 nomem_out);
2600 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302601 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002602 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002603 if (ret == skipped) {
2604 ret = -ENOENT;
2605 goto err_out;
2606 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002607
2608out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002609 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002610 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002611 return ret;
2612
2613nomem_out:
2614 ret = -ENOMEM;
2615err_out:
2616 clear_probe_trace_events(*tevs, num_matched_functions);
2617 zfree(tevs);
2618 goto out;
2619}
2620
Wang Nanda15bd92015-08-26 10:57:45 +00002621static int try_to_find_absolute_address(struct perf_probe_event *pev,
2622 struct probe_trace_event **tevs)
2623{
2624 struct perf_probe_point *pp = &pev->point;
2625 struct probe_trace_event *tev;
2626 struct probe_trace_point *tp;
2627 int i, err;
2628
2629 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2630 return -EINVAL;
2631 if (perf_probe_event_need_dwarf(pev))
2632 return -EINVAL;
2633
2634 /*
2635 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2636 * absolute address.
2637 *
2638 * Only one tev can be generated by this.
2639 */
2640 *tevs = zalloc(sizeof(*tev));
2641 if (!*tevs)
2642 return -ENOMEM;
2643
2644 tev = *tevs;
2645 tp = &tev->point;
2646
2647 /*
2648 * Don't use tp->offset, use address directly, because
2649 * in synthesize_probe_trace_command() address cannot be
2650 * zero.
2651 */
2652 tp->address = pev->point.abs_address;
2653 tp->retprobe = pp->retprobe;
2654 tev->uprobes = pev->uprobes;
2655
2656 err = -ENOMEM;
2657 /*
2658 * Give it a '0x' leading symbol name.
2659 * In __add_probe_trace_events, a NULL symbol is interpreted as
2660 * invalud.
2661 */
2662 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2663 goto errout;
2664
2665 /* For kprobe, check range */
2666 if ((!tev->uprobes) &&
2667 (kprobe_warn_out_range(tev->point.symbol,
2668 tev->point.address))) {
2669 err = -EACCES;
2670 goto errout;
2671 }
2672
2673 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2674 goto errout;
2675
2676 if (pev->target) {
2677 tp->module = strdup(pev->target);
2678 if (!tp->module)
2679 goto errout;
2680 }
2681
2682 if (tev->group) {
2683 tev->group = strdup(pev->group);
2684 if (!tev->group)
2685 goto errout;
2686 }
2687
2688 if (pev->event) {
2689 tev->event = strdup(pev->event);
2690 if (!tev->event)
2691 goto errout;
2692 }
2693
2694 tev->nargs = pev->nargs;
2695 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2696 if (!tev->args) {
2697 err = -ENOMEM;
2698 goto errout;
2699 }
2700 for (i = 0; i < tev->nargs; i++)
2701 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2702
2703 return 1;
2704
2705errout:
2706 if (*tevs) {
2707 clear_probe_trace_events(*tevs, 1);
2708 *tevs = NULL;
2709 }
2710 return err;
2711}
2712
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302713bool __weak arch__prefers_symtab(void) { return false; }
2714
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302715static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002716 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002717{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002718 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002719
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002720 if (pev->uprobes && !pev->group) {
2721 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002722 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002723 if (ret != 0) {
2724 pr_warning("Failed to make a group name.\n");
2725 return ret;
2726 }
2727 }
2728
Wang Nanda15bd92015-08-26 10:57:45 +00002729 ret = try_to_find_absolute_address(pev, tevs);
2730 if (ret > 0)
2731 return ret;
2732
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302733 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002734 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302735 if (ret > 0)
2736 return ret; /* Found in symbol table */
2737 }
2738
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002739 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002740 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002741 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002742 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002743
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002744 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002745}
2746
Wang Nan12fae5e2015-09-04 21:16:00 +09002747int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002748{
Namhyung Kim844dffa2015-09-04 21:15:59 +09002749 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002750
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002751 /* Loop 1: convert all events */
2752 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09002753 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09002754 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09002755 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002756 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09002757 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002758 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002759 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09002760 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002761 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002762 /* This just release blacklist only if allocated */
2763 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002764
Namhyung Kim844dffa2015-09-04 21:15:59 +09002765 return 0;
2766}
2767
Wang Nan12fae5e2015-09-04 21:16:00 +09002768int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002769{
2770 int i, ret = 0;
2771
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002772 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002773 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002774 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2775 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002776 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002777 if (ret < 0)
2778 break;
2779 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002780 return ret;
2781}
2782
Wang Nan12fae5e2015-09-04 21:16:00 +09002783void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002784{
2785 int i, j;
2786
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002787 /* Loop 3: cleanup and free trace events */
2788 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002789 for (j = 0; j < pevs[i].ntevs; j++)
2790 clear_probe_trace_event(&pevs[i].tevs[j]);
2791 zfree(&pevs[i].tevs);
2792 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09002793 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002794 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002795}
2796
2797int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2798{
2799 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09002800
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002801 ret = init_probe_symbol_maps(pevs->uprobes);
2802 if (ret < 0)
2803 return ret;
2804
Wang Nan12fae5e2015-09-04 21:16:00 +09002805 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002806 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09002807 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002808
Wang Nan12fae5e2015-09-04 21:16:00 +09002809 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002810
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002811 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002812 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002813}
2814
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002815int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002816{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002817 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002818 char *str = strfilter__string(filter);
2819
2820 if (!str)
2821 return -EINVAL;
2822
Masami Hiramatsufa282442009-12-08 17:03:23 -05002823 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002824 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2825 if (ret < 0)
2826 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302827
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002828 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002829 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302830 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002831
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002832 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002833 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002834 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002835 goto error;
2836 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002837 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302838
2839error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002840 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302841 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002842 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302843 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002844out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002845 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002846
2847 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002848}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302849
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002850/* TODO: don't use a global variable for filter ... */
2851static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002852
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002854 * If a symbol corresponds to a function with global binding and
2855 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002856 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002857static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002858 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002859{
Namhyung Kime578da32015-03-06 16:31:29 +09002860 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002861 return 0;
2862 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002863}
2864
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002865int show_available_funcs(const char *target, struct strfilter *_filter,
2866 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002867{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002868 struct map *map;
2869 int ret;
2870
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002871 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002872 if (ret < 0)
2873 return ret;
2874
2875 /* Get a symbol map */
2876 if (user)
2877 map = dso__new_map(target);
2878 else
2879 map = kernel_get_module_map(target);
2880 if (!map) {
2881 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002882 return -EINVAL;
2883 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002884
2885 /* Load symbols with given filter */
2886 available_func_filter = _filter;
2887 if (map__load(map, filter_available_functions)) {
2888 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2889 goto end;
2890 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002891 if (!dso__sorted_by_name(map->dso, map->type))
2892 dso__sort_by_name(map->dso, map->type);
2893
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002894 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302895 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002896 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2897end:
2898 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002899 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002900 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002901 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302902
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002903 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302904}
2905
Wang Nanda15bd92015-08-26 10:57:45 +00002906int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2907 struct perf_probe_arg *pvar)
2908{
2909 tvar->value = strdup(pvar->var);
2910 if (tvar->value == NULL)
2911 return -ENOMEM;
2912 if (pvar->type) {
2913 tvar->type = strdup(pvar->type);
2914 if (tvar->type == NULL)
2915 return -ENOMEM;
2916 }
2917 if (pvar->name) {
2918 tvar->name = strdup(pvar->name);
2919 if (tvar->name == NULL)
2920 return -ENOMEM;
2921 } else
2922 tvar->name = NULL;
2923 return 0;
2924}