blob: 4a57c8a60bd91990fad493b9a16e265831cb88a5 [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 Hiramatsu8a937a22017-01-04 12:30:19 +0900613/*
614 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
615 * and generate new symbols with suffixes such as .constprop.N or .isra.N
616 * etc. Since those symbols are not recorded in DWARF, we have to find
617 * correct generated symbols from offline ELF binary.
618 * For online kernel or uprobes we don't need this because those are
619 * rebased on _text, or already a section relative address.
620 */
621static int
622post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
623 int ntevs, const char *pathname)
624{
625 struct symbol *sym;
626 struct map *map;
627 unsigned long stext = 0;
628 u64 addr;
629 int i;
630
631 /* Prepare a map for offline binary */
632 map = dso__new_map(pathname);
633 if (!map || get_text_start_address(pathname, &stext) < 0) {
634 pr_warning("Failed to get ELF symbols for %s\n", pathname);
635 return -EINVAL;
636 }
637
638 for (i = 0; i < ntevs; i++) {
639 addr = tevs[i].point.address + tevs[i].point.offset - stext;
640 sym = map__find_symbol(map, addr);
641 if (!sym)
642 continue;
643 if (!strcmp(sym->name, tevs[i].point.symbol))
644 continue;
645 /* If we have no realname, use symbol for it */
646 if (!tevs[i].point.realname)
647 tevs[i].point.realname = tevs[i].point.symbol;
648 else
649 free(tevs[i].point.symbol);
650 tevs[i].point.symbol = strdup(sym->name);
651 tevs[i].point.offset = addr - sym->start;
652 }
653 map__put(map);
654
655 return 0;
656}
657
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000658static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
659 int ntevs, const char *exec)
660{
661 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000662 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000663
664 if (!exec)
665 return 0;
666
667 ret = get_text_start_address(exec, &stext);
668 if (ret < 0)
669 return ret;
670
671 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000672 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000673 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000674 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000675 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000676 ret = -ENOMEM;
677 break;
678 }
679 tevs[i].uprobes = true;
680 }
681
682 return ret;
683}
684
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900685static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
686 int ntevs, const char *module)
687{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900688 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530689 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900690
691 if (!module)
692 return 0;
693
Ravi Bangoria63a29612016-04-26 19:55:41 +0530694 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900695
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900696 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530697 tevs[i].point.module =
698 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900699 if (!tevs[i].point.module) {
700 ret = -ENOMEM;
701 break;
702 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900703 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900704
Ravi Bangoria63a29612016-04-26 19:55:41 +0530705 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900706 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900707}
708
Ravi Bangoriad8204562016-08-09 01:23:24 -0500709static int
710post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
711 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000712{
713 struct ref_reloc_sym *reloc_sym;
714 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900715 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000716
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900717 /* Skip post process if the target is an offline kernel */
718 if (symbol_conf.ignore_vmlinux_buildid)
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900719 return post_process_offline_probe_trace_events(tevs, ntevs,
720 symbol_conf.vmlinux_name);
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900721
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000722 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000723 if (!reloc_sym) {
724 pr_warning("Relocated base symbol is not found!\n");
725 return -EINVAL;
726 }
727
728 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900729 if (!tevs[i].point.address || tevs[i].point.retprobe)
730 continue;
731 /* If we found a wrong one, mark it by NULL symbol */
732 if (kprobe_warn_out_range(tevs[i].point.symbol,
733 tevs[i].point.address)) {
734 tmp = NULL;
735 skipped++;
736 } else {
737 tmp = strdup(reloc_sym->name);
738 if (!tmp)
739 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000740 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900741 /* If we have no realname, use symbol for it */
742 if (!tevs[i].point.realname)
743 tevs[i].point.realname = tevs[i].point.symbol;
744 else
745 free(tevs[i].point.symbol);
746 tevs[i].point.symbol = tmp;
747 tevs[i].point.offset = tevs[i].point.address -
748 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000749 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900750 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000751}
752
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500753void __weak
754arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
755 int ntevs __maybe_unused)
756{
757}
758
Ravi Bangoriad8204562016-08-09 01:23:24 -0500759/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500760static int post_process_probe_trace_events(struct perf_probe_event *pev,
761 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500762 int ntevs, const char *module,
763 bool uprobe)
764{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500765 int ret;
766
Ravi Bangoriad8204562016-08-09 01:23:24 -0500767 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500768 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
769 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500770 /* Currently ref_reloc_sym based probe is not for drivers */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500771 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
772 else
773 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500774
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500775 if (ret >= 0)
776 arch__post_process_probe_trace_events(pev, ntevs);
777
778 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500779}
780
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300781/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530782static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900783 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784{
785 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900786 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530787 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900788 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300789
Masami Hiramatsu44225522015-05-08 10:03:28 +0900790 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900791 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000792 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900793 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900794 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300795 return 0;
796 }
797
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000798 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900799 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900800 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900801
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900802 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900803 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900804 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900805 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900806 /*
807 * Write back to the original probe_event for
808 * setting appropriate (user given) event name
809 */
810 clear_perf_probe_point(&pev->point);
811 memcpy(&pev->point, &tmp, sizeof(tmp));
812 }
813 }
814
Masami Hiramatsuff741782011-06-27 16:27:39 +0900815 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400817 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000818 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500819 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900820 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900821 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000822 clear_probe_trace_events(*tevs, ntevs);
823 zfree(tevs);
824 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900825 if (ret != ntevs)
826 return ret < 0 ? ret : ntevs;
827 ntevs = 0;
828 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400829 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900832 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400833 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900834 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400835 }
836 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400837 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000838 if (ntevs < 0) {
839 if (ntevs == -EBADF)
840 pr_warning("Warning: No dwarf info found in the vmlinux - "
841 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400842 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900843 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400844 return 0;
845 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300846 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400847 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300848}
849
850#define LINEBUF_SIZE 256
851#define NR_ADDITIONAL_LINES 2
852
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100853static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300854{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000855 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100856 const char *color = show_num ? "" : PERF_COLOR_BLUE;
857 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300858
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100859 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300860 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
861 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100862 if (skip)
863 continue;
864 if (!prefix) {
865 prefix = show_num ? "%7d " : " ";
866 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300867 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100868 color_fprintf(stdout, color, "%s", buf);
869
870 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400871
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100872 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300873error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100874 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000875 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300876 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100877 return -1;
878 }
879 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300880}
881
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100882static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
883{
884 int rv = __show_one_line(fp, l, skip, show_num);
885 if (rv == 0) {
886 pr_warning("Source file is shorter than expected.\n");
887 rv = -1;
888 }
889 return rv;
890}
891
892#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
893#define show_one_line(f,l) _show_one_line(f,l,false,false)
894#define skip_one_line(f,l) _show_one_line(f,l,true,false)
895#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
896
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300897/*
898 * Show line-range always requires debuginfo to find source file and
899 * line number.
900 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900901static int __show_line_range(struct line_range *lr, const char *module,
902 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300903{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400904 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000905 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900906 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300907 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900908 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900909 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000910 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300911
912 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000913 dinfo = open_debuginfo(module, false);
914 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900915 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400916
Masami Hiramatsuff741782011-06-27 16:27:39 +0900917 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900918 if (!ret) { /* Not found, retry with an alternative */
919 ret = get_alternative_line_range(dinfo, lr, module, user);
920 if (!ret)
921 ret = debuginfo__find_line_range(dinfo, lr);
922 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900923 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000924 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400925 pr_warning("Specified source line is not found.\n");
926 return -ENOENT;
927 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000928 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400929 return ret;
930 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300931
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900932 /* Convert source file path */
933 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900934 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800935
936 /* Free old path when new path is assigned */
937 if (tmp != lr->path)
938 free(tmp);
939
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900940 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000941 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900942 return ret;
943 }
944
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300945 setup_pager();
946
947 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900948 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300949 lr->start - lr->offset);
950 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100951 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300952
953 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400954 if (fp == NULL) {
955 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300956 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400957 return -errno;
958 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300959 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100960 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100961 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100962 if (ret < 0)
963 goto end;
964 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300965
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300966 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000967 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100968 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100969 if (ret < 0)
970 goto end;
971 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100972 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 if (ret < 0)
974 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300975 }
976
977 if (lr->end == INT_MAX)
978 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100979 while (l <= lr->end) {
980 ret = show_one_line_or_eof(fp, l++ - lr->offset);
981 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100982 break;
983 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400984end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300987}
988
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000989int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000990{
991 int ret;
992
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900993 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000994 if (ret < 0)
995 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900996 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900997 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000998
999 return ret;
1000}
1001
Masami Hiramatsuff741782011-06-27 16:27:39 +09001002static int show_available_vars_at(struct debuginfo *dinfo,
1003 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001004 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001005{
1006 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001007 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001008 struct str_node *node;
1009 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001010 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001011 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001012
1013 buf = synthesize_perf_probe_point(&pev->point);
1014 if (!buf)
1015 return -EINVAL;
1016 pr_debug("Searching variables at %s\n", buf);
1017
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001018 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001019 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +09001020 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001021 if (!ret) {
1022 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001023 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001024 /* Release the old probe_point */
1025 clear_perf_probe_point(&tmp);
1026 }
1027 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001028 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001029 if (ret == 0 || ret == -ENOENT) {
1030 pr_err("Failed to find the address of %s\n", buf);
1031 ret = -ENOENT;
1032 } else
1033 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001034 goto end;
1035 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001036
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001037 /* Some variables are found */
1038 fprintf(stdout, "Available variables at %s\n", buf);
1039 for (i = 0; i < ret; i++) {
1040 vl = &vls[i];
1041 /*
1042 * A probe point might be converted to
1043 * several trace points.
1044 */
1045 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1046 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001047 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001048 nvars = 0;
1049 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001050 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001051 var = strchr(node->s, '\t') + 1;
1052 if (strfilter__compare(_filter, var)) {
1053 fprintf(stdout, "\t\t%s\n", node->s);
1054 nvars++;
1055 }
1056 }
1057 strlist__delete(vl->vars);
1058 }
1059 if (nvars == 0)
1060 fprintf(stdout, "\t\t(No matched variables)\n");
1061 }
1062 free(vls);
1063end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001064 free(buf);
1065 return ret;
1066}
1067
1068/* Show available variables on given probe point */
1069int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001070 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001071{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001072 int i, ret = 0;
1073 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001074
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001075 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001076 if (ret < 0)
1077 return ret;
1078
Masami Hiramatsu44225522015-05-08 10:03:28 +09001079 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001080 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001081 ret = -ENOENT;
1082 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001083 }
1084
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001085 setup_pager();
1086
Masami Hiramatsuff741782011-06-27 16:27:39 +09001087 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001088 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001089
1090 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001091out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001092 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001093 return ret;
1094}
1095
Ingo Molnar89fe8082013-09-30 12:07:11 +02001096#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001097
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001098static void debuginfo_cache__exit(void)
1099{
1100}
1101
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001102static int
1103find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1104 struct perf_probe_point *pp __maybe_unused,
1105 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001106{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001107 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001108}
1109
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301110static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001111 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001112{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001113 if (perf_probe_event_need_dwarf(pev)) {
1114 pr_warning("Debuginfo-analysis is not supported.\n");
1115 return -ENOSYS;
1116 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301117
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001118 return 0;
1119}
1120
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001121int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001122 const char *module __maybe_unused,
1123 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001124{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001125 pr_warning("Debuginfo-analysis is not supported.\n");
1126 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001127}
1128
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001129int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001130 int npevs __maybe_unused,
1131 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001132{
1133 pr_warning("Debuginfo-analysis is not supported.\n");
1134 return -ENOSYS;
1135}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001136#endif
1137
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001138void line_range__clear(struct line_range *lr)
1139{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001140 free(lr->function);
1141 free(lr->file);
1142 free(lr->path);
1143 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001144 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001145 memset(lr, 0, sizeof(*lr));
1146}
1147
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001148int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001149{
1150 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001151 lr->line_list = intlist__new(NULL);
1152 if (!lr->line_list)
1153 return -ENOMEM;
1154 else
1155 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001156}
1157
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001158static int parse_line_num(char **ptr, int *val, const char *what)
1159{
1160 const char *start = *ptr;
1161
1162 errno = 0;
1163 *val = strtol(*ptr, ptr, 0);
1164 if (errno || *ptr == start) {
1165 semantic_error("'%s' is not a valid number.\n", what);
1166 return -EINVAL;
1167 }
1168 return 0;
1169}
1170
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001171/* Check the name is good for event, group or function */
1172static bool is_c_func_name(const char *name)
1173{
1174 if (!isalpha(*name) && *name != '_')
1175 return false;
1176 while (*++name != '\0') {
1177 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1178 return false;
1179 }
1180 return true;
1181}
1182
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001183/*
1184 * Stuff 'lr' according to the line range described by 'arg'.
1185 * The line range syntax is described by:
1186 *
1187 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001188 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001189 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001191{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001192 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001193 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001194
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001195 if (!name)
1196 return -ENOMEM;
1197
1198 lr->start = 0;
1199 lr->end = INT_MAX;
1200
1201 range = strchr(name, ':');
1202 if (range) {
1203 *range++ = '\0';
1204
1205 err = parse_line_num(&range, &lr->start, "start line");
1206 if (err)
1207 goto err;
1208
1209 if (*range == '+' || *range == '-') {
1210 const char c = *range++;
1211
1212 err = parse_line_num(&range, &lr->end, "end line");
1213 if (err)
1214 goto err;
1215
1216 if (c == '+') {
1217 lr->end += lr->start;
1218 /*
1219 * Adjust the number of lines here.
1220 * If the number of lines == 1, the
1221 * the end of line should be equal to
1222 * the start of line.
1223 */
1224 lr->end--;
1225 }
1226 }
1227
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001228 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001229
1230 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001231 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001232 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001233 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001234 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001236 if (*range != '\0') {
1237 semantic_error("Tailing with invalid str '%s'.\n", range);
1238 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001240 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001241
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001242 file = strchr(name, '@');
1243 if (file) {
1244 *file = '\0';
1245 lr->file = strdup(++file);
1246 if (lr->file == NULL) {
1247 err = -ENOMEM;
1248 goto err;
1249 }
1250 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001251 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001252 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001253 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001254 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001255 else { /* Invalid name */
1256 semantic_error("'%s' is not a valid function name.\n", name);
1257 err = -EINVAL;
1258 goto err;
1259 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260
1261 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001262err:
1263 free(name);
1264 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001265}
1266
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001267static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1268{
1269 char *ptr;
1270
1271 ptr = strchr(*arg, ':');
1272 if (ptr) {
1273 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001274 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001275 goto ng_name;
1276 pev->group = strdup(*arg);
1277 if (!pev->group)
1278 return -ENOMEM;
1279 *arg = ptr + 1;
1280 } else
1281 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001282 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001283ng_name:
1284 semantic_error("%s is bad for event name -it must "
1285 "follow C symbol-naming rule.\n", *arg);
1286 return -EINVAL;
1287 }
1288 pev->event = strdup(*arg);
1289 if (pev->event == NULL)
1290 return -ENOMEM;
1291
1292 return 0;
1293}
1294
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001295/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001297{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001298 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001299 char *ptr, *tmp;
1300 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301301 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001302 int ret;
1303
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001304 /*
1305 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001306 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1307 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001308 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001309 */
Wang Nane59d29e2015-04-28 08:46:09 +00001310 if (!arg)
1311 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001312
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001313 /*
1314 * If the probe point starts with '%',
1315 * or starts with "sdt_" and has a ':' but no '=',
1316 * then it should be a SDT/cached probe point.
1317 */
1318 if (arg[0] == '%' ||
1319 (!strncmp(arg, "sdt_", 4) &&
1320 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001321 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001322 if (arg[0] == '%')
1323 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001324 }
1325
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001326 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001327 if (pev->sdt) {
1328 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001329 if (*ptr != '@') {
1330 semantic_error("%s must be an SDT name.\n",
1331 arg);
1332 return -EINVAL;
1333 }
1334 /* This must be a target file name or build id */
1335 tmp = build_id_cache__complement(ptr + 1);
1336 if (tmp) {
1337 pev->target = build_id_cache__origname(tmp);
1338 free(tmp);
1339 } else
1340 pev->target = strdup(ptr + 1);
1341 if (!pev->target)
1342 return -ENOMEM;
1343 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001344 }
1345 ret = parse_perf_probe_event_name(&arg, pev);
1346 if (ret == 0) {
1347 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1348 ret = -errno;
1349 }
1350 return ret;
1351 }
1352
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001353 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001354 *ptr = '\0';
1355 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001356 ret = parse_perf_probe_event_name(&arg, pev);
1357 if (ret < 0)
1358 return ret;
1359
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001360 arg = tmp;
1361 }
1362
Naveen N. Rao3099c022015-04-28 17:35:34 +05301363 /*
1364 * Check arg is function or file name and copy it.
1365 *
1366 * We consider arg to be a file spec if and only if it satisfies
1367 * all of the below criteria::
1368 * - it does not include any of "+@%",
1369 * - it includes one of ":;", and
1370 * - it has a period '.' in the name.
1371 *
1372 * Otherwise, we consider arg to be a function specification.
1373 */
1374 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1375 /* This is a file spec if it includes a '.' before ; or : */
1376 if (memchr(arg, '.', ptr - arg))
1377 file_spec = true;
1378 }
1379
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001380 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001381 if (ptr) {
1382 nc = *ptr;
1383 *ptr++ = '\0';
1384 }
1385
Wang Nan6c6e0242015-08-26 10:57:44 +00001386 if (arg[0] == '\0')
1387 tmp = NULL;
1388 else {
1389 tmp = strdup(arg);
1390 if (tmp == NULL)
1391 return -ENOMEM;
1392 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001393
Naveen N. Rao3099c022015-04-28 17:35:34 +05301394 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001395 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001396 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001397 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001398
Wang Nanda15bd92015-08-26 10:57:45 +00001399 /*
1400 * Keep pp->function even if this is absolute address,
1401 * so it can mark whether abs_address is valid.
1402 * Which make 'perf probe lib.bin 0x0' possible.
1403 *
1404 * Note that checking length of tmp is not needed
1405 * because when we access tmp[1] we know tmp[0] is '0',
1406 * so tmp[1] should always valid (but could be '\0').
1407 */
1408 if (tmp && !strncmp(tmp, "0x", 2)) {
1409 pp->abs_address = strtoul(pp->function, &tmp, 0);
1410 if (*tmp != '\0') {
1411 semantic_error("Invalid absolute address.\n");
1412 return -EINVAL;
1413 }
1414 }
1415 }
1416
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001417 /* Parse other options */
1418 while (ptr) {
1419 arg = ptr;
1420 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001421 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001422 pp->lazy_line = strdup(arg);
1423 if (pp->lazy_line == NULL)
1424 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001425 break;
1426 }
1427 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001428 if (ptr) {
1429 nc = *ptr;
1430 *ptr++ = '\0';
1431 }
1432 switch (c) {
1433 case ':': /* Line number */
1434 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001436 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 " in line number.\n");
1438 return -EINVAL;
1439 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001440 break;
1441 case '+': /* Byte offset from a symbol */
1442 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001444 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445 " in offset.\n");
1446 return -EINVAL;
1447 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001448 break;
1449 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450 if (pp->file) {
1451 semantic_error("SRC@SRC is not allowed.\n");
1452 return -EINVAL;
1453 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001454 pp->file = strdup(arg);
1455 if (pp->file == NULL)
1456 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001457 break;
1458 case '%': /* Probe places */
1459 if (strcmp(arg, "return") == 0) {
1460 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 } else { /* Others not supported yet */
1462 semantic_error("%%%s is not supported.\n", arg);
1463 return -ENOTSUP;
1464 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001465 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 default: /* Buggy case */
1467 pr_err("This program has a bug at %s:%d.\n",
1468 __FILE__, __LINE__);
1469 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001470 break;
1471 }
1472 }
1473
1474 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001475 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001476 semantic_error("Lazy pattern can't be used with"
1477 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478 return -EINVAL;
1479 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001480
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001481 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001482 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001483 return -EINVAL;
1484 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001485
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001486 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001487 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488 return -EINVAL;
1489 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001490
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001491 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001492 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001493 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494 return -EINVAL;
1495 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001496
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001497 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001498 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 return -EINVAL;
1500 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001501
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001502 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001503 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001504 return -EINVAL;
1505 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001506
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001508 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001509 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 return -EINVAL;
1511 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001512
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001514 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1515 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001517}
1518
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001519/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001520static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001521{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001522 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001523 struct perf_probe_arg_field **fieldp;
1524
1525 pr_debug("parsing arg: %s into ", str);
1526
Masami Hiramatsu48481932010-04-12 13:16:53 -04001527 tmp = strchr(str, '=');
1528 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001529 arg->name = strndup(str, tmp - str);
1530 if (arg->name == NULL)
1531 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001532 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001533 str = tmp + 1;
1534 }
1535
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001536 tmp = strchr(str, ':');
1537 if (tmp) { /* Type setting */
1538 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001539 arg->type = strdup(tmp + 1);
1540 if (arg->type == NULL)
1541 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001542 pr_debug("type:%s ", arg->type);
1543 }
1544
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001545 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001546 if (!is_c_varname(str) || !tmp) {
1547 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001548 arg->var = strdup(str);
1549 if (arg->var == NULL)
1550 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001551 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001552 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001553 }
1554
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001555 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001556 arg->var = strndup(str, tmp - str);
1557 if (arg->var == NULL)
1558 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001559 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001560 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001561 fieldp = &arg->field;
1562
1563 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001564 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1565 if (*fieldp == NULL)
1566 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001567 if (*tmp == '[') { /* Array */
1568 str = tmp;
1569 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001570 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001571 if (*tmp != ']' || tmp == str + 1) {
1572 semantic_error("Array index must be a"
1573 " number.\n");
1574 return -EINVAL;
1575 }
1576 tmp++;
1577 if (*tmp == '\0')
1578 tmp = NULL;
1579 } else { /* Structure */
1580 if (*tmp == '.') {
1581 str = tmp + 1;
1582 (*fieldp)->ref = false;
1583 } else if (tmp[1] == '>') {
1584 str = tmp + 2;
1585 (*fieldp)->ref = true;
1586 } else {
1587 semantic_error("Argument parse error: %s\n",
1588 str);
1589 return -EINVAL;
1590 }
1591 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001592 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001593 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001594 (*fieldp)->name = strndup(str, tmp - str);
1595 if ((*fieldp)->name == NULL)
1596 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001597 if (*str != '[')
1598 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001599 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1600 fieldp = &(*fieldp)->next;
1601 }
1602 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001603 (*fieldp)->name = strdup(str);
1604 if ((*fieldp)->name == NULL)
1605 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001606 if (*str != '[')
1607 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001608 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001609
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001610 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001611 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001612 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001613 if (arg->name == NULL)
1614 return -ENOMEM;
1615 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001616 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001617}
1618
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001619/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001620int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001621{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001622 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001623 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001624
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626 if (!argv) {
1627 pr_debug("Failed to split arguments.\n");
1628 return -ENOMEM;
1629 }
1630 if (argc - 1 > MAX_PROBE_ARGS) {
1631 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1632 ret = -ERANGE;
1633 goto out;
1634 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001635 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001636 ret = parse_perf_probe_point(argv[0], pev);
1637 if (ret < 0)
1638 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001639
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001640 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001641 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001642 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1643 if (pev->args == NULL) {
1644 ret = -ENOMEM;
1645 goto out;
1646 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001647 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1648 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1649 if (ret >= 0 &&
1650 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001652 " kretprobe.\n");
1653 ret = -EINVAL;
1654 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001655 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001656out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001657 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001658
1659 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001660}
1661
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301662/* Returns true if *any* ARG is either C variable, $params or $vars. */
1663bool perf_probe_with_var(struct perf_probe_event *pev)
1664{
1665 int i = 0;
1666
1667 for (i = 0; i < pev->nargs; i++)
1668 if (is_c_varname(pev->args[i].var) ||
1669 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1670 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1671 return true;
1672 return false;
1673}
1674
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001675/* Return true if this perf_probe_event requires debuginfo */
1676bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001677{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001678 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1679 return true;
1680
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301681 if (perf_probe_with_var(pev))
1682 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001683
1684 return false;
1685}
1686
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301687/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001688int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001689{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301690 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001691 char pr;
1692 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001693 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001694 int ret, i, argc;
1695 char **argv;
1696
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301697 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001698 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001699 if (!argv) {
1700 pr_debug("Failed to split arguments.\n");
1701 return -ENOMEM;
1702 }
1703 if (argc < 2) {
1704 semantic_error("Too few probe arguments.\n");
1705 ret = -ERANGE;
1706 goto out;
1707 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001708
1709 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001710 argv0_str = strdup(argv[0]);
1711 if (argv0_str == NULL) {
1712 ret = -ENOMEM;
1713 goto out;
1714 }
1715 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1716 fmt2_str = strtok_r(NULL, "/", &fmt);
1717 fmt3_str = strtok_r(NULL, " \t", &fmt);
1718 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1719 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001720 semantic_error("Failed to parse event name: %s\n", argv[0]);
1721 ret = -EINVAL;
1722 goto out;
1723 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001724 pr = fmt1_str[0];
1725 tev->group = strdup(fmt2_str);
1726 tev->event = strdup(fmt3_str);
1727 if (tev->group == NULL || tev->event == NULL) {
1728 ret = -ENOMEM;
1729 goto out;
1730 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001731 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001732
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001734
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001735 /* Scan module name(if there), function name and offset */
1736 p = strchr(argv[1], ':');
1737 if (p) {
1738 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001739 if (!tp->module) {
1740 ret = -ENOMEM;
1741 goto out;
1742 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001743 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001744 p++;
1745 } else
1746 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001747 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001748 /* only the address started with 0x */
1749 if (fmt1_str[0] == '0') {
1750 /*
1751 * Fix a special case:
1752 * if address == 0, kernel reports something like:
1753 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1754 * Newer kernel may fix that, but we want to
1755 * support old kernel also.
1756 */
1757 if (strcmp(fmt1_str, "0x") == 0) {
1758 if (!argv[2] || strcmp(argv[2], "(null)")) {
1759 ret = -EINVAL;
1760 goto out;
1761 }
1762 tp->address = 0;
1763
1764 free(argv[2]);
1765 for (i = 2; argv[i + 1] != NULL; i++)
1766 argv[i] = argv[i + 1];
1767
1768 argv[i] = NULL;
1769 argc -= 1;
1770 } else
1771 tp->address = strtoul(fmt1_str, NULL, 0);
1772 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001773 /* Only the symbol-based probe has offset */
1774 tp->symbol = strdup(fmt1_str);
1775 if (tp->symbol == NULL) {
1776 ret = -ENOMEM;
1777 goto out;
1778 }
1779 fmt2_str = strtok_r(NULL, "", &fmt);
1780 if (fmt2_str == NULL)
1781 tp->offset = 0;
1782 else
1783 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001784 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001785
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001786 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301787 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001788 if (tev->args == NULL) {
1789 ret = -ENOMEM;
1790 goto out;
1791 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001792 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001793 p = strchr(argv[i + 2], '=');
1794 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001795 *p++ = '\0';
1796 else
1797 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001798 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001799 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001800 tev->args[i].value = strdup(p);
1801 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1802 ret = -ENOMEM;
1803 goto out;
1804 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001805 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001806 ret = 0;
1807out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001808 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001809 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001810 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001811}
1812
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001813/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001814char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001815{
1816 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001817 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001818 char *ret = NULL;
1819 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001820
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001821 if (strbuf_init(&buf, 64) < 0)
1822 return NULL;
1823
Masami Hiramatsu48481932010-04-12 13:16:53 -04001824 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001825 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001826 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001827 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1828 if (err)
1829 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001830
1831 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001832 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001833 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001834 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001835 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1836 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001837 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001838 if (err)
1839 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001840 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001841
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001842 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001843 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1844 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001845
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001846 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001847out:
1848 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001850}
1851
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001852/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001853char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001854{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001855 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001856 char *tmp, *ret = NULL;
1857 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001858
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001859 if (strbuf_init(&buf, 64) < 0)
1860 return NULL;
1861
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001862 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001863 if (strbuf_addstr(&buf, pp->function) < 0)
1864 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001865 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001866 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001867 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001868 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001869 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001870 err = strbuf_addstr(&buf, "%return");
1871 if (err)
1872 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001873 }
1874 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001875 tmp = pp->file;
1876 len = strlen(tmp);
1877 if (len > 30) {
1878 tmp = strchr(pp->file + len - 30, '/');
1879 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1880 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001881 err = strbuf_addf(&buf, "@%s", tmp);
1882 if (!err && !pp->function && pp->line)
1883 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001884 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001885 if (!err)
1886 ret = strbuf_detach(&buf, NULL);
1887out:
1888 strbuf_release(&buf);
1889 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890}
1891
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001892char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1893{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001894 struct strbuf buf;
1895 char *tmp, *ret = NULL;
1896 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001898 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001899 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001900 if (pev->event)
1901 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1902 pev->event) < 0)
1903 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001905 tmp = synthesize_perf_probe_point(&pev->point);
1906 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1907 goto out;
1908 free(tmp);
1909
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001910 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001911 tmp = synthesize_perf_probe_arg(pev->args + i);
1912 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1913 goto out;
1914 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001915 }
1916
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001917 ret = strbuf_detach(&buf, NULL);
1918out:
1919 strbuf_release(&buf);
1920 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001921}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001922
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301923static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001924 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001925{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001926 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001927 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301928 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001929 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001930 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001931 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001932 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001933 err = strbuf_addf(buf, "%+ld(", ref->offset);
1934 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001935}
1936
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301937static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001938 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001939{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301940 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001941 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942
1943 /* Argument name or separator */
1944 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001945 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001946 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001947 err = strbuf_addch(buf, ' ');
1948 if (err)
1949 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001950
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001951 /* Special case: @XXX */
1952 if (arg->value[0] == '@' && arg->ref)
1953 ref = ref->next;
1954
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001955 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001956 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001957 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001958 if (depth < 0)
1959 return depth;
1960 }
1961
1962 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001963 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001964 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001965 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001966 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001967
1968 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001969 while (!err && depth--)
1970 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001972 /* Print argument type */
1973 if (!err && arg->type)
1974 err = strbuf_addf(buf, ":%s", arg->type);
1975
1976 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001977}
1978
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301979char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001980{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301981 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001982 struct strbuf buf;
1983 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001984 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001985
Wang Nanda15bd92015-08-26 10:57:45 +00001986 /* Uprobes must have tp->module */
1987 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001988 return NULL;
1989
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001990 if (strbuf_init(&buf, 32) < 0)
1991 return NULL;
1992
1993 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1994 tev->group, tev->event) < 0)
1995 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001996 /*
1997 * If tp->address == 0, then this point must be a
1998 * absolute address uprobe.
1999 * try_to_find_absolute_address() should have made
2000 * tp->symbol to "0x0".
2001 */
2002 if (tev->uprobes && !tp->address) {
2003 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2004 goto error;
2005 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002006
2007 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302008 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002009 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00002010 else if (!strncmp(tp->symbol, "0x", 2))
2011 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002012 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2013 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302014 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002015 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2016 tp->module ? ":" : "", tp->symbol, tp->offset);
2017 if (err)
2018 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302019
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002020 for (i = 0; i < tev->nargs; i++)
2021 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002022 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002023
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002024 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002025error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002026 strbuf_release(&buf);
2027 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002028}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002029
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002030static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2031 struct perf_probe_point *pp,
2032 bool is_kprobe)
2033{
2034 struct symbol *sym = NULL;
2035 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00002036 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002037 int ret = -ENOENT;
2038
2039 if (!is_kprobe) {
2040 map = dso__new_map(tp->module);
2041 if (!map)
2042 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002043 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002044 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002045 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002046 if (kernel_get_symbol_address_by_name(tp->symbol,
2047 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002048 goto out;
2049 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002050 if (addr) {
2051 addr += tp->offset;
2052 sym = __find_kernel_function(addr, &map);
2053 }
2054 }
Wang Nan98d3b252015-11-05 13:19:25 +00002055
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002056 if (!sym)
2057 goto out;
2058
2059 pp->retprobe = tp->retprobe;
2060 pp->offset = addr - map->unmap_ip(map, sym->start);
2061 pp->function = strdup(sym->name);
2062 ret = pp->function ? 0 : -ENOMEM;
2063
2064out:
2065 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002066 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002067 }
2068
2069 return ret;
2070}
2071
2072static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002073 struct perf_probe_point *pp,
2074 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002075{
2076 char buf[128];
2077 int ret;
2078
2079 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2080 if (!ret)
2081 return 0;
2082 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2083 if (!ret)
2084 return 0;
2085
2086 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2087
2088 if (tp->symbol) {
2089 pp->function = strdup(tp->symbol);
2090 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002091 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002092 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2093 if (ret < 0)
2094 return ret;
2095 pp->function = strdup(buf);
2096 pp->offset = 0;
2097 }
2098 if (pp->function == NULL)
2099 return -ENOMEM;
2100
2101 pp->retprobe = tp->retprobe;
2102
2103 return 0;
2104}
2105
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302106static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302107 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002108{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002109 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002110 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002111
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002112 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002113 pev->event = strdup(tev->event);
2114 pev->group = strdup(tev->group);
2115 if (pev->event == NULL || pev->group == NULL)
2116 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002117
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002118 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002119 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002120 if (ret < 0)
2121 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002122
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002123 /* Convert trace_arg to probe_arg */
2124 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002125 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2126 if (pev->args == NULL)
2127 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002128 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002129 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002130 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002131 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002132 if ((ret = strbuf_init(&buf, 32)) < 0)
2133 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002134 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2135 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002136 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002137 if (pev->args[i].name == NULL && ret >= 0)
2138 ret = -ENOMEM;
2139 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002140error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002141 if (ret < 0)
2142 clear_perf_probe_event(pev);
2143
2144 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002145}
2146
2147void clear_perf_probe_event(struct perf_probe_event *pev)
2148{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002149 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 int i;
2151
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002152 free(pev->event);
2153 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002154 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002155 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002156
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002157 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002158 free(pev->args[i].name);
2159 free(pev->args[i].var);
2160 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002161 field = pev->args[i].field;
2162 while (field) {
2163 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002164 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002165 free(field);
2166 field = next;
2167 }
2168 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002169 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002170 memset(pev, 0, sizeof(*pev));
2171}
2172
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002173#define strdup_or_goto(str, label) \
2174({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2175
2176static int perf_probe_point__copy(struct perf_probe_point *dst,
2177 struct perf_probe_point *src)
2178{
2179 dst->file = strdup_or_goto(src->file, out_err);
2180 dst->function = strdup_or_goto(src->function, out_err);
2181 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2182 dst->line = src->line;
2183 dst->retprobe = src->retprobe;
2184 dst->offset = src->offset;
2185 return 0;
2186
2187out_err:
2188 clear_perf_probe_point(dst);
2189 return -ENOMEM;
2190}
2191
2192static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2193 struct perf_probe_arg *src)
2194{
2195 struct perf_probe_arg_field *field, **ppfield;
2196
2197 dst->name = strdup_or_goto(src->name, out_err);
2198 dst->var = strdup_or_goto(src->var, out_err);
2199 dst->type = strdup_or_goto(src->type, out_err);
2200
2201 field = src->field;
2202 ppfield = &(dst->field);
2203 while (field) {
2204 *ppfield = zalloc(sizeof(*field));
2205 if (!*ppfield)
2206 goto out_err;
2207 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2208 (*ppfield)->index = field->index;
2209 (*ppfield)->ref = field->ref;
2210 field = field->next;
2211 ppfield = &((*ppfield)->next);
2212 }
2213 return 0;
2214out_err:
2215 return -ENOMEM;
2216}
2217
2218int perf_probe_event__copy(struct perf_probe_event *dst,
2219 struct perf_probe_event *src)
2220{
2221 int i;
2222
2223 dst->event = strdup_or_goto(src->event, out_err);
2224 dst->group = strdup_or_goto(src->group, out_err);
2225 dst->target = strdup_or_goto(src->target, out_err);
2226 dst->uprobes = src->uprobes;
2227
2228 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2229 goto out_err;
2230
2231 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2232 if (!dst->args)
2233 goto out_err;
2234 dst->nargs = src->nargs;
2235
2236 for (i = 0; i < src->nargs; i++)
2237 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2238 goto out_err;
2239 return 0;
2240
2241out_err:
2242 clear_perf_probe_event(dst);
2243 return -ENOMEM;
2244}
2245
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002246void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002247{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002249 int i;
2250
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002251 free(tev->event);
2252 free(tev->group);
2253 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002254 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002255 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002256 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002257 free(tev->args[i].name);
2258 free(tev->args[i].value);
2259 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002260 ref = tev->args[i].ref;
2261 while (ref) {
2262 next = ref->next;
2263 free(ref);
2264 ref = next;
2265 }
2266 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002267 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002268 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002269}
2270
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002271struct kprobe_blacklist_node {
2272 struct list_head list;
2273 unsigned long start;
2274 unsigned long end;
2275 char *symbol;
2276};
2277
2278static void kprobe_blacklist__delete(struct list_head *blacklist)
2279{
2280 struct kprobe_blacklist_node *node;
2281
2282 while (!list_empty(blacklist)) {
2283 node = list_first_entry(blacklist,
2284 struct kprobe_blacklist_node, list);
2285 list_del(&node->list);
2286 free(node->symbol);
2287 free(node);
2288 }
2289}
2290
2291static int kprobe_blacklist__load(struct list_head *blacklist)
2292{
2293 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002294 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002295 char buf[PATH_MAX], *p;
2296 FILE *fp;
2297 int ret;
2298
2299 if (__debugfs == NULL)
2300 return -ENOTSUP;
2301
2302 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2303 if (ret < 0)
2304 return ret;
2305
2306 fp = fopen(buf, "r");
2307 if (!fp)
2308 return -errno;
2309
2310 ret = 0;
2311 while (fgets(buf, PATH_MAX, fp)) {
2312 node = zalloc(sizeof(*node));
2313 if (!node) {
2314 ret = -ENOMEM;
2315 break;
2316 }
2317 INIT_LIST_HEAD(&node->list);
2318 list_add_tail(&node->list, blacklist);
2319 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2320 ret = -EINVAL;
2321 break;
2322 }
2323 p = strchr(buf, '\t');
2324 if (p) {
2325 p++;
2326 if (p[strlen(p) - 1] == '\n')
2327 p[strlen(p) - 1] = '\0';
2328 } else
2329 p = (char *)"unknown";
2330 node->symbol = strdup(p);
2331 if (!node->symbol) {
2332 ret = -ENOMEM;
2333 break;
2334 }
2335 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2336 node->start, node->end, node->symbol);
2337 ret++;
2338 }
2339 if (ret < 0)
2340 kprobe_blacklist__delete(blacklist);
2341 fclose(fp);
2342
2343 return ret;
2344}
2345
2346static struct kprobe_blacklist_node *
2347kprobe_blacklist__find_by_address(struct list_head *blacklist,
2348 unsigned long address)
2349{
2350 struct kprobe_blacklist_node *node;
2351
2352 list_for_each_entry(node, blacklist, list) {
2353 if (node->start <= address && address <= node->end)
2354 return node;
2355 }
2356
2357 return NULL;
2358}
2359
Masami Hiramatsub0312202015-06-16 20:50:55 +09002360static LIST_HEAD(kprobe_blacklist);
2361
2362static void kprobe_blacklist__init(void)
2363{
2364 if (!list_empty(&kprobe_blacklist))
2365 return;
2366
2367 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2368 pr_debug("No kprobe blacklist support, ignored\n");
2369}
2370
2371static void kprobe_blacklist__release(void)
2372{
2373 kprobe_blacklist__delete(&kprobe_blacklist);
2374}
2375
2376static bool kprobe_blacklist__listed(unsigned long address)
2377{
2378 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2379}
2380
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002381static int perf_probe_event__sprintf(const char *group, const char *event,
2382 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002383 const char *module,
2384 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002385{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002386 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002387 char *buf;
2388
2389 if (asprintf(&buf, "%s:%s", group, event) < 0)
2390 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002391 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002392 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002393 if (ret)
2394 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002395
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002396 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002397 buf = synthesize_perf_probe_point(&pev->point);
2398 if (!buf)
2399 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002400 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002401 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002402
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002403 if (!ret && module)
2404 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002405
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002406 if (!ret && pev->nargs > 0) {
2407 ret = strbuf_add(result, " with", 5);
2408 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002409 buf = synthesize_perf_probe_arg(&pev->args[i]);
2410 if (!buf)
2411 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002412 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002413 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002414 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002415 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002416 if (!ret)
2417 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002418
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002419 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002420}
2421
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002422/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002423int show_perf_probe_event(const char *group, const char *event,
2424 struct perf_probe_event *pev,
2425 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002426{
2427 struct strbuf buf = STRBUF_INIT;
2428 int ret;
2429
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002430 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002431 if (ret >= 0) {
2432 if (use_stdout)
2433 printf("%s\n", buf.buf);
2434 else
2435 pr_info("%s\n", buf.buf);
2436 }
2437 strbuf_release(&buf);
2438
2439 return ret;
2440}
2441
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002442static bool filter_probe_trace_event(struct probe_trace_event *tev,
2443 struct strfilter *filter)
2444{
2445 char tmp[128];
2446
2447 /* At first, check the event name itself */
2448 if (strfilter__compare(filter, tev->event))
2449 return true;
2450
2451 /* Next, check the combination of name and group */
2452 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2453 return false;
2454 return strfilter__compare(filter, tmp);
2455}
2456
2457static int __show_perf_probe_events(int fd, bool is_kprobe,
2458 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002459{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302460 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302461 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002462 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002463 struct strlist *rawlist;
2464 struct str_node *ent;
2465
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002466 memset(&tev, 0, sizeof(tev));
2467 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002468
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002469 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002470 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002471 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002472
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002473 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302474 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002475 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002476 if (!filter_probe_trace_event(&tev, filter))
2477 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302478 ret = convert_to_perf_probe_event(&tev, &pev,
2479 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002480 if (ret < 0)
2481 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002482 ret = show_perf_probe_event(pev.group, pev.event,
2483 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002484 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002485 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002486next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002487 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302488 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002489 if (ret < 0)
2490 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002491 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002492 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002493 /* Cleanup cached debuginfo if needed */
2494 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002495
2496 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002497}
2498
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302499/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002500int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302501{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002502 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302503
2504 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302505
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002506 if (probe_conf.cache)
2507 return probe_cache__show_all_caches(filter);
2508
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002509 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302510 if (ret < 0)
2511 return ret;
2512
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002513 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2514 if (ret < 0)
2515 return ret;
2516
2517 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002518 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002519 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002520 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002521 if (kp_fd > 0)
2522 close(kp_fd);
2523 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002524 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002525 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302526
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002527 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002528}
2529
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002530static int get_new_event_name(char *buf, size_t len, const char *base,
2531 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002532{
2533 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002534 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002535
Naveen N. Rao3099c022015-04-28 17:35:34 +05302536 if (*base == '.')
2537 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002538 nbase = strdup(base);
2539 if (!nbase)
2540 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302541
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002542 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2543 p = strchr(nbase, '.');
2544 if (p && p != nbase)
2545 *p = '\0';
2546
2547 /* Try no suffix number */
2548 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002549 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002550 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002551 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002552 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002553 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002554 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002555
Masami Hiramatsud761b082009-12-15 10:32:25 -05002556 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002557 pr_warning("Error: event \"%s\" already exists.\n"
2558 " Hint: Remove existing event by 'perf probe -d'\n"
2559 " or force duplicates by 'perf probe -f'\n"
2560 " or set 'force=yes' in BPF source.\n",
2561 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002562 ret = -EEXIST;
2563 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002564 }
2565
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002566 /* Try to add suffix */
2567 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002568 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002569 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002570 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002571 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002572 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002573 if (!strlist__has_entry(namelist, buf))
2574 break;
2575 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002576 if (i == MAX_EVENT_INDEX) {
2577 pr_warning("Too many events are on the same function.\n");
2578 ret = -ERANGE;
2579 }
2580
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002581out:
2582 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002583 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002584}
2585
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002586/* Warn if the current kernel's uprobe implementation is old */
2587static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2588{
2589 int i;
2590 char *buf = synthesize_probe_trace_command(tev);
2591
2592 /* Old uprobe event doesn't support memory dereference */
2593 if (!tev->uprobes || tev->nargs == 0 || !buf)
2594 goto out;
2595
2596 for (i = 0; i < tev->nargs; i++)
2597 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2598 pr_warning("Please upgrade your kernel to at least "
2599 "3.14 to have access to feature %s\n",
2600 tev->args[i].value);
2601 break;
2602 }
2603out:
2604 free(buf);
2605}
2606
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002607/* Set new name from original perf_probe_event and namelist */
2608static int probe_trace_event__set_name(struct probe_trace_event *tev,
2609 struct perf_probe_event *pev,
2610 struct strlist *namelist,
2611 bool allow_suffix)
2612{
2613 const char *event, *group;
2614 char buf[64];
2615 int ret;
2616
Masami Hiramatsubc062232016-07-01 17:03:12 +09002617 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002618 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002619 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002620 else if (tev->event)
2621 event = tev->event;
2622 else {
2623 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002624 if (pev->point.function &&
2625 (strncmp(pev->point.function, "0x", 2) != 0) &&
2626 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002627 event = pev->point.function;
2628 else
2629 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002630 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002631 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002632 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002633 else if (tev->group)
2634 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002635 else
2636 group = PERFPROBE_GROUP;
2637
2638 /* Get an unused new event name */
2639 ret = get_new_event_name(buf, 64, event,
2640 namelist, allow_suffix);
2641 if (ret < 0)
2642 return ret;
2643
2644 event = buf;
2645
2646 tev->event = strdup(event);
2647 tev->group = strdup(group);
2648 if (tev->event == NULL || tev->group == NULL)
2649 return -ENOMEM;
2650
2651 /* Add added event name to namelist */
2652 strlist__add(namelist, event);
2653 return 0;
2654}
2655
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002656static int __open_probe_file_and_namelist(bool uprobe,
2657 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002658{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002659 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002660
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002661 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002662 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002663 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002664
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002665 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002666 *namelist = probe_file__get_namelist(fd);
2667 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002668 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002669 close(fd);
2670 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002671 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002672 return fd;
2673}
2674
2675static int __add_probe_trace_events(struct perf_probe_event *pev,
2676 struct probe_trace_event *tevs,
2677 int ntevs, bool allow_suffix)
2678{
2679 int i, fd[2] = {-1, -1}, up, ret;
2680 struct probe_trace_event *tev = NULL;
2681 struct probe_cache *cache = NULL;
2682 struct strlist *namelist[2] = {NULL, NULL};
2683
2684 up = pev->uprobes ? 1 : 0;
2685 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2686 if (fd[up] < 0)
2687 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002688
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002690 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002691 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002692 up = tev->uprobes ? 1 : 0;
2693 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2694 fd[up] = __open_probe_file_and_namelist(up,
2695 &namelist[up]);
2696 if (fd[up] < 0)
2697 goto close_out;
2698 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002699 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002700 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002701 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002702
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002703 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002704 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002705 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002706 if (ret < 0)
2707 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002708
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002709 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002710 if (ret < 0)
2711 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002712
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002713 /*
2714 * Probes after the first probe which comes from same
2715 * user input are always allowed to add suffix, because
2716 * there might be several addresses corresponding to
2717 * one code line.
2718 */
2719 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002720 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002721 if (ret == -EINVAL && pev->uprobes)
2722 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002723 if (ret == 0 && probe_conf.cache) {
2724 cache = probe_cache__new(pev->target);
2725 if (!cache ||
2726 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2727 probe_cache__commit(cache) < 0)
2728 pr_warning("Failed to add event to probe cache\n");
2729 probe_cache__delete(cache);
2730 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002731
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002732close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002733 for (up = 0; up < 2; up++) {
2734 strlist__delete(namelist[up]);
2735 if (fd[up] >= 0)
2736 close(fd[up]);
2737 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002738 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002739}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002740
Wang Nan6bb536c2015-05-29 09:45:47 +00002741static int find_probe_functions(struct map *map, char *name,
2742 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002743{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002744 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002745 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002746 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002747
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002748 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002749 return 0;
2750
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002751 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002752 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002753 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002754 if (syms && found < probe_conf.max_probes)
2755 syms[found - 1] = sym;
2756 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002757 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002758
2759 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002760}
2761
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302762void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2763 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302764 struct map *map __maybe_unused,
2765 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302766
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002767/*
2768 * Find probe function addresses from map.
2769 * Return an error or the number of found probe_trace_event
2770 */
2771static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002772 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002773{
2774 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002775 struct ref_reloc_sym *reloc_sym = NULL;
2776 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002777 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002778 struct probe_trace_event *tev;
2779 struct perf_probe_point *pp = &pev->point;
2780 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002781 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002782 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302783 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002784
Masami Hiramatsu44225522015-05-08 10:03:28 +09002785 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002786 if (!map) {
2787 ret = -EINVAL;
2788 goto out;
2789 }
2790
Wang Nan6bb536c2015-05-29 09:45:47 +00002791 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2792 if (!syms) {
2793 ret = -ENOMEM;
2794 goto out;
2795 }
2796
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002797 /*
2798 * Load matched symbols: Since the different local symbols may have
2799 * same name but different addresses, this lists all the symbols.
2800 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002801 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002802 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002803 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002804 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002805 ret = -ENOENT;
2806 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002807 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002808 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002809 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002810 ret = -E2BIG;
2811 goto out;
2812 }
2813
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002814 /* Note that the symbols in the kmodule are not relocated */
2815 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002816 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002817 if (!reloc_sym) {
2818 pr_warning("Relocated base symbol is not found!\n");
2819 ret = -EINVAL;
2820 goto out;
2821 }
2822 }
2823
2824 /* Setup result trace-probe-events */
2825 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2826 if (!*tevs) {
2827 ret = -ENOMEM;
2828 goto out;
2829 }
2830
2831 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002832
Wang Nan6bb536c2015-05-29 09:45:47 +00002833 for (j = 0; j < num_matched_functions; j++) {
2834 sym = syms[j];
2835
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002836 tev = (*tevs) + ret;
2837 tp = &tev->point;
2838 if (ret == num_matched_functions) {
2839 pr_warning("Too many symbols are listed. Skip it.\n");
2840 break;
2841 }
2842 ret++;
2843
2844 if (pp->offset > sym->end - sym->start) {
2845 pr_warning("Offset %ld is bigger than the size of %s\n",
2846 pp->offset, sym->name);
2847 ret = -ENOENT;
2848 goto err_out;
2849 }
2850 /* Add one probe point */
2851 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002852
2853 /* Check the kprobe (not in module) is within .text */
2854 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002855 kprobe_warn_out_range(sym->name, tp->address)) {
2856 tp->symbol = NULL; /* Skip it */
2857 skipped++;
2858 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002859 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2860 tp->offset = tp->address - reloc_sym->addr;
2861 } else {
2862 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2863 tp->offset = pp->offset;
2864 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002865 tp->realname = strdup_or_goto(sym->name, nomem_out);
2866
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002867 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302868 if (pev->target) {
2869 if (pev->uprobes) {
2870 tev->point.module = strdup_or_goto(pev->target,
2871 nomem_out);
2872 } else {
2873 mod_name = find_module_name(pev->target);
2874 tev->point.module =
2875 strdup(mod_name ? mod_name : pev->target);
2876 free(mod_name);
2877 if (!tev->point.module)
2878 goto nomem_out;
2879 }
2880 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002881 tev->uprobes = pev->uprobes;
2882 tev->nargs = pev->nargs;
2883 if (tev->nargs) {
2884 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2885 tev->nargs);
2886 if (tev->args == NULL)
2887 goto nomem_out;
2888 }
2889 for (i = 0; i < tev->nargs; i++) {
2890 if (pev->args[i].name)
2891 tev->args[i].name =
2892 strdup_or_goto(pev->args[i].name,
2893 nomem_out);
2894
2895 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2896 nomem_out);
2897 if (pev->args[i].type)
2898 tev->args[i].type =
2899 strdup_or_goto(pev->args[i].type,
2900 nomem_out);
2901 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302902 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002903 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002904 if (ret == skipped) {
2905 ret = -ENOENT;
2906 goto err_out;
2907 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002908
2909out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09002910 map__put(map);
Wang Nan6bb536c2015-05-29 09:45:47 +00002911 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002912 return ret;
2913
2914nomem_out:
2915 ret = -ENOMEM;
2916err_out:
2917 clear_probe_trace_events(*tevs, num_matched_functions);
2918 zfree(tevs);
2919 goto out;
2920}
2921
Wang Nanda15bd92015-08-26 10:57:45 +00002922static int try_to_find_absolute_address(struct perf_probe_event *pev,
2923 struct probe_trace_event **tevs)
2924{
2925 struct perf_probe_point *pp = &pev->point;
2926 struct probe_trace_event *tev;
2927 struct probe_trace_point *tp;
2928 int i, err;
2929
2930 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2931 return -EINVAL;
2932 if (perf_probe_event_need_dwarf(pev))
2933 return -EINVAL;
2934
2935 /*
2936 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2937 * absolute address.
2938 *
2939 * Only one tev can be generated by this.
2940 */
2941 *tevs = zalloc(sizeof(*tev));
2942 if (!*tevs)
2943 return -ENOMEM;
2944
2945 tev = *tevs;
2946 tp = &tev->point;
2947
2948 /*
2949 * Don't use tp->offset, use address directly, because
2950 * in synthesize_probe_trace_command() address cannot be
2951 * zero.
2952 */
2953 tp->address = pev->point.abs_address;
2954 tp->retprobe = pp->retprobe;
2955 tev->uprobes = pev->uprobes;
2956
2957 err = -ENOMEM;
2958 /*
2959 * Give it a '0x' leading symbol name.
2960 * In __add_probe_trace_events, a NULL symbol is interpreted as
2961 * invalud.
2962 */
2963 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2964 goto errout;
2965
2966 /* For kprobe, check range */
2967 if ((!tev->uprobes) &&
2968 (kprobe_warn_out_range(tev->point.symbol,
2969 tev->point.address))) {
2970 err = -EACCES;
2971 goto errout;
2972 }
2973
2974 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2975 goto errout;
2976
2977 if (pev->target) {
2978 tp->module = strdup(pev->target);
2979 if (!tp->module)
2980 goto errout;
2981 }
2982
2983 if (tev->group) {
2984 tev->group = strdup(pev->group);
2985 if (!tev->group)
2986 goto errout;
2987 }
2988
2989 if (pev->event) {
2990 tev->event = strdup(pev->event);
2991 if (!tev->event)
2992 goto errout;
2993 }
2994
2995 tev->nargs = pev->nargs;
2996 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2997 if (!tev->args) {
2998 err = -ENOMEM;
2999 goto errout;
3000 }
3001 for (i = 0; i < tev->nargs; i++)
3002 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3003
3004 return 1;
3005
3006errout:
3007 if (*tevs) {
3008 clear_probe_trace_events(*tevs, 1);
3009 *tevs = NULL;
3010 }
3011 return err;
3012}
3013
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003014/* Concatinate two arrays */
3015static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3016{
3017 void *ret;
3018
3019 ret = malloc(sz_a + sz_b);
3020 if (ret) {
3021 memcpy(ret, a, sz_a);
3022 memcpy(ret + sz_a, b, sz_b);
3023 }
3024 return ret;
3025}
3026
3027static int
3028concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3029 struct probe_trace_event **tevs2, int ntevs2)
3030{
3031 struct probe_trace_event *new_tevs;
3032 int ret = 0;
3033
3034 if (ntevs == 0) {
3035 *tevs = *tevs2;
3036 *ntevs = ntevs2;
3037 *tevs2 = NULL;
3038 return 0;
3039 }
3040
3041 if (*ntevs + ntevs2 > probe_conf.max_probes)
3042 ret = -E2BIG;
3043 else {
3044 /* Concatinate the array of probe_trace_event */
3045 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3046 *tevs2, ntevs2 * sizeof(**tevs2));
3047 if (!new_tevs)
3048 ret = -ENOMEM;
3049 else {
3050 free(*tevs);
3051 *tevs = new_tevs;
3052 *ntevs += ntevs2;
3053 }
3054 }
3055 if (ret < 0)
3056 clear_probe_trace_events(*tevs2, ntevs2);
3057 zfree(tevs2);
3058
3059 return ret;
3060}
3061
3062/*
3063 * Try to find probe_trace_event from given probe caches. Return the number
3064 * of cached events found, if an error occurs return the error.
3065 */
3066static int find_cached_events(struct perf_probe_event *pev,
3067 struct probe_trace_event **tevs,
3068 const char *target)
3069{
3070 struct probe_cache *cache;
3071 struct probe_cache_entry *entry;
3072 struct probe_trace_event *tmp_tevs = NULL;
3073 int ntevs = 0;
3074 int ret = 0;
3075
3076 cache = probe_cache__new(target);
3077 /* Return 0 ("not found") if the target has no probe cache. */
3078 if (!cache)
3079 return 0;
3080
3081 for_each_probe_cache_entry(entry, cache) {
3082 /* Skip the cache entry which has no name */
3083 if (!entry->pev.event || !entry->pev.group)
3084 continue;
3085 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3086 strglobmatch(entry->pev.event, pev->event)) {
3087 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3088 if (ret > 0)
3089 ret = concat_probe_trace_events(tevs, &ntevs,
3090 &tmp_tevs, ret);
3091 if (ret < 0)
3092 break;
3093 }
3094 }
3095 probe_cache__delete(cache);
3096 if (ret < 0) {
3097 clear_probe_trace_events(*tevs, ntevs);
3098 zfree(tevs);
3099 } else {
3100 ret = ntevs;
3101 if (ntevs > 0 && target && target[0] == '/')
3102 pev->uprobes = true;
3103 }
3104
3105 return ret;
3106}
3107
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003108/* Try to find probe_trace_event from all probe caches */
3109static int find_cached_events_all(struct perf_probe_event *pev,
3110 struct probe_trace_event **tevs)
3111{
3112 struct probe_trace_event *tmp_tevs = NULL;
3113 struct strlist *bidlist;
3114 struct str_node *nd;
3115 char *pathname;
3116 int ntevs = 0;
3117 int ret;
3118
3119 /* Get the buildid list of all valid caches */
3120 bidlist = build_id_cache__list_all(true);
3121 if (!bidlist) {
3122 ret = -errno;
3123 pr_debug("Failed to get buildids: %d\n", ret);
3124 return ret;
3125 }
3126
3127 ret = 0;
3128 strlist__for_each_entry(nd, bidlist) {
3129 pathname = build_id_cache__origname(nd->s);
3130 ret = find_cached_events(pev, &tmp_tevs, pathname);
3131 /* In the case of cnt == 0, we just skip it */
3132 if (ret > 0)
3133 ret = concat_probe_trace_events(tevs, &ntevs,
3134 &tmp_tevs, ret);
3135 free(pathname);
3136 if (ret < 0)
3137 break;
3138 }
3139 strlist__delete(bidlist);
3140
3141 if (ret < 0) {
3142 clear_probe_trace_events(*tevs, ntevs);
3143 zfree(tevs);
3144 } else
3145 ret = ntevs;
3146
3147 return ret;
3148}
3149
Masami Hiramatsubc062232016-07-01 17:03:12 +09003150static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3151 struct probe_trace_event **tevs)
3152{
3153 struct probe_cache *cache;
3154 struct probe_cache_entry *entry;
3155 struct probe_trace_event *tev;
3156 struct str_node *node;
3157 int ret, i;
3158
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003159 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003160 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003161 if (!pev->target)
3162 return find_cached_events_all(pev, tevs);
3163 else
3164 return find_cached_events(pev, tevs, pev->target);
3165 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003166 cache = probe_cache__new(pev->target);
3167 if (!cache)
3168 return 0;
3169
3170 entry = probe_cache__find(cache, pev);
3171 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003172 /* SDT must be in the cache */
3173 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003174 goto out;
3175 }
3176
3177 ret = strlist__nr_entries(entry->tevlist);
3178 if (ret > probe_conf.max_probes) {
3179 pr_debug("Too many entries matched in the cache of %s\n",
3180 pev->target ? : "kernel");
3181 ret = -E2BIG;
3182 goto out;
3183 }
3184
3185 *tevs = zalloc(ret * sizeof(*tev));
3186 if (!*tevs) {
3187 ret = -ENOMEM;
3188 goto out;
3189 }
3190
3191 i = 0;
3192 strlist__for_each_entry(node, entry->tevlist) {
3193 tev = &(*tevs)[i++];
3194 ret = parse_probe_trace_command(node->s, tev);
3195 if (ret < 0)
3196 goto out;
3197 /* Set the uprobes attribute as same as original */
3198 tev->uprobes = pev->uprobes;
3199 }
3200 ret = i;
3201
3202out:
3203 probe_cache__delete(cache);
3204 return ret;
3205}
3206
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303207static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003208 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003209{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003210 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003211
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003212 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003213 /* Set group name if not given */
3214 if (!pev->uprobes) {
3215 pev->group = strdup(PERFPROBE_GROUP);
3216 ret = pev->group ? 0 : -ENOMEM;
3217 } else
3218 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003219 if (ret != 0) {
3220 pr_warning("Failed to make a group name.\n");
3221 return ret;
3222 }
3223 }
3224
Wang Nanda15bd92015-08-26 10:57:45 +00003225 ret = try_to_find_absolute_address(pev, tevs);
3226 if (ret > 0)
3227 return ret;
3228
Masami Hiramatsubc062232016-07-01 17:03:12 +09003229 /* At first, we need to lookup cache entry */
3230 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003231 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3232 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003233
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003234 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003235 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003236 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003237 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003238
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003239 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003240}
3241
Wang Nan12fae5e2015-09-04 21:16:00 +09003242int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003243{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003244 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003245
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003246 /* Loop 1: convert all events */
3247 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003248 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003249 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003250 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003251 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003252 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003253 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003254 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003255 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003256 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003257 /* This just release blacklist only if allocated */
3258 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003259
Namhyung Kim844dffa2015-09-04 21:15:59 +09003260 return 0;
3261}
3262
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003263static int show_probe_trace_event(struct probe_trace_event *tev)
3264{
3265 char *buf = synthesize_probe_trace_command(tev);
3266
3267 if (!buf) {
3268 pr_debug("Failed to synthesize probe trace event.\n");
3269 return -EINVAL;
3270 }
3271
3272 /* Showing definition always go stdout */
3273 printf("%s\n", buf);
3274 free(buf);
3275
3276 return 0;
3277}
3278
3279int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3280{
3281 struct strlist *namelist = strlist__new(NULL, NULL);
3282 struct probe_trace_event *tev;
3283 struct perf_probe_event *pev;
3284 int i, j, ret = 0;
3285
3286 if (!namelist)
3287 return -ENOMEM;
3288
3289 for (j = 0; j < npevs && !ret; j++) {
3290 pev = &pevs[j];
3291 for (i = 0; i < pev->ntevs && !ret; i++) {
3292 tev = &pev->tevs[i];
3293 /* Skip if the symbol is out of .text or blacklisted */
3294 if (!tev->point.symbol && !pev->uprobes)
3295 continue;
3296
3297 /* Set new name for tev (and update namelist) */
3298 ret = probe_trace_event__set_name(tev, pev,
3299 namelist, true);
3300 if (!ret)
3301 ret = show_probe_trace_event(tev);
3302 }
3303 }
3304 strlist__delete(namelist);
3305
3306 return ret;
3307}
3308
Wang Nan12fae5e2015-09-04 21:16:00 +09003309int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003310{
3311 int i, ret = 0;
3312
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003313 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003314 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003315 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3316 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003317 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003318 if (ret < 0)
3319 break;
3320 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003321 return ret;
3322}
3323
Wang Nan12fae5e2015-09-04 21:16:00 +09003324void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003325{
3326 int i, j;
3327
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003328 /* Loop 3: cleanup and free trace events */
3329 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003330 for (j = 0; j < pevs[i].ntevs; j++)
3331 clear_probe_trace_event(&pevs[i].tevs[j]);
3332 zfree(&pevs[i].tevs);
3333 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003334 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003335 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003336}
3337
3338int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3339{
3340 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003341
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003342 ret = init_probe_symbol_maps(pevs->uprobes);
3343 if (ret < 0)
3344 return ret;
3345
Wang Nan12fae5e2015-09-04 21:16:00 +09003346 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003347 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003348 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003349
Wang Nan12fae5e2015-09-04 21:16:00 +09003350 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003351
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003352 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003353 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003354}
3355
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003356int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003357{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003358 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003359 char *str = strfilter__string(filter);
3360
3361 if (!str)
3362 return -EINVAL;
3363
Masami Hiramatsufa282442009-12-08 17:03:23 -05003364 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003365 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3366 if (ret < 0)
3367 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303368
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003369 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003370 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303371 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003372
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003373 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003374 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003375 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003376 goto error;
3377 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003378 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303379
3380error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003381 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303382 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003383 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303384 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003385out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003386 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003387
3388 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003389}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303390
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003391int show_available_funcs(const char *target, struct strfilter *_filter,
3392 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003393{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003394 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003395 struct map *map;
3396 int ret;
3397
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003398 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003399 if (ret < 0)
3400 return ret;
3401
3402 /* Get a symbol map */
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003403 map = get_target_map(target, user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003404 if (!map) {
3405 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003406 return -EINVAL;
3407 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003408
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003409 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003410 if (ret) {
3411 if (ret == -2) {
3412 char *str = strfilter__string(_filter);
3413 pr_err("Failed to find symbols matched to \"%s\"\n",
3414 str);
3415 free(str);
3416 } else
3417 pr_err("Failed to load symbols in %s\n",
3418 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003419 goto end;
3420 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003421 if (!dso__sorted_by_name(map->dso, map->type))
3422 dso__sort_by_name(map->dso, map->type);
3423
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003424 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303425 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003426
3427 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3428 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3429
3430 if (strfilter__compare(_filter, pos->sym.name))
3431 printf("%s\n", pos->sym.name);
3432 }
3433
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003434end:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003435 map__put(map);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003436 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303437
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003438 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303439}
3440
Wang Nanda15bd92015-08-26 10:57:45 +00003441int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3442 struct perf_probe_arg *pvar)
3443{
3444 tvar->value = strdup(pvar->var);
3445 if (tvar->value == NULL)
3446 return -ENOMEM;
3447 if (pvar->type) {
3448 tvar->type = strdup(pvar->type);
3449 if (tvar->type == NULL)
3450 return -ENOMEM;
3451 }
3452 if (pvar->name) {
3453 tvar->name = strdup(pvar->name);
3454 if (tvar->name == NULL)
3455 return -ENOMEM;
3456 } else
3457 tvar->name = NULL;
3458 return 0;
3459}