blob: 6c50d9f8e2109da776d595b56743bd0483bda536 [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 Hiramatsuee45b6c2014-02-06 05:32:04 +000070static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040071
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090072/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090073int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040074{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040075 int ret;
76
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090078 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090079 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 if (ret < 0) {
81 pr_debug("Failed to init symbol map.\n");
82 goto out;
83 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000085 if (host_machine || user_only) /* already initialized */
86 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030087
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000088 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90
91 host_machine = machine__new_host();
92 if (!host_machine) {
93 pr_debug("machine__new_host() failed.\n");
94 symbol__exit();
95 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090096 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040097out:
98 if (ret < 0)
99 pr_warning("Failed to init vmlinux path.\n");
100 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400101}
102
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900103void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000104{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300105 machine__delete(host_machine);
106 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107 symbol__exit();
108}
109
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110static struct symbol *__find_kernel_function_by_name(const char *name,
111 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400112{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300113 return machine__find_kernel_function_by_name(host_machine, name, mapp);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900114}
115
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000116static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
117{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300118 return machine__find_kernel_function(host_machine, addr, mapp);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000119}
120
121static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
122{
123 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
124 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300125 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000126
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300127 if (map__load(map) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000128 return NULL;
129
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300130 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000131 if (!kmap)
132 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000133 return kmap->ref_reloc_sym;
134}
135
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900136static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
137 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000138{
139 struct ref_reloc_sym *reloc_sym;
140 struct symbol *sym;
141 struct map *map;
142
143 /* ref_reloc_sym is just a label. Need a special fix*/
144 reloc_sym = kernel_get_ref_reloc_sym();
145 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900146 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000147 else {
148 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900149 if (!sym)
150 return -ENOENT;
151 *addr = map->unmap_ip(map, sym->start) -
152 ((reloc) ? 0 : map->reloc) -
153 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000154 }
155 return 0;
156}
157
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900158static struct map *kernel_get_module_map(const char *module)
159{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000160 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300161 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300162 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300166 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300171 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300172 /* short_name is "[module]" */
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900173 if (strncmp(pos->dso->short_name + 1, module,
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300174 pos->dso->short_name_len - 2) == 0 &&
175 module[pos->dso->short_name_len - 2] == '\0') {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900176 return pos;
177 }
178 }
179 return NULL;
180}
181
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500182struct map *get_target_map(const char *target, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900183{
184 /* Init maps of given executable or kernel */
185 if (user)
186 return dso__new_map(target);
187 else
188 return kernel_get_module_map(target);
189}
190
191static void put_target_map(struct map *map, bool user)
192{
193 if (map && user) {
194 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300195 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900196 }
197}
198
199
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000200static int convert_exec_to_group(const char *exec, char **result)
201{
202 char *ptr1, *ptr2, *exec_copy;
203 char buf[64];
204 int ret;
205
206 exec_copy = strdup(exec);
207 if (!exec_copy)
208 return -ENOMEM;
209
210 ptr1 = basename(exec_copy);
211 if (!ptr1) {
212 ret = -EINVAL;
213 goto out;
214 }
215
Colin Ian Kingead1a572016-10-03 11:34:31 +0100216 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
Masami Hiramatsu35726d32016-09-24 00:35:16 +0900217 if (!isalnum(*ptr2) && *ptr2 != '_') {
218 *ptr2 = '\0';
219 break;
220 }
221 }
222
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000223 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
224 if (ret < 0)
225 goto out;
226
227 *result = strdup(buf);
228 ret = *result ? 0 : -ENOMEM;
229
230out:
231 free(exec_copy);
232 return ret;
233}
234
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900235static void clear_perf_probe_point(struct perf_probe_point *pp)
236{
237 free(pp->file);
238 free(pp->function);
239 free(pp->lazy_line);
240}
241
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000242static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
243{
244 int i;
245
246 for (i = 0; i < ntevs; i++)
247 clear_probe_trace_event(tevs + i);
248}
249
Masami Hiramatsub0312202015-06-16 20:50:55 +0900250static bool kprobe_blacklist__listed(unsigned long address);
251static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
252{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900253 u64 etext_addr = 0;
254 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000255
Masami Hiramatsub0312202015-06-16 20:50:55 +0900256 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900257 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
258 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000259
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900260 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900261 pr_warning("%s is out of .text, skip it.\n", symbol);
262 else if (kprobe_blacklist__listed(address))
263 pr_warning("%s is blacklisted function, skip it.\n", symbol);
264 else
265 return false;
266
267 return true;
268}
269
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530270/*
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530271 * @module can be module name of module file path. In case of path,
272 * inspect elf and find out what is actual module name.
273 * Caller has to free mod_name after using it.
274 */
275static char *find_module_name(const char *module)
276{
277 int fd;
278 Elf *elf;
279 GElf_Ehdr ehdr;
280 GElf_Shdr shdr;
281 Elf_Data *data;
282 Elf_Scn *sec;
283 char *mod_name = NULL;
Masami Hiramatsu69f3df12017-01-03 00:20:49 +0900284 int name_offset;
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530285
286 fd = open(module, O_RDONLY);
287 if (fd < 0)
288 return NULL;
289
290 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
291 if (elf == NULL)
292 goto elf_err;
293
294 if (gelf_getehdr(elf, &ehdr) == NULL)
295 goto ret_err;
296
297 sec = elf_section_by_name(elf, &ehdr, &shdr,
298 ".gnu.linkonce.this_module", NULL);
299 if (!sec)
300 goto ret_err;
301
302 data = elf_getdata(sec, NULL);
303 if (!data || !data->d_buf)
304 goto ret_err;
305
Masami Hiramatsu69f3df12017-01-03 00:20:49 +0900306 /*
307 * NOTE:
308 * '.gnu.linkonce.this_module' section of kernel module elf directly
309 * maps to 'struct module' from linux/module.h. This section contains
310 * actual module name which will be used by kernel after loading it.
311 * But, we cannot use 'struct module' here since linux/module.h is not
312 * exposed to user-space. Offset of 'name' has remained same from long
313 * time, so hardcoding it here.
314 */
315 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
316 name_offset = 12;
317 else /* expect ELFCLASS64 by default */
318 name_offset = 24;
319
320 mod_name = strdup((char *)data->d_buf + name_offset);
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530321
322ret_err:
323 elf_end(elf);
324elf_err:
325 close(fd);
326 return mod_name;
327}
328
Ingo Molnar89fe8082013-09-30 12:07:11 +0200329#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000330
331static int kernel_get_module_dso(const char *module, struct dso **pdso)
332{
333 struct dso *dso;
334 struct map *map;
335 const char *vmlinux_name;
336 int ret = 0;
337
338 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300339 char module_name[128];
340
341 snprintf(module_name, sizeof(module_name), "[%s]", module);
342 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
343 if (map) {
344 dso = map->dso;
345 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000346 }
347 pr_debug("Failed to find module %s.\n", module);
348 return -ENOENT;
349 }
350
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300351 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000352 dso = map->dso;
353
354 vmlinux_name = symbol_conf.vmlinux_name;
355 dso->load_errno = 0;
356 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300357 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000358 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300359 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000360found:
361 *pdso = dso;
362 return ret;
363}
364
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900365/*
366 * Some binaries like glibc have special symbols which are on the symbol
367 * table, but not in the debuginfo. If we can find the address of the
368 * symbol from map, we can translate the address back to the probe point.
369 */
370static int find_alternative_probe_point(struct debuginfo *dinfo,
371 struct perf_probe_point *pp,
372 struct perf_probe_point *result,
373 const char *target, bool uprobes)
374{
375 struct map *map = NULL;
376 struct symbol *sym;
377 u64 address = 0;
378 int ret = -ENOENT;
379
380 /* This can work only for function-name based one */
381 if (!pp->function || pp->file)
382 return -ENOTSUP;
383
384 map = get_target_map(target, uprobes);
385 if (!map)
386 return -EINVAL;
387
388 /* Find the address of given function */
389 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900390 if (uprobes)
391 address = sym->start;
392 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900393 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900394 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900395 }
396 if (!address) {
397 ret = -ENOENT;
398 goto out;
399 }
Wang Nanf6c15622015-04-08 02:14:34 +0000400 pr_debug("Symbol %s address found : %" PRIx64 "\n",
401 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900402
403 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
404 result);
405 if (ret <= 0)
406 ret = (!ret) ? -ENOENT : ret;
407 else {
408 result->offset += pp->offset;
409 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800410 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900411 ret = 0;
412 }
413
414out:
415 put_target_map(map, uprobes);
416 return ret;
417
418}
419
420static int get_alternative_probe_event(struct debuginfo *dinfo,
421 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900422 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900423{
424 int ret;
425
426 memcpy(tmp, &pev->point, sizeof(*tmp));
427 memset(&pev->point, 0, sizeof(pev->point));
428 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900429 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900430 if (ret < 0)
431 memcpy(&pev->point, tmp, sizeof(*tmp));
432
433 return ret;
434}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000435
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900436static int get_alternative_line_range(struct debuginfo *dinfo,
437 struct line_range *lr,
438 const char *target, bool user)
439{
David Ahern6d4a4892015-03-11 10:36:20 -0400440 struct perf_probe_point pp = { .function = lr->function,
441 .file = lr->file,
442 .line = lr->start };
443 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900444 int ret, len = 0;
445
David Ahern6d4a4892015-03-11 10:36:20 -0400446 memset(&result, 0, sizeof(result));
447
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900448 if (lr->end != INT_MAX)
449 len = lr->end - lr->start;
450 ret = find_alternative_probe_point(dinfo, &pp, &result,
451 target, user);
452 if (!ret) {
453 lr->function = result.function;
454 lr->file = result.file;
455 lr->start = result.line;
456 if (lr->end != INT_MAX)
457 lr->end = lr->start + len;
458 clear_perf_probe_point(&pp);
459 }
460 return ret;
461}
462
Masami Hiramatsuff741782011-06-27 16:27:39 +0900463/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000464static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900465{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000466 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900467 char reason[STRERR_BUFSIZE];
468 struct debuginfo *ret = NULL;
469 struct dso *dso = NULL;
470 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900471
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000472 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900473 err = kernel_get_module_dso(module, &dso);
474 if (err < 0) {
475 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300476 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900477 strcpy(reason, "(unknown)");
478 } else
479 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000480 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900481 pr_err("Failed to find the path for %s: %s\n",
482 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900483 return NULL;
484 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900485 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900486 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000487 ret = debuginfo__new(path);
488 if (!ret && !silent) {
489 pr_warning("The %s file has no debug information.\n", path);
490 if (!module || !strtailcmp(path, ".ko"))
491 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
492 else
493 pr_warning("Rebuild with -g, ");
494 pr_warning("or install an appropriate debuginfo package.\n");
495 }
496 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400497}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300498
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900499/* For caching the last debuginfo */
500static struct debuginfo *debuginfo_cache;
501static char *debuginfo_cache_path;
502
503static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
504{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900505 const char *path = module;
506
507 /* If the module is NULL, it should be the kernel. */
508 if (!module)
509 path = "kernel";
510
511 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900512 goto out;
513
514 /* Copy module path */
515 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900516 debuginfo_cache_path = strdup(path);
517 if (!debuginfo_cache_path) {
518 debuginfo__delete(debuginfo_cache);
519 debuginfo_cache = NULL;
520 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900521 }
522
523 debuginfo_cache = open_debuginfo(module, silent);
524 if (!debuginfo_cache)
525 zfree(&debuginfo_cache_path);
526out:
527 return debuginfo_cache;
528}
529
530static void debuginfo_cache__exit(void)
531{
532 debuginfo__delete(debuginfo_cache);
533 debuginfo_cache = NULL;
534 zfree(&debuginfo_cache_path);
535}
536
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000537
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000538static int get_text_start_address(const char *exec, unsigned long *address)
539{
540 Elf *elf;
541 GElf_Ehdr ehdr;
542 GElf_Shdr shdr;
543 int fd, ret = -ENOENT;
544
545 fd = open(exec, O_RDONLY);
546 if (fd < 0)
547 return -errno;
548
549 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900550 if (elf == NULL) {
551 ret = -EINVAL;
552 goto out_close;
553 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000554
555 if (gelf_getehdr(elf, &ehdr) == NULL)
556 goto out;
557
558 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
559 goto out;
560
561 *address = shdr.sh_addr - shdr.sh_offset;
562 ret = 0;
563out:
564 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900565out_close:
566 close(fd);
567
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000568 return ret;
569}
570
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000571/*
572 * Convert trace point to probe point with debuginfo
573 */
574static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
575 struct perf_probe_point *pp,
576 bool is_kprobe)
577{
578 struct debuginfo *dinfo = NULL;
579 unsigned long stext = 0;
580 u64 addr = tp->address;
581 int ret = -ENOENT;
582
583 /* convert the address to dwarf address */
584 if (!is_kprobe) {
585 if (!addr) {
586 ret = -EINVAL;
587 goto error;
588 }
589 ret = get_text_start_address(tp->module, &stext);
590 if (ret < 0)
591 goto error;
592 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000593 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900594 /* If the module is given, this returns relative address */
595 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
596 false, !!tp->module);
597 if (ret != 0)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000598 goto error;
599 addr += tp->offset;
600 }
601
602 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
603 tp->module ? : "kernel");
604
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900605 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
606 if (dinfo)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000607 ret = debuginfo__find_probe_point(dinfo,
608 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900609 else
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000610 ret = -ENOENT;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +0000611
612 if (ret > 0) {
613 pp->retprobe = tp->retprobe;
614 return 0;
615 }
616error:
617 pr_debug("Failed to find corresponding probes from debuginfo.\n");
618 return ret ? : -ENOENT;
619}
620
Masami Hiramatsuce02eff2017-01-11 15:00:47 +0900621/* Adjust symbol name and address */
622static int post_process_probe_trace_point(struct probe_trace_point *tp,
623 struct map *map, unsigned long offs)
624{
625 struct symbol *sym;
626 u64 addr = tp->address + tp->offset - offs;
627
628 sym = map__find_symbol(map, addr);
629 if (!sym)
630 return -ENOENT;
631
632 if (strcmp(sym->name, tp->symbol)) {
633 /* If we have no realname, use symbol for it */
634 if (!tp->realname)
635 tp->realname = tp->symbol;
636 else
637 free(tp->symbol);
638 tp->symbol = strdup(sym->name);
639 if (!tp->symbol)
640 return -ENOMEM;
641 }
642 tp->offset = addr - sym->start;
643 tp->address -= offs;
644
645 return 0;
646}
647
Masami Hiramatsu36497352017-01-04 12:30:19 +0900648/*
649 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
650 * and generate new symbols with suffixes such as .constprop.N or .isra.N
651 * etc. Since those symbols are not recorded in DWARF, we have to find
652 * correct generated symbols from offline ELF binary.
653 * For online kernel or uprobes we don't need this because those are
654 * rebased on _text, or already a section relative address.
655 */
656static int
657post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
658 int ntevs, const char *pathname)
659{
Masami Hiramatsu36497352017-01-04 12:30:19 +0900660 struct map *map;
661 unsigned long stext = 0;
Masami Hiramatsuce02eff2017-01-11 15:00:47 +0900662 int i, ret = 0;
Masami Hiramatsu36497352017-01-04 12:30:19 +0900663
664 /* Prepare a map for offline binary */
665 map = dso__new_map(pathname);
666 if (!map || get_text_start_address(pathname, &stext) < 0) {
667 pr_warning("Failed to get ELF symbols for %s\n", pathname);
668 return -EINVAL;
669 }
670
671 for (i = 0; i < ntevs; i++) {
Masami Hiramatsuce02eff2017-01-11 15:00:47 +0900672 ret = post_process_probe_trace_point(&tevs[i].point,
673 map, stext);
674 if (ret < 0)
675 break;
Masami Hiramatsu36497352017-01-04 12:30:19 +0900676 }
677 map__put(map);
678
Masami Hiramatsuce02eff2017-01-11 15:00:47 +0900679 return ret;
Masami Hiramatsu36497352017-01-04 12:30:19 +0900680}
681
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000682static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
683 int ntevs, const char *exec)
684{
685 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000686 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000687
688 if (!exec)
689 return 0;
690
691 ret = get_text_start_address(exec, &stext);
692 if (ret < 0)
693 return ret;
694
695 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000696 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000697 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000698 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000699 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000700 ret = -ENOMEM;
701 break;
702 }
703 tevs[i].uprobes = true;
704 }
705
706 return ret;
707}
708
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900709static int
710post_process_module_probe_trace_events(struct probe_trace_event *tevs,
711 int ntevs, const char *module,
712 struct debuginfo *dinfo)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900713{
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900714 Dwarf_Addr text_offs = 0;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900715 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530716 char *mod_name = NULL;
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900717 struct map *map;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900718
719 if (!module)
720 return 0;
721
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900722 map = get_target_map(module, false);
723 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
724 pr_warning("Failed to get ELF symbols for %s\n", module);
725 return -EINVAL;
726 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900727
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900728 mod_name = find_module_name(module);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900729 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900730 ret = post_process_probe_trace_point(&tevs[i].point,
731 map, (unsigned long)text_offs);
732 if (ret < 0)
733 break;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530734 tevs[i].point.module =
735 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900736 if (!tevs[i].point.module) {
737 ret = -ENOMEM;
738 break;
739 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900740 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900741
Ravi Bangoria63a29612016-04-26 19:55:41 +0530742 free(mod_name);
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900743 map__put(map);
744
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900745 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900746}
747
Ravi Bangoriad8204562016-08-09 01:23:24 -0500748static int
749post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
750 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000751{
752 struct ref_reloc_sym *reloc_sym;
753 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900754 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000755
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900756 /* Skip post process if the target is an offline kernel */
757 if (symbol_conf.ignore_vmlinux_buildid)
Masami Hiramatsu36497352017-01-04 12:30:19 +0900758 return post_process_offline_probe_trace_events(tevs, ntevs,
759 symbol_conf.vmlinux_name);
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900760
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000761 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000762 if (!reloc_sym) {
763 pr_warning("Relocated base symbol is not found!\n");
764 return -EINVAL;
765 }
766
767 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900768 if (!tevs[i].point.address || tevs[i].point.retprobe)
769 continue;
770 /* If we found a wrong one, mark it by NULL symbol */
771 if (kprobe_warn_out_range(tevs[i].point.symbol,
772 tevs[i].point.address)) {
773 tmp = NULL;
774 skipped++;
775 } else {
776 tmp = strdup(reloc_sym->name);
777 if (!tmp)
778 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000779 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900780 /* If we have no realname, use symbol for it */
781 if (!tevs[i].point.realname)
782 tevs[i].point.realname = tevs[i].point.symbol;
783 else
784 free(tevs[i].point.symbol);
785 tevs[i].point.symbol = tmp;
786 tevs[i].point.offset = tevs[i].point.address -
787 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000788 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900789 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000790}
791
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500792void __weak
793arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
794 int ntevs __maybe_unused)
795{
796}
797
Ravi Bangoriad8204562016-08-09 01:23:24 -0500798/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500799static int post_process_probe_trace_events(struct perf_probe_event *pev,
800 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500801 int ntevs, const char *module,
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900802 bool uprobe, struct debuginfo *dinfo)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500803{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500804 int ret;
805
Ravi Bangoriad8204562016-08-09 01:23:24 -0500806 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500807 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
808 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500809 /* Currently ref_reloc_sym based probe is not for drivers */
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900810 ret = post_process_module_probe_trace_events(tevs, ntevs,
811 module, dinfo);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500812 else
813 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500814
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500815 if (ret >= 0)
816 arch__post_process_probe_trace_events(pev, ntevs);
817
818 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500819}
820
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300821/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530822static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900823 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300824{
825 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900826 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530827 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900828 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300829
Masami Hiramatsu44225522015-05-08 10:03:28 +0900830 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900831 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000832 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900833 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900834 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300835 return 0;
836 }
837
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000838 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900839 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900840 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900841
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900842 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900843 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900844 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900845 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900846 /*
847 * Write back to the original probe_event for
848 * setting appropriate (user given) event name
849 */
850 clear_perf_probe_point(&pev->point);
851 memcpy(&pev->point, &tmp, sizeof(tmp));
852 }
853 }
854
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000856 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500857 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900858 pev->target, pev->uprobes, dinfo);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900859 if (ret < 0 || ret == ntevs) {
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900860 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000861 clear_probe_trace_events(*tevs, ntevs);
862 zfree(tevs);
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900863 ntevs = 0;
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000864 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300866
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900867 debuginfo__delete(dinfo);
868
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900870 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400871 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900872 return -ENOENT;
Masami Hiramatsub6f75b92017-01-11 15:01:57 +0900873 } else if (ntevs < 0) {
874 /* Error path : ntevs < 0 */
875 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000876 if (ntevs == -EBADF)
877 pr_warning("Warning: No dwarf info found in the vmlinux - "
878 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400879 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900880 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400881 return 0;
882 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300883 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400884 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300885}
886
887#define LINEBUF_SIZE 256
888#define NR_ADDITIONAL_LINES 2
889
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100890static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300891{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000892 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100893 const char *color = show_num ? "" : PERF_COLOR_BLUE;
894 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300895
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100896 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300897 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
898 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100899 if (skip)
900 continue;
901 if (!prefix) {
902 prefix = show_num ? "%7d " : " ";
903 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300904 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100905 color_fprintf(stdout, color, "%s", buf);
906
907 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400908
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100909 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300910error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100911 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000912 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300913 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100914 return -1;
915 }
916 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300917}
918
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100919static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
920{
921 int rv = __show_one_line(fp, l, skip, show_num);
922 if (rv == 0) {
923 pr_warning("Source file is shorter than expected.\n");
924 rv = -1;
925 }
926 return rv;
927}
928
929#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
930#define show_one_line(f,l) _show_one_line(f,l,false,false)
931#define skip_one_line(f,l) _show_one_line(f,l,true,false)
932#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
933
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300934/*
935 * Show line-range always requires debuginfo to find source file and
936 * line number.
937 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900938static int __show_line_range(struct line_range *lr, const char *module,
939 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300940{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400941 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000942 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900943 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300944 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900945 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900946 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000947 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300948
949 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000950 dinfo = open_debuginfo(module, false);
951 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900952 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400953
Masami Hiramatsuff741782011-06-27 16:27:39 +0900954 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900955 if (!ret) { /* Not found, retry with an alternative */
956 ret = get_alternative_line_range(dinfo, lr, module, user);
957 if (!ret)
958 ret = debuginfo__find_line_range(dinfo, lr);
959 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900960 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000961 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400962 pr_warning("Specified source line is not found.\n");
963 return -ENOENT;
964 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000965 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400966 return ret;
967 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300968
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900969 /* Convert source file path */
970 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900971 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800972
973 /* Free old path when new path is assigned */
974 if (tmp != lr->path)
975 free(tmp);
976
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900977 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000978 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900979 return ret;
980 }
981
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300982 setup_pager();
983
984 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900985 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300986 lr->start - lr->offset);
987 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100988 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300989
990 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400991 if (fp == NULL) {
992 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300993 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400994 return -errno;
995 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300996 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100997 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100998 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100999 if (ret < 0)
1000 goto end;
1001 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001002
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -03001003 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001004 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001005 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001006 if (ret < 0)
1007 goto end;
1008 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001009 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001010 if (ret < 0)
1011 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001012 }
1013
1014 if (lr->end == INT_MAX)
1015 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001016 while (l <= lr->end) {
1017 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1018 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001019 break;
1020 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001021end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001022 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001023 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001024}
1025
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001026int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001027{
1028 int ret;
1029
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001030 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001031 if (ret < 0)
1032 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +09001033 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001034 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001035
1036 return ret;
1037}
1038
Masami Hiramatsuff741782011-06-27 16:27:39 +09001039static int show_available_vars_at(struct debuginfo *dinfo,
1040 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001041 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001042{
1043 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001044 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001045 struct str_node *node;
1046 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001047 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001048 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001049
1050 buf = synthesize_perf_probe_point(&pev->point);
1051 if (!buf)
1052 return -EINVAL;
1053 pr_debug("Searching variables at %s\n", buf);
1054
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001055 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001056 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +09001057 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001058 if (!ret) {
1059 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001060 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001061 /* Release the old probe_point */
1062 clear_perf_probe_point(&tmp);
1063 }
1064 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001065 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001066 if (ret == 0 || ret == -ENOENT) {
1067 pr_err("Failed to find the address of %s\n", buf);
1068 ret = -ENOENT;
1069 } else
1070 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001071 goto end;
1072 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001073
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001074 /* Some variables are found */
1075 fprintf(stdout, "Available variables at %s\n", buf);
1076 for (i = 0; i < ret; i++) {
1077 vl = &vls[i];
1078 /*
1079 * A probe point might be converted to
1080 * several trace points.
1081 */
1082 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1083 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001084 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001085 nvars = 0;
1086 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001087 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001088 var = strchr(node->s, '\t') + 1;
1089 if (strfilter__compare(_filter, var)) {
1090 fprintf(stdout, "\t\t%s\n", node->s);
1091 nvars++;
1092 }
1093 }
1094 strlist__delete(vl->vars);
1095 }
1096 if (nvars == 0)
1097 fprintf(stdout, "\t\t(No matched variables)\n");
1098 }
1099 free(vls);
1100end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001101 free(buf);
1102 return ret;
1103}
1104
1105/* Show available variables on given probe point */
1106int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001107 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001108{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001109 int i, ret = 0;
1110 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001111
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001112 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001113 if (ret < 0)
1114 return ret;
1115
Masami Hiramatsu44225522015-05-08 10:03:28 +09001116 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001117 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001118 ret = -ENOENT;
1119 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001120 }
1121
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001122 setup_pager();
1123
Masami Hiramatsuff741782011-06-27 16:27:39 +09001124 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001125 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001126
1127 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001128out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001129 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001130 return ret;
1131}
1132
Ingo Molnar89fe8082013-09-30 12:07:11 +02001133#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001134
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001135static void debuginfo_cache__exit(void)
1136{
1137}
1138
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001139static int
1140find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1141 struct perf_probe_point *pp __maybe_unused,
1142 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001143{
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001144 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001145}
1146
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301147static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001148 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001149{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001150 if (perf_probe_event_need_dwarf(pev)) {
1151 pr_warning("Debuginfo-analysis is not supported.\n");
1152 return -ENOSYS;
1153 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301154
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001155 return 0;
1156}
1157
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001158int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001159 const char *module __maybe_unused,
1160 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001161{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001162 pr_warning("Debuginfo-analysis is not supported.\n");
1163 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001164}
1165
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001166int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001167 int npevs __maybe_unused,
1168 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001169{
1170 pr_warning("Debuginfo-analysis is not supported.\n");
1171 return -ENOSYS;
1172}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001173#endif
1174
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001175void line_range__clear(struct line_range *lr)
1176{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001177 free(lr->function);
1178 free(lr->file);
1179 free(lr->path);
1180 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001181 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001182 memset(lr, 0, sizeof(*lr));
1183}
1184
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001185int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001186{
1187 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001188 lr->line_list = intlist__new(NULL);
1189 if (!lr->line_list)
1190 return -ENOMEM;
1191 else
1192 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001193}
1194
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001195static int parse_line_num(char **ptr, int *val, const char *what)
1196{
1197 const char *start = *ptr;
1198
1199 errno = 0;
1200 *val = strtol(*ptr, ptr, 0);
1201 if (errno || *ptr == start) {
1202 semantic_error("'%s' is not a valid number.\n", what);
1203 return -EINVAL;
1204 }
1205 return 0;
1206}
1207
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001208/* Check the name is good for event, group or function */
1209static bool is_c_func_name(const char *name)
1210{
1211 if (!isalpha(*name) && *name != '_')
1212 return false;
1213 while (*++name != '\0') {
1214 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1215 return false;
1216 }
1217 return true;
1218}
1219
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001220/*
1221 * Stuff 'lr' according to the line range described by 'arg'.
1222 * The line range syntax is described by:
1223 *
1224 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001225 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001226 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001228{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001229 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001230 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001231
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001232 if (!name)
1233 return -ENOMEM;
1234
1235 lr->start = 0;
1236 lr->end = INT_MAX;
1237
1238 range = strchr(name, ':');
1239 if (range) {
1240 *range++ = '\0';
1241
1242 err = parse_line_num(&range, &lr->start, "start line");
1243 if (err)
1244 goto err;
1245
1246 if (*range == '+' || *range == '-') {
1247 const char c = *range++;
1248
1249 err = parse_line_num(&range, &lr->end, "end line");
1250 if (err)
1251 goto err;
1252
1253 if (c == '+') {
1254 lr->end += lr->start;
1255 /*
1256 * Adjust the number of lines here.
1257 * If the number of lines == 1, the
1258 * the end of line should be equal to
1259 * the start of line.
1260 */
1261 lr->end--;
1262 }
1263 }
1264
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001265 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001266
1267 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001268 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001269 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001270 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001271 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001272 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001273 if (*range != '\0') {
1274 semantic_error("Tailing with invalid str '%s'.\n", range);
1275 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001276 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001277 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001278
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001279 file = strchr(name, '@');
1280 if (file) {
1281 *file = '\0';
1282 lr->file = strdup(++file);
1283 if (lr->file == NULL) {
1284 err = -ENOMEM;
1285 goto err;
1286 }
1287 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001288 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001289 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001290 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001291 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001292 else { /* Invalid name */
1293 semantic_error("'%s' is not a valid function name.\n", name);
1294 err = -EINVAL;
1295 goto err;
1296 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001297
1298 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001299err:
1300 free(name);
1301 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001302}
1303
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001304static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1305{
1306 char *ptr;
1307
1308 ptr = strchr(*arg, ':');
1309 if (ptr) {
1310 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001311 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001312 goto ng_name;
1313 pev->group = strdup(*arg);
1314 if (!pev->group)
1315 return -ENOMEM;
1316 *arg = ptr + 1;
1317 } else
1318 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001319 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001320ng_name:
1321 semantic_error("%s is bad for event name -it must "
1322 "follow C symbol-naming rule.\n", *arg);
1323 return -EINVAL;
1324 }
1325 pev->event = strdup(*arg);
1326 if (pev->event == NULL)
1327 return -ENOMEM;
1328
1329 return 0;
1330}
1331
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001332/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001333static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001334{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001335 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001336 char *ptr, *tmp;
1337 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301338 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001339 int ret;
1340
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001341 /*
1342 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001343 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1344 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001345 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001346 */
Wang Nane59d29e2015-04-28 08:46:09 +00001347 if (!arg)
1348 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001349
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001350 /*
1351 * If the probe point starts with '%',
1352 * or starts with "sdt_" and has a ':' but no '=',
1353 * then it should be a SDT/cached probe point.
1354 */
1355 if (arg[0] == '%' ||
1356 (!strncmp(arg, "sdt_", 4) &&
1357 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001358 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001359 if (arg[0] == '%')
1360 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001361 }
1362
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001363 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001364 if (pev->sdt) {
1365 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001366 if (*ptr != '@') {
1367 semantic_error("%s must be an SDT name.\n",
1368 arg);
1369 return -EINVAL;
1370 }
1371 /* This must be a target file name or build id */
1372 tmp = build_id_cache__complement(ptr + 1);
1373 if (tmp) {
1374 pev->target = build_id_cache__origname(tmp);
1375 free(tmp);
1376 } else
1377 pev->target = strdup(ptr + 1);
1378 if (!pev->target)
1379 return -ENOMEM;
1380 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001381 }
1382 ret = parse_perf_probe_event_name(&arg, pev);
1383 if (ret == 0) {
1384 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1385 ret = -errno;
1386 }
1387 return ret;
1388 }
1389
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001390 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001391 *ptr = '\0';
1392 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001393 ret = parse_perf_probe_event_name(&arg, pev);
1394 if (ret < 0)
1395 return ret;
1396
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001397 arg = tmp;
1398 }
1399
Naveen N. Rao3099c022015-04-28 17:35:34 +05301400 /*
1401 * Check arg is function or file name and copy it.
1402 *
1403 * We consider arg to be a file spec if and only if it satisfies
1404 * all of the below criteria::
1405 * - it does not include any of "+@%",
1406 * - it includes one of ":;", and
1407 * - it has a period '.' in the name.
1408 *
1409 * Otherwise, we consider arg to be a function specification.
1410 */
1411 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1412 /* This is a file spec if it includes a '.' before ; or : */
1413 if (memchr(arg, '.', ptr - arg))
1414 file_spec = true;
1415 }
1416
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001417 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001418 if (ptr) {
1419 nc = *ptr;
1420 *ptr++ = '\0';
1421 }
1422
Wang Nan6c6e0242015-08-26 10:57:44 +00001423 if (arg[0] == '\0')
1424 tmp = NULL;
1425 else {
1426 tmp = strdup(arg);
1427 if (tmp == NULL)
1428 return -ENOMEM;
1429 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001430
Naveen N. Rao3099c022015-04-28 17:35:34 +05301431 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001432 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001433 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001434 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001435
Wang Nanda15bd92015-08-26 10:57:45 +00001436 /*
1437 * Keep pp->function even if this is absolute address,
1438 * so it can mark whether abs_address is valid.
1439 * Which make 'perf probe lib.bin 0x0' possible.
1440 *
1441 * Note that checking length of tmp is not needed
1442 * because when we access tmp[1] we know tmp[0] is '0',
1443 * so tmp[1] should always valid (but could be '\0').
1444 */
1445 if (tmp && !strncmp(tmp, "0x", 2)) {
1446 pp->abs_address = strtoul(pp->function, &tmp, 0);
1447 if (*tmp != '\0') {
1448 semantic_error("Invalid absolute address.\n");
1449 return -EINVAL;
1450 }
1451 }
1452 }
1453
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001454 /* Parse other options */
1455 while (ptr) {
1456 arg = ptr;
1457 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001458 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001459 pp->lazy_line = strdup(arg);
1460 if (pp->lazy_line == NULL)
1461 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001462 break;
1463 }
1464 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001465 if (ptr) {
1466 nc = *ptr;
1467 *ptr++ = '\0';
1468 }
1469 switch (c) {
1470 case ':': /* Line number */
1471 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001472 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001473 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474 " in line number.\n");
1475 return -EINVAL;
1476 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001477 break;
1478 case '+': /* Byte offset from a symbol */
1479 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001481 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482 " in offset.\n");
1483 return -EINVAL;
1484 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001485 break;
1486 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 if (pp->file) {
1488 semantic_error("SRC@SRC is not allowed.\n");
1489 return -EINVAL;
1490 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001491 pp->file = strdup(arg);
1492 if (pp->file == NULL)
1493 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001494 break;
1495 case '%': /* Probe places */
1496 if (strcmp(arg, "return") == 0) {
1497 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001498 } else { /* Others not supported yet */
1499 semantic_error("%%%s is not supported.\n", arg);
1500 return -ENOTSUP;
1501 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001502 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 default: /* Buggy case */
1504 pr_err("This program has a bug at %s:%d.\n",
1505 __FILE__, __LINE__);
1506 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001507 break;
1508 }
1509 }
1510
1511 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001512 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001513 semantic_error("Lazy pattern can't be used with"
1514 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001515 return -EINVAL;
1516 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001517
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001519 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001520 return -EINVAL;
1521 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001522
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001524 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001525 return -EINVAL;
1526 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001527
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001528 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001529 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001530 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001531 return -EINVAL;
1532 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001533
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001535 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001536 return -EINVAL;
1537 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001538
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001539 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001540 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541 return -EINVAL;
1542 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001543
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001544 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001545 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001546 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001547 return -EINVAL;
1548 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001549
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001550 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001551 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1552 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001553 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001554}
1555
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001556/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001557static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001558{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001559 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001560 struct perf_probe_arg_field **fieldp;
1561
1562 pr_debug("parsing arg: %s into ", str);
1563
Masami Hiramatsu48481932010-04-12 13:16:53 -04001564 tmp = strchr(str, '=');
1565 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001566 arg->name = strndup(str, tmp - str);
1567 if (arg->name == NULL)
1568 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001569 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001570 str = tmp + 1;
1571 }
1572
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001573 tmp = strchr(str, ':');
1574 if (tmp) { /* Type setting */
1575 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001576 arg->type = strdup(tmp + 1);
1577 if (arg->type == NULL)
1578 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001579 pr_debug("type:%s ", arg->type);
1580 }
1581
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001582 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001583 if (!is_c_varname(str) || !tmp) {
1584 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001585 arg->var = strdup(str);
1586 if (arg->var == NULL)
1587 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001588 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001589 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001590 }
1591
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001592 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001593 arg->var = strndup(str, tmp - str);
1594 if (arg->var == NULL)
1595 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001596 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001597 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001598 fieldp = &arg->field;
1599
1600 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001601 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1602 if (*fieldp == NULL)
1603 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001604 if (*tmp == '[') { /* Array */
1605 str = tmp;
1606 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001607 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001608 if (*tmp != ']' || tmp == str + 1) {
1609 semantic_error("Array index must be a"
1610 " number.\n");
1611 return -EINVAL;
1612 }
1613 tmp++;
1614 if (*tmp == '\0')
1615 tmp = NULL;
1616 } else { /* Structure */
1617 if (*tmp == '.') {
1618 str = tmp + 1;
1619 (*fieldp)->ref = false;
1620 } else if (tmp[1] == '>') {
1621 str = tmp + 2;
1622 (*fieldp)->ref = true;
1623 } else {
1624 semantic_error("Argument parse error: %s\n",
1625 str);
1626 return -EINVAL;
1627 }
1628 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001629 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001630 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001631 (*fieldp)->name = strndup(str, tmp - str);
1632 if ((*fieldp)->name == NULL)
1633 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001634 if (*str != '[')
1635 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001636 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1637 fieldp = &(*fieldp)->next;
1638 }
1639 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001640 (*fieldp)->name = strdup(str);
1641 if ((*fieldp)->name == NULL)
1642 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001643 if (*str != '[')
1644 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001645 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001646
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001647 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001648 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001649 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001650 if (arg->name == NULL)
1651 return -ENOMEM;
1652 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001654}
1655
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001656/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001657int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001658{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001659 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001660 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001661
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001662 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001663 if (!argv) {
1664 pr_debug("Failed to split arguments.\n");
1665 return -ENOMEM;
1666 }
1667 if (argc - 1 > MAX_PROBE_ARGS) {
1668 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1669 ret = -ERANGE;
1670 goto out;
1671 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001672 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001673 ret = parse_perf_probe_point(argv[0], pev);
1674 if (ret < 0)
1675 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001676
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001677 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001678 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001679 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1680 if (pev->args == NULL) {
1681 ret = -ENOMEM;
1682 goto out;
1683 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001684 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1685 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1686 if (ret >= 0 &&
1687 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001688 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001689 " kretprobe.\n");
1690 ret = -EINVAL;
1691 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001692 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001693out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001694 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001695
1696 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001697}
1698
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301699/* Returns true if *any* ARG is either C variable, $params or $vars. */
1700bool perf_probe_with_var(struct perf_probe_event *pev)
1701{
1702 int i = 0;
1703
1704 for (i = 0; i < pev->nargs; i++)
1705 if (is_c_varname(pev->args[i].var) ||
1706 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1707 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1708 return true;
1709 return false;
1710}
1711
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001712/* Return true if this perf_probe_event requires debuginfo */
1713bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001714{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001715 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1716 return true;
1717
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301718 if (perf_probe_with_var(pev))
1719 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001720
1721 return false;
1722}
1723
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301724/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001725int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001726{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301727 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001728 char pr;
1729 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001730 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001731 int ret, i, argc;
1732 char **argv;
1733
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736 if (!argv) {
1737 pr_debug("Failed to split arguments.\n");
1738 return -ENOMEM;
1739 }
1740 if (argc < 2) {
1741 semantic_error("Too few probe arguments.\n");
1742 ret = -ERANGE;
1743 goto out;
1744 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001745
1746 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001747 argv0_str = strdup(argv[0]);
1748 if (argv0_str == NULL) {
1749 ret = -ENOMEM;
1750 goto out;
1751 }
1752 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1753 fmt2_str = strtok_r(NULL, "/", &fmt);
1754 fmt3_str = strtok_r(NULL, " \t", &fmt);
1755 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1756 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001757 semantic_error("Failed to parse event name: %s\n", argv[0]);
1758 ret = -EINVAL;
1759 goto out;
1760 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001761 pr = fmt1_str[0];
1762 tev->group = strdup(fmt2_str);
1763 tev->event = strdup(fmt3_str);
1764 if (tev->group == NULL || tev->event == NULL) {
1765 ret = -ENOMEM;
1766 goto out;
1767 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001768 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001769
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001770 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001772 /* Scan module name(if there), function name and offset */
1773 p = strchr(argv[1], ':');
1774 if (p) {
1775 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001776 if (!tp->module) {
1777 ret = -ENOMEM;
1778 goto out;
1779 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001780 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001781 p++;
1782 } else
1783 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001784 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001785 /* only the address started with 0x */
1786 if (fmt1_str[0] == '0') {
1787 /*
1788 * Fix a special case:
1789 * if address == 0, kernel reports something like:
1790 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1791 * Newer kernel may fix that, but we want to
1792 * support old kernel also.
1793 */
1794 if (strcmp(fmt1_str, "0x") == 0) {
1795 if (!argv[2] || strcmp(argv[2], "(null)")) {
1796 ret = -EINVAL;
1797 goto out;
1798 }
1799 tp->address = 0;
1800
1801 free(argv[2]);
1802 for (i = 2; argv[i + 1] != NULL; i++)
1803 argv[i] = argv[i + 1];
1804
1805 argv[i] = NULL;
1806 argc -= 1;
1807 } else
1808 tp->address = strtoul(fmt1_str, NULL, 0);
1809 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00001810 /* Only the symbol-based probe has offset */
1811 tp->symbol = strdup(fmt1_str);
1812 if (tp->symbol == NULL) {
1813 ret = -ENOMEM;
1814 goto out;
1815 }
1816 fmt2_str = strtok_r(NULL, "", &fmt);
1817 if (fmt2_str == NULL)
1818 tp->offset = 0;
1819 else
1820 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001821 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001822
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001823 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301824 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001825 if (tev->args == NULL) {
1826 ret = -ENOMEM;
1827 goto out;
1828 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001829 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001830 p = strchr(argv[i + 2], '=');
1831 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001832 *p++ = '\0';
1833 else
1834 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001835 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001836 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001837 tev->args[i].value = strdup(p);
1838 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1839 ret = -ENOMEM;
1840 goto out;
1841 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001842 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001843 ret = 0;
1844out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001845 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001846 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001847 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001848}
1849
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001850/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001851char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001852{
1853 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001854 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001855 char *ret = NULL;
1856 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001857
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001858 if (strbuf_init(&buf, 64) < 0)
1859 return NULL;
1860
Masami Hiramatsu48481932010-04-12 13:16:53 -04001861 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001862 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001863 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001864 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1865 if (err)
1866 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001867
1868 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001869 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001870 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001871 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001872 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1873 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001874 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001875 if (err)
1876 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001877 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001878
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001879 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001880 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1881 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001882
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001883 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001884out:
1885 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001886 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001887}
1888
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001890char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001891{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001892 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001893 char *tmp, *ret = NULL;
1894 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001895
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001896 if (strbuf_init(&buf, 64) < 0)
1897 return NULL;
1898
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001899 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001900 if (strbuf_addstr(&buf, pp->function) < 0)
1901 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001902 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001903 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001904 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001905 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001906 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001907 err = strbuf_addstr(&buf, "%return");
1908 if (err)
1909 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001910 }
1911 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001912 tmp = pp->file;
1913 len = strlen(tmp);
1914 if (len > 30) {
1915 tmp = strchr(pp->file + len - 30, '/');
1916 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1917 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001918 err = strbuf_addf(&buf, "@%s", tmp);
1919 if (!err && !pp->function && pp->line)
1920 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001921 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001922 if (!err)
1923 ret = strbuf_detach(&buf, NULL);
1924out:
1925 strbuf_release(&buf);
1926 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927}
1928
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1930{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001931 struct strbuf buf;
1932 char *tmp, *ret = NULL;
1933 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001934
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001935 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001936 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001937 if (pev->event)
1938 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1939 pev->event) < 0)
1940 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001941
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001942 tmp = synthesize_perf_probe_point(&pev->point);
1943 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1944 goto out;
1945 free(tmp);
1946
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001947 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001948 tmp = synthesize_perf_probe_arg(pev->args + i);
1949 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1950 goto out;
1951 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001952 }
1953
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001954 ret = strbuf_detach(&buf, NULL);
1955out:
1956 strbuf_release(&buf);
1957 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001958}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001959
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301960static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001961 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001962{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001963 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001964 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301965 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001966 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001967 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001968 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001969 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001970 err = strbuf_addf(buf, "%+ld(", ref->offset);
1971 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001972}
1973
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301974static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001975 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001976{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301977 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001978 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001979
1980 /* Argument name or separator */
1981 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001982 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001983 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001984 err = strbuf_addch(buf, ' ');
1985 if (err)
1986 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001987
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001988 /* Special case: @XXX */
1989 if (arg->value[0] == '@' && arg->ref)
1990 ref = ref->next;
1991
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001992 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001993 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001994 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001995 if (depth < 0)
1996 return depth;
1997 }
1998
1999 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002000 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002001 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002002 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002003 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002004
2005 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002006 while (!err && depth--)
2007 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002008
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002009 /* Print argument type */
2010 if (!err && arg->type)
2011 err = strbuf_addf(buf, ":%s", arg->type);
2012
2013 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002014}
2015
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302016char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002017{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302018 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002019 struct strbuf buf;
2020 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002021 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002022
Wang Nanda15bd92015-08-26 10:57:45 +00002023 /* Uprobes must have tp->module */
2024 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002025 return NULL;
2026
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002027 if (strbuf_init(&buf, 32) < 0)
2028 return NULL;
2029
2030 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2031 tev->group, tev->event) < 0)
2032 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00002033 /*
2034 * If tp->address == 0, then this point must be a
2035 * absolute address uprobe.
2036 * try_to_find_absolute_address() should have made
2037 * tp->symbol to "0x0".
2038 */
2039 if (tev->uprobes && !tp->address) {
2040 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2041 goto error;
2042 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002043
2044 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302045 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002046 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00002047 else if (!strncmp(tp->symbol, "0x", 2))
2048 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002049 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2050 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302051 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002052 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2053 tp->module ? ":" : "", tp->symbol, tp->offset);
2054 if (err)
2055 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302056
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002057 for (i = 0; i < tev->nargs; i++)
2058 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002059 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002060
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002061 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002062error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002063 strbuf_release(&buf);
2064 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002065}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002066
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002067static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2068 struct perf_probe_point *pp,
2069 bool is_kprobe)
2070{
2071 struct symbol *sym = NULL;
2072 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00002073 u64 addr = tp->address;
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002074 int ret = -ENOENT;
2075
2076 if (!is_kprobe) {
2077 map = dso__new_map(tp->module);
2078 if (!map)
2079 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002080 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002081 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002082 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002083 if (kernel_get_symbol_address_by_name(tp->symbol,
2084 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002085 goto out;
2086 }
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002087 if (addr) {
2088 addr += tp->offset;
2089 sym = __find_kernel_function(addr, &map);
2090 }
2091 }
Wang Nan98d3b252015-11-05 13:19:25 +00002092
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002093 if (!sym)
2094 goto out;
2095
2096 pp->retprobe = tp->retprobe;
2097 pp->offset = addr - map->unmap_ip(map, sym->start);
2098 pp->function = strdup(sym->name);
2099 ret = pp->function ? 0 : -ENOMEM;
2100
2101out:
2102 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002103 map__put(map);
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002104 }
2105
2106 return ret;
2107}
2108
2109static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002110 struct perf_probe_point *pp,
2111 bool is_kprobe)
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002112{
2113 char buf[128];
2114 int ret;
2115
2116 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2117 if (!ret)
2118 return 0;
2119 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2120 if (!ret)
2121 return 0;
2122
2123 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2124
2125 if (tp->symbol) {
2126 pp->function = strdup(tp->symbol);
2127 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002128 } else {
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002129 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2130 if (ret < 0)
2131 return ret;
2132 pp->function = strdup(buf);
2133 pp->offset = 0;
2134 }
2135 if (pp->function == NULL)
2136 return -ENOMEM;
2137
2138 pp->retprobe = tp->retprobe;
2139
2140 return 0;
2141}
2142
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302143static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302144 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002145{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002146 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002147 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002148
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002149 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002150 pev->event = strdup(tev->event);
2151 pev->group = strdup(tev->group);
2152 if (pev->event == NULL || pev->group == NULL)
2153 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002154
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002155 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f63142014-02-06 05:32:23 +00002156 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002157 if (ret < 0)
2158 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002159
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002160 /* Convert trace_arg to probe_arg */
2161 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002162 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2163 if (pev->args == NULL)
2164 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002165 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002166 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002167 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002168 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002169 if ((ret = strbuf_init(&buf, 32)) < 0)
2170 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002171 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2172 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002173 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002174 if (pev->args[i].name == NULL && ret >= 0)
2175 ret = -ENOMEM;
2176 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002177error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002178 if (ret < 0)
2179 clear_perf_probe_event(pev);
2180
2181 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002182}
2183
2184void clear_perf_probe_event(struct perf_probe_event *pev)
2185{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002186 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002187 int i;
2188
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002189 free(pev->event);
2190 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002191 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002192 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002193
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002194 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002195 free(pev->args[i].name);
2196 free(pev->args[i].var);
2197 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002198 field = pev->args[i].field;
2199 while (field) {
2200 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002201 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002202 free(field);
2203 field = next;
2204 }
2205 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002206 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002207 memset(pev, 0, sizeof(*pev));
2208}
2209
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002210#define strdup_or_goto(str, label) \
2211({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2212
2213static int perf_probe_point__copy(struct perf_probe_point *dst,
2214 struct perf_probe_point *src)
2215{
2216 dst->file = strdup_or_goto(src->file, out_err);
2217 dst->function = strdup_or_goto(src->function, out_err);
2218 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2219 dst->line = src->line;
2220 dst->retprobe = src->retprobe;
2221 dst->offset = src->offset;
2222 return 0;
2223
2224out_err:
2225 clear_perf_probe_point(dst);
2226 return -ENOMEM;
2227}
2228
2229static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2230 struct perf_probe_arg *src)
2231{
2232 struct perf_probe_arg_field *field, **ppfield;
2233
2234 dst->name = strdup_or_goto(src->name, out_err);
2235 dst->var = strdup_or_goto(src->var, out_err);
2236 dst->type = strdup_or_goto(src->type, out_err);
2237
2238 field = src->field;
2239 ppfield = &(dst->field);
2240 while (field) {
2241 *ppfield = zalloc(sizeof(*field));
2242 if (!*ppfield)
2243 goto out_err;
2244 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2245 (*ppfield)->index = field->index;
2246 (*ppfield)->ref = field->ref;
2247 field = field->next;
2248 ppfield = &((*ppfield)->next);
2249 }
2250 return 0;
2251out_err:
2252 return -ENOMEM;
2253}
2254
2255int perf_probe_event__copy(struct perf_probe_event *dst,
2256 struct perf_probe_event *src)
2257{
2258 int i;
2259
2260 dst->event = strdup_or_goto(src->event, out_err);
2261 dst->group = strdup_or_goto(src->group, out_err);
2262 dst->target = strdup_or_goto(src->target, out_err);
2263 dst->uprobes = src->uprobes;
2264
2265 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2266 goto out_err;
2267
2268 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2269 if (!dst->args)
2270 goto out_err;
2271 dst->nargs = src->nargs;
2272
2273 for (i = 0; i < src->nargs; i++)
2274 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2275 goto out_err;
2276 return 0;
2277
2278out_err:
2279 clear_perf_probe_event(dst);
2280 return -ENOMEM;
2281}
2282
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002283void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002284{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302285 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002286 int i;
2287
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002288 free(tev->event);
2289 free(tev->group);
2290 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002291 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002292 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002293 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002294 free(tev->args[i].name);
2295 free(tev->args[i].value);
2296 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002297 ref = tev->args[i].ref;
2298 while (ref) {
2299 next = ref->next;
2300 free(ref);
2301 ref = next;
2302 }
2303 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002304 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002305 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002306}
2307
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002308struct kprobe_blacklist_node {
2309 struct list_head list;
2310 unsigned long start;
2311 unsigned long end;
2312 char *symbol;
2313};
2314
2315static void kprobe_blacklist__delete(struct list_head *blacklist)
2316{
2317 struct kprobe_blacklist_node *node;
2318
2319 while (!list_empty(blacklist)) {
2320 node = list_first_entry(blacklist,
2321 struct kprobe_blacklist_node, list);
2322 list_del(&node->list);
2323 free(node->symbol);
2324 free(node);
2325 }
2326}
2327
2328static int kprobe_blacklist__load(struct list_head *blacklist)
2329{
2330 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002331 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002332 char buf[PATH_MAX], *p;
2333 FILE *fp;
2334 int ret;
2335
2336 if (__debugfs == NULL)
2337 return -ENOTSUP;
2338
2339 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2340 if (ret < 0)
2341 return ret;
2342
2343 fp = fopen(buf, "r");
2344 if (!fp)
2345 return -errno;
2346
2347 ret = 0;
2348 while (fgets(buf, PATH_MAX, fp)) {
2349 node = zalloc(sizeof(*node));
2350 if (!node) {
2351 ret = -ENOMEM;
2352 break;
2353 }
2354 INIT_LIST_HEAD(&node->list);
2355 list_add_tail(&node->list, blacklist);
2356 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2357 ret = -EINVAL;
2358 break;
2359 }
2360 p = strchr(buf, '\t');
2361 if (p) {
2362 p++;
2363 if (p[strlen(p) - 1] == '\n')
2364 p[strlen(p) - 1] = '\0';
2365 } else
2366 p = (char *)"unknown";
2367 node->symbol = strdup(p);
2368 if (!node->symbol) {
2369 ret = -ENOMEM;
2370 break;
2371 }
2372 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2373 node->start, node->end, node->symbol);
2374 ret++;
2375 }
2376 if (ret < 0)
2377 kprobe_blacklist__delete(blacklist);
2378 fclose(fp);
2379
2380 return ret;
2381}
2382
2383static struct kprobe_blacklist_node *
2384kprobe_blacklist__find_by_address(struct list_head *blacklist,
2385 unsigned long address)
2386{
2387 struct kprobe_blacklist_node *node;
2388
2389 list_for_each_entry(node, blacklist, list) {
2390 if (node->start <= address && address <= node->end)
2391 return node;
2392 }
2393
2394 return NULL;
2395}
2396
Masami Hiramatsub0312202015-06-16 20:50:55 +09002397static LIST_HEAD(kprobe_blacklist);
2398
2399static void kprobe_blacklist__init(void)
2400{
2401 if (!list_empty(&kprobe_blacklist))
2402 return;
2403
2404 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2405 pr_debug("No kprobe blacklist support, ignored\n");
2406}
2407
2408static void kprobe_blacklist__release(void)
2409{
2410 kprobe_blacklist__delete(&kprobe_blacklist);
2411}
2412
2413static bool kprobe_blacklist__listed(unsigned long address)
2414{
2415 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2416}
2417
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002418static int perf_probe_event__sprintf(const char *group, const char *event,
2419 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002420 const char *module,
2421 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002422{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002423 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002424 char *buf;
2425
2426 if (asprintf(&buf, "%s:%s", group, event) < 0)
2427 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002428 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002429 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002430 if (ret)
2431 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002432
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002433 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002434 buf = synthesize_perf_probe_point(&pev->point);
2435 if (!buf)
2436 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002437 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002438 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002439
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002440 if (!ret && module)
2441 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002442
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002443 if (!ret && pev->nargs > 0) {
2444 ret = strbuf_add(result, " with", 5);
2445 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002446 buf = synthesize_perf_probe_arg(&pev->args[i]);
2447 if (!buf)
2448 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002449 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002450 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002451 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002452 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002453 if (!ret)
2454 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002455
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002456 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002457}
2458
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002459/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002460int show_perf_probe_event(const char *group, const char *event,
2461 struct perf_probe_event *pev,
2462 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002463{
2464 struct strbuf buf = STRBUF_INIT;
2465 int ret;
2466
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002467 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002468 if (ret >= 0) {
2469 if (use_stdout)
2470 printf("%s\n", buf.buf);
2471 else
2472 pr_info("%s\n", buf.buf);
2473 }
2474 strbuf_release(&buf);
2475
2476 return ret;
2477}
2478
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002479static bool filter_probe_trace_event(struct probe_trace_event *tev,
2480 struct strfilter *filter)
2481{
2482 char tmp[128];
2483
2484 /* At first, check the event name itself */
2485 if (strfilter__compare(filter, tev->event))
2486 return true;
2487
2488 /* Next, check the combination of name and group */
2489 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2490 return false;
2491 return strfilter__compare(filter, tmp);
2492}
2493
2494static int __show_perf_probe_events(int fd, bool is_kprobe,
2495 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002496{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302497 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302498 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002499 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002500 struct strlist *rawlist;
2501 struct str_node *ent;
2502
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002503 memset(&tev, 0, sizeof(tev));
2504 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002505
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002506 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002507 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002508 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002509
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002510 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302511 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002512 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002513 if (!filter_probe_trace_event(&tev, filter))
2514 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302515 ret = convert_to_perf_probe_event(&tev, &pev,
2516 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002517 if (ret < 0)
2518 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002519 ret = show_perf_probe_event(pev.group, pev.event,
2520 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002521 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002522 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002523next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002524 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302525 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002526 if (ret < 0)
2527 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002528 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002529 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002530 /* Cleanup cached debuginfo if needed */
2531 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002532
2533 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002534}
2535
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302536/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002537int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302538{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002539 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302540
2541 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302542
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002543 if (probe_conf.cache)
2544 return probe_cache__show_all_caches(filter);
2545
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002546 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302547 if (ret < 0)
2548 return ret;
2549
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002550 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2551 if (ret < 0)
2552 return ret;
2553
2554 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002555 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002556 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002557 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002558 if (kp_fd > 0)
2559 close(kp_fd);
2560 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002561 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002562 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302563
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002564 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002565}
2566
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002567static int get_new_event_name(char *buf, size_t len, const char *base,
2568 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002569{
2570 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002571 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002572
Naveen N. Rao3099c022015-04-28 17:35:34 +05302573 if (*base == '.')
2574 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002575 nbase = strdup(base);
2576 if (!nbase)
2577 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302578
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002579 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2580 p = strchr(nbase, '.');
2581 if (p && p != nbase)
2582 *p = '\0';
2583
2584 /* Try no suffix number */
2585 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002586 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002587 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002588 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002589 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002590 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002591 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002592
Masami Hiramatsud761b082009-12-15 10:32:25 -05002593 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002594 pr_warning("Error: event \"%s\" already exists.\n"
2595 " Hint: Remove existing event by 'perf probe -d'\n"
2596 " or force duplicates by 'perf probe -f'\n"
2597 " or set 'force=yes' in BPF source.\n",
2598 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002599 ret = -EEXIST;
2600 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002601 }
2602
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002603 /* Try to add suffix */
2604 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002605 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002606 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002607 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002608 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002609 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002610 if (!strlist__has_entry(namelist, buf))
2611 break;
2612 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002613 if (i == MAX_EVENT_INDEX) {
2614 pr_warning("Too many events are on the same function.\n");
2615 ret = -ERANGE;
2616 }
2617
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002618out:
2619 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002620 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002621}
2622
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002623/* Warn if the current kernel's uprobe implementation is old */
2624static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2625{
2626 int i;
2627 char *buf = synthesize_probe_trace_command(tev);
2628
2629 /* Old uprobe event doesn't support memory dereference */
2630 if (!tev->uprobes || tev->nargs == 0 || !buf)
2631 goto out;
2632
2633 for (i = 0; i < tev->nargs; i++)
2634 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2635 pr_warning("Please upgrade your kernel to at least "
2636 "3.14 to have access to feature %s\n",
2637 tev->args[i].value);
2638 break;
2639 }
2640out:
2641 free(buf);
2642}
2643
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002644/* Set new name from original perf_probe_event and namelist */
2645static int probe_trace_event__set_name(struct probe_trace_event *tev,
2646 struct perf_probe_event *pev,
2647 struct strlist *namelist,
2648 bool allow_suffix)
2649{
2650 const char *event, *group;
2651 char buf[64];
2652 int ret;
2653
Masami Hiramatsubc062232016-07-01 17:03:12 +09002654 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002655 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002656 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002657 else if (tev->event)
2658 event = tev->event;
2659 else {
2660 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002661 if (pev->point.function &&
2662 (strncmp(pev->point.function, "0x", 2) != 0) &&
2663 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002664 event = pev->point.function;
2665 else
2666 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002667 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002668 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002669 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002670 else if (tev->group)
2671 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002672 else
2673 group = PERFPROBE_GROUP;
2674
2675 /* Get an unused new event name */
2676 ret = get_new_event_name(buf, 64, event,
2677 namelist, allow_suffix);
2678 if (ret < 0)
2679 return ret;
2680
2681 event = buf;
2682
2683 tev->event = strdup(event);
2684 tev->group = strdup(group);
2685 if (tev->event == NULL || tev->group == NULL)
2686 return -ENOMEM;
2687
2688 /* Add added event name to namelist */
2689 strlist__add(namelist, event);
2690 return 0;
2691}
2692
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002693static int __open_probe_file_and_namelist(bool uprobe,
2694 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002695{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002696 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002697
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002698 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002699 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002700 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002701
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002702 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002703 *namelist = probe_file__get_namelist(fd);
2704 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002705 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002706 close(fd);
2707 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002708 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002709 return fd;
2710}
2711
2712static int __add_probe_trace_events(struct perf_probe_event *pev,
2713 struct probe_trace_event *tevs,
2714 int ntevs, bool allow_suffix)
2715{
2716 int i, fd[2] = {-1, -1}, up, ret;
2717 struct probe_trace_event *tev = NULL;
2718 struct probe_cache *cache = NULL;
2719 struct strlist *namelist[2] = {NULL, NULL};
2720
2721 up = pev->uprobes ? 1 : 0;
2722 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2723 if (fd[up] < 0)
2724 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002725
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002726 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002727 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002728 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002729 up = tev->uprobes ? 1 : 0;
2730 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2731 fd[up] = __open_probe_file_and_namelist(up,
2732 &namelist[up]);
2733 if (fd[up] < 0)
2734 goto close_out;
2735 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002736 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002737 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002738 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002739
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002740 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002741 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002742 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002743 if (ret < 0)
2744 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002745
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002746 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002747 if (ret < 0)
2748 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002749
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002750 /*
2751 * Probes after the first probe which comes from same
2752 * user input are always allowed to add suffix, because
2753 * there might be several addresses corresponding to
2754 * one code line.
2755 */
2756 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002757 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002758 if (ret == -EINVAL && pev->uprobes)
2759 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002760 if (ret == 0 && probe_conf.cache) {
2761 cache = probe_cache__new(pev->target);
2762 if (!cache ||
2763 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2764 probe_cache__commit(cache) < 0)
2765 pr_warning("Failed to add event to probe cache\n");
2766 probe_cache__delete(cache);
2767 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002768
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002769close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002770 for (up = 0; up < 2; up++) {
2771 strlist__delete(namelist[up]);
2772 if (fd[up] >= 0)
2773 close(fd[up]);
2774 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002775 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002776}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002777
Wang Nan6bb536c2015-05-29 09:45:47 +00002778static int find_probe_functions(struct map *map, char *name,
2779 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002780{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002781 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002782 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002783 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002784
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002785 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002786 return 0;
2787
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002788 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002789 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002790 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002791 if (syms && found < probe_conf.max_probes)
2792 syms[found - 1] = sym;
2793 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002794 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002795
2796 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002797}
2798
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302799void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2800 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302801 struct map *map __maybe_unused,
2802 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302803
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002804/*
2805 * Find probe function addresses from map.
2806 * Return an error or the number of found probe_trace_event
2807 */
2808static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002809 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002810{
2811 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002812 struct ref_reloc_sym *reloc_sym = NULL;
2813 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002814 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002815 struct probe_trace_event *tev;
2816 struct perf_probe_point *pp = &pev->point;
2817 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002818 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002819 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302820 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002821
Masami Hiramatsu44225522015-05-08 10:03:28 +09002822 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002823 if (!map) {
2824 ret = -EINVAL;
2825 goto out;
2826 }
2827
Wang Nan6bb536c2015-05-29 09:45:47 +00002828 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2829 if (!syms) {
2830 ret = -ENOMEM;
2831 goto out;
2832 }
2833
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002834 /*
2835 * Load matched symbols: Since the different local symbols may have
2836 * same name but different addresses, this lists all the symbols.
2837 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002838 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002839 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002840 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002841 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002842 ret = -ENOENT;
2843 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002844 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002845 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002846 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002847 ret = -E2BIG;
2848 goto out;
2849 }
2850
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002851 /* Note that the symbols in the kmodule are not relocated */
2852 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002853 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002854 if (!reloc_sym) {
2855 pr_warning("Relocated base symbol is not found!\n");
2856 ret = -EINVAL;
2857 goto out;
2858 }
2859 }
2860
2861 /* Setup result trace-probe-events */
2862 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2863 if (!*tevs) {
2864 ret = -ENOMEM;
2865 goto out;
2866 }
2867
2868 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002869
Wang Nan6bb536c2015-05-29 09:45:47 +00002870 for (j = 0; j < num_matched_functions; j++) {
2871 sym = syms[j];
2872
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002873 tev = (*tevs) + ret;
2874 tp = &tev->point;
2875 if (ret == num_matched_functions) {
2876 pr_warning("Too many symbols are listed. Skip it.\n");
2877 break;
2878 }
2879 ret++;
2880
2881 if (pp->offset > sym->end - sym->start) {
2882 pr_warning("Offset %ld is bigger than the size of %s\n",
2883 pp->offset, sym->name);
2884 ret = -ENOENT;
2885 goto err_out;
2886 }
2887 /* Add one probe point */
2888 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002889
2890 /* Check the kprobe (not in module) is within .text */
2891 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002892 kprobe_warn_out_range(sym->name, tp->address)) {
2893 tp->symbol = NULL; /* Skip it */
2894 skipped++;
2895 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002896 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2897 tp->offset = tp->address - reloc_sym->addr;
2898 } else {
2899 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2900 tp->offset = pp->offset;
2901 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002902 tp->realname = strdup_or_goto(sym->name, nomem_out);
2903
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002904 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302905 if (pev->target) {
2906 if (pev->uprobes) {
2907 tev->point.module = strdup_or_goto(pev->target,
2908 nomem_out);
2909 } else {
2910 mod_name = find_module_name(pev->target);
2911 tev->point.module =
2912 strdup(mod_name ? mod_name : pev->target);
2913 free(mod_name);
2914 if (!tev->point.module)
2915 goto nomem_out;
2916 }
2917 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002918 tev->uprobes = pev->uprobes;
2919 tev->nargs = pev->nargs;
2920 if (tev->nargs) {
2921 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2922 tev->nargs);
2923 if (tev->args == NULL)
2924 goto nomem_out;
2925 }
2926 for (i = 0; i < tev->nargs; i++) {
2927 if (pev->args[i].name)
2928 tev->args[i].name =
2929 strdup_or_goto(pev->args[i].name,
2930 nomem_out);
2931
2932 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2933 nomem_out);
2934 if (pev->args[i].type)
2935 tev->args[i].type =
2936 strdup_or_goto(pev->args[i].type,
2937 nomem_out);
2938 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302939 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002940 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002941 if (ret == skipped) {
2942 ret = -ENOENT;
2943 goto err_out;
2944 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002945
2946out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002947 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002948 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002949 return ret;
2950
2951nomem_out:
2952 ret = -ENOMEM;
2953err_out:
2954 clear_probe_trace_events(*tevs, num_matched_functions);
2955 zfree(tevs);
2956 goto out;
2957}
2958
Wang Nanda15bd92015-08-26 10:57:45 +00002959static int try_to_find_absolute_address(struct perf_probe_event *pev,
2960 struct probe_trace_event **tevs)
2961{
2962 struct perf_probe_point *pp = &pev->point;
2963 struct probe_trace_event *tev;
2964 struct probe_trace_point *tp;
2965 int i, err;
2966
2967 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2968 return -EINVAL;
2969 if (perf_probe_event_need_dwarf(pev))
2970 return -EINVAL;
2971
2972 /*
2973 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2974 * absolute address.
2975 *
2976 * Only one tev can be generated by this.
2977 */
2978 *tevs = zalloc(sizeof(*tev));
2979 if (!*tevs)
2980 return -ENOMEM;
2981
2982 tev = *tevs;
2983 tp = &tev->point;
2984
2985 /*
2986 * Don't use tp->offset, use address directly, because
2987 * in synthesize_probe_trace_command() address cannot be
2988 * zero.
2989 */
2990 tp->address = pev->point.abs_address;
2991 tp->retprobe = pp->retprobe;
2992 tev->uprobes = pev->uprobes;
2993
2994 err = -ENOMEM;
2995 /*
2996 * Give it a '0x' leading symbol name.
2997 * In __add_probe_trace_events, a NULL symbol is interpreted as
2998 * invalud.
2999 */
3000 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3001 goto errout;
3002
3003 /* For kprobe, check range */
3004 if ((!tev->uprobes) &&
3005 (kprobe_warn_out_range(tev->point.symbol,
3006 tev->point.address))) {
3007 err = -EACCES;
3008 goto errout;
3009 }
3010
3011 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3012 goto errout;
3013
3014 if (pev->target) {
3015 tp->module = strdup(pev->target);
3016 if (!tp->module)
3017 goto errout;
3018 }
3019
3020 if (tev->group) {
3021 tev->group = strdup(pev->group);
3022 if (!tev->group)
3023 goto errout;
3024 }
3025
3026 if (pev->event) {
3027 tev->event = strdup(pev->event);
3028 if (!tev->event)
3029 goto errout;
3030 }
3031
3032 tev->nargs = pev->nargs;
3033 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3034 if (!tev->args) {
3035 err = -ENOMEM;
3036 goto errout;
3037 }
3038 for (i = 0; i < tev->nargs; i++)
3039 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3040
3041 return 1;
3042
3043errout:
3044 if (*tevs) {
3045 clear_probe_trace_events(*tevs, 1);
3046 *tevs = NULL;
3047 }
3048 return err;
3049}
3050
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003051/* Concatinate two arrays */
3052static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3053{
3054 void *ret;
3055
3056 ret = malloc(sz_a + sz_b);
3057 if (ret) {
3058 memcpy(ret, a, sz_a);
3059 memcpy(ret + sz_a, b, sz_b);
3060 }
3061 return ret;
3062}
3063
3064static int
3065concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3066 struct probe_trace_event **tevs2, int ntevs2)
3067{
3068 struct probe_trace_event *new_tevs;
3069 int ret = 0;
3070
3071 if (ntevs == 0) {
3072 *tevs = *tevs2;
3073 *ntevs = ntevs2;
3074 *tevs2 = NULL;
3075 return 0;
3076 }
3077
3078 if (*ntevs + ntevs2 > probe_conf.max_probes)
3079 ret = -E2BIG;
3080 else {
3081 /* Concatinate the array of probe_trace_event */
3082 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3083 *tevs2, ntevs2 * sizeof(**tevs2));
3084 if (!new_tevs)
3085 ret = -ENOMEM;
3086 else {
3087 free(*tevs);
3088 *tevs = new_tevs;
3089 *ntevs += ntevs2;
3090 }
3091 }
3092 if (ret < 0)
3093 clear_probe_trace_events(*tevs2, ntevs2);
3094 zfree(tevs2);
3095
3096 return ret;
3097}
3098
3099/*
3100 * Try to find probe_trace_event from given probe caches. Return the number
3101 * of cached events found, if an error occurs return the error.
3102 */
3103static int find_cached_events(struct perf_probe_event *pev,
3104 struct probe_trace_event **tevs,
3105 const char *target)
3106{
3107 struct probe_cache *cache;
3108 struct probe_cache_entry *entry;
3109 struct probe_trace_event *tmp_tevs = NULL;
3110 int ntevs = 0;
3111 int ret = 0;
3112
3113 cache = probe_cache__new(target);
3114 /* Return 0 ("not found") if the target has no probe cache. */
3115 if (!cache)
3116 return 0;
3117
3118 for_each_probe_cache_entry(entry, cache) {
3119 /* Skip the cache entry which has no name */
3120 if (!entry->pev.event || !entry->pev.group)
3121 continue;
3122 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3123 strglobmatch(entry->pev.event, pev->event)) {
3124 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3125 if (ret > 0)
3126 ret = concat_probe_trace_events(tevs, &ntevs,
3127 &tmp_tevs, ret);
3128 if (ret < 0)
3129 break;
3130 }
3131 }
3132 probe_cache__delete(cache);
3133 if (ret < 0) {
3134 clear_probe_trace_events(*tevs, ntevs);
3135 zfree(tevs);
3136 } else {
3137 ret = ntevs;
3138 if (ntevs > 0 && target && target[0] == '/')
3139 pev->uprobes = true;
3140 }
3141
3142 return ret;
3143}
3144
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003145/* Try to find probe_trace_event from all probe caches */
3146static int find_cached_events_all(struct perf_probe_event *pev,
3147 struct probe_trace_event **tevs)
3148{
3149 struct probe_trace_event *tmp_tevs = NULL;
3150 struct strlist *bidlist;
3151 struct str_node *nd;
3152 char *pathname;
3153 int ntevs = 0;
3154 int ret;
3155
3156 /* Get the buildid list of all valid caches */
3157 bidlist = build_id_cache__list_all(true);
3158 if (!bidlist) {
3159 ret = -errno;
3160 pr_debug("Failed to get buildids: %d\n", ret);
3161 return ret;
3162 }
3163
3164 ret = 0;
3165 strlist__for_each_entry(nd, bidlist) {
3166 pathname = build_id_cache__origname(nd->s);
3167 ret = find_cached_events(pev, &tmp_tevs, pathname);
3168 /* In the case of cnt == 0, we just skip it */
3169 if (ret > 0)
3170 ret = concat_probe_trace_events(tevs, &ntevs,
3171 &tmp_tevs, ret);
3172 free(pathname);
3173 if (ret < 0)
3174 break;
3175 }
3176 strlist__delete(bidlist);
3177
3178 if (ret < 0) {
3179 clear_probe_trace_events(*tevs, ntevs);
3180 zfree(tevs);
3181 } else
3182 ret = ntevs;
3183
3184 return ret;
3185}
3186
Masami Hiramatsubc062232016-07-01 17:03:12 +09003187static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3188 struct probe_trace_event **tevs)
3189{
3190 struct probe_cache *cache;
3191 struct probe_cache_entry *entry;
3192 struct probe_trace_event *tev;
3193 struct str_node *node;
3194 int ret, i;
3195
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003196 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003197 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003198 if (!pev->target)
3199 return find_cached_events_all(pev, tevs);
3200 else
3201 return find_cached_events(pev, tevs, pev->target);
3202 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003203 cache = probe_cache__new(pev->target);
3204 if (!cache)
3205 return 0;
3206
3207 entry = probe_cache__find(cache, pev);
3208 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003209 /* SDT must be in the cache */
3210 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003211 goto out;
3212 }
3213
3214 ret = strlist__nr_entries(entry->tevlist);
3215 if (ret > probe_conf.max_probes) {
3216 pr_debug("Too many entries matched in the cache of %s\n",
3217 pev->target ? : "kernel");
3218 ret = -E2BIG;
3219 goto out;
3220 }
3221
3222 *tevs = zalloc(ret * sizeof(*tev));
3223 if (!*tevs) {
3224 ret = -ENOMEM;
3225 goto out;
3226 }
3227
3228 i = 0;
3229 strlist__for_each_entry(node, entry->tevlist) {
3230 tev = &(*tevs)[i++];
3231 ret = parse_probe_trace_command(node->s, tev);
3232 if (ret < 0)
3233 goto out;
3234 /* Set the uprobes attribute as same as original */
3235 tev->uprobes = pev->uprobes;
3236 }
3237 ret = i;
3238
3239out:
3240 probe_cache__delete(cache);
3241 return ret;
3242}
3243
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303244static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003245 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003246{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003247 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003248
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003249 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003250 /* Set group name if not given */
3251 if (!pev->uprobes) {
3252 pev->group = strdup(PERFPROBE_GROUP);
3253 ret = pev->group ? 0 : -ENOMEM;
3254 } else
3255 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003256 if (ret != 0) {
3257 pr_warning("Failed to make a group name.\n");
3258 return ret;
3259 }
3260 }
3261
Wang Nanda15bd92015-08-26 10:57:45 +00003262 ret = try_to_find_absolute_address(pev, tevs);
3263 if (ret > 0)
3264 return ret;
3265
Masami Hiramatsubc062232016-07-01 17:03:12 +09003266 /* At first, we need to lookup cache entry */
3267 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003268 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3269 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003270
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003271 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003272 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003273 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003274 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003275
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003276 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003277}
3278
Wang Nan12fae5e2015-09-04 21:16:00 +09003279int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003280{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003281 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003282
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003283 /* Loop 1: convert all events */
3284 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003285 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003286 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003287 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003288 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003289 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003290 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003291 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003292 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003293 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003294 /* This just release blacklist only if allocated */
3295 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003296
Namhyung Kim844dffa2015-09-04 21:15:59 +09003297 return 0;
3298}
3299
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003300static int show_probe_trace_event(struct probe_trace_event *tev)
3301{
3302 char *buf = synthesize_probe_trace_command(tev);
3303
3304 if (!buf) {
3305 pr_debug("Failed to synthesize probe trace event.\n");
3306 return -EINVAL;
3307 }
3308
3309 /* Showing definition always go stdout */
3310 printf("%s\n", buf);
3311 free(buf);
3312
3313 return 0;
3314}
3315
3316int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3317{
3318 struct strlist *namelist = strlist__new(NULL, NULL);
3319 struct probe_trace_event *tev;
3320 struct perf_probe_event *pev;
3321 int i, j, ret = 0;
3322
3323 if (!namelist)
3324 return -ENOMEM;
3325
3326 for (j = 0; j < npevs && !ret; j++) {
3327 pev = &pevs[j];
3328 for (i = 0; i < pev->ntevs && !ret; i++) {
3329 tev = &pev->tevs[i];
3330 /* Skip if the symbol is out of .text or blacklisted */
3331 if (!tev->point.symbol && !pev->uprobes)
3332 continue;
3333
3334 /* Set new name for tev (and update namelist) */
3335 ret = probe_trace_event__set_name(tev, pev,
3336 namelist, true);
3337 if (!ret)
3338 ret = show_probe_trace_event(tev);
3339 }
3340 }
3341 strlist__delete(namelist);
3342
3343 return ret;
3344}
3345
Wang Nan12fae5e2015-09-04 21:16:00 +09003346int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003347{
3348 int i, ret = 0;
3349
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003350 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003351 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003352 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3353 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003354 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003355 if (ret < 0)
3356 break;
3357 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003358 return ret;
3359}
3360
Wang Nan12fae5e2015-09-04 21:16:00 +09003361void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003362{
3363 int i, j;
3364
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003365 /* Loop 3: cleanup and free trace events */
3366 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003367 for (j = 0; j < pevs[i].ntevs; j++)
3368 clear_probe_trace_event(&pevs[i].tevs[j]);
3369 zfree(&pevs[i].tevs);
3370 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003371 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003372 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003373}
3374
3375int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3376{
3377 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003378
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003379 ret = init_probe_symbol_maps(pevs->uprobes);
3380 if (ret < 0)
3381 return ret;
3382
Wang Nan12fae5e2015-09-04 21:16:00 +09003383 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003384 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003385 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003386
Wang Nan12fae5e2015-09-04 21:16:00 +09003387 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003388
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003389 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003390 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003391}
3392
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003393int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003394{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003395 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003396 char *str = strfilter__string(filter);
3397
3398 if (!str)
3399 return -EINVAL;
3400
Masami Hiramatsufa282442009-12-08 17:03:23 -05003401 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003402 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3403 if (ret < 0)
3404 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303405
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003406 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003407 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303408 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003409
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003410 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003411 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003412 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003413 goto error;
3414 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003415 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303416
3417error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003418 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303419 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003420 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303421 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003422out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003423 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003424
3425 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003426}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303427
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003428int show_available_funcs(const char *target, struct strfilter *_filter,
3429 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003430{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003431 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003432 struct map *map;
3433 int ret;
3434
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003435 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003436 if (ret < 0)
3437 return ret;
3438
3439 /* Get a symbol map */
3440 if (user)
3441 map = dso__new_map(target);
3442 else
3443 map = kernel_get_module_map(target);
3444 if (!map) {
3445 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003446 return -EINVAL;
3447 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003448
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003449 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003450 if (ret) {
3451 if (ret == -2) {
3452 char *str = strfilter__string(_filter);
3453 pr_err("Failed to find symbols matched to \"%s\"\n",
3454 str);
3455 free(str);
3456 } else
3457 pr_err("Failed to load symbols in %s\n",
3458 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003459 goto end;
3460 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003461 if (!dso__sorted_by_name(map->dso, map->type))
3462 dso__sort_by_name(map->dso, map->type);
3463
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003464 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303465 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003466
3467 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3468 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3469
3470 if (strfilter__compare(_filter, pos->sym.name))
3471 printf("%s\n", pos->sym.name);
3472 }
3473
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003474end:
3475 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003476 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003477 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003478 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303479
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003480 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303481}
3482
Wang Nanda15bd92015-08-26 10:57:45 +00003483int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3484 struct perf_probe_arg *pvar)
3485{
3486 tvar->value = strdup(pvar->var);
3487 if (tvar->value == NULL)
3488 return -ENOMEM;
3489 if (pvar->type) {
3490 tvar->type = strdup(pvar->type);
3491 if (tvar->type == NULL)
3492 return -ENOMEM;
3493 }
3494 if (pvar->name) {
3495 tvar->name = strdup(pvar->name);
3496 if (tvar->name == NULL)
3497 return -ENOMEM;
3498 } else
3499 tvar->name = NULL;
3500 return 0;
3501}