blob: 85d82f4dc5e9e1e3c9fb6c4a5e060b25031d5b5f [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
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530268/*
269 * NOTE:
270 * '.gnu.linkonce.this_module' section of kernel module elf directly
271 * maps to 'struct module' from linux/module.h. This section contains
272 * actual module name which will be used by kernel after loading it.
273 * But, we cannot use 'struct module' here since linux/module.h is not
274 * exposed to user-space. Offset of 'name' has remained same from long
275 * time, so hardcoding it here.
276 */
277#ifdef __LP64__
278#define MOD_NAME_OFFSET 24
279#else
280#define MOD_NAME_OFFSET 12
281#endif
282
283/*
284 * @module can be module name of module file path. In case of path,
285 * inspect elf and find out what is actual module name.
286 * Caller has to free mod_name after using it.
287 */
288static char *find_module_name(const char *module)
289{
290 int fd;
291 Elf *elf;
292 GElf_Ehdr ehdr;
293 GElf_Shdr shdr;
294 Elf_Data *data;
295 Elf_Scn *sec;
296 char *mod_name = NULL;
297
298 fd = open(module, O_RDONLY);
299 if (fd < 0)
300 return NULL;
301
302 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
303 if (elf == NULL)
304 goto elf_err;
305
306 if (gelf_getehdr(elf, &ehdr) == NULL)
307 goto ret_err;
308
309 sec = elf_section_by_name(elf, &ehdr, &shdr,
310 ".gnu.linkonce.this_module", NULL);
311 if (!sec)
312 goto ret_err;
313
314 data = elf_getdata(sec, NULL);
315 if (!data || !data->d_buf)
316 goto ret_err;
317
318 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
319
320ret_err:
321 elf_end(elf);
322elf_err:
323 close(fd);
324 return mod_name;
325}
326
Ingo Molnar89fe8082013-09-30 12:07:11 +0200327#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000328
329static int kernel_get_module_dso(const char *module, struct dso **pdso)
330{
331 struct dso *dso;
332 struct map *map;
333 const char *vmlinux_name;
334 int ret = 0;
335
336 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300337 char module_name[128];
338
339 snprintf(module_name, sizeof(module_name), "[%s]", module);
340 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
341 if (map) {
342 dso = map->dso;
343 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000344 }
345 pr_debug("Failed to find module %s.\n", module);
346 return -ENOENT;
347 }
348
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300349 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000350 dso = map->dso;
351
352 vmlinux_name = symbol_conf.vmlinux_name;
353 dso->load_errno = 0;
354 if (vmlinux_name)
355 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
356 else
357 ret = dso__load_vmlinux_path(dso, map, NULL);
358found:
359 *pdso = dso;
360 return ret;
361}
362
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900363/*
364 * Some binaries like glibc have special symbols which are on the symbol
365 * table, but not in the debuginfo. If we can find the address of the
366 * symbol from map, we can translate the address back to the probe point.
367 */
368static int find_alternative_probe_point(struct debuginfo *dinfo,
369 struct perf_probe_point *pp,
370 struct perf_probe_point *result,
371 const char *target, bool uprobes)
372{
373 struct map *map = NULL;
374 struct symbol *sym;
375 u64 address = 0;
376 int ret = -ENOENT;
377
378 /* This can work only for function-name based one */
379 if (!pp->function || pp->file)
380 return -ENOTSUP;
381
382 map = get_target_map(target, uprobes);
383 if (!map)
384 return -EINVAL;
385
386 /* Find the address of given function */
387 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900388 if (uprobes)
389 address = sym->start;
390 else
391 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900392 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900393 }
394 if (!address) {
395 ret = -ENOENT;
396 goto out;
397 }
Wang Nanf6c15622015-04-08 02:14:34 +0000398 pr_debug("Symbol %s address found : %" PRIx64 "\n",
399 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900400
401 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
402 result);
403 if (ret <= 0)
404 ret = (!ret) ? -ENOENT : ret;
405 else {
406 result->offset += pp->offset;
407 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800408 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900409 ret = 0;
410 }
411
412out:
413 put_target_map(map, uprobes);
414 return ret;
415
416}
417
418static int get_alternative_probe_event(struct debuginfo *dinfo,
419 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900420 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900421{
422 int ret;
423
424 memcpy(tmp, &pev->point, sizeof(*tmp));
425 memset(&pev->point, 0, sizeof(pev->point));
426 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900427 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900428 if (ret < 0)
429 memcpy(&pev->point, tmp, sizeof(*tmp));
430
431 return ret;
432}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000433
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900434static int get_alternative_line_range(struct debuginfo *dinfo,
435 struct line_range *lr,
436 const char *target, bool user)
437{
David Ahern6d4a4892015-03-11 10:36:20 -0400438 struct perf_probe_point pp = { .function = lr->function,
439 .file = lr->file,
440 .line = lr->start };
441 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900442 int ret, len = 0;
443
David Ahern6d4a4892015-03-11 10:36:20 -0400444 memset(&result, 0, sizeof(result));
445
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900446 if (lr->end != INT_MAX)
447 len = lr->end - lr->start;
448 ret = find_alternative_probe_point(dinfo, &pp, &result,
449 target, user);
450 if (!ret) {
451 lr->function = result.function;
452 lr->file = result.file;
453 lr->start = result.line;
454 if (lr->end != INT_MAX)
455 lr->end = lr->start + len;
456 clear_perf_probe_point(&pp);
457 }
458 return ret;
459}
460
Masami Hiramatsuff741782011-06-27 16:27:39 +0900461/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000462static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900463{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000464 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900465 char reason[STRERR_BUFSIZE];
466 struct debuginfo *ret = NULL;
467 struct dso *dso = NULL;
468 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900469
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000470 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900471 err = kernel_get_module_dso(module, &dso);
472 if (err < 0) {
473 if (!dso || dso->load_errno == 0) {
474 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
475 strcpy(reason, "(unknown)");
476 } else
477 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000478 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900479 pr_err("Failed to find the path for %s: %s\n",
480 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900481 return NULL;
482 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900483 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900484 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000485 ret = debuginfo__new(path);
486 if (!ret && !silent) {
487 pr_warning("The %s file has no debug information.\n", path);
488 if (!module || !strtailcmp(path, ".ko"))
489 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
490 else
491 pr_warning("Rebuild with -g, ");
492 pr_warning("or install an appropriate debuginfo package.\n");
493 }
494 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400495}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300496
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900497/* For caching the last debuginfo */
498static struct debuginfo *debuginfo_cache;
499static char *debuginfo_cache_path;
500
501static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
502{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900503 const char *path = module;
504
505 /* If the module is NULL, it should be the kernel. */
506 if (!module)
507 path = "kernel";
508
509 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900510 goto out;
511
512 /* Copy module path */
513 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900514 debuginfo_cache_path = strdup(path);
515 if (!debuginfo_cache_path) {
516 debuginfo__delete(debuginfo_cache);
517 debuginfo_cache = NULL;
518 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900519 }
520
521 debuginfo_cache = open_debuginfo(module, silent);
522 if (!debuginfo_cache)
523 zfree(&debuginfo_cache_path);
524out:
525 return debuginfo_cache;
526}
527
528static void debuginfo_cache__exit(void)
529{
530 debuginfo__delete(debuginfo_cache);
531 debuginfo_cache = NULL;
532 zfree(&debuginfo_cache_path);
533}
534
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000535
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000536static int get_text_start_address(const char *exec, unsigned long *address)
537{
538 Elf *elf;
539 GElf_Ehdr ehdr;
540 GElf_Shdr shdr;
541 int fd, ret = -ENOENT;
542
543 fd = open(exec, O_RDONLY);
544 if (fd < 0)
545 return -errno;
546
547 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900548 if (elf == NULL) {
549 ret = -EINVAL;
550 goto out_close;
551 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000552
553 if (gelf_getehdr(elf, &ehdr) == NULL)
554 goto out;
555
556 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
557 goto out;
558
559 *address = shdr.sh_addr - shdr.sh_offset;
560 ret = 0;
561out:
562 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900563out_close:
564 close(fd);
565
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000566 return ret;
567}
568
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000569/*
570 * Convert trace point to probe point with debuginfo
571 */
572static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
573 struct perf_probe_point *pp,
574 bool is_kprobe)
575{
576 struct debuginfo *dinfo = NULL;
577 unsigned long stext = 0;
578 u64 addr = tp->address;
579 int ret = -ENOENT;
580
581 /* convert the address to dwarf address */
582 if (!is_kprobe) {
583 if (!addr) {
584 ret = -EINVAL;
585 goto error;
586 }
587 ret = get_text_start_address(tp->module, &stext);
588 if (ret < 0)
589 goto error;
590 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000591 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900592 /* If the module is given, this returns relative address */
593 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
594 false, !!tp->module);
595 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000596 goto error;
597 addr += tp->offset;
598 }
599
600 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
601 tp->module ? : "kernel");
602
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900603 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
604 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000605 ret = debuginfo__find_probe_point(dinfo,
606 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900607 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000608 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000609
610 if (ret > 0) {
611 pp->retprobe = tp->retprobe;
612 return 0;
613 }
614error:
615 pr_debug("Failed to find corresponding probes from debuginfo.\n");
616 return ret ? : -ENOENT;
617}
618
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000619static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
620 int ntevs, const char *exec)
621{
622 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000623 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000624
625 if (!exec)
626 return 0;
627
628 ret = get_text_start_address(exec, &stext);
629 if (ret < 0)
630 return ret;
631
632 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000633 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000634 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000635 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000636 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000637 ret = -ENOMEM;
638 break;
639 }
640 tevs[i].uprobes = true;
641 }
642
643 return ret;
644}
645
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900646static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
647 int ntevs, const char *module)
648{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900649 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530650 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900651
652 if (!module)
653 return 0;
654
Ravi Bangoria63a29612016-04-26 19:55:41 +0530655 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900656
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900657 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530658 tevs[i].point.module =
659 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900660 if (!tevs[i].point.module) {
661 ret = -ENOMEM;
662 break;
663 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900664 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900665
Ravi Bangoria63a29612016-04-26 19:55:41 +0530666 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900667 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900668}
669
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000670/* Post processing the probe events */
671static int post_process_probe_trace_events(struct probe_trace_event *tevs,
672 int ntevs, const char *module,
673 bool uprobe)
674{
675 struct ref_reloc_sym *reloc_sym;
676 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900677 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000678
679 if (uprobe)
680 return add_exec_to_probe_trace_events(tevs, ntevs, module);
681
682 /* Note that currently ref_reloc_sym based probe is not for drivers */
683 if (module)
684 return add_module_to_probe_trace_events(tevs, ntevs, module);
685
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000686 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000687 if (!reloc_sym) {
688 pr_warning("Relocated base symbol is not found!\n");
689 return -EINVAL;
690 }
691
692 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900693 if (!tevs[i].point.address || tevs[i].point.retprobe)
694 continue;
695 /* If we found a wrong one, mark it by NULL symbol */
696 if (kprobe_warn_out_range(tevs[i].point.symbol,
697 tevs[i].point.address)) {
698 tmp = NULL;
699 skipped++;
700 } else {
701 tmp = strdup(reloc_sym->name);
702 if (!tmp)
703 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000704 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900705 /* If we have no realname, use symbol for it */
706 if (!tevs[i].point.realname)
707 tevs[i].point.realname = tevs[i].point.symbol;
708 else
709 free(tevs[i].point.symbol);
710 tevs[i].point.symbol = tmp;
711 tevs[i].point.offset = tevs[i].point.address -
712 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000713 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900714 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000715}
716
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530718static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900719 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300720{
721 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900722 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530723 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900724 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725
Masami Hiramatsu44225522015-05-08 10:03:28 +0900726 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900727 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000728 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900729 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731 return 0;
732 }
733
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000734 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900735 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900736 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900737
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900738 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900739 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900740 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900741 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900742 /*
743 * Write back to the original probe_event for
744 * setting appropriate (user given) event name
745 */
746 clear_perf_probe_point(&pev->point);
747 memcpy(&pev->point, &tmp, sizeof(tmp));
748 }
749 }
750
Masami Hiramatsuff741782011-06-27 16:27:39 +0900751 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300752
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400753 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000754 pr_debug("Found %d probe_trace_events.\n", ntevs);
755 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900756 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900757 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000758 clear_probe_trace_events(*tevs, ntevs);
759 zfree(tevs);
760 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900761 if (ret != ntevs)
762 return ret < 0 ? ret : ntevs;
763 ntevs = 0;
764 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400765 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300766
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400767 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900768 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400769 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900770 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 }
772 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400773 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000774 if (ntevs < 0) {
775 if (ntevs == -EBADF)
776 pr_warning("Warning: No dwarf info found in the vmlinux - "
777 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400778 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900779 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400780 return 0;
781 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300782 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400783 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784}
785
786#define LINEBUF_SIZE 256
787#define NR_ADDITIONAL_LINES 2
788
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100789static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300790{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000791 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100792 const char *color = show_num ? "" : PERF_COLOR_BLUE;
793 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300794
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100795 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300796 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
797 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100798 if (skip)
799 continue;
800 if (!prefix) {
801 prefix = show_num ? "%7d " : " ";
802 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300803 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100804 color_fprintf(stdout, color, "%s", buf);
805
806 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400807
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100808 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300809error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100810 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000811 pr_warning("File read error: %s\n",
812 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100813 return -1;
814 }
815 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816}
817
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100818static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
819{
820 int rv = __show_one_line(fp, l, skip, show_num);
821 if (rv == 0) {
822 pr_warning("Source file is shorter than expected.\n");
823 rv = -1;
824 }
825 return rv;
826}
827
828#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
829#define show_one_line(f,l) _show_one_line(f,l,false,false)
830#define skip_one_line(f,l) _show_one_line(f,l,true,false)
831#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
832
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300833/*
834 * Show line-range always requires debuginfo to find source file and
835 * line number.
836 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900837static int __show_line_range(struct line_range *lr, const char *module,
838 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300839{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400840 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000841 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900842 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300843 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900844 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900845 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000846 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300847
848 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000849 dinfo = open_debuginfo(module, false);
850 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900851 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400852
Masami Hiramatsuff741782011-06-27 16:27:39 +0900853 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900854 if (!ret) { /* Not found, retry with an alternative */
855 ret = get_alternative_line_range(dinfo, lr, module, user);
856 if (!ret)
857 ret = debuginfo__find_line_range(dinfo, lr);
858 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900859 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000860 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 pr_warning("Specified source line is not found.\n");
862 return -ENOENT;
863 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000864 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 return ret;
866 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300867
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900868 /* Convert source file path */
869 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900870 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800871
872 /* Free old path when new path is assigned */
873 if (tmp != lr->path)
874 free(tmp);
875
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900876 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000877 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900878 return ret;
879 }
880
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300881 setup_pager();
882
883 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900884 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300885 lr->start - lr->offset);
886 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100887 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300888
889 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890 if (fp == NULL) {
891 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000892 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400893 return -errno;
894 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300895 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100896 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100897 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100898 if (ret < 0)
899 goto end;
900 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300901
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000902 intlist__for_each(ln, lr->line_list) {
903 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100904 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100905 if (ret < 0)
906 goto end;
907 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100908 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400909 if (ret < 0)
910 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300911 }
912
913 if (lr->end == INT_MAX)
914 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100915 while (l <= lr->end) {
916 ret = show_one_line_or_eof(fp, l++ - lr->offset);
917 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100918 break;
919 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400920end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300921 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400922 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300923}
924
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000925int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000926{
927 int ret;
928
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900929 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000930 if (ret < 0)
931 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900932 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900933 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000934
935 return ret;
936}
937
Masami Hiramatsuff741782011-06-27 16:27:39 +0900938static int show_available_vars_at(struct debuginfo *dinfo,
939 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900940 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900941{
942 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900943 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944 struct str_node *node;
945 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900946 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900947 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900948
949 buf = synthesize_perf_probe_point(&pev->point);
950 if (!buf)
951 return -EINVAL;
952 pr_debug("Searching variables at %s\n", buf);
953
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900954 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900955 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900956 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900957 if (!ret) {
958 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900959 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900960 /* Release the old probe_point */
961 clear_perf_probe_point(&tmp);
962 }
963 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900964 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000965 if (ret == 0 || ret == -ENOENT) {
966 pr_err("Failed to find the address of %s\n", buf);
967 ret = -ENOENT;
968 } else
969 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900970 goto end;
971 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000972
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900973 /* Some variables are found */
974 fprintf(stdout, "Available variables at %s\n", buf);
975 for (i = 0; i < ret; i++) {
976 vl = &vls[i];
977 /*
978 * A probe point might be converted to
979 * several trace points.
980 */
981 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
982 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300983 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900984 nvars = 0;
985 if (vl->vars) {
986 strlist__for_each(node, vl->vars) {
987 var = strchr(node->s, '\t') + 1;
988 if (strfilter__compare(_filter, var)) {
989 fprintf(stdout, "\t\t%s\n", node->s);
990 nvars++;
991 }
992 }
993 strlist__delete(vl->vars);
994 }
995 if (nvars == 0)
996 fprintf(stdout, "\t\t(No matched variables)\n");
997 }
998 free(vls);
999end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001000 free(buf);
1001 return ret;
1002}
1003
1004/* Show available variables on given probe point */
1005int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001006 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001007{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001008 int i, ret = 0;
1009 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001010
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001011 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001012 if (ret < 0)
1013 return ret;
1014
Masami Hiramatsu44225522015-05-08 10:03:28 +09001015 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001016 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001017 ret = -ENOENT;
1018 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001019 }
1020
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001021 setup_pager();
1022
Masami Hiramatsuff741782011-06-27 16:27:39 +09001023 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001024 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001025
1026 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001027out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001028 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001029 return ret;
1030}
1031
Ingo Molnar89fe8082013-09-30 12:07:11 +02001032#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001033
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001034static void debuginfo_cache__exit(void)
1035{
1036}
1037
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001038static int
1039find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1040 struct perf_probe_point *pp __maybe_unused,
1041 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001042{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001043 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001044}
1045
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301046static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001047 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001048{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 if (perf_probe_event_need_dwarf(pev)) {
1050 pr_warning("Debuginfo-analysis is not supported.\n");
1051 return -ENOSYS;
1052 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301053
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001054 return 0;
1055}
1056
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001057int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001058 const char *module __maybe_unused,
1059 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001060{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001061 pr_warning("Debuginfo-analysis is not supported.\n");
1062 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001063}
1064
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001065int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001066 int npevs __maybe_unused,
1067 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001068{
1069 pr_warning("Debuginfo-analysis is not supported.\n");
1070 return -ENOSYS;
1071}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001072#endif
1073
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001074void line_range__clear(struct line_range *lr)
1075{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001076 free(lr->function);
1077 free(lr->file);
1078 free(lr->path);
1079 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001080 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001081 memset(lr, 0, sizeof(*lr));
1082}
1083
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001084int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001085{
1086 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001087 lr->line_list = intlist__new(NULL);
1088 if (!lr->line_list)
1089 return -ENOMEM;
1090 else
1091 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001092}
1093
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001094static int parse_line_num(char **ptr, int *val, const char *what)
1095{
1096 const char *start = *ptr;
1097
1098 errno = 0;
1099 *val = strtol(*ptr, ptr, 0);
1100 if (errno || *ptr == start) {
1101 semantic_error("'%s' is not a valid number.\n", what);
1102 return -EINVAL;
1103 }
1104 return 0;
1105}
1106
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001107/* Check the name is good for event, group or function */
1108static bool is_c_func_name(const char *name)
1109{
1110 if (!isalpha(*name) && *name != '_')
1111 return false;
1112 while (*++name != '\0') {
1113 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1114 return false;
1115 }
1116 return true;
1117}
1118
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001119/*
1120 * Stuff 'lr' according to the line range described by 'arg'.
1121 * The line range syntax is described by:
1122 *
1123 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001124 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001125 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001126int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001127{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001128 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001129 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001130
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001131 if (!name)
1132 return -ENOMEM;
1133
1134 lr->start = 0;
1135 lr->end = INT_MAX;
1136
1137 range = strchr(name, ':');
1138 if (range) {
1139 *range++ = '\0';
1140
1141 err = parse_line_num(&range, &lr->start, "start line");
1142 if (err)
1143 goto err;
1144
1145 if (*range == '+' || *range == '-') {
1146 const char c = *range++;
1147
1148 err = parse_line_num(&range, &lr->end, "end line");
1149 if (err)
1150 goto err;
1151
1152 if (c == '+') {
1153 lr->end += lr->start;
1154 /*
1155 * Adjust the number of lines here.
1156 * If the number of lines == 1, the
1157 * the end of line should be equal to
1158 * the start of line.
1159 */
1160 lr->end--;
1161 }
1162 }
1163
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001164 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001165
1166 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001167 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001168 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001169 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001170 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001171 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001172 if (*range != '\0') {
1173 semantic_error("Tailing with invalid str '%s'.\n", range);
1174 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001176 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001177
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001178 file = strchr(name, '@');
1179 if (file) {
1180 *file = '\0';
1181 lr->file = strdup(++file);
1182 if (lr->file == NULL) {
1183 err = -ENOMEM;
1184 goto err;
1185 }
1186 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001187 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001188 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001189 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001190 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001191 else { /* Invalid name */
1192 semantic_error("'%s' is not a valid function name.\n", name);
1193 err = -EINVAL;
1194 goto err;
1195 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196
1197 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001198err:
1199 free(name);
1200 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001201}
1202
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001206 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001207 char *ptr, *tmp;
1208 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301209 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210 /*
1211 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001212 * perf probe [EVENT=]SRC[:LN|;PTN]
1213 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001214 *
1215 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001216 */
Wang Nane59d29e2015-04-28 08:46:09 +00001217 if (!arg)
1218 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001219
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001220 ptr = strpbrk(arg, ";=@+%");
1221 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001222 *ptr = '\0';
1223 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 if (strchr(arg, ':')) {
1225 semantic_error("Group name is not supported yet.\n");
1226 return -ENOTSUP;
1227 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001228 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001229 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 "follow C symbol-naming rule.\n", arg);
1231 return -EINVAL;
1232 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001233 pev->event = strdup(arg);
1234 if (pev->event == NULL)
1235 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001236 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001237 arg = tmp;
1238 }
1239
Naveen N. Rao3099c022015-04-28 17:35:34 +05301240 /*
1241 * Check arg is function or file name and copy it.
1242 *
1243 * We consider arg to be a file spec if and only if it satisfies
1244 * all of the below criteria::
1245 * - it does not include any of "+@%",
1246 * - it includes one of ":;", and
1247 * - it has a period '.' in the name.
1248 *
1249 * Otherwise, we consider arg to be a function specification.
1250 */
1251 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1252 /* This is a file spec if it includes a '.' before ; or : */
1253 if (memchr(arg, '.', ptr - arg))
1254 file_spec = true;
1255 }
1256
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001257 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 if (ptr) {
1259 nc = *ptr;
1260 *ptr++ = '\0';
1261 }
1262
Wang Nan6c6e0242015-08-26 10:57:44 +00001263 if (arg[0] == '\0')
1264 tmp = NULL;
1265 else {
1266 tmp = strdup(arg);
1267 if (tmp == NULL)
1268 return -ENOMEM;
1269 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001270
Naveen N. Rao3099c022015-04-28 17:35:34 +05301271 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001272 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001273 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001274 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001275
Wang Nanda15bd92015-08-26 10:57:45 +00001276 /*
1277 * Keep pp->function even if this is absolute address,
1278 * so it can mark whether abs_address is valid.
1279 * Which make 'perf probe lib.bin 0x0' possible.
1280 *
1281 * Note that checking length of tmp is not needed
1282 * because when we access tmp[1] we know tmp[0] is '0',
1283 * so tmp[1] should always valid (but could be '\0').
1284 */
1285 if (tmp && !strncmp(tmp, "0x", 2)) {
1286 pp->abs_address = strtoul(pp->function, &tmp, 0);
1287 if (*tmp != '\0') {
1288 semantic_error("Invalid absolute address.\n");
1289 return -EINVAL;
1290 }
1291 }
1292 }
1293
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001294 /* Parse other options */
1295 while (ptr) {
1296 arg = ptr;
1297 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001298 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001299 pp->lazy_line = strdup(arg);
1300 if (pp->lazy_line == NULL)
1301 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001302 break;
1303 }
1304 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001305 if (ptr) {
1306 nc = *ptr;
1307 *ptr++ = '\0';
1308 }
1309 switch (c) {
1310 case ':': /* Line number */
1311 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001313 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001314 " in line number.\n");
1315 return -EINVAL;
1316 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001317 break;
1318 case '+': /* Byte offset from a symbol */
1319 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001320 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001321 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 " in offset.\n");
1323 return -EINVAL;
1324 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001325 break;
1326 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 if (pp->file) {
1328 semantic_error("SRC@SRC is not allowed.\n");
1329 return -EINVAL;
1330 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001331 pp->file = strdup(arg);
1332 if (pp->file == NULL)
1333 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001334 break;
1335 case '%': /* Probe places */
1336 if (strcmp(arg, "return") == 0) {
1337 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001338 } else { /* Others not supported yet */
1339 semantic_error("%%%s is not supported.\n", arg);
1340 return -ENOTSUP;
1341 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001342 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001343 default: /* Buggy case */
1344 pr_err("This program has a bug at %s:%d.\n",
1345 __FILE__, __LINE__);
1346 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001347 break;
1348 }
1349 }
1350
1351 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001352 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001353 semantic_error("Lazy pattern can't be used with"
1354 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 return -EINVAL;
1356 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001357
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001358 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001359 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 return -EINVAL;
1361 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001362
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001364 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365 return -EINVAL;
1366 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001367
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001369 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001370 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 return -EINVAL;
1372 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001373
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001374 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001375 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 return -EINVAL;
1377 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001378
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001380 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 return -EINVAL;
1382 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001383
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001385 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001386 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 return -EINVAL;
1388 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001389
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001391 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1392 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394}
1395
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001396/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001398{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001399 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001400 struct perf_probe_arg_field **fieldp;
1401
1402 pr_debug("parsing arg: %s into ", str);
1403
Masami Hiramatsu48481932010-04-12 13:16:53 -04001404 tmp = strchr(str, '=');
1405 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001406 arg->name = strndup(str, tmp - str);
1407 if (arg->name == NULL)
1408 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001409 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001410 str = tmp + 1;
1411 }
1412
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001413 tmp = strchr(str, ':');
1414 if (tmp) { /* Type setting */
1415 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001416 arg->type = strdup(tmp + 1);
1417 if (arg->type == NULL)
1418 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001419 pr_debug("type:%s ", arg->type);
1420 }
1421
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001422 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001423 if (!is_c_varname(str) || !tmp) {
1424 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001425 arg->var = strdup(str);
1426 if (arg->var == NULL)
1427 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001428 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001430 }
1431
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001432 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001433 arg->var = strndup(str, tmp - str);
1434 if (arg->var == NULL)
1435 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001436 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001437 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001438 fieldp = &arg->field;
1439
1440 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001441 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1442 if (*fieldp == NULL)
1443 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001444 if (*tmp == '[') { /* Array */
1445 str = tmp;
1446 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001447 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001448 if (*tmp != ']' || tmp == str + 1) {
1449 semantic_error("Array index must be a"
1450 " number.\n");
1451 return -EINVAL;
1452 }
1453 tmp++;
1454 if (*tmp == '\0')
1455 tmp = NULL;
1456 } else { /* Structure */
1457 if (*tmp == '.') {
1458 str = tmp + 1;
1459 (*fieldp)->ref = false;
1460 } else if (tmp[1] == '>') {
1461 str = tmp + 2;
1462 (*fieldp)->ref = true;
1463 } else {
1464 semantic_error("Argument parse error: %s\n",
1465 str);
1466 return -EINVAL;
1467 }
1468 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001470 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001471 (*fieldp)->name = strndup(str, tmp - str);
1472 if ((*fieldp)->name == NULL)
1473 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001474 if (*str != '[')
1475 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001476 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1477 fieldp = &(*fieldp)->next;
1478 }
1479 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001480 (*fieldp)->name = strdup(str);
1481 if ((*fieldp)->name == NULL)
1482 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001483 if (*str != '[')
1484 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001485 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001486
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001487 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001488 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001489 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001490 if (arg->name == NULL)
1491 return -ENOMEM;
1492 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001493 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001494}
1495
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001497int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001498{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001499 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001500 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001501
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001502 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 if (!argv) {
1504 pr_debug("Failed to split arguments.\n");
1505 return -ENOMEM;
1506 }
1507 if (argc - 1 > MAX_PROBE_ARGS) {
1508 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1509 ret = -ERANGE;
1510 goto out;
1511 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001512 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001513 ret = parse_perf_probe_point(argv[0], pev);
1514 if (ret < 0)
1515 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001516
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001517 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001518 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001519 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1520 if (pev->args == NULL) {
1521 ret = -ENOMEM;
1522 goto out;
1523 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1525 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1526 if (ret >= 0 &&
1527 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001528 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 " kretprobe.\n");
1530 ret = -EINVAL;
1531 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001532 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001533out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001534 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535
1536 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001537}
1538
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001539/* Return true if this perf_probe_event requires debuginfo */
1540bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001542 int i;
1543
1544 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1545 return true;
1546
1547 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001548 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001549 return true;
1550
1551 return false;
1552}
1553
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301554/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001555int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001556{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301557 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001558 char pr;
1559 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001560 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001561 int ret, i, argc;
1562 char **argv;
1563
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301564 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001565 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001566 if (!argv) {
1567 pr_debug("Failed to split arguments.\n");
1568 return -ENOMEM;
1569 }
1570 if (argc < 2) {
1571 semantic_error("Too few probe arguments.\n");
1572 ret = -ERANGE;
1573 goto out;
1574 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001575
1576 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001577 argv0_str = strdup(argv[0]);
1578 if (argv0_str == NULL) {
1579 ret = -ENOMEM;
1580 goto out;
1581 }
1582 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1583 fmt2_str = strtok_r(NULL, "/", &fmt);
1584 fmt3_str = strtok_r(NULL, " \t", &fmt);
1585 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1586 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587 semantic_error("Failed to parse event name: %s\n", argv[0]);
1588 ret = -EINVAL;
1589 goto out;
1590 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001591 pr = fmt1_str[0];
1592 tev->group = strdup(fmt2_str);
1593 tev->event = strdup(fmt3_str);
1594 if (tev->group == NULL || tev->event == NULL) {
1595 ret = -ENOMEM;
1596 goto out;
1597 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001599
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001600 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001601
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001602 /* Scan module name(if there), function name and offset */
1603 p = strchr(argv[1], ':');
1604 if (p) {
1605 tp->module = strndup(argv[1], p - argv[1]);
1606 p++;
1607 } else
1608 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001609 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001610 /* only the address started with 0x */
1611 if (fmt1_str[0] == '0') {
1612 /*
1613 * Fix a special case:
1614 * if address == 0, kernel reports something like:
1615 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1616 * Newer kernel may fix that, but we want to
1617 * support old kernel also.
1618 */
1619 if (strcmp(fmt1_str, "0x") == 0) {
1620 if (!argv[2] || strcmp(argv[2], "(null)")) {
1621 ret = -EINVAL;
1622 goto out;
1623 }
1624 tp->address = 0;
1625
1626 free(argv[2]);
1627 for (i = 2; argv[i + 1] != NULL; i++)
1628 argv[i] = argv[i + 1];
1629
1630 argv[i] = NULL;
1631 argc -= 1;
1632 } else
1633 tp->address = strtoul(fmt1_str, NULL, 0);
1634 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001635 /* Only the symbol-based probe has offset */
1636 tp->symbol = strdup(fmt1_str);
1637 if (tp->symbol == NULL) {
1638 ret = -ENOMEM;
1639 goto out;
1640 }
1641 fmt2_str = strtok_r(NULL, "", &fmt);
1642 if (fmt2_str == NULL)
1643 tp->offset = 0;
1644 else
1645 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001646 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001647
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001648 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301649 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001650 if (tev->args == NULL) {
1651 ret = -ENOMEM;
1652 goto out;
1653 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001654 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001655 p = strchr(argv[i + 2], '=');
1656 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001657 *p++ = '\0';
1658 else
1659 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001660 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001662 tev->args[i].value = strdup(p);
1663 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1664 ret = -ENOMEM;
1665 goto out;
1666 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001667 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001668 ret = 0;
1669out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001670 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001671 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001672 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001673}
1674
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001675/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001676char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001677{
1678 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001679 struct strbuf buf;
1680 char *ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001681
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001682 strbuf_init(&buf, 64);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001683 if (pa->name && pa->var)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001684 strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001685 else
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001686 strbuf_addstr(&buf, pa->name ?: pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001687
1688 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001689 if (field->name[0] == '[')
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001690 strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001691 else
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001692 strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1693 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001694 field = field->next;
1695 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001696
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001697 if (pa->type)
1698 strbuf_addf(&buf, ":%s", pa->type);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001699
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001700 ret = strbuf_detach(&buf, NULL);
1701
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001702 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001703}
1704
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001705/* Compose only probe point (not argument) */
1706static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001707{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001708 struct strbuf buf;
1709 char *tmp;
1710 int len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001711
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001712 strbuf_init(&buf, 64);
1713 if (pp->function) {
1714 strbuf_addstr(&buf, pp->function);
1715 if (pp->offset)
1716 strbuf_addf(&buf, "+%lu", pp->offset);
1717 else if (pp->line)
1718 strbuf_addf(&buf, ":%d", pp->line);
1719 else if (pp->retprobe)
1720 strbuf_addstr(&buf, "%return");
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001721 }
1722 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001723 tmp = pp->file;
1724 len = strlen(tmp);
1725 if (len > 30) {
1726 tmp = strchr(pp->file + len - 30, '/');
1727 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1728 }
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001729 strbuf_addf(&buf, "@%s", tmp);
1730 if (!pp->function && pp->line)
1731 strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001732 }
1733
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001734 return strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735}
1736
1737#if 0
1738char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1739{
1740 char *buf;
1741 int i, len, ret;
1742
1743 buf = synthesize_perf_probe_point(&pev->point);
1744 if (!buf)
1745 return NULL;
1746
1747 len = strlen(buf);
1748 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001749 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001750 pev->args[i].name);
1751 if (ret <= 0) {
1752 free(buf);
1753 return NULL;
1754 }
1755 len += ret;
1756 }
1757
1758 return buf;
1759}
1760#endif
1761
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301762static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001763 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001764{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301766 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001767 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001768 if (depth < 0)
1769 goto out;
1770 }
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001771 strbuf_addf(buf, "%+ld(", ref->offset);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772out:
1773 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001774}
1775
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301776static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001777 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301779 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001780 int depth = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001781
1782 /* Argument name or separator */
1783 if (arg->name)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001784 strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001785 else
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001786 strbuf_addch(buf, ' ');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001787
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001788 /* Special case: @XXX */
1789 if (arg->value[0] == '@' && arg->ref)
1790 ref = ref->next;
1791
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001792 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001793 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001794 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001795 if (depth < 0)
1796 return depth;
1797 }
1798
1799 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001800 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001801 strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001802 else
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001803 strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001804
1805 /* Closing */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001806 while (depth--)
1807 strbuf_addch(buf, ')');
Masami Hiramatsu49849122010-04-12 13:17:15 -04001808 /* Print argument type */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001809 if (arg->type)
1810 strbuf_addf(buf, ":%s", arg->type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001811
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001812 return 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001813}
1814
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301815char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001816{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301817 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001818 struct strbuf buf;
1819 char *ret = NULL;
1820 int i;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001821
Wang Nanda15bd92015-08-26 10:57:45 +00001822 /* Uprobes must have tp->module */
1823 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001824 return NULL;
1825
1826 strbuf_init(&buf, 32);
1827 strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1828 tev->group, tev->event);
Wang Nanda15bd92015-08-26 10:57:45 +00001829 /*
1830 * If tp->address == 0, then this point must be a
1831 * absolute address uprobe.
1832 * try_to_find_absolute_address() should have made
1833 * tp->symbol to "0x0".
1834 */
1835 if (tev->uprobes && !tp->address) {
1836 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1837 goto error;
1838 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001839
1840 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301841 if (tev->uprobes)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001842 strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001843 else if (!strncmp(tp->symbol, "0x", 2))
1844 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001845 strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1846 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301847 else
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001848 strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1849 tp->module ? ":" : "", tp->symbol, tp->offset);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301850
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001851 for (i = 0; i < tev->nargs; i++)
1852 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001853 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001854
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001855 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001856error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001857 strbuf_release(&buf);
1858 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001860
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001861static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1862 struct perf_probe_point *pp,
1863 bool is_kprobe)
1864{
1865 struct symbol *sym = NULL;
1866 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001867 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001868 int ret = -ENOENT;
1869
1870 if (!is_kprobe) {
1871 map = dso__new_map(tp->module);
1872 if (!map)
1873 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001874 sym = map__find_symbol(map, addr, NULL);
1875 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001876 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09001877 if (kernel_get_symbol_address_by_name(tp->symbol,
1878 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001879 goto out;
1880 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001881 if (addr) {
1882 addr += tp->offset;
1883 sym = __find_kernel_function(addr, &map);
1884 }
1885 }
Wang Nan98d3b252015-11-05 13:19:25 +00001886
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001887 if (!sym)
1888 goto out;
1889
1890 pp->retprobe = tp->retprobe;
1891 pp->offset = addr - map->unmap_ip(map, sym->start);
1892 pp->function = strdup(sym->name);
1893 ret = pp->function ? 0 : -ENOMEM;
1894
1895out:
1896 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001897 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001898 }
1899
1900 return ret;
1901}
1902
1903static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001904 struct perf_probe_point *pp,
1905 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001906{
1907 char buf[128];
1908 int ret;
1909
1910 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1911 if (!ret)
1912 return 0;
1913 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1914 if (!ret)
1915 return 0;
1916
1917 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1918
1919 if (tp->symbol) {
1920 pp->function = strdup(tp->symbol);
1921 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001922 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001923 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1924 if (ret < 0)
1925 return ret;
1926 pp->function = strdup(buf);
1927 pp->offset = 0;
1928 }
1929 if (pp->function == NULL)
1930 return -ENOMEM;
1931
1932 pp->retprobe = tp->retprobe;
1933
1934 return 0;
1935}
1936
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301937static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301938 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001939{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001940 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001941 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001943 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001944 pev->event = strdup(tev->event);
1945 pev->group = strdup(tev->group);
1946 if (pev->event == NULL || pev->group == NULL)
1947 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001948
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001949 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001950 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001951 if (ret < 0)
1952 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001953
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001954 /* Convert trace_arg to probe_arg */
1955 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001956 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1957 if (pev->args == NULL)
1958 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001959 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001960 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001961 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001962 else {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001963 strbuf_init(&buf, 32);
1964 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
1965 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001966 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001967 if (pev->args[i].name == NULL && ret >= 0)
1968 ret = -ENOMEM;
1969 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001970
1971 if (ret < 0)
1972 clear_perf_probe_event(pev);
1973
1974 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001975}
1976
1977void clear_perf_probe_event(struct perf_probe_event *pev)
1978{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001979 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001980 int i;
1981
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001982 free(pev->event);
1983 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001984 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001985 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001986
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001987 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001988 free(pev->args[i].name);
1989 free(pev->args[i].var);
1990 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001991 field = pev->args[i].field;
1992 while (field) {
1993 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001994 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001995 free(field);
1996 field = next;
1997 }
1998 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001999 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002000 memset(pev, 0, sizeof(*pev));
2001}
2002
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002003void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002004{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302005 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002006 int i;
2007
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002008 free(tev->event);
2009 free(tev->group);
2010 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002011 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002012 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002013 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002014 free(tev->args[i].name);
2015 free(tev->args[i].value);
2016 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002017 ref = tev->args[i].ref;
2018 while (ref) {
2019 next = ref->next;
2020 free(ref);
2021 ref = next;
2022 }
2023 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002024 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002025 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002026}
2027
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002028struct kprobe_blacklist_node {
2029 struct list_head list;
2030 unsigned long start;
2031 unsigned long end;
2032 char *symbol;
2033};
2034
2035static void kprobe_blacklist__delete(struct list_head *blacklist)
2036{
2037 struct kprobe_blacklist_node *node;
2038
2039 while (!list_empty(blacklist)) {
2040 node = list_first_entry(blacklist,
2041 struct kprobe_blacklist_node, list);
2042 list_del(&node->list);
2043 free(node->symbol);
2044 free(node);
2045 }
2046}
2047
2048static int kprobe_blacklist__load(struct list_head *blacklist)
2049{
2050 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002051 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002052 char buf[PATH_MAX], *p;
2053 FILE *fp;
2054 int ret;
2055
2056 if (__debugfs == NULL)
2057 return -ENOTSUP;
2058
2059 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2060 if (ret < 0)
2061 return ret;
2062
2063 fp = fopen(buf, "r");
2064 if (!fp)
2065 return -errno;
2066
2067 ret = 0;
2068 while (fgets(buf, PATH_MAX, fp)) {
2069 node = zalloc(sizeof(*node));
2070 if (!node) {
2071 ret = -ENOMEM;
2072 break;
2073 }
2074 INIT_LIST_HEAD(&node->list);
2075 list_add_tail(&node->list, blacklist);
2076 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2077 ret = -EINVAL;
2078 break;
2079 }
2080 p = strchr(buf, '\t');
2081 if (p) {
2082 p++;
2083 if (p[strlen(p) - 1] == '\n')
2084 p[strlen(p) - 1] = '\0';
2085 } else
2086 p = (char *)"unknown";
2087 node->symbol = strdup(p);
2088 if (!node->symbol) {
2089 ret = -ENOMEM;
2090 break;
2091 }
2092 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2093 node->start, node->end, node->symbol);
2094 ret++;
2095 }
2096 if (ret < 0)
2097 kprobe_blacklist__delete(blacklist);
2098 fclose(fp);
2099
2100 return ret;
2101}
2102
2103static struct kprobe_blacklist_node *
2104kprobe_blacklist__find_by_address(struct list_head *blacklist,
2105 unsigned long address)
2106{
2107 struct kprobe_blacklist_node *node;
2108
2109 list_for_each_entry(node, blacklist, list) {
2110 if (node->start <= address && address <= node->end)
2111 return node;
2112 }
2113
2114 return NULL;
2115}
2116
Masami Hiramatsub0312202015-06-16 20:50:55 +09002117static LIST_HEAD(kprobe_blacklist);
2118
2119static void kprobe_blacklist__init(void)
2120{
2121 if (!list_empty(&kprobe_blacklist))
2122 return;
2123
2124 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2125 pr_debug("No kprobe blacklist support, ignored\n");
2126}
2127
2128static void kprobe_blacklist__release(void)
2129{
2130 kprobe_blacklist__delete(&kprobe_blacklist);
2131}
2132
2133static bool kprobe_blacklist__listed(unsigned long address)
2134{
2135 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2136}
2137
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002138static int perf_probe_event__sprintf(const char *group, const char *event,
2139 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002140 const char *module,
2141 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002142{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002143 int i;
2144 char *buf;
2145
2146 if (asprintf(&buf, "%s:%s", group, event) < 0)
2147 return -errno;
2148 strbuf_addf(result, " %-20s (on ", buf);
2149 free(buf);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002150
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002151 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002152 buf = synthesize_perf_probe_point(&pev->point);
2153 if (!buf)
2154 return -ENOMEM;
2155 strbuf_addstr(result, buf);
2156 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002157
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002158 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002159 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002160
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002161 if (pev->nargs > 0) {
Arnaldo Carvalho de Melo88fd6332016-03-23 16:44:49 -03002162 strbuf_add(result, " with", 5);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002163 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002164 buf = synthesize_perf_probe_arg(&pev->args[i]);
2165 if (!buf)
2166 return -ENOMEM;
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002167 strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002168 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002169 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002170 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002171 strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002172
2173 return 0;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002174}
2175
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002176/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002177int show_perf_probe_event(const char *group, const char *event,
2178 struct perf_probe_event *pev,
2179 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002180{
2181 struct strbuf buf = STRBUF_INIT;
2182 int ret;
2183
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002184 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002185 if (ret >= 0) {
2186 if (use_stdout)
2187 printf("%s\n", buf.buf);
2188 else
2189 pr_info("%s\n", buf.buf);
2190 }
2191 strbuf_release(&buf);
2192
2193 return ret;
2194}
2195
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002196static bool filter_probe_trace_event(struct probe_trace_event *tev,
2197 struct strfilter *filter)
2198{
2199 char tmp[128];
2200
2201 /* At first, check the event name itself */
2202 if (strfilter__compare(filter, tev->event))
2203 return true;
2204
2205 /* Next, check the combination of name and group */
2206 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2207 return false;
2208 return strfilter__compare(filter, tmp);
2209}
2210
2211static int __show_perf_probe_events(int fd, bool is_kprobe,
2212 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002213{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302214 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302215 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002216 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002217 struct strlist *rawlist;
2218 struct str_node *ent;
2219
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002220 memset(&tev, 0, sizeof(tev));
2221 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002222
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002223 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002224 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002225 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002226
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002227 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302228 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002229 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002230 if (!filter_probe_trace_event(&tev, filter))
2231 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302232 ret = convert_to_perf_probe_event(&tev, &pev,
2233 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002234 if (ret < 0)
2235 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002236 ret = show_perf_probe_event(pev.group, pev.event,
2237 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002238 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002239 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002240next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002241 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302242 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002243 if (ret < 0)
2244 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002245 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002246 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002247 /* Cleanup cached debuginfo if needed */
2248 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002249
2250 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002251}
2252
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302253/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002254int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302255{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002256 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302257
2258 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302259
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002260 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302261 if (ret < 0)
2262 return ret;
2263
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002264 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2265 if (ret < 0)
2266 return ret;
2267
2268 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002269 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002270 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002271 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002272 if (kp_fd > 0)
2273 close(kp_fd);
2274 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002275 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002276 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302277
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002278 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002279}
2280
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002281static int get_new_event_name(char *buf, size_t len, const char *base,
2282 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002283{
2284 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002285 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002286
Naveen N. Rao3099c022015-04-28 17:35:34 +05302287 if (*base == '.')
2288 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002289 nbase = strdup(base);
2290 if (!nbase)
2291 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302292
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002293 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2294 p = strchr(nbase, '.');
2295 if (p && p != nbase)
2296 *p = '\0';
2297
2298 /* Try no suffix number */
2299 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002300 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002301 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002302 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002303 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002304 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002305 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002306
Masami Hiramatsud761b082009-12-15 10:32:25 -05002307 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002308 pr_warning("Error: event \"%s\" already exists.\n"
2309 " Hint: Remove existing event by 'perf probe -d'\n"
2310 " or force duplicates by 'perf probe -f'\n"
2311 " or set 'force=yes' in BPF source.\n",
2312 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002313 ret = -EEXIST;
2314 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002315 }
2316
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002317 /* Try to add suffix */
2318 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002319 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002320 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002321 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002322 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002323 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002324 if (!strlist__has_entry(namelist, buf))
2325 break;
2326 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002327 if (i == MAX_EVENT_INDEX) {
2328 pr_warning("Too many events are on the same function.\n");
2329 ret = -ERANGE;
2330 }
2331
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002332out:
2333 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002334 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002335}
2336
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002337/* Warn if the current kernel's uprobe implementation is old */
2338static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2339{
2340 int i;
2341 char *buf = synthesize_probe_trace_command(tev);
2342
2343 /* Old uprobe event doesn't support memory dereference */
2344 if (!tev->uprobes || tev->nargs == 0 || !buf)
2345 goto out;
2346
2347 for (i = 0; i < tev->nargs; i++)
2348 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2349 pr_warning("Please upgrade your kernel to at least "
2350 "3.14 to have access to feature %s\n",
2351 tev->args[i].value);
2352 break;
2353 }
2354out:
2355 free(buf);
2356}
2357
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002358/* Set new name from original perf_probe_event and namelist */
2359static int probe_trace_event__set_name(struct probe_trace_event *tev,
2360 struct perf_probe_event *pev,
2361 struct strlist *namelist,
2362 bool allow_suffix)
2363{
2364 const char *event, *group;
2365 char buf[64];
2366 int ret;
2367
2368 if (pev->event)
2369 event = pev->event;
2370 else
Wang Nanda15bd92015-08-26 10:57:45 +00002371 if (pev->point.function &&
2372 (strncmp(pev->point.function, "0x", 2) != 0) &&
2373 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002374 event = pev->point.function;
2375 else
2376 event = tev->point.realname;
2377 if (pev->group)
2378 group = pev->group;
2379 else
2380 group = PERFPROBE_GROUP;
2381
2382 /* Get an unused new event name */
2383 ret = get_new_event_name(buf, 64, event,
2384 namelist, allow_suffix);
2385 if (ret < 0)
2386 return ret;
2387
2388 event = buf;
2389
2390 tev->event = strdup(event);
2391 tev->group = strdup(group);
2392 if (tev->event == NULL || tev->group == NULL)
2393 return -ENOMEM;
2394
2395 /* Add added event name to namelist */
2396 strlist__add(namelist, event);
2397 return 0;
2398}
2399
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302400static int __add_probe_trace_events(struct perf_probe_event *pev,
2401 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002403{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002404 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302405 struct probe_trace_event *tev = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002406 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002407
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002408 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2409 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002410 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002411
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002412 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002413 namelist = probe_file__get_namelist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002414 if (!namelist) {
2415 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002416 ret = -ENOMEM;
2417 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002418 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002419
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002420 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002421 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002422 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002423 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002424 if (!tev->point.symbol)
2425 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002426
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002427 /* Set new name for tev (and update namelist) */
2428 ret = probe_trace_event__set_name(tev, pev, namelist,
2429 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002430 if (ret < 0)
2431 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002432
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002433 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002434 if (ret < 0)
2435 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002436
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002437 /*
2438 * Probes after the first probe which comes from same
2439 * user input are always allowed to add suffix, because
2440 * there might be several addresses corresponding to
2441 * one code line.
2442 */
2443 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002444 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002445 if (ret == -EINVAL && pev->uprobes)
2446 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002448 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002449close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002450 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002451 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002452}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002453
Wang Nan6bb536c2015-05-29 09:45:47 +00002454static int find_probe_functions(struct map *map, char *name,
2455 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002456{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002457 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002458 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002459 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002460
Wang Nan75e4a2a2015-05-15 12:14:44 +00002461 if (map__load(map, NULL) < 0)
2462 return 0;
2463
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002464 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002465 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002466 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002467 if (syms && found < probe_conf.max_probes)
2468 syms[found - 1] = sym;
2469 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002470 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002471
2472 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002473}
2474
2475#define strdup_or_goto(str, label) \
2476 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2477
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302478void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2479 struct probe_trace_event *tev __maybe_unused,
2480 struct map *map __maybe_unused) { }
2481
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002482/*
2483 * Find probe function addresses from map.
2484 * Return an error or the number of found probe_trace_event
2485 */
2486static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002487 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002488{
2489 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002490 struct ref_reloc_sym *reloc_sym = NULL;
2491 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002492 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002493 struct probe_trace_event *tev;
2494 struct perf_probe_point *pp = &pev->point;
2495 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002496 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002497 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302498 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002499
Masami Hiramatsu44225522015-05-08 10:03:28 +09002500 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002501 if (!map) {
2502 ret = -EINVAL;
2503 goto out;
2504 }
2505
Wang Nan6bb536c2015-05-29 09:45:47 +00002506 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2507 if (!syms) {
2508 ret = -ENOMEM;
2509 goto out;
2510 }
2511
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002512 /*
2513 * Load matched symbols: Since the different local symbols may have
2514 * same name but different addresses, this lists all the symbols.
2515 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002516 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002517 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002518 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002519 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002520 ret = -ENOENT;
2521 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002522 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002523 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002524 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002525 ret = -E2BIG;
2526 goto out;
2527 }
2528
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002529 /* Note that the symbols in the kmodule are not relocated */
2530 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002531 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002532 if (!reloc_sym) {
2533 pr_warning("Relocated base symbol is not found!\n");
2534 ret = -EINVAL;
2535 goto out;
2536 }
2537 }
2538
2539 /* Setup result trace-probe-events */
2540 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2541 if (!*tevs) {
2542 ret = -ENOMEM;
2543 goto out;
2544 }
2545
2546 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002547
Wang Nan6bb536c2015-05-29 09:45:47 +00002548 for (j = 0; j < num_matched_functions; j++) {
2549 sym = syms[j];
2550
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002551 tev = (*tevs) + ret;
2552 tp = &tev->point;
2553 if (ret == num_matched_functions) {
2554 pr_warning("Too many symbols are listed. Skip it.\n");
2555 break;
2556 }
2557 ret++;
2558
2559 if (pp->offset > sym->end - sym->start) {
2560 pr_warning("Offset %ld is bigger than the size of %s\n",
2561 pp->offset, sym->name);
2562 ret = -ENOENT;
2563 goto err_out;
2564 }
2565 /* Add one probe point */
2566 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002567
2568 /* Check the kprobe (not in module) is within .text */
2569 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002570 kprobe_warn_out_range(sym->name, tp->address)) {
2571 tp->symbol = NULL; /* Skip it */
2572 skipped++;
2573 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002574 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2575 tp->offset = tp->address - reloc_sym->addr;
2576 } else {
2577 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2578 tp->offset = pp->offset;
2579 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002580 tp->realname = strdup_or_goto(sym->name, nomem_out);
2581
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002582 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302583 if (pev->target) {
2584 if (pev->uprobes) {
2585 tev->point.module = strdup_or_goto(pev->target,
2586 nomem_out);
2587 } else {
2588 mod_name = find_module_name(pev->target);
2589 tev->point.module =
2590 strdup(mod_name ? mod_name : pev->target);
2591 free(mod_name);
2592 if (!tev->point.module)
2593 goto nomem_out;
2594 }
2595 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002596 tev->uprobes = pev->uprobes;
2597 tev->nargs = pev->nargs;
2598 if (tev->nargs) {
2599 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2600 tev->nargs);
2601 if (tev->args == NULL)
2602 goto nomem_out;
2603 }
2604 for (i = 0; i < tev->nargs; i++) {
2605 if (pev->args[i].name)
2606 tev->args[i].name =
2607 strdup_or_goto(pev->args[i].name,
2608 nomem_out);
2609
2610 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2611 nomem_out);
2612 if (pev->args[i].type)
2613 tev->args[i].type =
2614 strdup_or_goto(pev->args[i].type,
2615 nomem_out);
2616 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302617 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002618 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002619 if (ret == skipped) {
2620 ret = -ENOENT;
2621 goto err_out;
2622 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002623
2624out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002625 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002626 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002627 return ret;
2628
2629nomem_out:
2630 ret = -ENOMEM;
2631err_out:
2632 clear_probe_trace_events(*tevs, num_matched_functions);
2633 zfree(tevs);
2634 goto out;
2635}
2636
Wang Nanda15bd92015-08-26 10:57:45 +00002637static int try_to_find_absolute_address(struct perf_probe_event *pev,
2638 struct probe_trace_event **tevs)
2639{
2640 struct perf_probe_point *pp = &pev->point;
2641 struct probe_trace_event *tev;
2642 struct probe_trace_point *tp;
2643 int i, err;
2644
2645 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2646 return -EINVAL;
2647 if (perf_probe_event_need_dwarf(pev))
2648 return -EINVAL;
2649
2650 /*
2651 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2652 * absolute address.
2653 *
2654 * Only one tev can be generated by this.
2655 */
2656 *tevs = zalloc(sizeof(*tev));
2657 if (!*tevs)
2658 return -ENOMEM;
2659
2660 tev = *tevs;
2661 tp = &tev->point;
2662
2663 /*
2664 * Don't use tp->offset, use address directly, because
2665 * in synthesize_probe_trace_command() address cannot be
2666 * zero.
2667 */
2668 tp->address = pev->point.abs_address;
2669 tp->retprobe = pp->retprobe;
2670 tev->uprobes = pev->uprobes;
2671
2672 err = -ENOMEM;
2673 /*
2674 * Give it a '0x' leading symbol name.
2675 * In __add_probe_trace_events, a NULL symbol is interpreted as
2676 * invalud.
2677 */
2678 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2679 goto errout;
2680
2681 /* For kprobe, check range */
2682 if ((!tev->uprobes) &&
2683 (kprobe_warn_out_range(tev->point.symbol,
2684 tev->point.address))) {
2685 err = -EACCES;
2686 goto errout;
2687 }
2688
2689 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2690 goto errout;
2691
2692 if (pev->target) {
2693 tp->module = strdup(pev->target);
2694 if (!tp->module)
2695 goto errout;
2696 }
2697
2698 if (tev->group) {
2699 tev->group = strdup(pev->group);
2700 if (!tev->group)
2701 goto errout;
2702 }
2703
2704 if (pev->event) {
2705 tev->event = strdup(pev->event);
2706 if (!tev->event)
2707 goto errout;
2708 }
2709
2710 tev->nargs = pev->nargs;
2711 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2712 if (!tev->args) {
2713 err = -ENOMEM;
2714 goto errout;
2715 }
2716 for (i = 0; i < tev->nargs; i++)
2717 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2718
2719 return 1;
2720
2721errout:
2722 if (*tevs) {
2723 clear_probe_trace_events(*tevs, 1);
2724 *tevs = NULL;
2725 }
2726 return err;
2727}
2728
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302729bool __weak arch__prefers_symtab(void) { return false; }
2730
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302731static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002732 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002733{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002734 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002735
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09002736 if (!pev->group) {
2737 /* Set group name if not given */
2738 if (!pev->uprobes) {
2739 pev->group = strdup(PERFPROBE_GROUP);
2740 ret = pev->group ? 0 : -ENOMEM;
2741 } else
2742 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002743 if (ret != 0) {
2744 pr_warning("Failed to make a group name.\n");
2745 return ret;
2746 }
2747 }
2748
Wang Nanda15bd92015-08-26 10:57:45 +00002749 ret = try_to_find_absolute_address(pev, tevs);
2750 if (ret > 0)
2751 return ret;
2752
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302753 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002754 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302755 if (ret > 0)
2756 return ret; /* Found in symbol table */
2757 }
2758
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002759 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002760 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002761 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002762 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002763
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002764 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002765}
2766
Wang Nan12fae5e2015-09-04 21:16:00 +09002767int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002768{
Namhyung Kim844dffa2015-09-04 21:15:59 +09002769 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002770
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002771 /* Loop 1: convert all events */
2772 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09002773 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09002774 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09002775 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002776 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09002777 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002778 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002779 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09002780 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002781 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002782 /* This just release blacklist only if allocated */
2783 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002784
Namhyung Kim844dffa2015-09-04 21:15:59 +09002785 return 0;
2786}
2787
Wang Nan12fae5e2015-09-04 21:16:00 +09002788int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002789{
2790 int i, ret = 0;
2791
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002792 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002793 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002794 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2795 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002796 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002797 if (ret < 0)
2798 break;
2799 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002800 return ret;
2801}
2802
Wang Nan12fae5e2015-09-04 21:16:00 +09002803void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002804{
2805 int i, j;
2806
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002807 /* Loop 3: cleanup and free trace events */
2808 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002809 for (j = 0; j < pevs[i].ntevs; j++)
2810 clear_probe_trace_event(&pevs[i].tevs[j]);
2811 zfree(&pevs[i].tevs);
2812 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09002813 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002814 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002815}
2816
2817int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2818{
2819 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09002820
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002821 ret = init_probe_symbol_maps(pevs->uprobes);
2822 if (ret < 0)
2823 return ret;
2824
Wang Nan12fae5e2015-09-04 21:16:00 +09002825 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002826 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09002827 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002828
Wang Nan12fae5e2015-09-04 21:16:00 +09002829 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002830
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002831 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002832 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002833}
2834
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002835int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002836{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002837 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002838 char *str = strfilter__string(filter);
2839
2840 if (!str)
2841 return -EINVAL;
2842
Masami Hiramatsufa282442009-12-08 17:03:23 -05002843 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002844 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2845 if (ret < 0)
2846 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302847
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002848 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002849 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302850 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002851
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002852 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002853 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002854 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002855 goto error;
2856 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002857 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302858
2859error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002860 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302861 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002862 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302863 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002864out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002865 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002866
2867 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002868}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302869
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002870/* TODO: don't use a global variable for filter ... */
2871static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002872
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002873/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002874 * If a symbol corresponds to a function with global binding and
2875 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002876 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002877static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002878 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002879{
Namhyung Kime578da32015-03-06 16:31:29 +09002880 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002881 return 0;
2882 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002883}
2884
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002885int show_available_funcs(const char *target, struct strfilter *_filter,
2886 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002887{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002888 struct map *map;
2889 int ret;
2890
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002891 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002892 if (ret < 0)
2893 return ret;
2894
2895 /* Get a symbol map */
2896 if (user)
2897 map = dso__new_map(target);
2898 else
2899 map = kernel_get_module_map(target);
2900 if (!map) {
2901 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002902 return -EINVAL;
2903 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002904
2905 /* Load symbols with given filter */
2906 available_func_filter = _filter;
2907 if (map__load(map, filter_available_functions)) {
2908 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2909 goto end;
2910 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002911 if (!dso__sorted_by_name(map->dso, map->type))
2912 dso__sort_by_name(map->dso, map->type);
2913
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002914 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302915 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002916 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2917end:
2918 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002919 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002920 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002921 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302922
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002923 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302924}
2925
Wang Nanda15bd92015-08-26 10:57:45 +00002926int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2927 struct perf_probe_arg *pvar)
2928{
2929 tvar->value = strdup(pvar->var);
2930 if (tvar->value == NULL)
2931 return -ENOMEM;
2932 if (pvar->type) {
2933 tvar->type = strdup(pvar->type);
2934 if (tvar->type == NULL)
2935 return -ENOMEM;
2936 }
2937 if (pvar->name) {
2938 tvar->name = strdup(pvar->name);
2939 if (tvar->name == NULL)
2940 return -ENOMEM;
2941 } else
2942 tvar->name = NULL;
2943 return 0;
2944}