blob: bc2eb7cda2d16c1f1738f237e186228d0b8615bb [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 Meloa5e813c2015-09-30 11:54:04 -0300129 struct map *map = machine__kernel_map(host_machine);
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
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900140static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
141 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000142{
143 struct ref_reloc_sym *reloc_sym;
144 struct symbol *sym;
145 struct map *map;
146
147 /* ref_reloc_sym is just a label. Need a special fix*/
148 reloc_sym = kernel_get_ref_reloc_sym();
149 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900150 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000151 else {
152 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900153 if (!sym)
154 return -ENOENT;
155 *addr = map->unmap_ip(map, sym->start) -
156 ((reloc) ? 0 : map->reloc) -
157 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000158 }
159 return 0;
160}
161
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900162static struct map *kernel_get_module_map(const char *module)
163{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000164 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300165 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300166 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900167
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168 /* A file path -- this is an offline module */
169 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300170 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900171
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900172 if (!module)
173 module = "kernel";
174
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300175 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900176 if (strncmp(pos->dso->short_name + 1, module,
177 pos->dso->short_name_len - 2) == 0) {
178 return pos;
179 }
180 }
181 return NULL;
182}
183
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900184static struct map *get_target_map(const char *target, bool user)
185{
186 /* Init maps of given executable or kernel */
187 if (user)
188 return dso__new_map(target);
189 else
190 return kernel_get_module_map(target);
191}
192
193static void put_target_map(struct map *map, bool user)
194{
195 if (map && user) {
196 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300197 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900198 }
199}
200
201
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000202static int convert_exec_to_group(const char *exec, char **result)
203{
204 char *ptr1, *ptr2, *exec_copy;
205 char buf[64];
206 int ret;
207
208 exec_copy = strdup(exec);
209 if (!exec_copy)
210 return -ENOMEM;
211
212 ptr1 = basename(exec_copy);
213 if (!ptr1) {
214 ret = -EINVAL;
215 goto out;
216 }
217
218 ptr2 = strpbrk(ptr1, "-._");
219 if (ptr2)
220 *ptr2 = '\0';
221 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
222 if (ret < 0)
223 goto out;
224
225 *result = strdup(buf);
226 ret = *result ? 0 : -ENOMEM;
227
228out:
229 free(exec_copy);
230 return ret;
231}
232
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900233static void clear_perf_probe_point(struct perf_probe_point *pp)
234{
235 free(pp->file);
236 free(pp->function);
237 free(pp->lazy_line);
238}
239
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000240static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
241{
242 int i;
243
244 for (i = 0; i < ntevs; i++)
245 clear_probe_trace_event(tevs + i);
246}
247
Masami Hiramatsub0312202015-06-16 20:50:55 +0900248static bool kprobe_blacklist__listed(unsigned long address);
249static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
250{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900251 u64 etext_addr = 0;
252 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000253
Masami Hiramatsub0312202015-06-16 20:50:55 +0900254 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900255 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
256 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000257
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900258 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900259 pr_warning("%s is out of .text, skip it.\n", symbol);
260 else if (kprobe_blacklist__listed(address))
261 pr_warning("%s is blacklisted function, skip it.\n", symbol);
262 else
263 return false;
264
265 return true;
266}
267
Ingo Molnar89fe8082013-09-30 12:07:11 +0200268#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000269
270static int kernel_get_module_dso(const char *module, struct dso **pdso)
271{
272 struct dso *dso;
273 struct map *map;
274 const char *vmlinux_name;
275 int ret = 0;
276
277 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300278 char module_name[128];
279
280 snprintf(module_name, sizeof(module_name), "[%s]", module);
281 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
282 if (map) {
283 dso = map->dso;
284 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000285 }
286 pr_debug("Failed to find module %s.\n", module);
287 return -ENOENT;
288 }
289
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300290 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000291 dso = map->dso;
292
293 vmlinux_name = symbol_conf.vmlinux_name;
294 dso->load_errno = 0;
295 if (vmlinux_name)
296 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
297 else
298 ret = dso__load_vmlinux_path(dso, map, NULL);
299found:
300 *pdso = dso;
301 return ret;
302}
303
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900304/*
305 * Some binaries like glibc have special symbols which are on the symbol
306 * table, but not in the debuginfo. If we can find the address of the
307 * symbol from map, we can translate the address back to the probe point.
308 */
309static int find_alternative_probe_point(struct debuginfo *dinfo,
310 struct perf_probe_point *pp,
311 struct perf_probe_point *result,
312 const char *target, bool uprobes)
313{
314 struct map *map = NULL;
315 struct symbol *sym;
316 u64 address = 0;
317 int ret = -ENOENT;
318
319 /* This can work only for function-name based one */
320 if (!pp->function || pp->file)
321 return -ENOTSUP;
322
323 map = get_target_map(target, uprobes);
324 if (!map)
325 return -EINVAL;
326
327 /* Find the address of given function */
328 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900329 if (uprobes)
330 address = sym->start;
331 else
332 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900333 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900334 }
335 if (!address) {
336 ret = -ENOENT;
337 goto out;
338 }
Wang Nanf6c15622015-04-08 02:14:34 +0000339 pr_debug("Symbol %s address found : %" PRIx64 "\n",
340 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900341
342 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
343 result);
344 if (ret <= 0)
345 ret = (!ret) ? -ENOENT : ret;
346 else {
347 result->offset += pp->offset;
348 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800349 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900350 ret = 0;
351 }
352
353out:
354 put_target_map(map, uprobes);
355 return ret;
356
357}
358
359static int get_alternative_probe_event(struct debuginfo *dinfo,
360 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900361 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900362{
363 int ret;
364
365 memcpy(tmp, &pev->point, sizeof(*tmp));
366 memset(&pev->point, 0, sizeof(pev->point));
367 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900368 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900369 if (ret < 0)
370 memcpy(&pev->point, tmp, sizeof(*tmp));
371
372 return ret;
373}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000374
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900375static int get_alternative_line_range(struct debuginfo *dinfo,
376 struct line_range *lr,
377 const char *target, bool user)
378{
David Ahern6d4a4892015-03-11 10:36:20 -0400379 struct perf_probe_point pp = { .function = lr->function,
380 .file = lr->file,
381 .line = lr->start };
382 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900383 int ret, len = 0;
384
David Ahern6d4a4892015-03-11 10:36:20 -0400385 memset(&result, 0, sizeof(result));
386
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900387 if (lr->end != INT_MAX)
388 len = lr->end - lr->start;
389 ret = find_alternative_probe_point(dinfo, &pp, &result,
390 target, user);
391 if (!ret) {
392 lr->function = result.function;
393 lr->file = result.file;
394 lr->start = result.line;
395 if (lr->end != INT_MAX)
396 lr->end = lr->start + len;
397 clear_perf_probe_point(&pp);
398 }
399 return ret;
400}
401
Masami Hiramatsuff741782011-06-27 16:27:39 +0900402/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000403static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900404{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000405 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900406 char reason[STRERR_BUFSIZE];
407 struct debuginfo *ret = NULL;
408 struct dso *dso = NULL;
409 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900410
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000411 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900412 err = kernel_get_module_dso(module, &dso);
413 if (err < 0) {
414 if (!dso || dso->load_errno == 0) {
415 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
416 strcpy(reason, "(unknown)");
417 } else
418 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000419 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900420 pr_err("Failed to find the path for %s: %s\n",
421 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900422 return NULL;
423 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900424 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900425 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000426 ret = debuginfo__new(path);
427 if (!ret && !silent) {
428 pr_warning("The %s file has no debug information.\n", path);
429 if (!module || !strtailcmp(path, ".ko"))
430 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
431 else
432 pr_warning("Rebuild with -g, ");
433 pr_warning("or install an appropriate debuginfo package.\n");
434 }
435 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400436}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300437
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900438/* For caching the last debuginfo */
439static struct debuginfo *debuginfo_cache;
440static char *debuginfo_cache_path;
441
442static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
443{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900444 const char *path = module;
445
446 /* If the module is NULL, it should be the kernel. */
447 if (!module)
448 path = "kernel";
449
450 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900451 goto out;
452
453 /* Copy module path */
454 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900455 debuginfo_cache_path = strdup(path);
456 if (!debuginfo_cache_path) {
457 debuginfo__delete(debuginfo_cache);
458 debuginfo_cache = NULL;
459 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900460 }
461
462 debuginfo_cache = open_debuginfo(module, silent);
463 if (!debuginfo_cache)
464 zfree(&debuginfo_cache_path);
465out:
466 return debuginfo_cache;
467}
468
469static void debuginfo_cache__exit(void)
470{
471 debuginfo__delete(debuginfo_cache);
472 debuginfo_cache = NULL;
473 zfree(&debuginfo_cache_path);
474}
475
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000476
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000477static int get_text_start_address(const char *exec, unsigned long *address)
478{
479 Elf *elf;
480 GElf_Ehdr ehdr;
481 GElf_Shdr shdr;
482 int fd, ret = -ENOENT;
483
484 fd = open(exec, O_RDONLY);
485 if (fd < 0)
486 return -errno;
487
488 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900489 if (elf == NULL) {
490 ret = -EINVAL;
491 goto out_close;
492 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000493
494 if (gelf_getehdr(elf, &ehdr) == NULL)
495 goto out;
496
497 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
498 goto out;
499
500 *address = shdr.sh_addr - shdr.sh_offset;
501 ret = 0;
502out:
503 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900504out_close:
505 close(fd);
506
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000507 return ret;
508}
509
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000510/*
511 * Convert trace point to probe point with debuginfo
512 */
513static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
514 struct perf_probe_point *pp,
515 bool is_kprobe)
516{
517 struct debuginfo *dinfo = NULL;
518 unsigned long stext = 0;
519 u64 addr = tp->address;
520 int ret = -ENOENT;
521
522 /* convert the address to dwarf address */
523 if (!is_kprobe) {
524 if (!addr) {
525 ret = -EINVAL;
526 goto error;
527 }
528 ret = get_text_start_address(tp->module, &stext);
529 if (ret < 0)
530 goto error;
531 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000532 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900533 /* If the module is given, this returns relative address */
534 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
535 false, !!tp->module);
536 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000537 goto error;
538 addr += tp->offset;
539 }
540
541 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
542 tp->module ? : "kernel");
543
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900544 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
545 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000546 ret = debuginfo__find_probe_point(dinfo,
547 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900548 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000549 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000550
551 if (ret > 0) {
552 pp->retprobe = tp->retprobe;
553 return 0;
554 }
555error:
556 pr_debug("Failed to find corresponding probes from debuginfo.\n");
557 return ret ? : -ENOENT;
558}
559
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000560static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
561 int ntevs, const char *exec)
562{
563 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000564 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000565
566 if (!exec)
567 return 0;
568
569 ret = get_text_start_address(exec, &stext);
570 if (ret < 0)
571 return ret;
572
573 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000574 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000575 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000576 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000577 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000578 ret = -ENOMEM;
579 break;
580 }
581 tevs[i].uprobes = true;
582 }
583
584 return ret;
585}
586
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900587static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
588 int ntevs, const char *module)
589{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900590 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530591 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900592
593 if (!module)
594 return 0;
595
Ravi Bangoria63a29612016-04-26 19:55:41 +0530596 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900597
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900598 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530599 tevs[i].point.module =
600 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900601 if (!tevs[i].point.module) {
602 ret = -ENOMEM;
603 break;
604 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900605 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900606
Ravi Bangoria63a29612016-04-26 19:55:41 +0530607 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900608 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900609}
610
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000611/* Post processing the probe events */
612static int post_process_probe_trace_events(struct probe_trace_event *tevs,
613 int ntevs, const char *module,
614 bool uprobe)
615{
616 struct ref_reloc_sym *reloc_sym;
617 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900618 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000619
620 if (uprobe)
621 return add_exec_to_probe_trace_events(tevs, ntevs, module);
622
623 /* Note that currently ref_reloc_sym based probe is not for drivers */
624 if (module)
625 return add_module_to_probe_trace_events(tevs, ntevs, module);
626
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000627 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000628 if (!reloc_sym) {
629 pr_warning("Relocated base symbol is not found!\n");
630 return -EINVAL;
631 }
632
633 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900634 if (!tevs[i].point.address || tevs[i].point.retprobe)
635 continue;
636 /* If we found a wrong one, mark it by NULL symbol */
637 if (kprobe_warn_out_range(tevs[i].point.symbol,
638 tevs[i].point.address)) {
639 tmp = NULL;
640 skipped++;
641 } else {
642 tmp = strdup(reloc_sym->name);
643 if (!tmp)
644 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000645 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900646 /* If we have no realname, use symbol for it */
647 if (!tevs[i].point.realname)
648 tevs[i].point.realname = tevs[i].point.symbol;
649 else
650 free(tevs[i].point.symbol);
651 tevs[i].point.symbol = tmp;
652 tevs[i].point.offset = tevs[i].point.address -
653 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000654 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900655 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000656}
657
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300658/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530659static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900660 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300661{
662 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900663 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530664 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900665 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300666
Masami Hiramatsu44225522015-05-08 10:03:28 +0900667 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900668 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000669 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900670 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900671 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300672 return 0;
673 }
674
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000675 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900676 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900677 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900678
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900679 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900680 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900681 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900682 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900683 /*
684 * Write back to the original probe_event for
685 * setting appropriate (user given) event name
686 */
687 clear_perf_probe_point(&pev->point);
688 memcpy(&pev->point, &tmp, sizeof(tmp));
689 }
690 }
691
Masami Hiramatsuff741782011-06-27 16:27:39 +0900692 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300693
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400694 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000695 pr_debug("Found %d probe_trace_events.\n", ntevs);
696 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900697 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900698 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000699 clear_probe_trace_events(*tevs, ntevs);
700 zfree(tevs);
701 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900702 if (ret != ntevs)
703 return ret < 0 ? ret : ntevs;
704 ntevs = 0;
705 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400706 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300707
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400708 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900709 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400710 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900711 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400712 }
713 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400714 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000715 if (ntevs < 0) {
716 if (ntevs == -EBADF)
717 pr_warning("Warning: No dwarf info found in the vmlinux - "
718 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400719 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900720 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400721 return 0;
722 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300723 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400724 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725}
726
727#define LINEBUF_SIZE 256
728#define NR_ADDITIONAL_LINES 2
729
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100730static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000732 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100733 const char *color = show_num ? "" : PERF_COLOR_BLUE;
734 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300735
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100736 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300737 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
738 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100739 if (skip)
740 continue;
741 if (!prefix) {
742 prefix = show_num ? "%7d " : " ";
743 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300744 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100745 color_fprintf(stdout, color, "%s", buf);
746
747 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400748
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100749 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300750error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100751 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000752 pr_warning("File read error: %s\n",
753 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100754 return -1;
755 }
756 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300757}
758
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100759static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
760{
761 int rv = __show_one_line(fp, l, skip, show_num);
762 if (rv == 0) {
763 pr_warning("Source file is shorter than expected.\n");
764 rv = -1;
765 }
766 return rv;
767}
768
769#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
770#define show_one_line(f,l) _show_one_line(f,l,false,false)
771#define skip_one_line(f,l) _show_one_line(f,l,true,false)
772#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
773
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300774/*
775 * Show line-range always requires debuginfo to find source file and
776 * line number.
777 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900778static int __show_line_range(struct line_range *lr, const char *module,
779 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300780{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400781 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000782 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900783 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900785 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900786 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000787 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300788
789 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000790 dinfo = open_debuginfo(module, false);
791 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900792 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400793
Masami Hiramatsuff741782011-06-27 16:27:39 +0900794 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900795 if (!ret) { /* Not found, retry with an alternative */
796 ret = get_alternative_line_range(dinfo, lr, module, user);
797 if (!ret)
798 ret = debuginfo__find_line_range(dinfo, lr);
799 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900800 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000801 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400802 pr_warning("Specified source line is not found.\n");
803 return -ENOENT;
804 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000805 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400806 return ret;
807 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900809 /* Convert source file path */
810 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900811 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800812
813 /* Free old path when new path is assigned */
814 if (tmp != lr->path)
815 free(tmp);
816
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900817 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000818 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900819 return ret;
820 }
821
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822 setup_pager();
823
824 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900825 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300826 lr->start - lr->offset);
827 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100828 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300829
830 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 if (fp == NULL) {
832 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000833 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400834 return -errno;
835 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300836 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100837 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100838 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100839 if (ret < 0)
840 goto end;
841 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300842
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000843 intlist__for_each(ln, lr->line_list) {
844 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100845 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100846 if (ret < 0)
847 goto end;
848 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100849 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400850 if (ret < 0)
851 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300852 }
853
854 if (lr->end == INT_MAX)
855 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100856 while (l <= lr->end) {
857 ret = show_one_line_or_eof(fp, l++ - lr->offset);
858 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100859 break;
860 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300862 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400863 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300864}
865
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000866int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000867{
868 int ret;
869
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900870 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000871 if (ret < 0)
872 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900873 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900874 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000875
876 return ret;
877}
878
Masami Hiramatsuff741782011-06-27 16:27:39 +0900879static int show_available_vars_at(struct debuginfo *dinfo,
880 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900881 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900882{
883 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900884 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900885 struct str_node *node;
886 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900887 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900888 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900889
890 buf = synthesize_perf_probe_point(&pev->point);
891 if (!buf)
892 return -EINVAL;
893 pr_debug("Searching variables at %s\n", buf);
894
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900895 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900896 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900897 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900898 if (!ret) {
899 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900900 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900901 /* Release the old probe_point */
902 clear_perf_probe_point(&tmp);
903 }
904 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900905 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000906 if (ret == 0 || ret == -ENOENT) {
907 pr_err("Failed to find the address of %s\n", buf);
908 ret = -ENOENT;
909 } else
910 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900911 goto end;
912 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000913
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900914 /* Some variables are found */
915 fprintf(stdout, "Available variables at %s\n", buf);
916 for (i = 0; i < ret; i++) {
917 vl = &vls[i];
918 /*
919 * A probe point might be converted to
920 * several trace points.
921 */
922 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
923 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300924 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900925 nvars = 0;
926 if (vl->vars) {
927 strlist__for_each(node, vl->vars) {
928 var = strchr(node->s, '\t') + 1;
929 if (strfilter__compare(_filter, var)) {
930 fprintf(stdout, "\t\t%s\n", node->s);
931 nvars++;
932 }
933 }
934 strlist__delete(vl->vars);
935 }
936 if (nvars == 0)
937 fprintf(stdout, "\t\t(No matched variables)\n");
938 }
939 free(vls);
940end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900941 free(buf);
942 return ret;
943}
944
945/* Show available variables on given probe point */
946int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900947 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900948{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900949 int i, ret = 0;
950 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900951
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900952 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900953 if (ret < 0)
954 return ret;
955
Masami Hiramatsu44225522015-05-08 10:03:28 +0900956 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900957 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000958 ret = -ENOENT;
959 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900960 }
961
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900962 setup_pager();
963
Masami Hiramatsuff741782011-06-27 16:27:39 +0900964 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900965 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900966
967 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000968out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900969 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900970 return ret;
971}
972
Ingo Molnar89fe8082013-09-30 12:07:11 +0200973#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300974
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900975static void debuginfo_cache__exit(void)
976{
977}
978
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000979static int
980find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
981 struct perf_probe_point *pp __maybe_unused,
982 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300983{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000984 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985}
986
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530987static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900988 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300989{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400990 if (perf_probe_event_need_dwarf(pev)) {
991 pr_warning("Debuginfo-analysis is not supported.\n");
992 return -ENOSYS;
993 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530994
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300995 return 0;
996}
997
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300998int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000999 const char *module __maybe_unused,
1000 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001001{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001002 pr_warning("Debuginfo-analysis is not supported.\n");
1003 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001004}
1005
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001006int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001007 int npevs __maybe_unused,
1008 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001009{
1010 pr_warning("Debuginfo-analysis is not supported.\n");
1011 return -ENOSYS;
1012}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001013#endif
1014
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001015void line_range__clear(struct line_range *lr)
1016{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001017 free(lr->function);
1018 free(lr->file);
1019 free(lr->path);
1020 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001021 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001022 memset(lr, 0, sizeof(*lr));
1023}
1024
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001025int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001026{
1027 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001028 lr->line_list = intlist__new(NULL);
1029 if (!lr->line_list)
1030 return -ENOMEM;
1031 else
1032 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001033}
1034
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001035static int parse_line_num(char **ptr, int *val, const char *what)
1036{
1037 const char *start = *ptr;
1038
1039 errno = 0;
1040 *val = strtol(*ptr, ptr, 0);
1041 if (errno || *ptr == start) {
1042 semantic_error("'%s' is not a valid number.\n", what);
1043 return -EINVAL;
1044 }
1045 return 0;
1046}
1047
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001048/* Check the name is good for event, group or function */
1049static bool is_c_func_name(const char *name)
1050{
1051 if (!isalpha(*name) && *name != '_')
1052 return false;
1053 while (*++name != '\0') {
1054 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1055 return false;
1056 }
1057 return true;
1058}
1059
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001060/*
1061 * Stuff 'lr' according to the line range described by 'arg'.
1062 * The line range syntax is described by:
1063 *
1064 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001065 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001066 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001067int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001068{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001069 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001070 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001071
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001072 if (!name)
1073 return -ENOMEM;
1074
1075 lr->start = 0;
1076 lr->end = INT_MAX;
1077
1078 range = strchr(name, ':');
1079 if (range) {
1080 *range++ = '\0';
1081
1082 err = parse_line_num(&range, &lr->start, "start line");
1083 if (err)
1084 goto err;
1085
1086 if (*range == '+' || *range == '-') {
1087 const char c = *range++;
1088
1089 err = parse_line_num(&range, &lr->end, "end line");
1090 if (err)
1091 goto err;
1092
1093 if (c == '+') {
1094 lr->end += lr->start;
1095 /*
1096 * Adjust the number of lines here.
1097 * If the number of lines == 1, the
1098 * the end of line should be equal to
1099 * the start of line.
1100 */
1101 lr->end--;
1102 }
1103 }
1104
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001105 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001106
1107 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001108 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001109 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001110 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001111 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001112 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001113 if (*range != '\0') {
1114 semantic_error("Tailing with invalid str '%s'.\n", range);
1115 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001116 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001117 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001118
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001119 file = strchr(name, '@');
1120 if (file) {
1121 *file = '\0';
1122 lr->file = strdup(++file);
1123 if (lr->file == NULL) {
1124 err = -ENOMEM;
1125 goto err;
1126 }
1127 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001128 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001129 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001130 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001131 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001132 else { /* Invalid name */
1133 semantic_error("'%s' is not a valid function name.\n", name);
1134 err = -EINVAL;
1135 goto err;
1136 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001137
1138 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001139err:
1140 free(name);
1141 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001142}
1143
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001144/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001145static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001146{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001147 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001148 char *ptr, *tmp;
1149 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301150 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001151 /*
1152 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001153 * perf probe [EVENT=]SRC[:LN|;PTN]
1154 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001155 *
1156 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001157 */
Wang Nane59d29e2015-04-28 08:46:09 +00001158 if (!arg)
1159 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001160
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001161 ptr = strpbrk(arg, ";=@+%");
1162 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001163 *ptr = '\0';
1164 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 if (strchr(arg, ':')) {
1166 semantic_error("Group name is not supported yet.\n");
1167 return -ENOTSUP;
1168 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001169 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001170 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001171 "follow C symbol-naming rule.\n", arg);
1172 return -EINVAL;
1173 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001174 pev->event = strdup(arg);
1175 if (pev->event == NULL)
1176 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001177 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001178 arg = tmp;
1179 }
1180
Naveen N. Rao3099c022015-04-28 17:35:34 +05301181 /*
1182 * Check arg is function or file name and copy it.
1183 *
1184 * We consider arg to be a file spec if and only if it satisfies
1185 * all of the below criteria::
1186 * - it does not include any of "+@%",
1187 * - it includes one of ":;", and
1188 * - it has a period '.' in the name.
1189 *
1190 * Otherwise, we consider arg to be a function specification.
1191 */
1192 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1193 /* This is a file spec if it includes a '.' before ; or : */
1194 if (memchr(arg, '.', ptr - arg))
1195 file_spec = true;
1196 }
1197
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001198 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001199 if (ptr) {
1200 nc = *ptr;
1201 *ptr++ = '\0';
1202 }
1203
Wang Nan6c6e0242015-08-26 10:57:44 +00001204 if (arg[0] == '\0')
1205 tmp = NULL;
1206 else {
1207 tmp = strdup(arg);
1208 if (tmp == NULL)
1209 return -ENOMEM;
1210 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001211
Naveen N. Rao3099c022015-04-28 17:35:34 +05301212 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001213 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001214 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001215 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001216
Wang Nanda15bd92015-08-26 10:57:45 +00001217 /*
1218 * Keep pp->function even if this is absolute address,
1219 * so it can mark whether abs_address is valid.
1220 * Which make 'perf probe lib.bin 0x0' possible.
1221 *
1222 * Note that checking length of tmp is not needed
1223 * because when we access tmp[1] we know tmp[0] is '0',
1224 * so tmp[1] should always valid (but could be '\0').
1225 */
1226 if (tmp && !strncmp(tmp, "0x", 2)) {
1227 pp->abs_address = strtoul(pp->function, &tmp, 0);
1228 if (*tmp != '\0') {
1229 semantic_error("Invalid absolute address.\n");
1230 return -EINVAL;
1231 }
1232 }
1233 }
1234
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001235 /* Parse other options */
1236 while (ptr) {
1237 arg = ptr;
1238 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001239 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001240 pp->lazy_line = strdup(arg);
1241 if (pp->lazy_line == NULL)
1242 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001243 break;
1244 }
1245 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001246 if (ptr) {
1247 nc = *ptr;
1248 *ptr++ = '\0';
1249 }
1250 switch (c) {
1251 case ':': /* Line number */
1252 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001254 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255 " in line number.\n");
1256 return -EINVAL;
1257 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 break;
1259 case '+': /* Byte offset from a symbol */
1260 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001262 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001263 " in offset.\n");
1264 return -EINVAL;
1265 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266 break;
1267 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001268 if (pp->file) {
1269 semantic_error("SRC@SRC is not allowed.\n");
1270 return -EINVAL;
1271 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001272 pp->file = strdup(arg);
1273 if (pp->file == NULL)
1274 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001275 break;
1276 case '%': /* Probe places */
1277 if (strcmp(arg, "return") == 0) {
1278 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001279 } else { /* Others not supported yet */
1280 semantic_error("%%%s is not supported.\n", arg);
1281 return -ENOTSUP;
1282 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001283 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001284 default: /* Buggy case */
1285 pr_err("This program has a bug at %s:%d.\n",
1286 __FILE__, __LINE__);
1287 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001288 break;
1289 }
1290 }
1291
1292 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001293 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001294 semantic_error("Lazy pattern can't be used with"
1295 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 return -EINVAL;
1297 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001298
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001299 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001300 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001301 return -EINVAL;
1302 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001303
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001304 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001305 semantic_error("Offset can't be used with line number.\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->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001310 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001311 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 return -EINVAL;
1313 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001314
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001315 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001316 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 return -EINVAL;
1318 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001319
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001320 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001321 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 return -EINVAL;
1323 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001324
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001325 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001326 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001327 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001328 return -EINVAL;
1329 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001330
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001331 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001332 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1333 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001334 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001335}
1336
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001337/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001338static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001339{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001340 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001341 struct perf_probe_arg_field **fieldp;
1342
1343 pr_debug("parsing arg: %s into ", str);
1344
Masami Hiramatsu48481932010-04-12 13:16:53 -04001345 tmp = strchr(str, '=');
1346 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001347 arg->name = strndup(str, tmp - str);
1348 if (arg->name == NULL)
1349 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001350 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001351 str = tmp + 1;
1352 }
1353
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001354 tmp = strchr(str, ':');
1355 if (tmp) { /* Type setting */
1356 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001357 arg->type = strdup(tmp + 1);
1358 if (arg->type == NULL)
1359 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001360 pr_debug("type:%s ", arg->type);
1361 }
1362
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001363 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001364 if (!is_c_varname(str) || !tmp) {
1365 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001366 arg->var = strdup(str);
1367 if (arg->var == NULL)
1368 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001369 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001371 }
1372
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001373 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001374 arg->var = strndup(str, tmp - str);
1375 if (arg->var == NULL)
1376 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001377 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001378 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001379 fieldp = &arg->field;
1380
1381 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001382 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1383 if (*fieldp == NULL)
1384 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001385 if (*tmp == '[') { /* Array */
1386 str = tmp;
1387 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001388 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001389 if (*tmp != ']' || tmp == str + 1) {
1390 semantic_error("Array index must be a"
1391 " number.\n");
1392 return -EINVAL;
1393 }
1394 tmp++;
1395 if (*tmp == '\0')
1396 tmp = NULL;
1397 } else { /* Structure */
1398 if (*tmp == '.') {
1399 str = tmp + 1;
1400 (*fieldp)->ref = false;
1401 } else if (tmp[1] == '>') {
1402 str = tmp + 2;
1403 (*fieldp)->ref = true;
1404 } else {
1405 semantic_error("Argument parse error: %s\n",
1406 str);
1407 return -EINVAL;
1408 }
1409 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001410 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001411 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001412 (*fieldp)->name = strndup(str, tmp - str);
1413 if ((*fieldp)->name == NULL)
1414 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001415 if (*str != '[')
1416 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001417 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1418 fieldp = &(*fieldp)->next;
1419 }
1420 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001421 (*fieldp)->name = strdup(str);
1422 if ((*fieldp)->name == NULL)
1423 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001424 if (*str != '[')
1425 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001426 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001427
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001428 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001429 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001430 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001431 if (arg->name == NULL)
1432 return -ENOMEM;
1433 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001434 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001435}
1436
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001438int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001439{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001440 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001441 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001442
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001443 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444 if (!argv) {
1445 pr_debug("Failed to split arguments.\n");
1446 return -ENOMEM;
1447 }
1448 if (argc - 1 > MAX_PROBE_ARGS) {
1449 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1450 ret = -ERANGE;
1451 goto out;
1452 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001453 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001454 ret = parse_perf_probe_point(argv[0], pev);
1455 if (ret < 0)
1456 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001457
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001458 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001459 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001460 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1461 if (pev->args == NULL) {
1462 ret = -ENOMEM;
1463 goto out;
1464 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1466 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1467 if (ret >= 0 &&
1468 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001469 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 " kretprobe.\n");
1471 ret = -EINVAL;
1472 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001473 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001475 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001476
1477 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001478}
1479
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001480/* Return true if this perf_probe_event requires debuginfo */
1481bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001482{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001483 int i;
1484
1485 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1486 return true;
1487
1488 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001489 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490 return true;
1491
1492 return false;
1493}
1494
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301495/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001496int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001497{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301498 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001499 char pr;
1500 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001501 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502 int ret, i, argc;
1503 char **argv;
1504
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301505 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001506 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 if (!argv) {
1508 pr_debug("Failed to split arguments.\n");
1509 return -ENOMEM;
1510 }
1511 if (argc < 2) {
1512 semantic_error("Too few probe arguments.\n");
1513 ret = -ERANGE;
1514 goto out;
1515 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001516
1517 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001518 argv0_str = strdup(argv[0]);
1519 if (argv0_str == NULL) {
1520 ret = -ENOMEM;
1521 goto out;
1522 }
1523 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1524 fmt2_str = strtok_r(NULL, "/", &fmt);
1525 fmt3_str = strtok_r(NULL, " \t", &fmt);
1526 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1527 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 semantic_error("Failed to parse event name: %s\n", argv[0]);
1529 ret = -EINVAL;
1530 goto out;
1531 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001532 pr = fmt1_str[0];
1533 tev->group = strdup(fmt2_str);
1534 tev->event = strdup(fmt3_str);
1535 if (tev->group == NULL || tev->event == NULL) {
1536 ret = -ENOMEM;
1537 goto out;
1538 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001539 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001540
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001541 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001542
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001543 /* Scan module name(if there), function name and offset */
1544 p = strchr(argv[1], ':');
1545 if (p) {
1546 tp->module = strndup(argv[1], p - argv[1]);
1547 p++;
1548 } else
1549 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001550 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001551 /* only the address started with 0x */
1552 if (fmt1_str[0] == '0') {
1553 /*
1554 * Fix a special case:
1555 * if address == 0, kernel reports something like:
1556 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1557 * Newer kernel may fix that, but we want to
1558 * support old kernel also.
1559 */
1560 if (strcmp(fmt1_str, "0x") == 0) {
1561 if (!argv[2] || strcmp(argv[2], "(null)")) {
1562 ret = -EINVAL;
1563 goto out;
1564 }
1565 tp->address = 0;
1566
1567 free(argv[2]);
1568 for (i = 2; argv[i + 1] != NULL; i++)
1569 argv[i] = argv[i + 1];
1570
1571 argv[i] = NULL;
1572 argc -= 1;
1573 } else
1574 tp->address = strtoul(fmt1_str, NULL, 0);
1575 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001576 /* Only the symbol-based probe has offset */
1577 tp->symbol = strdup(fmt1_str);
1578 if (tp->symbol == NULL) {
1579 ret = -ENOMEM;
1580 goto out;
1581 }
1582 fmt2_str = strtok_r(NULL, "", &fmt);
1583 if (fmt2_str == NULL)
1584 tp->offset = 0;
1585 else
1586 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001587 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001588
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001589 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301590 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001591 if (tev->args == NULL) {
1592 ret = -ENOMEM;
1593 goto out;
1594 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001595 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001596 p = strchr(argv[i + 2], '=');
1597 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 *p++ = '\0';
1599 else
1600 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001601 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001602 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001603 tev->args[i].value = strdup(p);
1604 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1605 ret = -ENOMEM;
1606 goto out;
1607 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001608 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001609 ret = 0;
1610out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001611 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001612 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001613 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001614}
1615
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001616/* Compose only probe arg */
1617int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1618{
1619 struct perf_probe_arg_field *field = pa->field;
1620 int ret;
1621 char *tmp = buf;
1622
Masami Hiramatsu48481932010-04-12 13:16:53 -04001623 if (pa->name && pa->var)
1624 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1625 else
1626 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001627 if (ret <= 0)
1628 goto error;
1629 tmp += ret;
1630 len -= ret;
1631
1632 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001633 if (field->name[0] == '[')
1634 ret = e_snprintf(tmp, len, "%s", field->name);
1635 else
1636 ret = e_snprintf(tmp, len, "%s%s",
1637 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001638 if (ret <= 0)
1639 goto error;
1640 tmp += ret;
1641 len -= ret;
1642 field = field->next;
1643 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001644
1645 if (pa->type) {
1646 ret = e_snprintf(tmp, len, ":%s", pa->type);
1647 if (ret <= 0)
1648 goto error;
1649 tmp += ret;
1650 len -= ret;
1651 }
1652
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001653 return tmp - buf;
1654error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001655 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001656 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001657}
1658
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001659/* Compose only probe point (not argument) */
1660static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001661{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001662 char *buf, *tmp;
1663 char offs[32] = "", line[32] = "", file[32] = "";
1664 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001665
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001666 buf = zalloc(MAX_CMDLEN);
1667 if (buf == NULL) {
1668 ret = -ENOMEM;
1669 goto error;
1670 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001671 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001672 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001673 if (ret <= 0)
1674 goto error;
1675 }
1676 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001677 ret = e_snprintf(line, 32, ":%d", pp->line);
1678 if (ret <= 0)
1679 goto error;
1680 }
1681 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001682 tmp = pp->file;
1683 len = strlen(tmp);
1684 if (len > 30) {
1685 tmp = strchr(pp->file + len - 30, '/');
1686 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1687 }
1688 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001689 if (ret <= 0)
1690 goto error;
1691 }
1692
1693 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001694 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1695 offs, pp->retprobe ? "%return" : "", line,
1696 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001697 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001698 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001699 if (ret <= 0)
1700 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001701
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 return buf;
1703error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001704 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001705 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001706 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707}
1708
1709#if 0
1710char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1711{
1712 char *buf;
1713 int i, len, ret;
1714
1715 buf = synthesize_perf_probe_point(&pev->point);
1716 if (!buf)
1717 return NULL;
1718
1719 len = strlen(buf);
1720 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001721 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001722 pev->args[i].name);
1723 if (ret <= 0) {
1724 free(buf);
1725 return NULL;
1726 }
1727 len += ret;
1728 }
1729
1730 return buf;
1731}
1732#endif
1733
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 char **buf, size_t *buflen,
1736 int depth)
1737{
1738 int ret;
1739 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301740 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001741 buflen, depth + 1);
1742 if (depth < 0)
1743 goto out;
1744 }
1745
1746 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1747 if (ret < 0)
1748 depth = ret;
1749 else {
1750 *buf += ret;
1751 *buflen -= ret;
1752 }
1753out:
1754 return depth;
1755
1756}
1757
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301758static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001759 char *buf, size_t buflen)
1760{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301761 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001762 int ret, depth = 0;
1763 char *tmp = buf;
1764
1765 /* Argument name or separator */
1766 if (arg->name)
1767 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1768 else
1769 ret = e_snprintf(buf, buflen, " ");
1770 if (ret < 0)
1771 return ret;
1772 buf += ret;
1773 buflen -= ret;
1774
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001775 /* Special case: @XXX */
1776 if (arg->value[0] == '@' && arg->ref)
1777 ref = ref->next;
1778
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001779 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001780 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301781 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001782 &buflen, 1);
1783 if (depth < 0)
1784 return depth;
1785 }
1786
1787 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001788 if (arg->value[0] == '@' && arg->ref)
1789 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1790 arg->ref->offset);
1791 else
1792 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001793 if (ret < 0)
1794 return ret;
1795 buf += ret;
1796 buflen -= ret;
1797
1798 /* Closing */
1799 while (depth--) {
1800 ret = e_snprintf(buf, buflen, ")");
1801 if (ret < 0)
1802 return ret;
1803 buf += ret;
1804 buflen -= ret;
1805 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001806 /* Print argument type */
1807 if (arg->type) {
1808 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1809 if (ret <= 0)
1810 return ret;
1811 buf += ret;
1812 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001813
1814 return buf - tmp;
1815}
1816
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301817char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001818{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301819 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001820 char *buf;
1821 int i, len, ret;
1822
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001823 buf = zalloc(MAX_CMDLEN);
1824 if (buf == NULL)
1825 return NULL;
1826
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001827 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1828 tev->group, tev->event);
1829 if (len <= 0)
1830 goto error;
1831
Wang Nanda15bd92015-08-26 10:57:45 +00001832 /* Uprobes must have tp->module */
1833 if (tev->uprobes && !tp->module)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001834 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001835 /*
1836 * If tp->address == 0, then this point must be a
1837 * absolute address uprobe.
1838 * try_to_find_absolute_address() should have made
1839 * tp->symbol to "0x0".
1840 */
1841 if (tev->uprobes && !tp->address) {
1842 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1843 goto error;
1844 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001845
1846 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301847 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001848 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1849 tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001850 else if (!strncmp(tp->symbol, "0x", 2))
1851 /* Absolute address. See try_to_find_absolute_address() */
1852 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1853 tp->module ?: "", tp->module ? ":" : "",
1854 tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301855 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001856 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301857 tp->module ?: "", tp->module ? ":" : "",
1858 tp->symbol, tp->offset);
1859
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001860 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001862 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001863
1864 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301865 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001867 if (ret <= 0)
1868 goto error;
1869 len += ret;
1870 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001871
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001872 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001873error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001874 free(buf);
1875 return NULL;
1876}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001877
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001878static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1879 struct perf_probe_point *pp,
1880 bool is_kprobe)
1881{
1882 struct symbol *sym = NULL;
1883 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001884 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001885 int ret = -ENOENT;
1886
1887 if (!is_kprobe) {
1888 map = dso__new_map(tp->module);
1889 if (!map)
1890 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001891 sym = map__find_symbol(map, addr, NULL);
1892 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001893 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09001894 if (kernel_get_symbol_address_by_name(tp->symbol,
1895 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001896 goto out;
1897 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001898 if (addr) {
1899 addr += tp->offset;
1900 sym = __find_kernel_function(addr, &map);
1901 }
1902 }
Wang Nan98d3b252015-11-05 13:19:25 +00001903
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001904 if (!sym)
1905 goto out;
1906
1907 pp->retprobe = tp->retprobe;
1908 pp->offset = addr - map->unmap_ip(map, sym->start);
1909 pp->function = strdup(sym->name);
1910 ret = pp->function ? 0 : -ENOMEM;
1911
1912out:
1913 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001914 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001915 }
1916
1917 return ret;
1918}
1919
1920static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001921 struct perf_probe_point *pp,
1922 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001923{
1924 char buf[128];
1925 int ret;
1926
1927 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1928 if (!ret)
1929 return 0;
1930 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1931 if (!ret)
1932 return 0;
1933
1934 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1935
1936 if (tp->symbol) {
1937 pp->function = strdup(tp->symbol);
1938 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001939 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001940 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1941 if (ret < 0)
1942 return ret;
1943 pp->function = strdup(buf);
1944 pp->offset = 0;
1945 }
1946 if (pp->function == NULL)
1947 return -ENOMEM;
1948
1949 pp->retprobe = tp->retprobe;
1950
1951 return 0;
1952}
1953
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301954static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301955 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001956{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001957 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001958 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001959
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001960 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001961 pev->event = strdup(tev->event);
1962 pev->group = strdup(tev->group);
1963 if (pev->event == NULL || pev->group == NULL)
1964 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001965
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001966 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001967 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001968 if (ret < 0)
1969 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001970
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971 /* Convert trace_arg to probe_arg */
1972 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001973 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1974 if (pev->args == NULL)
1975 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001976 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001977 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001978 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001979 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301980 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001981 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001982 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001983 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001984 if (pev->args[i].name == NULL && ret >= 0)
1985 ret = -ENOMEM;
1986 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001987
1988 if (ret < 0)
1989 clear_perf_probe_event(pev);
1990
1991 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001992}
1993
1994void clear_perf_probe_event(struct perf_probe_event *pev)
1995{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001996 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001997 int i;
1998
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001999 free(pev->event);
2000 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002001 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002002 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002003
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002004 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002005 free(pev->args[i].name);
2006 free(pev->args[i].var);
2007 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002008 field = pev->args[i].field;
2009 while (field) {
2010 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002011 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002012 free(field);
2013 field = next;
2014 }
2015 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002016 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002017 memset(pev, 0, sizeof(*pev));
2018}
2019
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002020void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002021{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302022 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002023 int i;
2024
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002025 free(tev->event);
2026 free(tev->group);
2027 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002028 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002029 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002030 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002031 free(tev->args[i].name);
2032 free(tev->args[i].value);
2033 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002034 ref = tev->args[i].ref;
2035 while (ref) {
2036 next = ref->next;
2037 free(ref);
2038 ref = next;
2039 }
2040 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002041 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002042 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002043}
2044
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002045struct kprobe_blacklist_node {
2046 struct list_head list;
2047 unsigned long start;
2048 unsigned long end;
2049 char *symbol;
2050};
2051
2052static void kprobe_blacklist__delete(struct list_head *blacklist)
2053{
2054 struct kprobe_blacklist_node *node;
2055
2056 while (!list_empty(blacklist)) {
2057 node = list_first_entry(blacklist,
2058 struct kprobe_blacklist_node, list);
2059 list_del(&node->list);
2060 free(node->symbol);
2061 free(node);
2062 }
2063}
2064
2065static int kprobe_blacklist__load(struct list_head *blacklist)
2066{
2067 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002068 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002069 char buf[PATH_MAX], *p;
2070 FILE *fp;
2071 int ret;
2072
2073 if (__debugfs == NULL)
2074 return -ENOTSUP;
2075
2076 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2077 if (ret < 0)
2078 return ret;
2079
2080 fp = fopen(buf, "r");
2081 if (!fp)
2082 return -errno;
2083
2084 ret = 0;
2085 while (fgets(buf, PATH_MAX, fp)) {
2086 node = zalloc(sizeof(*node));
2087 if (!node) {
2088 ret = -ENOMEM;
2089 break;
2090 }
2091 INIT_LIST_HEAD(&node->list);
2092 list_add_tail(&node->list, blacklist);
2093 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2094 ret = -EINVAL;
2095 break;
2096 }
2097 p = strchr(buf, '\t');
2098 if (p) {
2099 p++;
2100 if (p[strlen(p) - 1] == '\n')
2101 p[strlen(p) - 1] = '\0';
2102 } else
2103 p = (char *)"unknown";
2104 node->symbol = strdup(p);
2105 if (!node->symbol) {
2106 ret = -ENOMEM;
2107 break;
2108 }
2109 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2110 node->start, node->end, node->symbol);
2111 ret++;
2112 }
2113 if (ret < 0)
2114 kprobe_blacklist__delete(blacklist);
2115 fclose(fp);
2116
2117 return ret;
2118}
2119
2120static struct kprobe_blacklist_node *
2121kprobe_blacklist__find_by_address(struct list_head *blacklist,
2122 unsigned long address)
2123{
2124 struct kprobe_blacklist_node *node;
2125
2126 list_for_each_entry(node, blacklist, list) {
2127 if (node->start <= address && address <= node->end)
2128 return node;
2129 }
2130
2131 return NULL;
2132}
2133
Masami Hiramatsub0312202015-06-16 20:50:55 +09002134static LIST_HEAD(kprobe_blacklist);
2135
2136static void kprobe_blacklist__init(void)
2137{
2138 if (!list_empty(&kprobe_blacklist))
2139 return;
2140
2141 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2142 pr_debug("No kprobe blacklist support, ignored\n");
2143}
2144
2145static void kprobe_blacklist__release(void)
2146{
2147 kprobe_blacklist__delete(&kprobe_blacklist);
2148}
2149
2150static bool kprobe_blacklist__listed(unsigned long address)
2151{
2152 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2153}
2154
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002155static int perf_probe_event__sprintf(const char *group, const char *event,
2156 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002157 const char *module,
2158 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002159{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002160 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002161 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002162 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002163
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002164 /* Synthesize only event probe point */
2165 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002166 if (!place)
2167 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002168
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002169 ret = e_snprintf(buf, 128, "%s:%s", group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002170 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002171 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002172
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002173 strbuf_addf(result, " %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002174 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002175 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002176
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002177 if (pev->nargs > 0) {
Arnaldo Carvalho de Melo88fd6332016-03-23 16:44:49 -03002178 strbuf_add(result, " with", 5);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002179 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002180 ret = synthesize_perf_probe_arg(&pev->args[i],
2181 buf, 128);
2182 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002183 goto out;
2184 strbuf_addf(result, " %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002185 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002186 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002187 strbuf_addch(result, ')');
2188out:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002190 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002191}
2192
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002193/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002194int show_perf_probe_event(const char *group, const char *event,
2195 struct perf_probe_event *pev,
2196 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002197{
2198 struct strbuf buf = STRBUF_INIT;
2199 int ret;
2200
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002201 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002202 if (ret >= 0) {
2203 if (use_stdout)
2204 printf("%s\n", buf.buf);
2205 else
2206 pr_info("%s\n", buf.buf);
2207 }
2208 strbuf_release(&buf);
2209
2210 return ret;
2211}
2212
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002213static bool filter_probe_trace_event(struct probe_trace_event *tev,
2214 struct strfilter *filter)
2215{
2216 char tmp[128];
2217
2218 /* At first, check the event name itself */
2219 if (strfilter__compare(filter, tev->event))
2220 return true;
2221
2222 /* Next, check the combination of name and group */
2223 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2224 return false;
2225 return strfilter__compare(filter, tmp);
2226}
2227
2228static int __show_perf_probe_events(int fd, bool is_kprobe,
2229 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002230{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302231 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302232 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002233 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002234 struct strlist *rawlist;
2235 struct str_node *ent;
2236
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002237 memset(&tev, 0, sizeof(tev));
2238 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002239
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002240 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002241 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002242 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002243
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002244 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302245 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002246 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002247 if (!filter_probe_trace_event(&tev, filter))
2248 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302249 ret = convert_to_perf_probe_event(&tev, &pev,
2250 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002251 if (ret < 0)
2252 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002253 ret = show_perf_probe_event(pev.group, pev.event,
2254 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002255 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002256 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002257next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002258 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302259 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002260 if (ret < 0)
2261 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002262 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002263 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002264 /* Cleanup cached debuginfo if needed */
2265 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002266
2267 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002268}
2269
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302270/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002271int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302272{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002273 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302274
2275 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302276
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002277 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302278 if (ret < 0)
2279 return ret;
2280
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002281 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2282 if (ret < 0)
2283 return ret;
2284
2285 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002286 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002287 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002288 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002289 if (kp_fd > 0)
2290 close(kp_fd);
2291 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002292 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002293 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302294
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002295 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002296}
2297
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002298static int get_new_event_name(char *buf, size_t len, const char *base,
2299 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002300{
2301 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002302 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002303
Naveen N. Rao3099c022015-04-28 17:35:34 +05302304 if (*base == '.')
2305 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002306 nbase = strdup(base);
2307 if (!nbase)
2308 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302309
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002310 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2311 p = strchr(nbase, '.');
2312 if (p && p != nbase)
2313 *p = '\0';
2314
2315 /* Try no suffix number */
2316 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002317 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002318 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002319 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002320 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002321 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002322 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002323
Masami Hiramatsud761b082009-12-15 10:32:25 -05002324 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002325 pr_warning("Error: event \"%s\" already exists.\n"
2326 " Hint: Remove existing event by 'perf probe -d'\n"
2327 " or force duplicates by 'perf probe -f'\n"
2328 " or set 'force=yes' in BPF source.\n",
2329 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002330 ret = -EEXIST;
2331 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002332 }
2333
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002334 /* Try to add suffix */
2335 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002336 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002337 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002338 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002339 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002340 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002341 if (!strlist__has_entry(namelist, buf))
2342 break;
2343 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002344 if (i == MAX_EVENT_INDEX) {
2345 pr_warning("Too many events are on the same function.\n");
2346 ret = -ERANGE;
2347 }
2348
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002349out:
2350 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002351 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002352}
2353
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002354/* Warn if the current kernel's uprobe implementation is old */
2355static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2356{
2357 int i;
2358 char *buf = synthesize_probe_trace_command(tev);
2359
2360 /* Old uprobe event doesn't support memory dereference */
2361 if (!tev->uprobes || tev->nargs == 0 || !buf)
2362 goto out;
2363
2364 for (i = 0; i < tev->nargs; i++)
2365 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2366 pr_warning("Please upgrade your kernel to at least "
2367 "3.14 to have access to feature %s\n",
2368 tev->args[i].value);
2369 break;
2370 }
2371out:
2372 free(buf);
2373}
2374
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002375/* Set new name from original perf_probe_event and namelist */
2376static int probe_trace_event__set_name(struct probe_trace_event *tev,
2377 struct perf_probe_event *pev,
2378 struct strlist *namelist,
2379 bool allow_suffix)
2380{
2381 const char *event, *group;
2382 char buf[64];
2383 int ret;
2384
2385 if (pev->event)
2386 event = pev->event;
2387 else
Wang Nanda15bd92015-08-26 10:57:45 +00002388 if (pev->point.function &&
2389 (strncmp(pev->point.function, "0x", 2) != 0) &&
2390 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002391 event = pev->point.function;
2392 else
2393 event = tev->point.realname;
2394 if (pev->group)
2395 group = pev->group;
2396 else
2397 group = PERFPROBE_GROUP;
2398
2399 /* Get an unused new event name */
2400 ret = get_new_event_name(buf, 64, event,
2401 namelist, allow_suffix);
2402 if (ret < 0)
2403 return ret;
2404
2405 event = buf;
2406
2407 tev->event = strdup(event);
2408 tev->group = strdup(group);
2409 if (tev->event == NULL || tev->group == NULL)
2410 return -ENOMEM;
2411
2412 /* Add added event name to namelist */
2413 strlist__add(namelist, event);
2414 return 0;
2415}
2416
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302417static int __add_probe_trace_events(struct perf_probe_event *pev,
2418 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002419 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002420{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002421 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302422 struct probe_trace_event *tev = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002423 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002424
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002425 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2426 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002428
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002429 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002430 namelist = probe_file__get_namelist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002431 if (!namelist) {
2432 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002433 ret = -ENOMEM;
2434 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002435 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002436
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002437 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002438 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002439 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002440 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002441 if (!tev->point.symbol)
2442 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002443
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002444 /* Set new name for tev (and update namelist) */
2445 ret = probe_trace_event__set_name(tev, pev, namelist,
2446 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447 if (ret < 0)
2448 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002449
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002450 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002451 if (ret < 0)
2452 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002453
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002454 /*
2455 * Probes after the first probe which comes from same
2456 * user input are always allowed to add suffix, because
2457 * there might be several addresses corresponding to
2458 * one code line.
2459 */
2460 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002461 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002462 if (ret == -EINVAL && pev->uprobes)
2463 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002464
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002465 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002466close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002467 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002468 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002469}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002470
Wang Nan6bb536c2015-05-29 09:45:47 +00002471static int find_probe_functions(struct map *map, char *name,
2472 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002473{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002474 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002475 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002476 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002477
Wang Nan75e4a2a2015-05-15 12:14:44 +00002478 if (map__load(map, NULL) < 0)
2479 return 0;
2480
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002481 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002482 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002483 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002484 if (syms && found < probe_conf.max_probes)
2485 syms[found - 1] = sym;
2486 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002487 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002488
2489 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002490}
2491
2492#define strdup_or_goto(str, label) \
2493 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2494
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302495void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2496 struct probe_trace_event *tev __maybe_unused,
2497 struct map *map __maybe_unused) { }
2498
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002499/*
2500 * Find probe function addresses from map.
2501 * Return an error or the number of found probe_trace_event
2502 */
2503static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002504 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002505{
2506 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002507 struct ref_reloc_sym *reloc_sym = NULL;
2508 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002509 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002510 struct probe_trace_event *tev;
2511 struct perf_probe_point *pp = &pev->point;
2512 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002513 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002514 int ret, i, j, skipped = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002515
Masami Hiramatsu44225522015-05-08 10:03:28 +09002516 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002517 if (!map) {
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521
Wang Nan6bb536c2015-05-29 09:45:47 +00002522 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2523 if (!syms) {
2524 ret = -ENOMEM;
2525 goto out;
2526 }
2527
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002528 /*
2529 * Load matched symbols: Since the different local symbols may have
2530 * same name but different addresses, this lists all the symbols.
2531 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002532 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002533 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002534 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002535 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002536 ret = -ENOENT;
2537 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002538 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002539 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002540 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002541 ret = -E2BIG;
2542 goto out;
2543 }
2544
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002545 /* Note that the symbols in the kmodule are not relocated */
2546 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002547 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002548 if (!reloc_sym) {
2549 pr_warning("Relocated base symbol is not found!\n");
2550 ret = -EINVAL;
2551 goto out;
2552 }
2553 }
2554
2555 /* Setup result trace-probe-events */
2556 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2557 if (!*tevs) {
2558 ret = -ENOMEM;
2559 goto out;
2560 }
2561
2562 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002563
Wang Nan6bb536c2015-05-29 09:45:47 +00002564 for (j = 0; j < num_matched_functions; j++) {
2565 sym = syms[j];
2566
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002567 tev = (*tevs) + ret;
2568 tp = &tev->point;
2569 if (ret == num_matched_functions) {
2570 pr_warning("Too many symbols are listed. Skip it.\n");
2571 break;
2572 }
2573 ret++;
2574
2575 if (pp->offset > sym->end - sym->start) {
2576 pr_warning("Offset %ld is bigger than the size of %s\n",
2577 pp->offset, sym->name);
2578 ret = -ENOENT;
2579 goto err_out;
2580 }
2581 /* Add one probe point */
2582 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002583
2584 /* Check the kprobe (not in module) is within .text */
2585 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002586 kprobe_warn_out_range(sym->name, tp->address)) {
2587 tp->symbol = NULL; /* Skip it */
2588 skipped++;
2589 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002590 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2591 tp->offset = tp->address - reloc_sym->addr;
2592 } else {
2593 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2594 tp->offset = pp->offset;
2595 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002596 tp->realname = strdup_or_goto(sym->name, nomem_out);
2597
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002598 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002599 if (pev->target)
2600 tev->point.module = strdup_or_goto(pev->target,
2601 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002602 tev->uprobes = pev->uprobes;
2603 tev->nargs = pev->nargs;
2604 if (tev->nargs) {
2605 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2606 tev->nargs);
2607 if (tev->args == NULL)
2608 goto nomem_out;
2609 }
2610 for (i = 0; i < tev->nargs; i++) {
2611 if (pev->args[i].name)
2612 tev->args[i].name =
2613 strdup_or_goto(pev->args[i].name,
2614 nomem_out);
2615
2616 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2617 nomem_out);
2618 if (pev->args[i].type)
2619 tev->args[i].type =
2620 strdup_or_goto(pev->args[i].type,
2621 nomem_out);
2622 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302623 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002624 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002625 if (ret == skipped) {
2626 ret = -ENOENT;
2627 goto err_out;
2628 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002629
2630out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002631 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002632 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002633 return ret;
2634
2635nomem_out:
2636 ret = -ENOMEM;
2637err_out:
2638 clear_probe_trace_events(*tevs, num_matched_functions);
2639 zfree(tevs);
2640 goto out;
2641}
2642
Wang Nanda15bd92015-08-26 10:57:45 +00002643static int try_to_find_absolute_address(struct perf_probe_event *pev,
2644 struct probe_trace_event **tevs)
2645{
2646 struct perf_probe_point *pp = &pev->point;
2647 struct probe_trace_event *tev;
2648 struct probe_trace_point *tp;
2649 int i, err;
2650
2651 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2652 return -EINVAL;
2653 if (perf_probe_event_need_dwarf(pev))
2654 return -EINVAL;
2655
2656 /*
2657 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2658 * absolute address.
2659 *
2660 * Only one tev can be generated by this.
2661 */
2662 *tevs = zalloc(sizeof(*tev));
2663 if (!*tevs)
2664 return -ENOMEM;
2665
2666 tev = *tevs;
2667 tp = &tev->point;
2668
2669 /*
2670 * Don't use tp->offset, use address directly, because
2671 * in synthesize_probe_trace_command() address cannot be
2672 * zero.
2673 */
2674 tp->address = pev->point.abs_address;
2675 tp->retprobe = pp->retprobe;
2676 tev->uprobes = pev->uprobes;
2677
2678 err = -ENOMEM;
2679 /*
2680 * Give it a '0x' leading symbol name.
2681 * In __add_probe_trace_events, a NULL symbol is interpreted as
2682 * invalud.
2683 */
2684 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2685 goto errout;
2686
2687 /* For kprobe, check range */
2688 if ((!tev->uprobes) &&
2689 (kprobe_warn_out_range(tev->point.symbol,
2690 tev->point.address))) {
2691 err = -EACCES;
2692 goto errout;
2693 }
2694
2695 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2696 goto errout;
2697
2698 if (pev->target) {
2699 tp->module = strdup(pev->target);
2700 if (!tp->module)
2701 goto errout;
2702 }
2703
2704 if (tev->group) {
2705 tev->group = strdup(pev->group);
2706 if (!tev->group)
2707 goto errout;
2708 }
2709
2710 if (pev->event) {
2711 tev->event = strdup(pev->event);
2712 if (!tev->event)
2713 goto errout;
2714 }
2715
2716 tev->nargs = pev->nargs;
2717 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2718 if (!tev->args) {
2719 err = -ENOMEM;
2720 goto errout;
2721 }
2722 for (i = 0; i < tev->nargs; i++)
2723 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2724
2725 return 1;
2726
2727errout:
2728 if (*tevs) {
2729 clear_probe_trace_events(*tevs, 1);
2730 *tevs = NULL;
2731 }
2732 return err;
2733}
2734
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302735bool __weak arch__prefers_symtab(void) { return false; }
2736
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302737static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002738 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002739{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002740 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002741
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09002742 if (!pev->group) {
2743 /* Set group name if not given */
2744 if (!pev->uprobes) {
2745 pev->group = strdup(PERFPROBE_GROUP);
2746 ret = pev->group ? 0 : -ENOMEM;
2747 } else
2748 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002749 if (ret != 0) {
2750 pr_warning("Failed to make a group name.\n");
2751 return ret;
2752 }
2753 }
2754
Wang Nanda15bd92015-08-26 10:57:45 +00002755 ret = try_to_find_absolute_address(pev, tevs);
2756 if (ret > 0)
2757 return ret;
2758
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302759 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002760 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302761 if (ret > 0)
2762 return ret; /* Found in symbol table */
2763 }
2764
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002765 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002766 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002767 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002768 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002769
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002770 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002771}
2772
Wang Nan12fae5e2015-09-04 21:16:00 +09002773int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002774{
Namhyung Kim844dffa2015-09-04 21:15:59 +09002775 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002776
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002777 /* Loop 1: convert all events */
2778 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09002779 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09002780 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09002781 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002782 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09002783 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002784 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002785 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09002786 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002787 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002788 /* This just release blacklist only if allocated */
2789 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002790
Namhyung Kim844dffa2015-09-04 21:15:59 +09002791 return 0;
2792}
2793
Wang Nan12fae5e2015-09-04 21:16:00 +09002794int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002795{
2796 int i, ret = 0;
2797
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002798 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002799 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002800 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2801 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002802 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002803 if (ret < 0)
2804 break;
2805 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002806 return ret;
2807}
2808
Wang Nan12fae5e2015-09-04 21:16:00 +09002809void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002810{
2811 int i, j;
2812
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002813 /* Loop 3: cleanup and free trace events */
2814 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002815 for (j = 0; j < pevs[i].ntevs; j++)
2816 clear_probe_trace_event(&pevs[i].tevs[j]);
2817 zfree(&pevs[i].tevs);
2818 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09002819 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002820 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002821}
2822
2823int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2824{
2825 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09002826
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002827 ret = init_probe_symbol_maps(pevs->uprobes);
2828 if (ret < 0)
2829 return ret;
2830
Wang Nan12fae5e2015-09-04 21:16:00 +09002831 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002832 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09002833 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002834
Wang Nan12fae5e2015-09-04 21:16:00 +09002835 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002836
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002837 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002838 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002839}
2840
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002841int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002842{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002843 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002844 char *str = strfilter__string(filter);
2845
2846 if (!str)
2847 return -EINVAL;
2848
Masami Hiramatsufa282442009-12-08 17:03:23 -05002849 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002850 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2851 if (ret < 0)
2852 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302853
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002854 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002855 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302856 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002857
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002858 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002859 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002860 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002861 goto error;
2862 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002863 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302864
2865error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002866 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302867 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002868 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302869 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002870out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002871 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002872
2873 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002874}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302875
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002876/* TODO: don't use a global variable for filter ... */
2877static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002878
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002879/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002880 * If a symbol corresponds to a function with global binding and
2881 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002882 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002883static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002884 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002885{
Namhyung Kime578da32015-03-06 16:31:29 +09002886 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002887 return 0;
2888 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002889}
2890
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002891int show_available_funcs(const char *target, struct strfilter *_filter,
2892 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002893{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002894 struct map *map;
2895 int ret;
2896
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002897 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002898 if (ret < 0)
2899 return ret;
2900
2901 /* Get a symbol map */
2902 if (user)
2903 map = dso__new_map(target);
2904 else
2905 map = kernel_get_module_map(target);
2906 if (!map) {
2907 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002908 return -EINVAL;
2909 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002910
2911 /* Load symbols with given filter */
2912 available_func_filter = _filter;
2913 if (map__load(map, filter_available_functions)) {
2914 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2915 goto end;
2916 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002917 if (!dso__sorted_by_name(map->dso, map->type))
2918 dso__sort_by_name(map->dso, map->type);
2919
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002920 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302921 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002922 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2923end:
2924 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002925 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002926 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002927 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302928
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002929 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302930}
2931
Wang Nanda15bd92015-08-26 10:57:45 +00002932int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2933 struct perf_probe_arg *pvar)
2934{
2935 tvar->value = strdup(pvar->var);
2936 if (tvar->value == NULL)
2937 return -ENOMEM;
2938 if (pvar->type) {
2939 tvar->type = strdup(pvar->type);
2940 if (tvar->type == NULL)
2941 return -ENOMEM;
2942 }
2943 if (pvar->name) {
2944 tvar->name = strdup(pvar->name);
2945 if (tvar->name == NULL)
2946 return -ENOMEM;
2947 } else
2948 tvar->name = NULL;
2949 return 0;
2950}