blob: 542e6472c4d74af02fa15fd2e2edb09dac9b6bc3 [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, '/'))
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900166 return dso__new_map(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 Hiramatsueebc5092017-01-04 12:29:05 +0900176 map__get(pos);
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900177 return pos;
178 }
179 }
180 return NULL;
181}
182
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500183struct map *get_target_map(const char *target, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900184{
185 /* Init maps of given executable or kernel */
186 if (user)
187 return dso__new_map(target);
188 else
189 return kernel_get_module_map(target);
190}
191
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000192static int convert_exec_to_group(const char *exec, char **result)
193{
194 char *ptr1, *ptr2, *exec_copy;
195 char buf[64];
196 int ret;
197
198 exec_copy = strdup(exec);
199 if (!exec_copy)
200 return -ENOMEM;
201
202 ptr1 = basename(exec_copy);
203 if (!ptr1) {
204 ret = -EINVAL;
205 goto out;
206 }
207
Colin Ian Kingead1a572016-10-03 11:34:31 +0100208 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
Masami Hiramatsu35726d32016-09-24 00:35:16 +0900209 if (!isalnum(*ptr2) && *ptr2 != '_') {
210 *ptr2 = '\0';
211 break;
212 }
213 }
214
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000215 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
216 if (ret < 0)
217 goto out;
218
219 *result = strdup(buf);
220 ret = *result ? 0 : -ENOMEM;
221
222out:
223 free(exec_copy);
224 return ret;
225}
226
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900227static void clear_perf_probe_point(struct perf_probe_point *pp)
228{
229 free(pp->file);
230 free(pp->function);
231 free(pp->lazy_line);
232}
233
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000234static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
235{
236 int i;
237
238 for (i = 0; i < ntevs; i++)
239 clear_probe_trace_event(tevs + i);
240}
241
Masami Hiramatsub0312202015-06-16 20:50:55 +0900242static bool kprobe_blacklist__listed(unsigned long address);
243static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
244{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900245 u64 etext_addr = 0;
246 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000247
Masami Hiramatsub0312202015-06-16 20:50:55 +0900248 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900249 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
250 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000251
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900252 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900253 pr_warning("%s is out of .text, skip it.\n", symbol);
254 else if (kprobe_blacklist__listed(address))
255 pr_warning("%s is blacklisted function, skip it.\n", symbol);
256 else
257 return false;
258
259 return true;
260}
261
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530262/*
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530263 * @module can be module name of module file path. In case of path,
264 * inspect elf and find out what is actual module name.
265 * Caller has to free mod_name after using it.
266 */
267static char *find_module_name(const char *module)
268{
269 int fd;
270 Elf *elf;
271 GElf_Ehdr ehdr;
272 GElf_Shdr shdr;
273 Elf_Data *data;
274 Elf_Scn *sec;
275 char *mod_name = NULL;
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900276 int name_offset;
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530277
278 fd = open(module, O_RDONLY);
279 if (fd < 0)
280 return NULL;
281
282 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
283 if (elf == NULL)
284 goto elf_err;
285
286 if (gelf_getehdr(elf, &ehdr) == NULL)
287 goto ret_err;
288
289 sec = elf_section_by_name(elf, &ehdr, &shdr,
290 ".gnu.linkonce.this_module", NULL);
291 if (!sec)
292 goto ret_err;
293
294 data = elf_getdata(sec, NULL);
295 if (!data || !data->d_buf)
296 goto ret_err;
297
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900298 /*
299 * NOTE:
300 * '.gnu.linkonce.this_module' section of kernel module elf directly
301 * maps to 'struct module' from linux/module.h. This section contains
302 * actual module name which will be used by kernel after loading it.
303 * But, we cannot use 'struct module' here since linux/module.h is not
304 * exposed to user-space. Offset of 'name' has remained same from long
305 * time, so hardcoding it here.
306 */
307 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
308 name_offset = 12;
309 else /* expect ELFCLASS64 by default */
310 name_offset = 24;
311
312 mod_name = strdup((char *)data->d_buf + name_offset);
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530313
314ret_err:
315 elf_end(elf);
316elf_err:
317 close(fd);
318 return mod_name;
319}
320
Ingo Molnar89fe8082013-09-30 12:07:11 +0200321#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000322
323static int kernel_get_module_dso(const char *module, struct dso **pdso)
324{
325 struct dso *dso;
326 struct map *map;
327 const char *vmlinux_name;
328 int ret = 0;
329
330 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300331 char module_name[128];
332
333 snprintf(module_name, sizeof(module_name), "[%s]", module);
334 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
335 if (map) {
336 dso = map->dso;
337 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000338 }
339 pr_debug("Failed to find module %s.\n", module);
340 return -ENOENT;
341 }
342
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300343 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000344 dso = map->dso;
345
346 vmlinux_name = symbol_conf.vmlinux_name;
347 dso->load_errno = 0;
348 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300349 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000350 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300351 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000352found:
353 *pdso = dso;
354 return ret;
355}
356
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900357/*
358 * Some binaries like glibc have special symbols which are on the symbol
359 * table, but not in the debuginfo. If we can find the address of the
360 * symbol from map, we can translate the address back to the probe point.
361 */
362static int find_alternative_probe_point(struct debuginfo *dinfo,
363 struct perf_probe_point *pp,
364 struct perf_probe_point *result,
365 const char *target, bool uprobes)
366{
367 struct map *map = NULL;
368 struct symbol *sym;
369 u64 address = 0;
370 int ret = -ENOENT;
371
372 /* This can work only for function-name based one */
373 if (!pp->function || pp->file)
374 return -ENOTSUP;
375
376 map = get_target_map(target, uprobes);
377 if (!map)
378 return -EINVAL;
379
380 /* Find the address of given function */
381 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900382 if (uprobes)
383 address = sym->start;
384 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900385 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900386 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900387 }
388 if (!address) {
389 ret = -ENOENT;
390 goto out;
391 }
Wang Nanf6c15622015-04-08 02:14:34 +0000392 pr_debug("Symbol %s address found : %" PRIx64 "\n",
393 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900394
395 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
396 result);
397 if (ret <= 0)
398 ret = (!ret) ? -ENOENT : ret;
399 else {
400 result->offset += pp->offset;
401 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800402 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900403 ret = 0;
404 }
405
406out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900407 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900408 return ret;
409
410}
411
412static int get_alternative_probe_event(struct debuginfo *dinfo,
413 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900414 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900415{
416 int ret;
417
418 memcpy(tmp, &pev->point, sizeof(*tmp));
419 memset(&pev->point, 0, sizeof(pev->point));
420 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900421 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900422 if (ret < 0)
423 memcpy(&pev->point, tmp, sizeof(*tmp));
424
425 return ret;
426}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000427
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900428static int get_alternative_line_range(struct debuginfo *dinfo,
429 struct line_range *lr,
430 const char *target, bool user)
431{
David Ahern6d4a4892015-03-11 10:36:20 -0400432 struct perf_probe_point pp = { .function = lr->function,
433 .file = lr->file,
434 .line = lr->start };
435 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900436 int ret, len = 0;
437
David Ahern6d4a4892015-03-11 10:36:20 -0400438 memset(&result, 0, sizeof(result));
439
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900440 if (lr->end != INT_MAX)
441 len = lr->end - lr->start;
442 ret = find_alternative_probe_point(dinfo, &pp, &result,
443 target, user);
444 if (!ret) {
445 lr->function = result.function;
446 lr->file = result.file;
447 lr->start = result.line;
448 if (lr->end != INT_MAX)
449 lr->end = lr->start + len;
450 clear_perf_probe_point(&pp);
451 }
452 return ret;
453}
454
Masami Hiramatsuff741782011-06-27 16:27:39 +0900455/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000456static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900457{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000458 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900459 char reason[STRERR_BUFSIZE];
460 struct debuginfo *ret = NULL;
461 struct dso *dso = NULL;
462 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900463
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000464 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900465 err = kernel_get_module_dso(module, &dso);
466 if (err < 0) {
467 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300468 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900469 strcpy(reason, "(unknown)");
470 } else
471 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000472 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900473 pr_err("Failed to find the path for %s: %s\n",
474 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900475 return NULL;
476 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900477 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900478 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000479 ret = debuginfo__new(path);
480 if (!ret && !silent) {
481 pr_warning("The %s file has no debug information.\n", path);
482 if (!module || !strtailcmp(path, ".ko"))
483 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
484 else
485 pr_warning("Rebuild with -g, ");
486 pr_warning("or install an appropriate debuginfo package.\n");
487 }
488 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400489}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300490
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900491/* For caching the last debuginfo */
492static struct debuginfo *debuginfo_cache;
493static char *debuginfo_cache_path;
494
495static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
496{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900497 const char *path = module;
498
499 /* If the module is NULL, it should be the kernel. */
500 if (!module)
501 path = "kernel";
502
503 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900504 goto out;
505
506 /* Copy module path */
507 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900508 debuginfo_cache_path = strdup(path);
509 if (!debuginfo_cache_path) {
510 debuginfo__delete(debuginfo_cache);
511 debuginfo_cache = NULL;
512 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900513 }
514
515 debuginfo_cache = open_debuginfo(module, silent);
516 if (!debuginfo_cache)
517 zfree(&debuginfo_cache_path);
518out:
519 return debuginfo_cache;
520}
521
522static void debuginfo_cache__exit(void)
523{
524 debuginfo__delete(debuginfo_cache);
525 debuginfo_cache = NULL;
526 zfree(&debuginfo_cache_path);
527}
528
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000529
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000530static int get_text_start_address(const char *exec, unsigned long *address)
531{
532 Elf *elf;
533 GElf_Ehdr ehdr;
534 GElf_Shdr shdr;
535 int fd, ret = -ENOENT;
536
537 fd = open(exec, O_RDONLY);
538 if (fd < 0)
539 return -errno;
540
541 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900542 if (elf == NULL) {
543 ret = -EINVAL;
544 goto out_close;
545 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000546
547 if (gelf_getehdr(elf, &ehdr) == NULL)
548 goto out;
549
550 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
551 goto out;
552
553 *address = shdr.sh_addr - shdr.sh_offset;
554 ret = 0;
555out:
556 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900557out_close:
558 close(fd);
559
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000560 return ret;
561}
562
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000563/*
564 * Convert trace point to probe point with debuginfo
565 */
566static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
567 struct perf_probe_point *pp,
568 bool is_kprobe)
569{
570 struct debuginfo *dinfo = NULL;
571 unsigned long stext = 0;
572 u64 addr = tp->address;
573 int ret = -ENOENT;
574
575 /* convert the address to dwarf address */
576 if (!is_kprobe) {
577 if (!addr) {
578 ret = -EINVAL;
579 goto error;
580 }
581 ret = get_text_start_address(tp->module, &stext);
582 if (ret < 0)
583 goto error;
584 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000585 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900586 /* If the module is given, this returns relative address */
587 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
588 false, !!tp->module);
589 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000590 goto error;
591 addr += tp->offset;
592 }
593
594 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
595 tp->module ? : "kernel");
596
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900597 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
598 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000599 ret = debuginfo__find_probe_point(dinfo,
600 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900601 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000602 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000603
604 if (ret > 0) {
605 pp->retprobe = tp->retprobe;
606 return 0;
607 }
608error:
609 pr_debug("Failed to find corresponding probes from debuginfo.\n");
610 return ret ? : -ENOENT;
611}
612
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000613static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
614 int ntevs, const char *exec)
615{
616 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000617 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000618
619 if (!exec)
620 return 0;
621
622 ret = get_text_start_address(exec, &stext);
623 if (ret < 0)
624 return ret;
625
626 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000627 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000628 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000629 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000630 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000631 ret = -ENOMEM;
632 break;
633 }
634 tevs[i].uprobes = true;
635 }
636
637 return ret;
638}
639
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900640static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
641 int ntevs, const char *module)
642{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900643 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530644 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900645
646 if (!module)
647 return 0;
648
Ravi Bangoria63a29612016-04-26 19:55:41 +0530649 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900650
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900651 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530652 tevs[i].point.module =
653 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900654 if (!tevs[i].point.module) {
655 ret = -ENOMEM;
656 break;
657 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900658 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900659
Ravi Bangoria63a29612016-04-26 19:55:41 +0530660 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900661 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900662}
663
Ravi Bangoriad8204562016-08-09 01:23:24 -0500664static int
665post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
666 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000667{
668 struct ref_reloc_sym *reloc_sym;
669 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900670 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000671
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900672 /* Skip post process if the target is an offline kernel */
673 if (symbol_conf.ignore_vmlinux_buildid)
674 return 0;
675
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000676 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000677 if (!reloc_sym) {
678 pr_warning("Relocated base symbol is not found!\n");
679 return -EINVAL;
680 }
681
682 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900683 if (!tevs[i].point.address || tevs[i].point.retprobe)
684 continue;
685 /* If we found a wrong one, mark it by NULL symbol */
686 if (kprobe_warn_out_range(tevs[i].point.symbol,
687 tevs[i].point.address)) {
688 tmp = NULL;
689 skipped++;
690 } else {
691 tmp = strdup(reloc_sym->name);
692 if (!tmp)
693 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000694 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900695 /* If we have no realname, use symbol for it */
696 if (!tevs[i].point.realname)
697 tevs[i].point.realname = tevs[i].point.symbol;
698 else
699 free(tevs[i].point.symbol);
700 tevs[i].point.symbol = tmp;
701 tevs[i].point.offset = tevs[i].point.address -
702 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000703 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900704 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000705}
706
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500707void __weak
708arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
709 int ntevs __maybe_unused)
710{
711}
712
Ravi Bangoriad8204562016-08-09 01:23:24 -0500713/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500714static int post_process_probe_trace_events(struct perf_probe_event *pev,
715 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500716 int ntevs, const char *module,
717 bool uprobe)
718{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500719 int ret;
720
Ravi Bangoriad8204562016-08-09 01:23:24 -0500721 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500722 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
723 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500724 /* Currently ref_reloc_sym based probe is not for drivers */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500725 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
726 else
727 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500728
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500729 if (ret >= 0)
730 arch__post_process_probe_trace_events(pev, ntevs);
731
732 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500733}
734
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300735/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530736static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900737 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300738{
739 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900740 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530741 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900742 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743
Masami Hiramatsu44225522015-05-08 10:03:28 +0900744 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900745 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000746 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900747 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900748 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300749 return 0;
750 }
751
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000752 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900753 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900754 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900755
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900756 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900757 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900758 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900759 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900760 /*
761 * Write back to the original probe_event for
762 * setting appropriate (user given) event name
763 */
764 clear_perf_probe_point(&pev->point);
765 memcpy(&pev->point, &tmp, sizeof(tmp));
766 }
767 }
768
Masami Hiramatsuff741782011-06-27 16:27:39 +0900769 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300770
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000772 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500773 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900774 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900775 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000776 clear_probe_trace_events(*tevs, ntevs);
777 zfree(tevs);
778 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900779 if (ret != ntevs)
780 return ret < 0 ? ret : ntevs;
781 ntevs = 0;
782 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400783 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400785 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900786 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400787 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900788 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400789 }
790 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400791 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000792 if (ntevs < 0) {
793 if (ntevs == -EBADF)
794 pr_warning("Warning: No dwarf info found in the vmlinux - "
795 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400796 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900797 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400798 return 0;
799 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400801 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300802}
803
804#define LINEBUF_SIZE 256
805#define NR_ADDITIONAL_LINES 2
806
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100807static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000809 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100810 const char *color = show_num ? "" : PERF_COLOR_BLUE;
811 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100813 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300814 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
815 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100816 if (skip)
817 continue;
818 if (!prefix) {
819 prefix = show_num ? "%7d " : " ";
820 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300821 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100822 color_fprintf(stdout, color, "%s", buf);
823
824 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400825
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100826 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300827error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100828 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000829 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300830 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100831 return -1;
832 }
833 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300834}
835
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100836static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
837{
838 int rv = __show_one_line(fp, l, skip, show_num);
839 if (rv == 0) {
840 pr_warning("Source file is shorter than expected.\n");
841 rv = -1;
842 }
843 return rv;
844}
845
846#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
847#define show_one_line(f,l) _show_one_line(f,l,false,false)
848#define skip_one_line(f,l) _show_one_line(f,l,true,false)
849#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
850
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300851/*
852 * Show line-range always requires debuginfo to find source file and
853 * line number.
854 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900855static int __show_line_range(struct line_range *lr, const char *module,
856 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300857{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400858 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000859 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900860 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300861 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900862 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900863 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000864 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300865
866 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000867 dinfo = open_debuginfo(module, false);
868 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900869 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400870
Masami Hiramatsuff741782011-06-27 16:27:39 +0900871 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900872 if (!ret) { /* Not found, retry with an alternative */
873 ret = get_alternative_line_range(dinfo, lr, module, user);
874 if (!ret)
875 ret = debuginfo__find_line_range(dinfo, lr);
876 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900877 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000878 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400879 pr_warning("Specified source line is not found.\n");
880 return -ENOENT;
881 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000882 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400883 return ret;
884 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300885
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900886 /* Convert source file path */
887 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900888 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800889
890 /* Free old path when new path is assigned */
891 if (tmp != lr->path)
892 free(tmp);
893
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900894 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000895 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900896 return ret;
897 }
898
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300899 setup_pager();
900
901 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900902 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300903 lr->start - lr->offset);
904 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100905 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300906
907 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400908 if (fp == NULL) {
909 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300910 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400911 return -errno;
912 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300913 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100914 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100915 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100916 if (ret < 0)
917 goto end;
918 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300919
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300920 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000921 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100922 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100923 if (ret < 0)
924 goto end;
925 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100926 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400927 if (ret < 0)
928 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300929 }
930
931 if (lr->end == INT_MAX)
932 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100933 while (l <= lr->end) {
934 ret = show_one_line_or_eof(fp, l++ - lr->offset);
935 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100936 break;
937 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400938end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300939 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400940 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300941}
942
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000943int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000944{
945 int ret;
946
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900947 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000948 if (ret < 0)
949 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900950 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900951 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000952
953 return ret;
954}
955
Masami Hiramatsuff741782011-06-27 16:27:39 +0900956static int show_available_vars_at(struct debuginfo *dinfo,
957 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900958 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900959{
960 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900961 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900962 struct str_node *node;
963 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900964 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900965 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900966
967 buf = synthesize_perf_probe_point(&pev->point);
968 if (!buf)
969 return -EINVAL;
970 pr_debug("Searching variables at %s\n", buf);
971
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900972 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900973 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900974 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900975 if (!ret) {
976 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900977 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900978 /* Release the old probe_point */
979 clear_perf_probe_point(&tmp);
980 }
981 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900982 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000983 if (ret == 0 || ret == -ENOENT) {
984 pr_err("Failed to find the address of %s\n", buf);
985 ret = -ENOENT;
986 } else
987 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900988 goto end;
989 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000990
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900991 /* Some variables are found */
992 fprintf(stdout, "Available variables at %s\n", buf);
993 for (i = 0; i < ret; i++) {
994 vl = &vls[i];
995 /*
996 * A probe point might be converted to
997 * several trace points.
998 */
999 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1000 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001001 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001002 nvars = 0;
1003 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001004 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001005 var = strchr(node->s, '\t') + 1;
1006 if (strfilter__compare(_filter, var)) {
1007 fprintf(stdout, "\t\t%s\n", node->s);
1008 nvars++;
1009 }
1010 }
1011 strlist__delete(vl->vars);
1012 }
1013 if (nvars == 0)
1014 fprintf(stdout, "\t\t(No matched variables)\n");
1015 }
1016 free(vls);
1017end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001018 free(buf);
1019 return ret;
1020}
1021
1022/* Show available variables on given probe point */
1023int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001024 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001025{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001026 int i, ret = 0;
1027 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001028
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001029 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001030 if (ret < 0)
1031 return ret;
1032
Masami Hiramatsu44225522015-05-08 10:03:28 +09001033 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001034 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001035 ret = -ENOENT;
1036 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001037 }
1038
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001039 setup_pager();
1040
Masami Hiramatsuff741782011-06-27 16:27:39 +09001041 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001042 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001043
1044 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001045out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001046 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001047 return ret;
1048}
1049
Ingo Molnar89fe8082013-09-30 12:07:11 +02001050#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001051
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001052static void debuginfo_cache__exit(void)
1053{
1054}
1055
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001056static int
1057find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1058 struct perf_probe_point *pp __maybe_unused,
1059 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001060{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001061 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001062}
1063
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301064static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001065 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001066{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001067 if (perf_probe_event_need_dwarf(pev)) {
1068 pr_warning("Debuginfo-analysis is not supported.\n");
1069 return -ENOSYS;
1070 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301071
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001072 return 0;
1073}
1074
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001075int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001076 const char *module __maybe_unused,
1077 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001078{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001079 pr_warning("Debuginfo-analysis is not supported.\n");
1080 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001081}
1082
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001083int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001084 int npevs __maybe_unused,
1085 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001086{
1087 pr_warning("Debuginfo-analysis is not supported.\n");
1088 return -ENOSYS;
1089}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001090#endif
1091
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001092void line_range__clear(struct line_range *lr)
1093{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001094 free(lr->function);
1095 free(lr->file);
1096 free(lr->path);
1097 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001098 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001099 memset(lr, 0, sizeof(*lr));
1100}
1101
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001102int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001103{
1104 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001105 lr->line_list = intlist__new(NULL);
1106 if (!lr->line_list)
1107 return -ENOMEM;
1108 else
1109 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001110}
1111
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001112static int parse_line_num(char **ptr, int *val, const char *what)
1113{
1114 const char *start = *ptr;
1115
1116 errno = 0;
1117 *val = strtol(*ptr, ptr, 0);
1118 if (errno || *ptr == start) {
1119 semantic_error("'%s' is not a valid number.\n", what);
1120 return -EINVAL;
1121 }
1122 return 0;
1123}
1124
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001125/* Check the name is good for event, group or function */
1126static bool is_c_func_name(const char *name)
1127{
1128 if (!isalpha(*name) && *name != '_')
1129 return false;
1130 while (*++name != '\0') {
1131 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1132 return false;
1133 }
1134 return true;
1135}
1136
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001137/*
1138 * Stuff 'lr' according to the line range described by 'arg'.
1139 * The line range syntax is described by:
1140 *
1141 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001142 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001143 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001144int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001145{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001146 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001147 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001148
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001149 if (!name)
1150 return -ENOMEM;
1151
1152 lr->start = 0;
1153 lr->end = INT_MAX;
1154
1155 range = strchr(name, ':');
1156 if (range) {
1157 *range++ = '\0';
1158
1159 err = parse_line_num(&range, &lr->start, "start line");
1160 if (err)
1161 goto err;
1162
1163 if (*range == '+' || *range == '-') {
1164 const char c = *range++;
1165
1166 err = parse_line_num(&range, &lr->end, "end line");
1167 if (err)
1168 goto err;
1169
1170 if (c == '+') {
1171 lr->end += lr->start;
1172 /*
1173 * Adjust the number of lines here.
1174 * If the number of lines == 1, the
1175 * the end of line should be equal to
1176 * the start of line.
1177 */
1178 lr->end--;
1179 }
1180 }
1181
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001182 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001183
1184 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001185 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001186 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001188 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001190 if (*range != '\0') {
1191 semantic_error("Tailing with invalid str '%s'.\n", range);
1192 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001194 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001195
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001196 file = strchr(name, '@');
1197 if (file) {
1198 *file = '\0';
1199 lr->file = strdup(++file);
1200 if (lr->file == NULL) {
1201 err = -ENOMEM;
1202 goto err;
1203 }
1204 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001205 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001206 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001207 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001208 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001209 else { /* Invalid name */
1210 semantic_error("'%s' is not a valid function name.\n", name);
1211 err = -EINVAL;
1212 goto err;
1213 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001214
1215 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001216err:
1217 free(name);
1218 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001219}
1220
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001221static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1222{
1223 char *ptr;
1224
1225 ptr = strchr(*arg, ':');
1226 if (ptr) {
1227 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001228 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001229 goto ng_name;
1230 pev->group = strdup(*arg);
1231 if (!pev->group)
1232 return -ENOMEM;
1233 *arg = ptr + 1;
1234 } else
1235 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001236 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001237ng_name:
1238 semantic_error("%s is bad for event name -it must "
1239 "follow C symbol-naming rule.\n", *arg);
1240 return -EINVAL;
1241 }
1242 pev->event = strdup(*arg);
1243 if (pev->event == NULL)
1244 return -ENOMEM;
1245
1246 return 0;
1247}
1248
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001249/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001251{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001252 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001253 char *ptr, *tmp;
1254 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301255 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001256 int ret;
1257
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 /*
1259 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001260 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1261 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001262 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001263 */
Wang Nane59d29e2015-04-28 08:46:09 +00001264 if (!arg)
1265 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001267 /*
1268 * If the probe point starts with '%',
1269 * or starts with "sdt_" and has a ':' but no '=',
1270 * then it should be a SDT/cached probe point.
1271 */
1272 if (arg[0] == '%' ||
1273 (!strncmp(arg, "sdt_", 4) &&
1274 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001275 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001276 if (arg[0] == '%')
1277 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001278 }
1279
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001280 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001281 if (pev->sdt) {
1282 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001283 if (*ptr != '@') {
1284 semantic_error("%s must be an SDT name.\n",
1285 arg);
1286 return -EINVAL;
1287 }
1288 /* This must be a target file name or build id */
1289 tmp = build_id_cache__complement(ptr + 1);
1290 if (tmp) {
1291 pev->target = build_id_cache__origname(tmp);
1292 free(tmp);
1293 } else
1294 pev->target = strdup(ptr + 1);
1295 if (!pev->target)
1296 return -ENOMEM;
1297 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001298 }
1299 ret = parse_perf_probe_event_name(&arg, pev);
1300 if (ret == 0) {
1301 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1302 ret = -errno;
1303 }
1304 return ret;
1305 }
1306
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001307 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001308 *ptr = '\0';
1309 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001310 ret = parse_perf_probe_event_name(&arg, pev);
1311 if (ret < 0)
1312 return ret;
1313
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001314 arg = tmp;
1315 }
1316
Naveen N. Rao3099c022015-04-28 17:35:34 +05301317 /*
1318 * Check arg is function or file name and copy it.
1319 *
1320 * We consider arg to be a file spec if and only if it satisfies
1321 * all of the below criteria::
1322 * - it does not include any of "+@%",
1323 * - it includes one of ":;", and
1324 * - it has a period '.' in the name.
1325 *
1326 * Otherwise, we consider arg to be a function specification.
1327 */
1328 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1329 /* This is a file spec if it includes a '.' before ; or : */
1330 if (memchr(arg, '.', ptr - arg))
1331 file_spec = true;
1332 }
1333
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001334 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001335 if (ptr) {
1336 nc = *ptr;
1337 *ptr++ = '\0';
1338 }
1339
Wang Nan6c6e0242015-08-26 10:57:44 +00001340 if (arg[0] == '\0')
1341 tmp = NULL;
1342 else {
1343 tmp = strdup(arg);
1344 if (tmp == NULL)
1345 return -ENOMEM;
1346 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001347
Naveen N. Rao3099c022015-04-28 17:35:34 +05301348 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001349 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001350 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001351 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001352
Wang Nanda15bd92015-08-26 10:57:45 +00001353 /*
1354 * Keep pp->function even if this is absolute address,
1355 * so it can mark whether abs_address is valid.
1356 * Which make 'perf probe lib.bin 0x0' possible.
1357 *
1358 * Note that checking length of tmp is not needed
1359 * because when we access tmp[1] we know tmp[0] is '0',
1360 * so tmp[1] should always valid (but could be '\0').
1361 */
1362 if (tmp && !strncmp(tmp, "0x", 2)) {
1363 pp->abs_address = strtoul(pp->function, &tmp, 0);
1364 if (*tmp != '\0') {
1365 semantic_error("Invalid absolute address.\n");
1366 return -EINVAL;
1367 }
1368 }
1369 }
1370
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001371 /* Parse other options */
1372 while (ptr) {
1373 arg = ptr;
1374 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001375 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001376 pp->lazy_line = strdup(arg);
1377 if (pp->lazy_line == NULL)
1378 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001379 break;
1380 }
1381 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001382 if (ptr) {
1383 nc = *ptr;
1384 *ptr++ = '\0';
1385 }
1386 switch (c) {
1387 case ':': /* Line number */
1388 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001389 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001390 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 " in line number.\n");
1392 return -EINVAL;
1393 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394 break;
1395 case '+': /* Byte offset from a symbol */
1396 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001398 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399 " in offset.\n");
1400 return -EINVAL;
1401 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001402 break;
1403 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 if (pp->file) {
1405 semantic_error("SRC@SRC is not allowed.\n");
1406 return -EINVAL;
1407 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001408 pp->file = strdup(arg);
1409 if (pp->file == NULL)
1410 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001411 break;
1412 case '%': /* Probe places */
1413 if (strcmp(arg, "return") == 0) {
1414 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415 } else { /* Others not supported yet */
1416 semantic_error("%%%s is not supported.\n", arg);
1417 return -ENOTSUP;
1418 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001419 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420 default: /* Buggy case */
1421 pr_err("This program has a bug at %s:%d.\n",
1422 __FILE__, __LINE__);
1423 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001424 break;
1425 }
1426 }
1427
1428 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001430 semantic_error("Lazy pattern can't be used with"
1431 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001432 return -EINVAL;
1433 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001434
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001436 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 return -EINVAL;
1438 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001439
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001441 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001442 return -EINVAL;
1443 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001444
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001446 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001447 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448 return -EINVAL;
1449 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001450
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001452 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001453 return -EINVAL;
1454 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001455
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001457 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 return -EINVAL;
1459 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001460
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001462 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001463 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 return -EINVAL;
1465 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001466
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001467 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001468 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1469 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001471}
1472
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001473/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001475{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001476 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001477 struct perf_probe_arg_field **fieldp;
1478
1479 pr_debug("parsing arg: %s into ", str);
1480
Masami Hiramatsu48481932010-04-12 13:16:53 -04001481 tmp = strchr(str, '=');
1482 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001483 arg->name = strndup(str, tmp - str);
1484 if (arg->name == NULL)
1485 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001486 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001487 str = tmp + 1;
1488 }
1489
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001490 tmp = strchr(str, ':');
1491 if (tmp) { /* Type setting */
1492 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001493 arg->type = strdup(tmp + 1);
1494 if (arg->type == NULL)
1495 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001496 pr_debug("type:%s ", arg->type);
1497 }
1498
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001499 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001500 if (!is_c_varname(str) || !tmp) {
1501 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001502 arg->var = strdup(str);
1503 if (arg->var == NULL)
1504 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001505 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001506 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001507 }
1508
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001509 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001510 arg->var = strndup(str, tmp - str);
1511 if (arg->var == NULL)
1512 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001513 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001514 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001515 fieldp = &arg->field;
1516
1517 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001518 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1519 if (*fieldp == NULL)
1520 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001521 if (*tmp == '[') { /* Array */
1522 str = tmp;
1523 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001524 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001525 if (*tmp != ']' || tmp == str + 1) {
1526 semantic_error("Array index must be a"
1527 " number.\n");
1528 return -EINVAL;
1529 }
1530 tmp++;
1531 if (*tmp == '\0')
1532 tmp = NULL;
1533 } else { /* Structure */
1534 if (*tmp == '.') {
1535 str = tmp + 1;
1536 (*fieldp)->ref = false;
1537 } else if (tmp[1] == '>') {
1538 str = tmp + 2;
1539 (*fieldp)->ref = true;
1540 } else {
1541 semantic_error("Argument parse error: %s\n",
1542 str);
1543 return -EINVAL;
1544 }
1545 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001546 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001547 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001548 (*fieldp)->name = strndup(str, tmp - str);
1549 if ((*fieldp)->name == NULL)
1550 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001551 if (*str != '[')
1552 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001553 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1554 fieldp = &(*fieldp)->next;
1555 }
1556 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001557 (*fieldp)->name = strdup(str);
1558 if ((*fieldp)->name == NULL)
1559 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001560 if (*str != '[')
1561 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001562 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001563
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001564 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001565 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001566 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001567 if (arg->name == NULL)
1568 return -ENOMEM;
1569 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001570 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001571}
1572
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001575{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001576 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001577 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001578
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001579 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001580 if (!argv) {
1581 pr_debug("Failed to split arguments.\n");
1582 return -ENOMEM;
1583 }
1584 if (argc - 1 > MAX_PROBE_ARGS) {
1585 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1586 ret = -ERANGE;
1587 goto out;
1588 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001589 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001590 ret = parse_perf_probe_point(argv[0], pev);
1591 if (ret < 0)
1592 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001593
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001594 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001595 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001596 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1597 if (pev->args == NULL) {
1598 ret = -ENOMEM;
1599 goto out;
1600 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1602 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1603 if (ret >= 0 &&
1604 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001606 " kretprobe.\n");
1607 ret = -EINVAL;
1608 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001609 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001611 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001612
1613 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001614}
1615
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301616/* Returns true if *any* ARG is either C variable, $params or $vars. */
1617bool perf_probe_with_var(struct perf_probe_event *pev)
1618{
1619 int i = 0;
1620
1621 for (i = 0; i < pev->nargs; i++)
1622 if (is_c_varname(pev->args[i].var) ||
1623 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1624 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1625 return true;
1626 return false;
1627}
1628
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001629/* Return true if this perf_probe_event requires debuginfo */
1630bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001631{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1633 return true;
1634
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301635 if (perf_probe_with_var(pev))
1636 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001637
1638 return false;
1639}
1640
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301641/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001642int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001643{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301644 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001645 char pr;
1646 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001647 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001648 int ret, i, argc;
1649 char **argv;
1650
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301651 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 if (!argv) {
1654 pr_debug("Failed to split arguments.\n");
1655 return -ENOMEM;
1656 }
1657 if (argc < 2) {
1658 semantic_error("Too few probe arguments.\n");
1659 ret = -ERANGE;
1660 goto out;
1661 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001662
1663 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001664 argv0_str = strdup(argv[0]);
1665 if (argv0_str == NULL) {
1666 ret = -ENOMEM;
1667 goto out;
1668 }
1669 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1670 fmt2_str = strtok_r(NULL, "/", &fmt);
1671 fmt3_str = strtok_r(NULL, " \t", &fmt);
1672 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1673 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001674 semantic_error("Failed to parse event name: %s\n", argv[0]);
1675 ret = -EINVAL;
1676 goto out;
1677 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001678 pr = fmt1_str[0];
1679 tev->group = strdup(fmt2_str);
1680 tev->event = strdup(fmt3_str);
1681 if (tev->group == NULL || tev->event == NULL) {
1682 ret = -ENOMEM;
1683 goto out;
1684 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001685 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001686
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001687 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001688
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001689 /* Scan module name(if there), function name and offset */
1690 p = strchr(argv[1], ':');
1691 if (p) {
1692 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001693 if (!tp->module) {
1694 ret = -ENOMEM;
1695 goto out;
1696 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001697 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001698 p++;
1699 } else
1700 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001701 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001702 /* only the address started with 0x */
1703 if (fmt1_str[0] == '0') {
1704 /*
1705 * Fix a special case:
1706 * if address == 0, kernel reports something like:
1707 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1708 * Newer kernel may fix that, but we want to
1709 * support old kernel also.
1710 */
1711 if (strcmp(fmt1_str, "0x") == 0) {
1712 if (!argv[2] || strcmp(argv[2], "(null)")) {
1713 ret = -EINVAL;
1714 goto out;
1715 }
1716 tp->address = 0;
1717
1718 free(argv[2]);
1719 for (i = 2; argv[i + 1] != NULL; i++)
1720 argv[i] = argv[i + 1];
1721
1722 argv[i] = NULL;
1723 argc -= 1;
1724 } else
1725 tp->address = strtoul(fmt1_str, NULL, 0);
1726 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001727 /* Only the symbol-based probe has offset */
1728 tp->symbol = strdup(fmt1_str);
1729 if (tp->symbol == NULL) {
1730 ret = -ENOMEM;
1731 goto out;
1732 }
1733 fmt2_str = strtok_r(NULL, "", &fmt);
1734 if (fmt2_str == NULL)
1735 tp->offset = 0;
1736 else
1737 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001738 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001739
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001740 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301741 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001742 if (tev->args == NULL) {
1743 ret = -ENOMEM;
1744 goto out;
1745 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001746 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001747 p = strchr(argv[i + 2], '=');
1748 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001749 *p++ = '\0';
1750 else
1751 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001752 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001753 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001754 tev->args[i].value = strdup(p);
1755 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1756 ret = -ENOMEM;
1757 goto out;
1758 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001759 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001760 ret = 0;
1761out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001762 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001763 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001764 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001765}
1766
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001767/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001768char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001769{
1770 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001771 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001772 char *ret = NULL;
1773 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001774
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001775 if (strbuf_init(&buf, 64) < 0)
1776 return NULL;
1777
Masami Hiramatsu48481932010-04-12 13:16:53 -04001778 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001779 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001780 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001781 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1782 if (err)
1783 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001784
1785 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001786 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001787 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001788 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001789 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1790 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001791 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001792 if (err)
1793 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001794 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001795
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001796 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001797 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1798 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001799
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001800 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001801out:
1802 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001803 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001804}
1805
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001807char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001808{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001809 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001810 char *tmp, *ret = NULL;
1811 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001812
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001813 if (strbuf_init(&buf, 64) < 0)
1814 return NULL;
1815
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001816 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001817 if (strbuf_addstr(&buf, pp->function) < 0)
1818 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001819 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001820 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001821 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001822 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001823 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001824 err = strbuf_addstr(&buf, "%return");
1825 if (err)
1826 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001827 }
1828 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001829 tmp = pp->file;
1830 len = strlen(tmp);
1831 if (len > 30) {
1832 tmp = strchr(pp->file + len - 30, '/');
1833 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1834 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001835 err = strbuf_addf(&buf, "@%s", tmp);
1836 if (!err && !pp->function && pp->line)
1837 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001838 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001839 if (!err)
1840 ret = strbuf_detach(&buf, NULL);
1841out:
1842 strbuf_release(&buf);
1843 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001844}
1845
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001846char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1847{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001848 struct strbuf buf;
1849 char *tmp, *ret = NULL;
1850 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001851
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001852 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001853 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001854 if (pev->event)
1855 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1856 pev->event) < 0)
1857 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001858
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001859 tmp = synthesize_perf_probe_point(&pev->point);
1860 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1861 goto out;
1862 free(tmp);
1863
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001864 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001865 tmp = synthesize_perf_probe_arg(pev->args + i);
1866 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1867 goto out;
1868 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001869 }
1870
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001871 ret = strbuf_detach(&buf, NULL);
1872out:
1873 strbuf_release(&buf);
1874 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001875}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301877static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001878 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001880 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301882 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001883 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001885 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001886 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001887 err = strbuf_addf(buf, "%+ld(", ref->offset);
1888 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889}
1890
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301891static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001892 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301894 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001895 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001896
1897 /* Argument name or separator */
1898 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001899 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001901 err = strbuf_addch(buf, ' ');
1902 if (err)
1903 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001905 /* Special case: @XXX */
1906 if (arg->value[0] == '@' && arg->ref)
1907 ref = ref->next;
1908
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001909 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001910 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001911 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912 if (depth < 0)
1913 return depth;
1914 }
1915
1916 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001917 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001918 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001919 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001920 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001921
1922 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001923 while (!err && depth--)
1924 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001925
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001926 /* Print argument type */
1927 if (!err && arg->type)
1928 err = strbuf_addf(buf, ":%s", arg->type);
1929
1930 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001931}
1932
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301933char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001934{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301935 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001936 struct strbuf buf;
1937 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001938 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001939
Wang Nanda15bd92015-08-26 10:57:45 +00001940 /* Uprobes must have tp->module */
1941 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001942 return NULL;
1943
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001944 if (strbuf_init(&buf, 32) < 0)
1945 return NULL;
1946
1947 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1948 tev->group, tev->event) < 0)
1949 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001950 /*
1951 * If tp->address == 0, then this point must be a
1952 * absolute address uprobe.
1953 * try_to_find_absolute_address() should have made
1954 * tp->symbol to "0x0".
1955 */
1956 if (tev->uprobes && !tp->address) {
1957 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1958 goto error;
1959 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001960
1961 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301962 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001963 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001964 else if (!strncmp(tp->symbol, "0x", 2))
1965 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001966 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1967 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301968 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001969 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1970 tp->module ? ":" : "", tp->symbol, tp->offset);
1971 if (err)
1972 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301973
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001974 for (i = 0; i < tev->nargs; i++)
1975 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001976 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001977
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001978 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001979error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001980 strbuf_release(&buf);
1981 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001982}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001983
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001984static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1985 struct perf_probe_point *pp,
1986 bool is_kprobe)
1987{
1988 struct symbol *sym = NULL;
1989 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001990 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001991 int ret = -ENOENT;
1992
1993 if (!is_kprobe) {
1994 map = dso__new_map(tp->module);
1995 if (!map)
1996 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001997 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001998 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001999 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002000 if (kernel_get_symbol_address_by_name(tp->symbol,
2001 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002002 goto out;
2003 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002004 if (addr) {
2005 addr += tp->offset;
2006 sym = __find_kernel_function(addr, &map);
2007 }
2008 }
Wang Nan98d3b252015-11-05 13:19:25 +00002009
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002010 if (!sym)
2011 goto out;
2012
2013 pp->retprobe = tp->retprobe;
2014 pp->offset = addr - map->unmap_ip(map, sym->start);
2015 pp->function = strdup(sym->name);
2016 ret = pp->function ? 0 : -ENOMEM;
2017
2018out:
2019 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002020 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002021 }
2022
2023 return ret;
2024}
2025
2026static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002027 struct perf_probe_point *pp,
2028 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002029{
2030 char buf[128];
2031 int ret;
2032
2033 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2034 if (!ret)
2035 return 0;
2036 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2037 if (!ret)
2038 return 0;
2039
2040 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2041
2042 if (tp->symbol) {
2043 pp->function = strdup(tp->symbol);
2044 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002045 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002046 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2047 if (ret < 0)
2048 return ret;
2049 pp->function = strdup(buf);
2050 pp->offset = 0;
2051 }
2052 if (pp->function == NULL)
2053 return -ENOMEM;
2054
2055 pp->retprobe = tp->retprobe;
2056
2057 return 0;
2058}
2059
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302060static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302061 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002062{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002063 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002064 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002065
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002066 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002067 pev->event = strdup(tev->event);
2068 pev->group = strdup(tev->group);
2069 if (pev->event == NULL || pev->group == NULL)
2070 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002071
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002072 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002073 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002074 if (ret < 0)
2075 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002076
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002077 /* Convert trace_arg to probe_arg */
2078 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002079 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2080 if (pev->args == NULL)
2081 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002082 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002083 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002084 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002085 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002086 if ((ret = strbuf_init(&buf, 32)) < 0)
2087 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002088 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2089 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002090 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002091 if (pev->args[i].name == NULL && ret >= 0)
2092 ret = -ENOMEM;
2093 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002094error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002095 if (ret < 0)
2096 clear_perf_probe_event(pev);
2097
2098 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002099}
2100
2101void clear_perf_probe_event(struct perf_probe_event *pev)
2102{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002103 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002104 int i;
2105
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002106 free(pev->event);
2107 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002108 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002109 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002110
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002111 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002112 free(pev->args[i].name);
2113 free(pev->args[i].var);
2114 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002115 field = pev->args[i].field;
2116 while (field) {
2117 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002118 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002119 free(field);
2120 field = next;
2121 }
2122 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002123 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002124 memset(pev, 0, sizeof(*pev));
2125}
2126
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002127#define strdup_or_goto(str, label) \
2128({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2129
2130static int perf_probe_point__copy(struct perf_probe_point *dst,
2131 struct perf_probe_point *src)
2132{
2133 dst->file = strdup_or_goto(src->file, out_err);
2134 dst->function = strdup_or_goto(src->function, out_err);
2135 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2136 dst->line = src->line;
2137 dst->retprobe = src->retprobe;
2138 dst->offset = src->offset;
2139 return 0;
2140
2141out_err:
2142 clear_perf_probe_point(dst);
2143 return -ENOMEM;
2144}
2145
2146static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2147 struct perf_probe_arg *src)
2148{
2149 struct perf_probe_arg_field *field, **ppfield;
2150
2151 dst->name = strdup_or_goto(src->name, out_err);
2152 dst->var = strdup_or_goto(src->var, out_err);
2153 dst->type = strdup_or_goto(src->type, out_err);
2154
2155 field = src->field;
2156 ppfield = &(dst->field);
2157 while (field) {
2158 *ppfield = zalloc(sizeof(*field));
2159 if (!*ppfield)
2160 goto out_err;
2161 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2162 (*ppfield)->index = field->index;
2163 (*ppfield)->ref = field->ref;
2164 field = field->next;
2165 ppfield = &((*ppfield)->next);
2166 }
2167 return 0;
2168out_err:
2169 return -ENOMEM;
2170}
2171
2172int perf_probe_event__copy(struct perf_probe_event *dst,
2173 struct perf_probe_event *src)
2174{
2175 int i;
2176
2177 dst->event = strdup_or_goto(src->event, out_err);
2178 dst->group = strdup_or_goto(src->group, out_err);
2179 dst->target = strdup_or_goto(src->target, out_err);
2180 dst->uprobes = src->uprobes;
2181
2182 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2183 goto out_err;
2184
2185 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2186 if (!dst->args)
2187 goto out_err;
2188 dst->nargs = src->nargs;
2189
2190 for (i = 0; i < src->nargs; i++)
2191 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2192 goto out_err;
2193 return 0;
2194
2195out_err:
2196 clear_perf_probe_event(dst);
2197 return -ENOMEM;
2198}
2199
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002200void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002201{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302202 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002203 int i;
2204
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002205 free(tev->event);
2206 free(tev->group);
2207 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002208 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002209 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002210 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002211 free(tev->args[i].name);
2212 free(tev->args[i].value);
2213 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002214 ref = tev->args[i].ref;
2215 while (ref) {
2216 next = ref->next;
2217 free(ref);
2218 ref = next;
2219 }
2220 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002221 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002222 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002223}
2224
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002225struct kprobe_blacklist_node {
2226 struct list_head list;
2227 unsigned long start;
2228 unsigned long end;
2229 char *symbol;
2230};
2231
2232static void kprobe_blacklist__delete(struct list_head *blacklist)
2233{
2234 struct kprobe_blacklist_node *node;
2235
2236 while (!list_empty(blacklist)) {
2237 node = list_first_entry(blacklist,
2238 struct kprobe_blacklist_node, list);
2239 list_del(&node->list);
2240 free(node->symbol);
2241 free(node);
2242 }
2243}
2244
2245static int kprobe_blacklist__load(struct list_head *blacklist)
2246{
2247 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002248 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002249 char buf[PATH_MAX], *p;
2250 FILE *fp;
2251 int ret;
2252
2253 if (__debugfs == NULL)
2254 return -ENOTSUP;
2255
2256 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2257 if (ret < 0)
2258 return ret;
2259
2260 fp = fopen(buf, "r");
2261 if (!fp)
2262 return -errno;
2263
2264 ret = 0;
2265 while (fgets(buf, PATH_MAX, fp)) {
2266 node = zalloc(sizeof(*node));
2267 if (!node) {
2268 ret = -ENOMEM;
2269 break;
2270 }
2271 INIT_LIST_HEAD(&node->list);
2272 list_add_tail(&node->list, blacklist);
2273 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2274 ret = -EINVAL;
2275 break;
2276 }
2277 p = strchr(buf, '\t');
2278 if (p) {
2279 p++;
2280 if (p[strlen(p) - 1] == '\n')
2281 p[strlen(p) - 1] = '\0';
2282 } else
2283 p = (char *)"unknown";
2284 node->symbol = strdup(p);
2285 if (!node->symbol) {
2286 ret = -ENOMEM;
2287 break;
2288 }
2289 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2290 node->start, node->end, node->symbol);
2291 ret++;
2292 }
2293 if (ret < 0)
2294 kprobe_blacklist__delete(blacklist);
2295 fclose(fp);
2296
2297 return ret;
2298}
2299
2300static struct kprobe_blacklist_node *
2301kprobe_blacklist__find_by_address(struct list_head *blacklist,
2302 unsigned long address)
2303{
2304 struct kprobe_blacklist_node *node;
2305
2306 list_for_each_entry(node, blacklist, list) {
2307 if (node->start <= address && address <= node->end)
2308 return node;
2309 }
2310
2311 return NULL;
2312}
2313
Masami Hiramatsub0312202015-06-16 20:50:55 +09002314static LIST_HEAD(kprobe_blacklist);
2315
2316static void kprobe_blacklist__init(void)
2317{
2318 if (!list_empty(&kprobe_blacklist))
2319 return;
2320
2321 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2322 pr_debug("No kprobe blacklist support, ignored\n");
2323}
2324
2325static void kprobe_blacklist__release(void)
2326{
2327 kprobe_blacklist__delete(&kprobe_blacklist);
2328}
2329
2330static bool kprobe_blacklist__listed(unsigned long address)
2331{
2332 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2333}
2334
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002335static int perf_probe_event__sprintf(const char *group, const char *event,
2336 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002337 const char *module,
2338 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002339{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002340 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002341 char *buf;
2342
2343 if (asprintf(&buf, "%s:%s", group, event) < 0)
2344 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002345 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002346 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002347 if (ret)
2348 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002349
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002350 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002351 buf = synthesize_perf_probe_point(&pev->point);
2352 if (!buf)
2353 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002354 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002355 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002356
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002357 if (!ret && module)
2358 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002359
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002360 if (!ret && pev->nargs > 0) {
2361 ret = strbuf_add(result, " with", 5);
2362 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002363 buf = synthesize_perf_probe_arg(&pev->args[i]);
2364 if (!buf)
2365 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002366 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002367 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002368 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002369 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002370 if (!ret)
2371 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002372
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002373 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002374}
2375
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002376/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002377int show_perf_probe_event(const char *group, const char *event,
2378 struct perf_probe_event *pev,
2379 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002380{
2381 struct strbuf buf = STRBUF_INIT;
2382 int ret;
2383
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002384 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002385 if (ret >= 0) {
2386 if (use_stdout)
2387 printf("%s\n", buf.buf);
2388 else
2389 pr_info("%s\n", buf.buf);
2390 }
2391 strbuf_release(&buf);
2392
2393 return ret;
2394}
2395
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002396static bool filter_probe_trace_event(struct probe_trace_event *tev,
2397 struct strfilter *filter)
2398{
2399 char tmp[128];
2400
2401 /* At first, check the event name itself */
2402 if (strfilter__compare(filter, tev->event))
2403 return true;
2404
2405 /* Next, check the combination of name and group */
2406 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2407 return false;
2408 return strfilter__compare(filter, tmp);
2409}
2410
2411static int __show_perf_probe_events(int fd, bool is_kprobe,
2412 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002413{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302414 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302415 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002416 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002417 struct strlist *rawlist;
2418 struct str_node *ent;
2419
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002420 memset(&tev, 0, sizeof(tev));
2421 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002422
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002423 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002424 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002425 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002426
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002427 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302428 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002429 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002430 if (!filter_probe_trace_event(&tev, filter))
2431 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302432 ret = convert_to_perf_probe_event(&tev, &pev,
2433 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002434 if (ret < 0)
2435 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002436 ret = show_perf_probe_event(pev.group, pev.event,
2437 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002438 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002439 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002440next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002441 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302442 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 if (ret < 0)
2444 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002445 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002446 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002447 /* Cleanup cached debuginfo if needed */
2448 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002449
2450 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002451}
2452
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302453/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002454int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302455{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002456 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302457
2458 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302459
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002460 if (probe_conf.cache)
2461 return probe_cache__show_all_caches(filter);
2462
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002463 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302464 if (ret < 0)
2465 return ret;
2466
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002467 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2468 if (ret < 0)
2469 return ret;
2470
2471 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002472 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002473 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002474 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002475 if (kp_fd > 0)
2476 close(kp_fd);
2477 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002478 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002479 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302480
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002482}
2483
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002484static int get_new_event_name(char *buf, size_t len, const char *base,
2485 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002486{
2487 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002488 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002489
Naveen N. Rao3099c022015-04-28 17:35:34 +05302490 if (*base == '.')
2491 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002492 nbase = strdup(base);
2493 if (!nbase)
2494 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302495
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002496 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2497 p = strchr(nbase, '.');
2498 if (p && p != nbase)
2499 *p = '\0';
2500
2501 /* Try no suffix number */
2502 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002503 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002504 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002505 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002506 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002507 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002508 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002509
Masami Hiramatsud761b082009-12-15 10:32:25 -05002510 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002511 pr_warning("Error: event \"%s\" already exists.\n"
2512 " Hint: Remove existing event by 'perf probe -d'\n"
2513 " or force duplicates by 'perf probe -f'\n"
2514 " or set 'force=yes' in BPF source.\n",
2515 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002516 ret = -EEXIST;
2517 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002518 }
2519
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002520 /* Try to add suffix */
2521 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002522 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002523 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002524 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002525 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002526 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002527 if (!strlist__has_entry(namelist, buf))
2528 break;
2529 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002530 if (i == MAX_EVENT_INDEX) {
2531 pr_warning("Too many events are on the same function.\n");
2532 ret = -ERANGE;
2533 }
2534
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002535out:
2536 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002537 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002538}
2539
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002540/* Warn if the current kernel's uprobe implementation is old */
2541static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2542{
2543 int i;
2544 char *buf = synthesize_probe_trace_command(tev);
2545
2546 /* Old uprobe event doesn't support memory dereference */
2547 if (!tev->uprobes || tev->nargs == 0 || !buf)
2548 goto out;
2549
2550 for (i = 0; i < tev->nargs; i++)
2551 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2552 pr_warning("Please upgrade your kernel to at least "
2553 "3.14 to have access to feature %s\n",
2554 tev->args[i].value);
2555 break;
2556 }
2557out:
2558 free(buf);
2559}
2560
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002561/* Set new name from original perf_probe_event and namelist */
2562static int probe_trace_event__set_name(struct probe_trace_event *tev,
2563 struct perf_probe_event *pev,
2564 struct strlist *namelist,
2565 bool allow_suffix)
2566{
2567 const char *event, *group;
2568 char buf[64];
2569 int ret;
2570
Masami Hiramatsubc062232016-07-01 17:03:12 +09002571 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002572 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002573 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002574 else if (tev->event)
2575 event = tev->event;
2576 else {
2577 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002578 if (pev->point.function &&
2579 (strncmp(pev->point.function, "0x", 2) != 0) &&
2580 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002581 event = pev->point.function;
2582 else
2583 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002584 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002585 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002586 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002587 else if (tev->group)
2588 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002589 else
2590 group = PERFPROBE_GROUP;
2591
2592 /* Get an unused new event name */
2593 ret = get_new_event_name(buf, 64, event,
2594 namelist, allow_suffix);
2595 if (ret < 0)
2596 return ret;
2597
2598 event = buf;
2599
2600 tev->event = strdup(event);
2601 tev->group = strdup(group);
2602 if (tev->event == NULL || tev->group == NULL)
2603 return -ENOMEM;
2604
2605 /* Add added event name to namelist */
2606 strlist__add(namelist, event);
2607 return 0;
2608}
2609
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002610static int __open_probe_file_and_namelist(bool uprobe,
2611 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002612{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002613 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002614
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002615 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002616 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002617 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002618
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002619 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002620 *namelist = probe_file__get_namelist(fd);
2621 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002622 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002623 close(fd);
2624 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002625 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002626 return fd;
2627}
2628
2629static int __add_probe_trace_events(struct perf_probe_event *pev,
2630 struct probe_trace_event *tevs,
2631 int ntevs, bool allow_suffix)
2632{
2633 int i, fd[2] = {-1, -1}, up, ret;
2634 struct probe_trace_event *tev = NULL;
2635 struct probe_cache *cache = NULL;
2636 struct strlist *namelist[2] = {NULL, NULL};
2637
2638 up = pev->uprobes ? 1 : 0;
2639 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2640 if (fd[up] < 0)
2641 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002642
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002643 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002644 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002645 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002646 up = tev->uprobes ? 1 : 0;
2647 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2648 fd[up] = __open_probe_file_and_namelist(up,
2649 &namelist[up]);
2650 if (fd[up] < 0)
2651 goto close_out;
2652 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002653 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002654 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002655 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002656
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002657 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002658 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002659 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002660 if (ret < 0)
2661 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002662
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002663 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002664 if (ret < 0)
2665 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002666
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002667 /*
2668 * Probes after the first probe which comes from same
2669 * user input are always allowed to add suffix, because
2670 * there might be several addresses corresponding to
2671 * one code line.
2672 */
2673 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002674 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002675 if (ret == -EINVAL && pev->uprobes)
2676 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002677 if (ret == 0 && probe_conf.cache) {
2678 cache = probe_cache__new(pev->target);
2679 if (!cache ||
2680 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2681 probe_cache__commit(cache) < 0)
2682 pr_warning("Failed to add event to probe cache\n");
2683 probe_cache__delete(cache);
2684 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002685
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002686close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002687 for (up = 0; up < 2; up++) {
2688 strlist__delete(namelist[up]);
2689 if (fd[up] >= 0)
2690 close(fd[up]);
2691 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002692 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002693}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002694
Wang Nan6bb536c2015-05-29 09:45:47 +00002695static int find_probe_functions(struct map *map, char *name,
2696 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002697{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002698 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002699 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002700 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002701
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002702 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002703 return 0;
2704
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002705 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002706 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002707 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002708 if (syms && found < probe_conf.max_probes)
2709 syms[found - 1] = sym;
2710 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002711 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002712
2713 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002714}
2715
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302716void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2717 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302718 struct map *map __maybe_unused,
2719 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302720
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002721/*
2722 * Find probe function addresses from map.
2723 * Return an error or the number of found probe_trace_event
2724 */
2725static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002726 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002727{
2728 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002729 struct ref_reloc_sym *reloc_sym = NULL;
2730 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002731 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002732 struct probe_trace_event *tev;
2733 struct perf_probe_point *pp = &pev->point;
2734 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002735 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002736 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302737 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002738
Masami Hiramatsu44225522015-05-08 10:03:28 +09002739 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002740 if (!map) {
2741 ret = -EINVAL;
2742 goto out;
2743 }
2744
Wang Nan6bb536c2015-05-29 09:45:47 +00002745 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2746 if (!syms) {
2747 ret = -ENOMEM;
2748 goto out;
2749 }
2750
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002751 /*
2752 * Load matched symbols: Since the different local symbols may have
2753 * same name but different addresses, this lists all the symbols.
2754 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002755 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002756 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002757 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002758 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002759 ret = -ENOENT;
2760 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002761 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002762 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002763 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002764 ret = -E2BIG;
2765 goto out;
2766 }
2767
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002768 /* Note that the symbols in the kmodule are not relocated */
2769 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002770 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002771 if (!reloc_sym) {
2772 pr_warning("Relocated base symbol is not found!\n");
2773 ret = -EINVAL;
2774 goto out;
2775 }
2776 }
2777
2778 /* Setup result trace-probe-events */
2779 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2780 if (!*tevs) {
2781 ret = -ENOMEM;
2782 goto out;
2783 }
2784
2785 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002786
Wang Nan6bb536c2015-05-29 09:45:47 +00002787 for (j = 0; j < num_matched_functions; j++) {
2788 sym = syms[j];
2789
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002790 tev = (*tevs) + ret;
2791 tp = &tev->point;
2792 if (ret == num_matched_functions) {
2793 pr_warning("Too many symbols are listed. Skip it.\n");
2794 break;
2795 }
2796 ret++;
2797
2798 if (pp->offset > sym->end - sym->start) {
2799 pr_warning("Offset %ld is bigger than the size of %s\n",
2800 pp->offset, sym->name);
2801 ret = -ENOENT;
2802 goto err_out;
2803 }
2804 /* Add one probe point */
2805 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002806
2807 /* Check the kprobe (not in module) is within .text */
2808 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002809 kprobe_warn_out_range(sym->name, tp->address)) {
2810 tp->symbol = NULL; /* Skip it */
2811 skipped++;
2812 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002813 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2814 tp->offset = tp->address - reloc_sym->addr;
2815 } else {
2816 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2817 tp->offset = pp->offset;
2818 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002819 tp->realname = strdup_or_goto(sym->name, nomem_out);
2820
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002821 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302822 if (pev->target) {
2823 if (pev->uprobes) {
2824 tev->point.module = strdup_or_goto(pev->target,
2825 nomem_out);
2826 } else {
2827 mod_name = find_module_name(pev->target);
2828 tev->point.module =
2829 strdup(mod_name ? mod_name : pev->target);
2830 free(mod_name);
2831 if (!tev->point.module)
2832 goto nomem_out;
2833 }
2834 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002835 tev->uprobes = pev->uprobes;
2836 tev->nargs = pev->nargs;
2837 if (tev->nargs) {
2838 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2839 tev->nargs);
2840 if (tev->args == NULL)
2841 goto nomem_out;
2842 }
2843 for (i = 0; i < tev->nargs; i++) {
2844 if (pev->args[i].name)
2845 tev->args[i].name =
2846 strdup_or_goto(pev->args[i].name,
2847 nomem_out);
2848
2849 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2850 nomem_out);
2851 if (pev->args[i].type)
2852 tev->args[i].type =
2853 strdup_or_goto(pev->args[i].type,
2854 nomem_out);
2855 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302856 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002857 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002858 if (ret == skipped) {
2859 ret = -ENOENT;
2860 goto err_out;
2861 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002862
2863out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09002864 map__put(map);
Wang Nan6bb536c2015-05-29 09:45:47 +00002865 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002866 return ret;
2867
2868nomem_out:
2869 ret = -ENOMEM;
2870err_out:
2871 clear_probe_trace_events(*tevs, num_matched_functions);
2872 zfree(tevs);
2873 goto out;
2874}
2875
Wang Nanda15bd92015-08-26 10:57:45 +00002876static int try_to_find_absolute_address(struct perf_probe_event *pev,
2877 struct probe_trace_event **tevs)
2878{
2879 struct perf_probe_point *pp = &pev->point;
2880 struct probe_trace_event *tev;
2881 struct probe_trace_point *tp;
2882 int i, err;
2883
2884 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2885 return -EINVAL;
2886 if (perf_probe_event_need_dwarf(pev))
2887 return -EINVAL;
2888
2889 /*
2890 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2891 * absolute address.
2892 *
2893 * Only one tev can be generated by this.
2894 */
2895 *tevs = zalloc(sizeof(*tev));
2896 if (!*tevs)
2897 return -ENOMEM;
2898
2899 tev = *tevs;
2900 tp = &tev->point;
2901
2902 /*
2903 * Don't use tp->offset, use address directly, because
2904 * in synthesize_probe_trace_command() address cannot be
2905 * zero.
2906 */
2907 tp->address = pev->point.abs_address;
2908 tp->retprobe = pp->retprobe;
2909 tev->uprobes = pev->uprobes;
2910
2911 err = -ENOMEM;
2912 /*
2913 * Give it a '0x' leading symbol name.
2914 * In __add_probe_trace_events, a NULL symbol is interpreted as
2915 * invalud.
2916 */
2917 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2918 goto errout;
2919
2920 /* For kprobe, check range */
2921 if ((!tev->uprobes) &&
2922 (kprobe_warn_out_range(tev->point.symbol,
2923 tev->point.address))) {
2924 err = -EACCES;
2925 goto errout;
2926 }
2927
2928 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2929 goto errout;
2930
2931 if (pev->target) {
2932 tp->module = strdup(pev->target);
2933 if (!tp->module)
2934 goto errout;
2935 }
2936
2937 if (tev->group) {
2938 tev->group = strdup(pev->group);
2939 if (!tev->group)
2940 goto errout;
2941 }
2942
2943 if (pev->event) {
2944 tev->event = strdup(pev->event);
2945 if (!tev->event)
2946 goto errout;
2947 }
2948
2949 tev->nargs = pev->nargs;
2950 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2951 if (!tev->args) {
2952 err = -ENOMEM;
2953 goto errout;
2954 }
2955 for (i = 0; i < tev->nargs; i++)
2956 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2957
2958 return 1;
2959
2960errout:
2961 if (*tevs) {
2962 clear_probe_trace_events(*tevs, 1);
2963 *tevs = NULL;
2964 }
2965 return err;
2966}
2967
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002968/* Concatinate two arrays */
2969static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2970{
2971 void *ret;
2972
2973 ret = malloc(sz_a + sz_b);
2974 if (ret) {
2975 memcpy(ret, a, sz_a);
2976 memcpy(ret + sz_a, b, sz_b);
2977 }
2978 return ret;
2979}
2980
2981static int
2982concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2983 struct probe_trace_event **tevs2, int ntevs2)
2984{
2985 struct probe_trace_event *new_tevs;
2986 int ret = 0;
2987
2988 if (ntevs == 0) {
2989 *tevs = *tevs2;
2990 *ntevs = ntevs2;
2991 *tevs2 = NULL;
2992 return 0;
2993 }
2994
2995 if (*ntevs + ntevs2 > probe_conf.max_probes)
2996 ret = -E2BIG;
2997 else {
2998 /* Concatinate the array of probe_trace_event */
2999 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3000 *tevs2, ntevs2 * sizeof(**tevs2));
3001 if (!new_tevs)
3002 ret = -ENOMEM;
3003 else {
3004 free(*tevs);
3005 *tevs = new_tevs;
3006 *ntevs += ntevs2;
3007 }
3008 }
3009 if (ret < 0)
3010 clear_probe_trace_events(*tevs2, ntevs2);
3011 zfree(tevs2);
3012
3013 return ret;
3014}
3015
3016/*
3017 * Try to find probe_trace_event from given probe caches. Return the number
3018 * of cached events found, if an error occurs return the error.
3019 */
3020static int find_cached_events(struct perf_probe_event *pev,
3021 struct probe_trace_event **tevs,
3022 const char *target)
3023{
3024 struct probe_cache *cache;
3025 struct probe_cache_entry *entry;
3026 struct probe_trace_event *tmp_tevs = NULL;
3027 int ntevs = 0;
3028 int ret = 0;
3029
3030 cache = probe_cache__new(target);
3031 /* Return 0 ("not found") if the target has no probe cache. */
3032 if (!cache)
3033 return 0;
3034
3035 for_each_probe_cache_entry(entry, cache) {
3036 /* Skip the cache entry which has no name */
3037 if (!entry->pev.event || !entry->pev.group)
3038 continue;
3039 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3040 strglobmatch(entry->pev.event, pev->event)) {
3041 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3042 if (ret > 0)
3043 ret = concat_probe_trace_events(tevs, &ntevs,
3044 &tmp_tevs, ret);
3045 if (ret < 0)
3046 break;
3047 }
3048 }
3049 probe_cache__delete(cache);
3050 if (ret < 0) {
3051 clear_probe_trace_events(*tevs, ntevs);
3052 zfree(tevs);
3053 } else {
3054 ret = ntevs;
3055 if (ntevs > 0 && target && target[0] == '/')
3056 pev->uprobes = true;
3057 }
3058
3059 return ret;
3060}
3061
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003062/* Try to find probe_trace_event from all probe caches */
3063static int find_cached_events_all(struct perf_probe_event *pev,
3064 struct probe_trace_event **tevs)
3065{
3066 struct probe_trace_event *tmp_tevs = NULL;
3067 struct strlist *bidlist;
3068 struct str_node *nd;
3069 char *pathname;
3070 int ntevs = 0;
3071 int ret;
3072
3073 /* Get the buildid list of all valid caches */
3074 bidlist = build_id_cache__list_all(true);
3075 if (!bidlist) {
3076 ret = -errno;
3077 pr_debug("Failed to get buildids: %d\n", ret);
3078 return ret;
3079 }
3080
3081 ret = 0;
3082 strlist__for_each_entry(nd, bidlist) {
3083 pathname = build_id_cache__origname(nd->s);
3084 ret = find_cached_events(pev, &tmp_tevs, pathname);
3085 /* In the case of cnt == 0, we just skip it */
3086 if (ret > 0)
3087 ret = concat_probe_trace_events(tevs, &ntevs,
3088 &tmp_tevs, ret);
3089 free(pathname);
3090 if (ret < 0)
3091 break;
3092 }
3093 strlist__delete(bidlist);
3094
3095 if (ret < 0) {
3096 clear_probe_trace_events(*tevs, ntevs);
3097 zfree(tevs);
3098 } else
3099 ret = ntevs;
3100
3101 return ret;
3102}
3103
Masami Hiramatsubc062232016-07-01 17:03:12 +09003104static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3105 struct probe_trace_event **tevs)
3106{
3107 struct probe_cache *cache;
3108 struct probe_cache_entry *entry;
3109 struct probe_trace_event *tev;
3110 struct str_node *node;
3111 int ret, i;
3112
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003113 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003114 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003115 if (!pev->target)
3116 return find_cached_events_all(pev, tevs);
3117 else
3118 return find_cached_events(pev, tevs, pev->target);
3119 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003120 cache = probe_cache__new(pev->target);
3121 if (!cache)
3122 return 0;
3123
3124 entry = probe_cache__find(cache, pev);
3125 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003126 /* SDT must be in the cache */
3127 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003128 goto out;
3129 }
3130
3131 ret = strlist__nr_entries(entry->tevlist);
3132 if (ret > probe_conf.max_probes) {
3133 pr_debug("Too many entries matched in the cache of %s\n",
3134 pev->target ? : "kernel");
3135 ret = -E2BIG;
3136 goto out;
3137 }
3138
3139 *tevs = zalloc(ret * sizeof(*tev));
3140 if (!*tevs) {
3141 ret = -ENOMEM;
3142 goto out;
3143 }
3144
3145 i = 0;
3146 strlist__for_each_entry(node, entry->tevlist) {
3147 tev = &(*tevs)[i++];
3148 ret = parse_probe_trace_command(node->s, tev);
3149 if (ret < 0)
3150 goto out;
3151 /* Set the uprobes attribute as same as original */
3152 tev->uprobes = pev->uprobes;
3153 }
3154 ret = i;
3155
3156out:
3157 probe_cache__delete(cache);
3158 return ret;
3159}
3160
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303161static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003162 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003163{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003164 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003165
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003166 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003167 /* Set group name if not given */
3168 if (!pev->uprobes) {
3169 pev->group = strdup(PERFPROBE_GROUP);
3170 ret = pev->group ? 0 : -ENOMEM;
3171 } else
3172 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003173 if (ret != 0) {
3174 pr_warning("Failed to make a group name.\n");
3175 return ret;
3176 }
3177 }
3178
Wang Nanda15bd92015-08-26 10:57:45 +00003179 ret = try_to_find_absolute_address(pev, tevs);
3180 if (ret > 0)
3181 return ret;
3182
Masami Hiramatsubc062232016-07-01 17:03:12 +09003183 /* At first, we need to lookup cache entry */
3184 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003185 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3186 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003187
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003188 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003189 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003190 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003191 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003192
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003193 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003194}
3195
Wang Nan12fae5e2015-09-04 21:16:00 +09003196int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003197{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003198 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003199
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003200 /* Loop 1: convert all events */
3201 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003202 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003203 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003204 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003205 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003206 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003207 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003208 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003209 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003210 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003211 /* This just release blacklist only if allocated */
3212 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003213
Namhyung Kim844dffa2015-09-04 21:15:59 +09003214 return 0;
3215}
3216
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003217static int show_probe_trace_event(struct probe_trace_event *tev)
3218{
3219 char *buf = synthesize_probe_trace_command(tev);
3220
3221 if (!buf) {
3222 pr_debug("Failed to synthesize probe trace event.\n");
3223 return -EINVAL;
3224 }
3225
3226 /* Showing definition always go stdout */
3227 printf("%s\n", buf);
3228 free(buf);
3229
3230 return 0;
3231}
3232
3233int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3234{
3235 struct strlist *namelist = strlist__new(NULL, NULL);
3236 struct probe_trace_event *tev;
3237 struct perf_probe_event *pev;
3238 int i, j, ret = 0;
3239
3240 if (!namelist)
3241 return -ENOMEM;
3242
3243 for (j = 0; j < npevs && !ret; j++) {
3244 pev = &pevs[j];
3245 for (i = 0; i < pev->ntevs && !ret; i++) {
3246 tev = &pev->tevs[i];
3247 /* Skip if the symbol is out of .text or blacklisted */
3248 if (!tev->point.symbol && !pev->uprobes)
3249 continue;
3250
3251 /* Set new name for tev (and update namelist) */
3252 ret = probe_trace_event__set_name(tev, pev,
3253 namelist, true);
3254 if (!ret)
3255 ret = show_probe_trace_event(tev);
3256 }
3257 }
3258 strlist__delete(namelist);
3259
3260 return ret;
3261}
3262
Wang Nan12fae5e2015-09-04 21:16:00 +09003263int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003264{
3265 int i, ret = 0;
3266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003267 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003268 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003269 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3270 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003271 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003272 if (ret < 0)
3273 break;
3274 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003275 return ret;
3276}
3277
Wang Nan12fae5e2015-09-04 21:16:00 +09003278void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003279{
3280 int i, j;
3281
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003282 /* Loop 3: cleanup and free trace events */
3283 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003284 for (j = 0; j < pevs[i].ntevs; j++)
3285 clear_probe_trace_event(&pevs[i].tevs[j]);
3286 zfree(&pevs[i].tevs);
3287 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003288 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003289 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003290}
3291
3292int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3293{
3294 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003295
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003296 ret = init_probe_symbol_maps(pevs->uprobes);
3297 if (ret < 0)
3298 return ret;
3299
Wang Nan12fae5e2015-09-04 21:16:00 +09003300 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003301 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003302 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003303
Wang Nan12fae5e2015-09-04 21:16:00 +09003304 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003305
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003306 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003307 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003308}
3309
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003310int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003311{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003312 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003313 char *str = strfilter__string(filter);
3314
3315 if (!str)
3316 return -EINVAL;
3317
Masami Hiramatsufa282442009-12-08 17:03:23 -05003318 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003319 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3320 if (ret < 0)
3321 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303322
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003323 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003324 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303325 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003326
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003327 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003328 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003329 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003330 goto error;
3331 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003332 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303333
3334error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003335 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303336 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003337 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303338 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003339out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003340 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003341
3342 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003343}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303344
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003345int show_available_funcs(const char *target, struct strfilter *_filter,
3346 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003347{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003348 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003349 struct map *map;
3350 int ret;
3351
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003352 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003353 if (ret < 0)
3354 return ret;
3355
3356 /* Get a symbol map */
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003357 map = get_target_map(target, user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003358 if (!map) {
3359 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003360 return -EINVAL;
3361 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003362
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003363 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003364 if (ret) {
3365 if (ret == -2) {
3366 char *str = strfilter__string(_filter);
3367 pr_err("Failed to find symbols matched to \"%s\"\n",
3368 str);
3369 free(str);
3370 } else
3371 pr_err("Failed to load symbols in %s\n",
3372 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003373 goto end;
3374 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003375 if (!dso__sorted_by_name(map->dso, map->type))
3376 dso__sort_by_name(map->dso, map->type);
3377
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003378 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303379 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003380
3381 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3382 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3383
3384 if (strfilter__compare(_filter, pos->sym.name))
3385 printf("%s\n", pos->sym.name);
3386 }
3387
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003388end:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003389 map__put(map);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003390 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303391
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003392 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303393}
3394
Wang Nanda15bd92015-08-26 10:57:45 +00003395int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3396 struct perf_probe_arg *pvar)
3397{
3398 tvar->value = strdup(pvar->var);
3399 if (tvar->value == NULL)
3400 return -ENOMEM;
3401 if (pvar->type) {
3402 tvar->type = strdup(pvar->type);
3403 if (tvar->type == NULL)
3404 return -ENOMEM;
3405 }
3406 if (pvar->name) {
3407 tvar->name = strdup(pvar->name);
3408 if (tvar->name == NULL)
3409 return -ENOMEM;
3410 } else
3411 tvar->name = NULL;
3412 return 0;
3413}